From d1ff222bcf94152cd657233cffd8c14a45788c26 Mon Sep 17 00:00:00 2001 From: Akshay Date: Wed, 29 Dec 2021 10:53:38 +0530 Subject: allow for version based lints --- bin/tests/data/faster_groupby.nix | 15 ++++++ bin/tests/main.rs | 66 +++++++++++++++++---------- bin/tests/snapshots/main__faster_groupby.snap | 20 ++++++++ 3 files changed, 77 insertions(+), 24 deletions(-) create mode 100644 bin/tests/data/faster_groupby.nix create mode 100644 bin/tests/snapshots/main__faster_groupby.snap (limited to 'bin/tests') 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 @@ +{ + # trivial case + _ = lib.groupBy (x: if x > 2 then "big" else "small") [ 1 2 3 4 5 ]; + + # offer lint heuristically on this too + _ = nixpkgs.lib.groupBy (x: if x > 2 then "big" else "small") [ 1 2 3 4 5 ]; + + # do not lint on `builtins` + _ = builtins.groupBy (x: x.name) [ + { name = "foo"; idx = 1; } + { name = "foo"; idx = 2; } + { name = "bar"; idx = 1; } + { name = "bar"; idx = 2; } + ]; +} 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 @@ +use lib::session::{SessionInfo, Version}; + +macro_rules! session_info { + ($version:expr) => {{ + let v: Version = $version.parse().unwrap(); + SessionInfo::from_version(v) + }}; +} + mod util { #[macro_export] macro_rules! test_lint { - ($($tname:ident),*,) => { - test_lint!($($tname),*); + ($tname:ident => $sess:expr, $($tail:tt)*) => { + test_lint!($tname => $sess); + test_lint!($($tail)*); + }; + ($tname:ident, $($tail:tt)*) => { + test_lint!($tname); + test_lint!($($tail)*); + }; + ($tname:ident) => { + test_lint!($tname => session_info!("nix (Nix) 2.5")); }; - ($($tname:ident),*) => { - $( - #[test] - fn $tname() { - use statix::{config::OutFormat, traits::WriteDiagnostic, lint}; - use vfs::ReadOnlyVfs; - - let file_path = concat!("data/", stringify!($tname), ".nix"); - let contents = include_str!(concat!("data/", stringify!($tname), ".nix")); - - let vfs = ReadOnlyVfs::singleton(file_path, contents.as_bytes()); - - let mut buffer = Vec::new(); - vfs.iter().map(lint::lint).for_each(|r| { - buffer.write(&r, &vfs, OutFormat::StdErr).unwrap(); - }); - - let stripped = strip_ansi_escapes::strip(&buffer).unwrap(); - let out = std::str::from_utf8(&stripped).unwrap(); - insta::assert_snapshot!(&out); - } - )* + ($tname:ident => $sess:expr) => { + #[test] + fn $tname() { + use statix::{config::OutFormat, traits::WriteDiagnostic, lint}; + use vfs::ReadOnlyVfs; + + let file_path = concat!("data/", stringify!($tname), ".nix"); + let contents = include_str!(concat!("data/", stringify!($tname), ".nix")); + + let vfs = ReadOnlyVfs::singleton(file_path, contents.as_bytes()); + + let session = $sess; + + let mut buffer = Vec::new(); + vfs.iter().map(|entry| lint::lint(entry, &session)).for_each(|r| { + buffer.write(&r, &vfs, OutFormat::StdErr).unwrap(); + }); + + let stripped = strip_ansi_escapes::strip(&buffer).unwrap(); + let out = std::str::from_utf8(&stripped).unwrap(); + insta::assert_snapshot!(&out); + } }; } } @@ -44,4 +61,5 @@ test_lint! { unquoted_uri, deprecated_is_null, empty_inherit, + faster_groupby => session_info!("nix (Nix) 2.5") } 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 @@ +--- +source: bin/tests/main.rs +expression: "&out" + +--- +[W15] Warning: Found lib.groupBy + ╭─[data/faster_groupby.nix:3:7] + │ + 3 │ _ = lib.groupBy (x: if x > 2 then "big" else "small") [ 1 2 3 4 5 ]; + · ─────┬───── + · ╰─────── Prefer builtins.groupBy over lib.groupBy +───╯ +[W15] Warning: Found lib.groupBy + ╭─[data/faster_groupby.nix:6:7] + │ + 6 │ _ = nixpkgs.lib.groupBy (x: if x > 2 then "big" else "small") [ 1 2 3 4 5 ]; + · ─────────┬───────── + · ╰─────────── Prefer builtins.groupBy over nixpkgs.lib.groupBy +───╯ + -- cgit v1.2.3