aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay <[email protected]>2022-01-14 12:39:42 +0000
committerAkshay <[email protected]>2022-01-14 12:39:42 +0000
commit7d732a051e695353db5a3ddbb26ab766ff736043 (patch)
treeaceda0db9a4c1d5ac64b9f061924e36ecf3f8ee9
parentd1ff222bcf94152cd657233cffd8c14a45788c26 (diff)
refactor parsing code, simplify tests
-rw-r--r--bin/src/utils.rs4
-rw-r--r--bin/tests/main.rs4
-rw-r--r--lib/src/lints/faster_groupby.rs4
-rw-r--r--lib/src/session.rs39
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<String> {
30 .output() 30 .output()
31 .expect("failed to execute"); 31 .expect("failed to execute");
32 std::str::from_utf8(&program.stdout) 32 std::str::from_utf8(&program.stdout)
33 .ok() 33 .ok()?
34 .split(' ')
35 .nth(2)
34 .map(ToOwned::to_owned) 36 .map(ToOwned::to_owned)
35} 37}
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 {
19 test_lint!($($tail)*); 19 test_lint!($($tail)*);
20 }; 20 };
21 ($tname:ident) => { 21 ($tname:ident) => {
22 test_lint!($tname => session_info!("nix (Nix) 2.5")); 22 test_lint!($tname => session_info!("2.5"));
23 }; 23 };
24 ($tname:ident => $sess:expr) => { 24 ($tname:ident => $sess:expr) => {
25 #[test] 25 #[test]
@@ -61,5 +61,5 @@ test_lint! {
61 unquoted_uri, 61 unquoted_uri,
62 deprecated_is_null, 62 deprecated_is_null,
63 empty_inherit, 63 empty_inherit,
64 faster_groupby => session_info!("nix (Nix) 2.5") 64 faster_groupby => session_info!("2.5")
65} 65}
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::{
27/// 27///
28/// Replace `lib.groupBy` with `builtins.groupBy`: 28/// Replace `lib.groupBy` with `builtins.groupBy`:
29/// 29///
30/// ``` 30/// ```nix
31/// builtins.groupBy (x: if x > 2 then "big" else "small") [ 1 2 3 4 5 6 ]; 31/// builtins.groupBy (x: if x > 2 then "big" else "small") [ 1 2 3 4 5 6 ];
32/// ``` 32/// ```
33#[lint( 33#[lint(
@@ -40,7 +40,7 @@ struct FasterGroupBy;
40 40
41impl Rule for FasterGroupBy { 41impl Rule for FasterGroupBy {
42 fn validate(&self, node: &SyntaxElement, sess: &SessionInfo) -> Option<Report> { 42 fn validate(&self, node: &SyntaxElement, sess: &SessionInfo) -> Option<Report> {
43 let lint_version = "nix (Nix) 2.5".parse::<Version>().unwrap(); 43 let lint_version = "2.5".parse::<Version>().unwrap();
44 if_chain! { 44 if_chain! {
45 if sess.version() >= &lint_version; 45 if sess.version() >= &lint_version;
46 if let NodeOrToken::Node(node) = node; 46 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<u16> {
29} 29}
30 30
31fn parse_version(s: &str) -> Option<Version> { 31fn parse_version(s: &str) -> Option<Version> {
32 match s.split(' ').collect::<Vec<_>>().as_slice() { 32 let mut parts = s.split('.');
33 [_, _, version] => { 33 let major = parse_number(parts.next()?)?;
34 let mut parts = version.split('.'); 34 let minor = parse_number(parts.next()?)?;
35 let major = parse_number(parts.next()?)?; 35 let patch = parts.next().map(|p| parse_number(p)).flatten();
36 let minor = parse_number(parts.next()?)?; 36 Some(Version {
37 let patch = parts.next().map(|p| parse_number(p)).flatten(); 37 major,
38 Some(Version { 38 minor,
39 major, 39 patch,
40 minor, 40 })
41 patch,
42 })
43 }
44 _ => None,
45 }
46} 41}
47 42
48impl FromStr for Version { 43impl FromStr for Version {
@@ -67,37 +62,33 @@ impl SessionInfo {
67 } 62 }
68} 63}
69 64
70pub fn get_nix_version() -> Option<Version> {
71 "nix (Nix) 2.4pre20211006_53e4794".parse::<Version>().ok()
72}
73
74#[cfg(test)] 65#[cfg(test)]
75mod tests { 66mod tests {
76 use super::*; 67 use super::*;
77 68
78 #[test] 69 #[test]
79 fn parse_trivial() { 70 fn parse_trivial() {
80 let v = "nix (Nix) 1.6.1".parse::<Version>().ok(); 71 let v = "1.6.1".parse::<Version>().ok();
81 assert!(v.is_some()) 72 assert!(v.is_some())
82 } 73 }
83 74
84 #[test] 75 #[test]
85 fn parse() { 76 fn parse() {
86 let v = "nix (Nix) 2.4pre20211006_53e4794".parse::<Version>().ok(); 77 let v = "2.4pre20211006_53e4794".parse::<Version>().ok();
87 assert!(v.is_some()) 78 assert!(v.is_some())
88 } 79 }
89 80
90 #[test] 81 #[test]
91 fn compare_trivial() { 82 fn compare_trivial() {
92 let v1 = "nix (Nix) 1.6.1".parse::<Version>().ok(); 83 let v1 = "1.6.1".parse::<Version>().ok();
93 let v2 = "nix (Nix) 1.7.2".parse::<Version>().ok(); 84 let v2 = "1.7.2".parse::<Version>().ok();
94 assert!(v2 > v1); 85 assert!(v2 > v1);
95 } 86 }
96 87
97 #[test] 88 #[test]
98 fn compare() { 89 fn compare() {
99 let v1 = "nix (Nix) 1.7".parse::<Version>().ok(); 90 let v1 = "1.7".parse::<Version>().ok();
100 let v2 = "nix (Nix) 2.4pre20211006_53e4794".parse::<Version>().ok(); 91 let v2 = "2.4pre20211006_53e4794".parse::<Version>().ok();
101 assert!(v2 >= v1); 92 assert!(v2 >= v1);
102 } 93 }
103} 94}