diff options
author | Akshay <[email protected]> | 2021-12-29 05:23:38 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2022-01-08 10:33:10 +0000 |
commit | d1ff222bcf94152cd657233cffd8c14a45788c26 (patch) | |
tree | fb8c94daefe0384a48b503fdd4bfaff905d78e2f /bin/tests | |
parent | 94a2edf57340ac3f3a2276c88a221ba3125172af (diff) |
allow for version based lints
Diffstat (limited to 'bin/tests')
-rw-r--r-- | bin/tests/data/faster_groupby.nix | 15 | ||||
-rw-r--r-- | bin/tests/main.rs | 66 | ||||
-rw-r--r-- | bin/tests/snapshots/main__faster_groupby.snap | 20 |
3 files changed, 77 insertions, 24 deletions
diff --git a/bin/tests/data/faster_groupby.nix b/bin/tests/data/faster_groupby.nix new file mode 100644 index 0000000..30d1031 --- /dev/null +++ b/bin/tests/data/faster_groupby.nix | |||
@@ -0,0 +1,15 @@ | |||
1 | { | ||
2 | # trivial case | ||
3 | _ = lib.groupBy (x: if x > 2 then "big" else "small") [ 1 2 3 4 5 ]; | ||
4 | |||
5 | # offer lint heuristically on this too | ||
6 | _ = nixpkgs.lib.groupBy (x: if x > 2 then "big" else "small") [ 1 2 3 4 5 ]; | ||
7 | |||
8 | # do not lint on `builtins` | ||
9 | _ = builtins.groupBy (x: x.name) [ | ||
10 | { name = "foo"; idx = 1; } | ||
11 | { name = "foo"; idx = 2; } | ||
12 | { name = "bar"; idx = 1; } | ||
13 | { name = "bar"; idx = 2; } | ||
14 | ]; | ||
15 | } | ||
diff --git a/bin/tests/main.rs b/bin/tests/main.rs index de5266f..991c876 100644 --- a/bin/tests/main.rs +++ b/bin/tests/main.rs | |||
@@ -1,31 +1,48 @@ | |||
1 | use lib::session::{SessionInfo, Version}; | ||
2 | |||
3 | macro_rules! session_info { | ||
4 | ($version:expr) => {{ | ||
5 | let v: Version = $version.parse().unwrap(); | ||
6 | SessionInfo::from_version(v) | ||
7 | }}; | ||
8 | } | ||
9 | |||
1 | mod util { | 10 | mod util { |
2 | #[macro_export] | 11 | #[macro_export] |
3 | macro_rules! test_lint { | 12 | macro_rules! test_lint { |
4 | ($($tname:ident),*,) => { | 13 | ($tname:ident => $sess:expr, $($tail:tt)*) => { |
5 | test_lint!($($tname),*); | 14 | test_lint!($tname => $sess); |
15 | test_lint!($($tail)*); | ||
16 | }; | ||
17 | ($tname:ident, $($tail:tt)*) => { | ||
18 | test_lint!($tname); | ||
19 | test_lint!($($tail)*); | ||
20 | }; | ||
21 | ($tname:ident) => { | ||
22 | test_lint!($tname => session_info!("nix (Nix) 2.5")); | ||
6 | }; | 23 | }; |
7 | ($($tname:ident),*) => { | 24 | ($tname:ident => $sess:expr) => { |
8 | $( | 25 | #[test] |
9 | #[test] | 26 | fn $tname() { |
10 | fn $tname() { | 27 | use statix::{config::OutFormat, traits::WriteDiagnostic, lint}; |
11 | use statix::{config::OutFormat, traits::WriteDiagnostic, lint}; | 28 | use vfs::ReadOnlyVfs; |
12 | use vfs::ReadOnlyVfs; | 29 | |
13 | 30 | let file_path = concat!("data/", stringify!($tname), ".nix"); | |
14 | let file_path = concat!("data/", stringify!($tname), ".nix"); | 31 | let contents = include_str!(concat!("data/", stringify!($tname), ".nix")); |
15 | let contents = include_str!(concat!("data/", stringify!($tname), ".nix")); | 32 | |
16 | 33 | let vfs = ReadOnlyVfs::singleton(file_path, contents.as_bytes()); | |
17 | let vfs = ReadOnlyVfs::singleton(file_path, contents.as_bytes()); | 34 | |
18 | 35 | let session = $sess; | |
19 | let mut buffer = Vec::new(); | 36 | |
20 | vfs.iter().map(lint::lint).for_each(|r| { | 37 | let mut buffer = Vec::new(); |
21 | buffer.write(&r, &vfs, OutFormat::StdErr).unwrap(); | 38 | vfs.iter().map(|entry| lint::lint(entry, &session)).for_each(|r| { |
22 | }); | 39 | buffer.write(&r, &vfs, OutFormat::StdErr).unwrap(); |
23 | 40 | }); | |
24 | let stripped = strip_ansi_escapes::strip(&buffer).unwrap(); | 41 | |
25 | let out = std::str::from_utf8(&stripped).unwrap(); | 42 | let stripped = strip_ansi_escapes::strip(&buffer).unwrap(); |
26 | insta::assert_snapshot!(&out); | 43 | let out = std::str::from_utf8(&stripped).unwrap(); |
27 | } | 44 | insta::assert_snapshot!(&out); |
28 | )* | 45 | } |
29 | }; | 46 | }; |
30 | } | 47 | } |
31 | } | 48 | } |
@@ -44,4 +61,5 @@ test_lint! { | |||
44 | unquoted_uri, | 61 | unquoted_uri, |
45 | deprecated_is_null, | 62 | deprecated_is_null, |
46 | empty_inherit, | 63 | empty_inherit, |
64 | faster_groupby => session_info!("nix (Nix) 2.5") | ||
47 | } | 65 | } |
diff --git a/bin/tests/snapshots/main__faster_groupby.snap b/bin/tests/snapshots/main__faster_groupby.snap new file mode 100644 index 0000000..6ff3380 --- /dev/null +++ b/bin/tests/snapshots/main__faster_groupby.snap | |||
@@ -0,0 +1,20 @@ | |||
1 | --- | ||
2 | source: bin/tests/main.rs | ||
3 | expression: "&out" | ||
4 | |||
5 | --- | ||
6 | [W15] Warning: Found lib.groupBy | ||
7 | ╭─[data/faster_groupby.nix:3:7] | ||
8 | │ | ||
9 | 3 │ _ = lib.groupBy (x: if x > 2 then "big" else "small") [ 1 2 3 4 5 ]; | ||
10 | · ─────┬───── | ||
11 | · ╰─────── Prefer builtins.groupBy over lib.groupBy | ||
12 | ───╯ | ||
13 | [W15] Warning: Found lib.groupBy | ||
14 | ╭─[data/faster_groupby.nix:6:7] | ||
15 | │ | ||
16 | 6 │ _ = nixpkgs.lib.groupBy (x: if x > 2 then "big" else "small") [ 1 2 3 4 5 ]; | ||
17 | · ─────────┬───────── | ||
18 | · ╰─────────── Prefer builtins.groupBy over nixpkgs.lib.groupBy | ||
19 | ───╯ | ||
20 | |||