From 7d732a051e695353db5a3ddbb26ab766ff736043 Mon Sep 17 00:00:00 2001 From: Akshay Date: Fri, 14 Jan 2022 18:09:42 +0530 Subject: refactor parsing code, simplify tests --- bin/src/utils.rs | 4 +++- bin/tests/main.rs | 4 ++-- lib/src/lints/faster_groupby.rs | 4 ++-- lib/src/session.rs | 39 +++++++++++++++------------------------ 4 files changed, 22 insertions(+), 29 deletions(-) diff --git a/bin/src/utils.rs b/bin/src/utils.rs index d374b4b..a3d51b4 100644 --- a/bin/src/utils.rs +++ b/bin/src/utils.rs @@ -30,6 +30,8 @@ pub fn get_version_info() -> Option { .output() .expect("failed to execute"); std::str::from_utf8(&program.stdout) - .ok() + .ok()? + .split(' ') + .nth(2) .map(ToOwned::to_owned) } diff --git a/bin/tests/main.rs b/bin/tests/main.rs index 991c876..89d58e7 100644 --- a/bin/tests/main.rs +++ b/bin/tests/main.rs @@ -19,7 +19,7 @@ mod util { test_lint!($($tail)*); }; ($tname:ident) => { - test_lint!($tname => session_info!("nix (Nix) 2.5")); + test_lint!($tname => session_info!("2.5")); }; ($tname:ident => $sess:expr) => { #[test] @@ -61,5 +61,5 @@ test_lint! { unquoted_uri, deprecated_is_null, empty_inherit, - faster_groupby => session_info!("nix (Nix) 2.5") + faster_groupby => session_info!("2.5") } diff --git a/lib/src/lints/faster_groupby.rs b/lib/src/lints/faster_groupby.rs index c496125..2a36f5f 100644 --- a/lib/src/lints/faster_groupby.rs +++ b/lib/src/lints/faster_groupby.rs @@ -27,7 +27,7 @@ use rnix::{ /// /// Replace `lib.groupBy` with `builtins.groupBy`: /// -/// ``` +/// ```nix /// builtins.groupBy (x: if x > 2 then "big" else "small") [ 1 2 3 4 5 6 ]; /// ``` #[lint( @@ -40,7 +40,7 @@ struct FasterGroupBy; impl Rule for FasterGroupBy { fn validate(&self, node: &SyntaxElement, sess: &SessionInfo) -> Option { - let lint_version = "nix (Nix) 2.5".parse::().unwrap(); + let lint_version = "2.5".parse::().unwrap(); if_chain! { if sess.version() >= &lint_version; if let NodeOrToken::Node(node) = node; diff --git a/lib/src/session.rs b/lib/src/session.rs index 8d142ec..0fb8ae2 100644 --- a/lib/src/session.rs +++ b/lib/src/session.rs @@ -29,20 +29,15 @@ fn parse_number(s: &str) -> Option { } fn parse_version(s: &str) -> Option { - match s.split(' ').collect::>().as_slice() { - [_, _, version] => { - let mut parts = version.split('.'); - let major = parse_number(parts.next()?)?; - let minor = parse_number(parts.next()?)?; - let patch = parts.next().map(|p| parse_number(p)).flatten(); - Some(Version { - major, - minor, - patch, - }) - } - _ => None, - } + let mut parts = s.split('.'); + let major = parse_number(parts.next()?)?; + let minor = parse_number(parts.next()?)?; + let patch = parts.next().map(|p| parse_number(p)).flatten(); + Some(Version { + major, + minor, + patch, + }) } impl FromStr for Version { @@ -67,37 +62,33 @@ impl SessionInfo { } } -pub fn get_nix_version() -> Option { - "nix (Nix) 2.4pre20211006_53e4794".parse::().ok() -} - #[cfg(test)] mod tests { use super::*; #[test] fn parse_trivial() { - let v = "nix (Nix) 1.6.1".parse::().ok(); + let v = "1.6.1".parse::().ok(); assert!(v.is_some()) } #[test] fn parse() { - let v = "nix (Nix) 2.4pre20211006_53e4794".parse::().ok(); + let v = "2.4pre20211006_53e4794".parse::().ok(); assert!(v.is_some()) } #[test] fn compare_trivial() { - let v1 = "nix (Nix) 1.6.1".parse::().ok(); - let v2 = "nix (Nix) 1.7.2".parse::().ok(); + let v1 = "1.6.1".parse::().ok(); + let v2 = "1.7.2".parse::().ok(); assert!(v2 > v1); } #[test] fn compare() { - let v1 = "nix (Nix) 1.7".parse::().ok(); - let v2 = "nix (Nix) 2.4pre20211006_53e4794".parse::().ok(); + let v1 = "1.7".parse::().ok(); + let v2 = "2.4pre20211006_53e4794".parse::().ok(); assert!(v2 >= v1); } } -- cgit v1.2.3