From 4e063b2abc402ac4d6902647e821978269025c7d Mon Sep 17 00:00:00 2001 From: Akshay Date: Wed, 3 Nov 2021 14:48:35 +0530 Subject: add snapshot test suite --- bin/tests/data/bool_comparison.nix | 13 +++++ bin/tests/data/collapsible_let_in.nix | 9 ++++ bin/tests/data/deprecated_is_null.nix | 6 +++ bin/tests/data/empty_let_in.nix | 3 ++ bin/tests/data/empty_pattern.nix | 9 ++++ bin/tests/data/eta_reduction.nix | 18 +++++++ bin/tests/data/legacy_let_syntax.nix | 5 ++ bin/tests/data/manual_inherit.nix | 12 +++++ bin/tests/data/manual_inherit_from.nix | 8 +++ bin/tests/data/redundant_pattern_bind.nix | 1 + bin/tests/data/unquoted_splices.nix | 15 ++++++ bin/tests/data/unquoted_uri.nix | 1 + bin/tests/data/useless_parens.nix | 16 ++++++ bin/tests/main.rs | 47 ++++++++++++++++ bin/tests/snapshots/main__bool_comparison.snap | 62 ++++++++++++++++++++++ bin/tests/snapshots/main__collapsible_let_in.snap | 17 ++++++ bin/tests/snapshots/main__deprecated_is_null.snap | 13 +++++ bin/tests/snapshots/main__empty_let_in.snap | 14 +++++ bin/tests/snapshots/main__empty_pattern.snap | 20 +++++++ bin/tests/snapshots/main__eta_reduction.snap | 13 +++++ bin/tests/snapshots/main__legacy_let_syntax.snap | 14 +++++ bin/tests/snapshots/main__manual_inherit.snap | 13 +++++ bin/tests/snapshots/main__manual_inherit_from.snap | 20 +++++++ .../snapshots/main__redundant_pattern_bind.snap | 13 +++++ bin/tests/snapshots/main__unquoted_splices.snap | 35 ++++++++++++ bin/tests/snapshots/main__unquoted_uri.snap | 13 +++++ bin/tests/snapshots/main__useless_parens.snap | 48 +++++++++++++++++ 27 files changed, 458 insertions(+) create mode 100644 bin/tests/data/bool_comparison.nix create mode 100644 bin/tests/data/collapsible_let_in.nix create mode 100644 bin/tests/data/deprecated_is_null.nix create mode 100644 bin/tests/data/empty_let_in.nix create mode 100644 bin/tests/data/empty_pattern.nix create mode 100644 bin/tests/data/eta_reduction.nix create mode 100644 bin/tests/data/legacy_let_syntax.nix create mode 100644 bin/tests/data/manual_inherit.nix create mode 100644 bin/tests/data/manual_inherit_from.nix create mode 100644 bin/tests/data/redundant_pattern_bind.nix create mode 100644 bin/tests/data/unquoted_splices.nix create mode 100644 bin/tests/data/unquoted_uri.nix create mode 100644 bin/tests/data/useless_parens.nix create mode 100644 bin/tests/main.rs create mode 100644 bin/tests/snapshots/main__bool_comparison.snap create mode 100644 bin/tests/snapshots/main__collapsible_let_in.snap create mode 100644 bin/tests/snapshots/main__deprecated_is_null.snap create mode 100644 bin/tests/snapshots/main__empty_let_in.snap create mode 100644 bin/tests/snapshots/main__empty_pattern.snap create mode 100644 bin/tests/snapshots/main__eta_reduction.snap create mode 100644 bin/tests/snapshots/main__legacy_let_syntax.snap create mode 100644 bin/tests/snapshots/main__manual_inherit.snap create mode 100644 bin/tests/snapshots/main__manual_inherit_from.snap create mode 100644 bin/tests/snapshots/main__redundant_pattern_bind.snap create mode 100644 bin/tests/snapshots/main__unquoted_splices.snap create mode 100644 bin/tests/snapshots/main__unquoted_uri.snap create mode 100644 bin/tests/snapshots/main__useless_parens.snap (limited to 'bin/tests') diff --git a/bin/tests/data/bool_comparison.nix b/bin/tests/data/bool_comparison.nix new file mode 100644 index 0000000..dee2d08 --- /dev/null +++ b/bin/tests/data/bool_comparison.nix @@ -0,0 +1,13 @@ +[ + # trivial + (a == true) + (b == true) + (true == c) + (true == d) + + # not equals + (e != true) + (f != false) + (true != g) + (false != h) +] diff --git a/bin/tests/data/collapsible_let_in.nix b/bin/tests/data/collapsible_let_in.nix new file mode 100644 index 0000000..7b41014 --- /dev/null +++ b/bin/tests/data/collapsible_let_in.nix @@ -0,0 +1,9 @@ +let + a = 2; + b = 3; +in + let + c = 5; + d = 6; + in + a + b + c + d diff --git a/bin/tests/data/deprecated_is_null.nix b/bin/tests/data/deprecated_is_null.nix new file mode 100644 index 0000000..42596d7 --- /dev/null +++ b/bin/tests/data/deprecated_is_null.nix @@ -0,0 +1,6 @@ +let + e = null; +in +if isNull e +then "no" +else "yes" diff --git a/bin/tests/data/empty_let_in.nix b/bin/tests/data/empty_let_in.nix new file mode 100644 index 0000000..3ecb6e4 --- /dev/null +++ b/bin/tests/data/empty_let_in.nix @@ -0,0 +1,3 @@ +let +in + null diff --git a/bin/tests/data/empty_pattern.nix b/bin/tests/data/empty_pattern.nix new file mode 100644 index 0000000..23d99e8 --- /dev/null +++ b/bin/tests/data/empty_pattern.nix @@ -0,0 +1,9 @@ +[ + # match + ({ ... }: 42) + + # don't match + ({ a, ... }: a) + ({ ... } @ inputs: inputs) +] + diff --git a/bin/tests/data/eta_reduction.nix b/bin/tests/data/eta_reduction.nix new file mode 100644 index 0000000..e717ee7 --- /dev/null +++ b/bin/tests/data/eta_reduction.nix @@ -0,0 +1,18 @@ +let + double = x: x * 2; + inherit (builtins) map; + xs = [ 1 2 3 ]; + f = { + inherit double; + val = 2; + }; +in +[ + (map (x: double x) xs) + + # don't lint on non-free exprs + (map (f: f.double f.val) [ f ]) + + # other non-free forms + (map (f: {inherit f;}.double f.val) [ f ]) +] diff --git a/bin/tests/data/legacy_let_syntax.nix b/bin/tests/data/legacy_let_syntax.nix new file mode 100644 index 0000000..46e3191 --- /dev/null +++ b/bin/tests/data/legacy_let_syntax.nix @@ -0,0 +1,5 @@ +let { + body = x + y; + x = "hello,"; + y = " world!"; +} diff --git a/bin/tests/data/manual_inherit.nix b/bin/tests/data/manual_inherit.nix new file mode 100644 index 0000000..53ae4d7 --- /dev/null +++ b/bin/tests/data/manual_inherit.nix @@ -0,0 +1,12 @@ +let + a = 2; + y = "y"; +in +{ + # trivial + a = a; + + # don't lint + x.y = y; +} + diff --git a/bin/tests/data/manual_inherit_from.nix b/bin/tests/data/manual_inherit_from.nix new file mode 100644 index 0000000..214b2a3 --- /dev/null +++ b/bin/tests/data/manual_inherit_from.nix @@ -0,0 +1,8 @@ +let + a = {b = 2; c = 3;}; +in +{ + b = a.b; + c = a.c; +} + diff --git a/bin/tests/data/redundant_pattern_bind.nix b/bin/tests/data/redundant_pattern_bind.nix new file mode 100644 index 0000000..d328c50 --- /dev/null +++ b/bin/tests/data/redundant_pattern_bind.nix @@ -0,0 +1 @@ +{ ... } @ inputs: null diff --git a/bin/tests/data/unquoted_splices.nix b/bin/tests/data/unquoted_splices.nix new file mode 100644 index 0000000..30935b0 --- /dev/null +++ b/bin/tests/data/unquoted_splices.nix @@ -0,0 +1,15 @@ +let + x = 2; + y = 3; + a = { "2" = y; }; +in +[ + ${x} + ${toString (x + y)} + a.${toString x} + + # multiline test + ${ + toString x + } +] diff --git a/bin/tests/data/unquoted_uri.nix b/bin/tests/data/unquoted_uri.nix new file mode 100644 index 0000000..e56574a --- /dev/null +++ b/bin/tests/data/unquoted_uri.nix @@ -0,0 +1 @@ +github:nerdypepper/statix diff --git a/bin/tests/data/useless_parens.nix b/bin/tests/data/useless_parens.nix new file mode 100644 index 0000000..cf26441 --- /dev/null +++ b/bin/tests/data/useless_parens.nix @@ -0,0 +1,16 @@ +let + # parens around primitives + a = { + b = ("hello"); + c = (d); + e = ({ f = 2; }); + }; + + # parens around let-value + g = (1 + 2); + h = ({ inherit i; }); + + # TODO: binary exprs, function args etc. +in + # parens around let body + (null) diff --git a/bin/tests/main.rs b/bin/tests/main.rs new file mode 100644 index 0000000..6175c90 --- /dev/null +++ b/bin/tests/main.rs @@ -0,0 +1,47 @@ +mod util { + #[macro_export] + macro_rules! test_lint { + ($($tname:ident),*,) => { + test_lint!($($tname),*); + }; + ($($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); + } + )* + }; + } +} + +test_lint! { + bool_comparison, + empty_let_in, + manual_inherit, + manual_inherit_from, + legacy_let_syntax, + collapsible_let_in, + eta_reduction, + useless_parens, + unquoted_splices, + empty_pattern, + redundant_pattern_bind, + unquoted_uri, + deprecated_is_null, +} diff --git a/bin/tests/snapshots/main__bool_comparison.snap b/bin/tests/snapshots/main__bool_comparison.snap new file mode 100644 index 0000000..0865332 --- /dev/null +++ b/bin/tests/snapshots/main__bool_comparison.snap @@ -0,0 +1,62 @@ +--- +source: bin/tests/main.rs +expression: "&out" + +--- +[W01] Warning: Unnecessary comparison with boolean + ╭─[data/bool_comparison.nix:3:4] + │ + 3 │ (a == true) + · ────┬──── + · ╰────── Comparing a with boolean literal true +───╯ +[W01] Warning: Unnecessary comparison with boolean + ╭─[data/bool_comparison.nix:4:4] + │ + 4 │ (b == true) + · ────┬──── + · ╰────── Comparing b with boolean literal true +───╯ +[W01] Warning: Unnecessary comparison with boolean + ╭─[data/bool_comparison.nix:5:4] + │ + 5 │ (true == c) + · ────┬──── + · ╰────── Comparing c with boolean literal true +───╯ +[W01] Warning: Unnecessary comparison with boolean + ╭─[data/bool_comparison.nix:6:4] + │ + 6 │ (true == d) + · ────┬──── + · ╰────── Comparing d with boolean literal true +───╯ +[W01] Warning: Unnecessary comparison with boolean + ╭─[data/bool_comparison.nix:9:4] + │ + 9 │ (e != true) + · ────┬──── + · ╰────── Comparing e with boolean literal true +───╯ +[W01] Warning: Unnecessary comparison with boolean + ╭─[data/bool_comparison.nix:10:4] + │ + 10 │ (f != false) + · ─────┬──── + · ╰────── Comparing f with boolean literal false +────╯ +[W01] Warning: Unnecessary comparison with boolean + ╭─[data/bool_comparison.nix:11:4] + │ + 11 │ (true != g) + · ────┬──── + · ╰────── Comparing g with boolean literal true +────╯ +[W01] Warning: Unnecessary comparison with boolean + ╭─[data/bool_comparison.nix:12:4] + │ + 12 │ (false != h) + · ─────┬──── + · ╰────── Comparing h with boolean literal false +────╯ + diff --git a/bin/tests/snapshots/main__collapsible_let_in.snap b/bin/tests/snapshots/main__collapsible_let_in.snap new file mode 100644 index 0000000..b135abc --- /dev/null +++ b/bin/tests/snapshots/main__collapsible_let_in.snap @@ -0,0 +1,17 @@ +--- +source: bin/tests/main.rs +expression: "&out" + +--- +[W06] Warning: These let-in expressions are collapsible + ╭─[data/collapsible_let_in.nix:1:1] + │ + 1 │ ╭───▶ let + 5 │ │ ╭─▶ let + 9 │ │ ├─▶ a + b + c + d + · │ │ │ + · │ ╰───────────────────── This let in expression is nested + · │ │ + · ╰───────────────────┴─── This let in expression contains a nested let in expression +───╯ + diff --git a/bin/tests/snapshots/main__deprecated_is_null.snap b/bin/tests/snapshots/main__deprecated_is_null.snap new file mode 100644 index 0000000..d49b381 --- /dev/null +++ b/bin/tests/snapshots/main__deprecated_is_null.snap @@ -0,0 +1,13 @@ +--- +source: bin/tests/main.rs +expression: "&out" + +--- +[W13] Warning: Found usage of deprecated builtin isNull + ╭─[data/deprecated_is_null.nix:4:4] + │ + 4 │ if isNull e + · ────┬─── + · ╰───── isNull is deprecated, check equality with null instead +───╯ + diff --git a/bin/tests/snapshots/main__empty_let_in.snap b/bin/tests/snapshots/main__empty_let_in.snap new file mode 100644 index 0000000..426692f --- /dev/null +++ b/bin/tests/snapshots/main__empty_let_in.snap @@ -0,0 +1,14 @@ +--- +source: bin/tests/main.rs +expression: "&out" + +--- +[W02] Warning: Useless let-in expression + ╭─[data/empty_let_in.nix:1:1] + │ + 1 │ ╭─▶ let + 3 │ ├─▶ null + · │ + · ╰──────────── This let-in expression has no entries +───╯ + diff --git a/bin/tests/snapshots/main__empty_pattern.snap b/bin/tests/snapshots/main__empty_pattern.snap new file mode 100644 index 0000000..3ea7ae0 --- /dev/null +++ b/bin/tests/snapshots/main__empty_pattern.snap @@ -0,0 +1,20 @@ +--- +source: bin/tests/main.rs +expression: "&out" + +--- +[W10] Warning: Found empty pattern in function argument + ╭─[data/empty_pattern.nix:3:4] + │ + 3 │ ({ ... }: 42) + · ───┬─── + · ╰───── This pattern is empty, use _ instead +───╯ +[W11] Warning: Found redundant pattern bind in function argument + ╭─[data/empty_pattern.nix:7:4] + │ + 7 │ ({ ... } @ inputs: inputs) + · ────────┬─────── + · ╰───────── This pattern bind is redundant, use inputs instead +───╯ + diff --git a/bin/tests/snapshots/main__eta_reduction.snap b/bin/tests/snapshots/main__eta_reduction.snap new file mode 100644 index 0000000..6271980 --- /dev/null +++ b/bin/tests/snapshots/main__eta_reduction.snap @@ -0,0 +1,13 @@ +--- +source: bin/tests/main.rs +expression: "&out" + +--- +[W07] Warning: This function expression is eta reducible + ╭─[data/eta_reduction.nix:11:9] + │ + 11 │ (map (x: double x) xs) + · ─────┬───── + · ╰─────── Found eta-reduction: double +────╯ + diff --git a/bin/tests/snapshots/main__legacy_let_syntax.snap b/bin/tests/snapshots/main__legacy_let_syntax.snap new file mode 100644 index 0000000..35aa7ee --- /dev/null +++ b/bin/tests/snapshots/main__legacy_let_syntax.snap @@ -0,0 +1,14 @@ +--- +source: bin/tests/main.rs +expression: "&out" + +--- +[W05] Warning: Using undocumented `let` syntax + ╭─[data/legacy_let_syntax.nix:1:1] + │ + 1 │ ╭─▶ let { + 5 │ ├─▶ } + · │ + · ╰─────── Prefer rec over undocumented let syntax +───╯ + diff --git a/bin/tests/snapshots/main__manual_inherit.snap b/bin/tests/snapshots/main__manual_inherit.snap new file mode 100644 index 0000000..063867c --- /dev/null +++ b/bin/tests/snapshots/main__manual_inherit.snap @@ -0,0 +1,13 @@ +--- +source: bin/tests/main.rs +expression: "&out" + +--- +[W03] Warning: Assignment instead of inherit + ╭─[data/manual_inherit.nix:7:3] + │ + 7 │ a = a; + · ───┬── + · ╰──── This assignment is better written with inherit +───╯ + diff --git a/bin/tests/snapshots/main__manual_inherit_from.snap b/bin/tests/snapshots/main__manual_inherit_from.snap new file mode 100644 index 0000000..9cf1f5d --- /dev/null +++ b/bin/tests/snapshots/main__manual_inherit_from.snap @@ -0,0 +1,20 @@ +--- +source: bin/tests/main.rs +expression: "&out" + +--- +[W04] Warning: Assignment instead of inherit from + ╭─[data/manual_inherit_from.nix:5:3] + │ + 5 │ b = a.b; + · ────┬─── + · ╰───── This assignment is better written with inherit +───╯ +[W04] Warning: Assignment instead of inherit from + ╭─[data/manual_inherit_from.nix:6:3] + │ + 6 │ c = a.c; + · ────┬─── + · ╰───── This assignment is better written with inherit +───╯ + diff --git a/bin/tests/snapshots/main__redundant_pattern_bind.snap b/bin/tests/snapshots/main__redundant_pattern_bind.snap new file mode 100644 index 0000000..2f26818 --- /dev/null +++ b/bin/tests/snapshots/main__redundant_pattern_bind.snap @@ -0,0 +1,13 @@ +--- +source: bin/tests/main.rs +expression: "&out" + +--- +[W11] Warning: Found redundant pattern bind in function argument + ╭─[data/redundant_pattern_bind.nix:1:1] + │ + 1 │ { ... } @ inputs: null + · ────────┬──────── + · ╰────────── This pattern bind is redundant, use inputs instead +───╯ + diff --git a/bin/tests/snapshots/main__unquoted_splices.snap b/bin/tests/snapshots/main__unquoted_splices.snap new file mode 100644 index 0000000..5fd1917 --- /dev/null +++ b/bin/tests/snapshots/main__unquoted_splices.snap @@ -0,0 +1,35 @@ +--- +source: bin/tests/main.rs +expression: "&out" + +--- +[W09] Warning: Found unquoted splice expression + ╭─[data/unquoted_splices.nix:7:3] + │ + 7 │ ${x} + · ──┬─ + · ╰─── Consider quoting this splice expression +───╯ +[W09] Warning: Found unquoted splice expression + ╭─[data/unquoted_splices.nix:8:3] + │ + 8 │ ${toString (x + y)} + · ─────────┬───────── + · ╰─────────── Consider quoting this splice expression +───╯ +[W09] Warning: Found unquoted splice expression + ╭─[data/unquoted_splices.nix:9:5] + │ + 9 │ a.${toString x} + · ──────┬────── + · ╰──────── Consider quoting this splice expression +───╯ +[W09] Warning: Found unquoted splice expression + ╭─[data/unquoted_splices.nix:12:3] + │ + 12 │ ╭─▶ ${ + 14 │ ├─▶ } + · │ + · ╰───────── Consider quoting this splice expression +────╯ + diff --git a/bin/tests/snapshots/main__unquoted_uri.snap b/bin/tests/snapshots/main__unquoted_uri.snap new file mode 100644 index 0000000..2f0e5a9 --- /dev/null +++ b/bin/tests/snapshots/main__unquoted_uri.snap @@ -0,0 +1,13 @@ +--- +source: bin/tests/main.rs +expression: "&out" + +--- +[W12] Warning: Found unquoted URI expression + ╭─[data/unquoted_uri.nix:1:1] + │ + 1 │ github:nerdypepper/statix + · ────────────┬──────────── + · ╰────────────── Consider quoting this URI expression +───╯ + diff --git a/bin/tests/snapshots/main__useless_parens.snap b/bin/tests/snapshots/main__useless_parens.snap new file mode 100644 index 0000000..d44176e --- /dev/null +++ b/bin/tests/snapshots/main__useless_parens.snap @@ -0,0 +1,48 @@ +--- +source: bin/tests/main.rs +expression: "&out" + +--- +[W08] Warning: These parentheses can be omitted + ╭─[data/useless_parens.nix:16:3] + │ + 16 │ (null) + · ───┬── + · ╰──── Useless parentheses around body of let expression +────╯ +[W08] Warning: These parentheses can be omitted + ╭─[data/useless_parens.nix:4:9] + │ + 4 │ b = ("hello"); + · ────┬──── + · ╰────── Useless parentheses around value in binding +───╯ +[W08] Warning: These parentheses can be omitted + ╭─[data/useless_parens.nix:5:9] + │ + 5 │ c = (d); + · ─┬─ + · ╰─── Useless parentheses around value in binding +───╯ +[W08] Warning: These parentheses can be omitted + ╭─[data/useless_parens.nix:6:9] + │ + 6 │ e = ({ f = 2; }); + · ──────┬───── + · ╰─────── Useless parentheses around value in binding +───╯ +[W08] Warning: These parentheses can be omitted + ╭─[data/useless_parens.nix:10:7] + │ + 10 │ g = (1 + 2); + · ───┬─── + · ╰───── Useless parentheses around value in binding +────╯ +[W08] Warning: These parentheses can be omitted + ╭─[data/useless_parens.nix:11:7] + │ + 11 │ h = ({ inherit i; }); + · ────────┬─────── + · ╰───────── Useless parentheses around value in binding +────╯ + -- cgit v1.2.3