chore: add more helpers & add pos to test suite

This commit is contained in:
technofab 2025-06-13 20:47:26 +02:00
parent 3f1b6317b4
commit 4a55db9797
No known key found for this signature in database
2 changed files with 82 additions and 76 deletions

View file

@ -2,4 +2,6 @@
path = pkgs: "export PATH=${lib.makeBinPath pkgs}"; path = pkgs: "export PATH=${lib.makeBinPath pkgs}";
pathAdd = pkgs: "export PATH=$PATH:${lib.makeBinPath pkgs}"; pathAdd = pkgs: "export PATH=$PATH:${lib.makeBinPath pkgs}";
scriptHelpers = builtins.readFile ./scriptHelpers.sh; scriptHelpers = builtins.readFile ./scriptHelpers.sh;
toJsonFile = any: builtins.toFile "actual" (builtins.toJSON any);
toPrettyFile = any: builtins.toFile "actual" (lib.generators.toPretty {} any);
} }

View file

@ -1,86 +1,90 @@
{ {
lib,
pkgs, pkgs,
ntlib, ntlib,
... ...
}: { }: {
suites."Lib Tests".tests = [ suites."Lib Tests" = {
{ pos = __curPos;
name = "autodiscovery"; tests = [
type = "script"; {
script = let name = "autodiscovery";
actual = builtins.toFile "actual" (builtins.toJSON (ntlib.autodiscover { type = "script";
dir = ./fixtures; script = let
})); actual = ntlib.helpers.toPrettyFile (ntlib.autodiscover {
in dir = ./fixtures;
# sh });
'' in
${ntlib.helpers.path [pkgs.gnugrep]} # sh
${ntlib.helpers.scriptHelpers} ''
assert_file_contains ${actual} "sample_test.nix" "should find sample_test.nix" ${ntlib.helpers.path [pkgs.gnugrep]}
assert_file_contains ${actual} "\"base\":\"/nix/store/.*-source/tests/fixtures" "should set base to fixtures dir" ${ntlib.helpers.scriptHelpers}
''; assert_file_contains ${actual} "sample_test.nix" "should find sample_test.nix"
} assert_file_contains ${actual} "base = \"/nix/store/.*-source/tests/fixtures/\"" "should set base to fixtures dir"
{ '';
name = "binary"; }
type = "script"; {
script = let name = "binary";
binary = type = "script";
(ntlib.mkBinary { script = let
nixtests = "stub"; binary =
extraParams = "--pure"; (ntlib.mkBinary {
}) nixtests = "stub";
+ "/bin/nixtests:run"; extraParams = "--pure";
in })
# sh + "/bin/nixtests:run";
'' in
${ntlib.helpers.path [pkgs.gnugrep]} # sh
${ntlib.helpers.scriptHelpers} ''
assert_file_contains ${binary} "nixtest" "should contain nixtest" ${ntlib.helpers.path [pkgs.gnugrep]}
assert_file_contains ${binary} "--pure" "should contain --pure arg" ${ntlib.helpers.scriptHelpers}
assert_file_contains ${binary} "--tests=stub" "should contain --tests arg" assert_file_contains ${binary} "nixtest" "should contain nixtest"
assert_file_contains ${binary} "--pure" "should contain --pure arg"
assert_file_contains ${binary} "--tests=stub" "should contain --tests arg"
run "${binary} --help" run "${binary} --help"
assert_eq $exit_code 0 "should exit 0" assert_eq $exit_code 0 "should exit 0"
assert_contains "$output" "Usage of nixtest" "should show help" assert_contains "$output" "Usage of nixtest" "should show help"
run "${binary}" run "${binary}"
assert_eq $exit_code 1 "should exit 1" assert_eq $exit_code 1 "should exit 1"
assert_contains "$output" "Tests file does not exist" assert_contains "$output" "Tests file does not exist"
''; '';
} }
{ {
name = "full run with fixtures"; name = "full run with fixtures";
type = "script"; type = "script";
script = let script = let
binary = binary =
(ntlib.mkNixtest { (ntlib.mkNixtest {
modules = ntlib.autodiscover {dir = ./fixtures;}; modules = ntlib.autodiscover {dir = ./fixtures;};
args = {inherit pkgs;}; args = {inherit pkgs;};
}) })
+ "/bin/nixtests:run"; + "/bin/nixtests:run";
in in
# sh # sh
'' ''
${ntlib.helpers.path [pkgs.gnugrep pkgs.mktemp]} ${ntlib.helpers.path [pkgs.gnugrep pkgs.mktemp]}
${ntlib.helpers.scriptHelpers} ${ntlib.helpers.scriptHelpers}
TMPDIR=$(tmpdir) TMPDIR=$(tmpdir)
run "${binary} --pure --junit=$TMPDIR/junit.xml" # start without nix & env binaries to expect errors
assert "$exit_code -eq 2" "should exit 2" run "${binary} --pure --junit=$TMPDIR/junit.xml"
assert "-f $TMPDIR/junit.xml" "should create junit.xml" assert "$exit_code -eq 2" "should exit 2"
assert_contains "$output" "executable file not found" "nix should not be found in pure mode" assert "-f $TMPDIR/junit.xml" "should create junit.xml"
assert_contains "$output" "executable file not found" "nix should not be found in pure mode"
${ntlib.helpers.pathAdd [pkgs.nix pkgs.coreutils]} # now add required deps
run "${binary} --pure --junit=$TMPDIR/junit2.xml" ${ntlib.helpers.pathAdd [pkgs.nix pkgs.coreutils]}
assert "$exit_code -eq 2" "should exit 2" run "${binary} --pure --junit=$TMPDIR/junit2.xml"
assert "-f $TMPDIR/junit2.xml" "should create junit2.xml" assert "$exit_code -eq 2" "should exit 2"
assert_not_contains "$output" "executable file not found" "nix should now exist" assert "-f $TMPDIR/junit2.xml" "should create junit2.xml"
assert_contains "$output" "suite-one" "should contain suite-one" assert_not_contains "$output" "executable file not found" "nix should now exist"
assert_contains "$output" "8/11 (1 SKIPPED)" "should be 8/11 total" assert_contains "$output" "suite-one" "should contain suite-one"
assert_contains "$output" "ERROR" "should contain an error" assert_contains "$output" "8/11 (1 SKIPPED)" "should be 8/11 total"
assert_contains "$output" "SKIP" "should contain a skip" assert_contains "$output" "ERROR" "should contain an error"
''; assert_contains "$output" "SKIP" "should contain a skip"
} '';
]; }
];
};
} }