mirror of
https://gitlab.com/TECHNOFAB/nixtest.git
synced 2025-12-12 02:00:18 +01:00
test: nest sample tests as fixtures in real tests
also use new test helpers
This commit is contained in:
parent
bed029f4a9
commit
006537e6ab
4 changed files with 186 additions and 142 deletions
101
flake.nix
101
flake.nix
|
|
@ -11,7 +11,6 @@
|
||||||
inputs.nix-gitlab-ci.flakeModule
|
inputs.nix-gitlab-ci.flakeModule
|
||||||
inputs.nix-devtools.flakeModule
|
inputs.nix-devtools.flakeModule
|
||||||
inputs.nix-mkdocs.flakeModule
|
inputs.nix-mkdocs.flakeModule
|
||||||
./lib/flakeModule.nix
|
|
||||||
];
|
];
|
||||||
systems = import systems;
|
systems = import systems;
|
||||||
flake = {};
|
flake = {};
|
||||||
|
|
@ -63,100 +62,6 @@
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
nixtest = {
|
|
||||||
skip = "skip.*d";
|
|
||||||
suites = {
|
|
||||||
"suite-one" = {
|
|
||||||
pos = __curPos;
|
|
||||||
tests = [
|
|
||||||
{
|
|
||||||
name = "test-one";
|
|
||||||
# required to figure out file and line, but optional
|
|
||||||
expected = 1;
|
|
||||||
actual = 1;
|
|
||||||
}
|
|
||||||
{
|
|
||||||
name = "fail";
|
|
||||||
expected = 0;
|
|
||||||
actual = "meow";
|
|
||||||
}
|
|
||||||
{
|
|
||||||
name = "snapshot-test";
|
|
||||||
type = "snapshot";
|
|
||||||
actual = "test";
|
|
||||||
}
|
|
||||||
{
|
|
||||||
name = "test-snapshot-drv";
|
|
||||||
type = "snapshot";
|
|
||||||
actualDrv = pkgs.runCommand "test-snapshot" {} ''
|
|
||||||
echo '"snapshot drv"' > $out
|
|
||||||
'';
|
|
||||||
}
|
|
||||||
{
|
|
||||||
name = "test-error-drv";
|
|
||||||
expected = null;
|
|
||||||
actualDrv = pkgs.runCommand "test-error-drv" {} ''
|
|
||||||
echo "This works, but its better to just write 'fail' to \$out and expect 'success' or sth."
|
|
||||||
exit 1
|
|
||||||
'';
|
|
||||||
}
|
|
||||||
{
|
|
||||||
name = "test-script";
|
|
||||||
type = "script";
|
|
||||||
script = ''
|
|
||||||
echo Test something here
|
|
||||||
# required in pure mode:
|
|
||||||
export PATH="${lib.makeBinPath [pkgs.gnugrep]}"
|
|
||||||
grep -q "test" ${builtins.toFile "test" "test"}
|
|
||||||
'';
|
|
||||||
}
|
|
||||||
];
|
|
||||||
};
|
|
||||||
"other-suite".tests = [
|
|
||||||
{
|
|
||||||
name = "obj-snapshot";
|
|
||||||
type = "snapshot";
|
|
||||||
pos = __curPos;
|
|
||||||
actual = {hello = "world";};
|
|
||||||
}
|
|
||||||
{
|
|
||||||
name = "pretty-snapshot";
|
|
||||||
type = "snapshot";
|
|
||||||
format = "pretty";
|
|
||||||
pos = __curPos;
|
|
||||||
actual = {
|
|
||||||
example = args: {};
|
|
||||||
example2 = {
|
|
||||||
drv = pkgs.hello;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
}
|
|
||||||
{
|
|
||||||
name = "pretty-unit";
|
|
||||||
format = "pretty";
|
|
||||||
pos = __curPos;
|
|
||||||
expected = pkgs.hello;
|
|
||||||
actual = pkgs.hello;
|
|
||||||
}
|
|
||||||
{
|
|
||||||
name = "test-drv";
|
|
||||||
pos = __curPos;
|
|
||||||
expected = {a = "b";};
|
|
||||||
actualDrv = pkgs.runCommand "test-something" {} ''
|
|
||||||
echo "Simulating taking some time"
|
|
||||||
sleep 1
|
|
||||||
echo '{"a":"b"}' > $out
|
|
||||||
'';
|
|
||||||
}
|
|
||||||
{
|
|
||||||
name = "skipped";
|
|
||||||
expected = null;
|
|
||||||
actual = null;
|
|
||||||
}
|
|
||||||
];
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
doc = {
|
doc = {
|
||||||
path = ./docs;
|
path = ./docs;
|
||||||
deps = pp: [
|
deps = pp: [
|
||||||
|
|
@ -315,10 +220,10 @@
|
||||||
ntlib = import ./lib {inherit pkgs lib;};
|
ntlib = import ./lib {inherit pkgs lib;};
|
||||||
in {
|
in {
|
||||||
default = pkgs.callPackage ./package.nix {};
|
default = pkgs.callPackage ./package.nix {};
|
||||||
lib-tests = ntlib.mkNixtest {
|
tests = ntlib.mkNixtest {
|
||||||
modules = ntlib.autodiscover {dir = ./lib;};
|
modules = ntlib.autodiscover {dir = ./tests;};
|
||||||
args = {
|
args = {
|
||||||
inherit pkgs;
|
inherit pkgs ntlib;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,44 +0,0 @@
|
||||||
{
|
|
||||||
pkgs,
|
|
||||||
lib,
|
|
||||||
...
|
|
||||||
}: let
|
|
||||||
ntlib = import ./. {inherit pkgs lib;};
|
|
||||||
in {
|
|
||||||
suites."Lib Tests".tests = [
|
|
||||||
{
|
|
||||||
name = "autodiscovery";
|
|
||||||
type = "script";
|
|
||||||
script = let
|
|
||||||
actual = builtins.toFile "actual" (builtins.toJSON (ntlib.autodiscover {
|
|
||||||
dir = ./.;
|
|
||||||
}));
|
|
||||||
in
|
|
||||||
# sh
|
|
||||||
''
|
|
||||||
export PATH="${pkgs.gnugrep}/bin"
|
|
||||||
grep -q lib_test.nix ${actual}
|
|
||||||
grep -q "\"base\":\"/nix/store/.*-source/lib/" ${actual}
|
|
||||||
'';
|
|
||||||
}
|
|
||||||
{
|
|
||||||
name = "binary";
|
|
||||||
type = "script";
|
|
||||||
script = let
|
|
||||||
binary =
|
|
||||||
(ntlib.mkBinary {
|
|
||||||
nixtests = "stub";
|
|
||||||
extraParams = "--pure";
|
|
||||||
})
|
|
||||||
+ "/bin/nixtests:run";
|
|
||||||
in
|
|
||||||
# sh
|
|
||||||
''
|
|
||||||
export PATH="${pkgs.gnugrep}/bin"
|
|
||||||
grep -q nixtest ${binary}
|
|
||||||
grep -q -- "--pure" ${binary}
|
|
||||||
grep -q -- "--tests=stub" ${binary}
|
|
||||||
'';
|
|
||||||
}
|
|
||||||
];
|
|
||||||
}
|
|
||||||
97
tests/fixtures/sample_test.nix
vendored
Normal file
97
tests/fixtures/sample_test.nix
vendored
Normal file
|
|
@ -0,0 +1,97 @@
|
||||||
|
{
|
||||||
|
lib,
|
||||||
|
pkgs,
|
||||||
|
...
|
||||||
|
}: {
|
||||||
|
skip = "skip.*d";
|
||||||
|
suites = {
|
||||||
|
"suite-one" = {
|
||||||
|
# required to figure out file and line, but optional
|
||||||
|
pos = __curPos;
|
||||||
|
tests = [
|
||||||
|
{
|
||||||
|
name = "test-one";
|
||||||
|
expected = 1;
|
||||||
|
actual = 1;
|
||||||
|
}
|
||||||
|
{
|
||||||
|
name = "fail";
|
||||||
|
expected = 0;
|
||||||
|
actual = "meow";
|
||||||
|
}
|
||||||
|
{
|
||||||
|
name = "snapshot-test";
|
||||||
|
type = "snapshot";
|
||||||
|
actual = "test";
|
||||||
|
}
|
||||||
|
{
|
||||||
|
name = "test-snapshot-drv";
|
||||||
|
type = "snapshot";
|
||||||
|
actualDrv = pkgs.runCommand "test-snapshot" {} ''
|
||||||
|
echo '"snapshot drv"' > $out
|
||||||
|
'';
|
||||||
|
}
|
||||||
|
{
|
||||||
|
name = "test-error-drv";
|
||||||
|
expected = null;
|
||||||
|
actualDrv = pkgs.runCommand "test-error-drv" {} ''
|
||||||
|
echo "This works, but its better to just write 'fail' to \$out and expect 'success' or sth."
|
||||||
|
exit 1
|
||||||
|
'';
|
||||||
|
}
|
||||||
|
{
|
||||||
|
name = "test-script";
|
||||||
|
type = "script";
|
||||||
|
script = ''
|
||||||
|
echo Test something here
|
||||||
|
# required in pure mode:
|
||||||
|
export PATH="${lib.makeBinPath [pkgs.gnugrep]}"
|
||||||
|
grep -q "test" ${builtins.toFile "test" "test"}
|
||||||
|
'';
|
||||||
|
}
|
||||||
|
];
|
||||||
|
};
|
||||||
|
"other-suite".tests = [
|
||||||
|
{
|
||||||
|
name = "obj-snapshot";
|
||||||
|
type = "snapshot";
|
||||||
|
pos = __curPos;
|
||||||
|
actual = {hello = "world";};
|
||||||
|
}
|
||||||
|
{
|
||||||
|
name = "pretty-snapshot";
|
||||||
|
type = "snapshot";
|
||||||
|
format = "pretty";
|
||||||
|
pos = __curPos;
|
||||||
|
actual = {
|
||||||
|
example = args: {};
|
||||||
|
example2 = {
|
||||||
|
drv = pkgs.hello;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
{
|
||||||
|
name = "pretty-unit";
|
||||||
|
format = "pretty";
|
||||||
|
pos = __curPos;
|
||||||
|
expected = pkgs.hello;
|
||||||
|
actual = pkgs.hello;
|
||||||
|
}
|
||||||
|
{
|
||||||
|
name = "test-drv";
|
||||||
|
pos = __curPos;
|
||||||
|
expected = {a = "b";};
|
||||||
|
actualDrv = pkgs.runCommand "test-something" {} ''
|
||||||
|
echo "Simulating taking some time"
|
||||||
|
sleep 1
|
||||||
|
echo '{"a":"b"}' > $out
|
||||||
|
'';
|
||||||
|
}
|
||||||
|
{
|
||||||
|
name = "skipped";
|
||||||
|
expected = null;
|
||||||
|
actual = null;
|
||||||
|
}
|
||||||
|
];
|
||||||
|
};
|
||||||
|
}
|
||||||
86
tests/lib_test.nix
Normal file
86
tests/lib_test.nix
Normal file
|
|
@ -0,0 +1,86 @@
|
||||||
|
{
|
||||||
|
lib,
|
||||||
|
pkgs,
|
||||||
|
ntlib,
|
||||||
|
...
|
||||||
|
}: {
|
||||||
|
suites."Lib Tests".tests = [
|
||||||
|
{
|
||||||
|
name = "autodiscovery";
|
||||||
|
type = "script";
|
||||||
|
script = let
|
||||||
|
actual = builtins.toFile "actual" (builtins.toJSON (ntlib.autodiscover {
|
||||||
|
dir = ./fixtures;
|
||||||
|
}));
|
||||||
|
in
|
||||||
|
# sh
|
||||||
|
''
|
||||||
|
${ntlib.helpers.path [pkgs.gnugrep]}
|
||||||
|
${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
|
||||||
|
binary =
|
||||||
|
(ntlib.mkBinary {
|
||||||
|
nixtests = "stub";
|
||||||
|
extraParams = "--pure";
|
||||||
|
})
|
||||||
|
+ "/bin/nixtests:run";
|
||||||
|
in
|
||||||
|
# sh
|
||||||
|
''
|
||||||
|
${ntlib.helpers.path [pkgs.gnugrep]}
|
||||||
|
${ntlib.helpers.scriptHelpers}
|
||||||
|
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"
|
||||||
|
assert_eq $exit_code 0 "should exit 0"
|
||||||
|
assert_contains "$output" "Usage of nixtest" "should show help"
|
||||||
|
|
||||||
|
run "${binary}"
|
||||||
|
assert_eq $exit_code 1 "should exit 1"
|
||||||
|
assert_contains "$output" "Tests file does not exist"
|
||||||
|
'';
|
||||||
|
}
|
||||||
|
{
|
||||||
|
name = "full run with fixtures";
|
||||||
|
type = "script";
|
||||||
|
script = let
|
||||||
|
binary =
|
||||||
|
(ntlib.mkNixtest {
|
||||||
|
modules = ntlib.autodiscover {dir = ./fixtures;};
|
||||||
|
args = {inherit pkgs;};
|
||||||
|
})
|
||||||
|
+ "/bin/nixtests:run";
|
||||||
|
in
|
||||||
|
# sh
|
||||||
|
''
|
||||||
|
${ntlib.helpers.path [pkgs.gnugrep pkgs.mktemp]}
|
||||||
|
${ntlib.helpers.scriptHelpers}
|
||||||
|
|
||||||
|
TMPDIR=$(tmpdir)
|
||||||
|
run "${binary} --pure --junit=$TMPDIR/junit.xml"
|
||||||
|
assert "$exit_code -eq 2" "should exit 2"
|
||||||
|
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]}
|
||||||
|
run "${binary} --pure --junit=$TMPDIR/junit2.xml"
|
||||||
|
assert "$exit_code -eq 2" "should exit 2"
|
||||||
|
assert "-f $TMPDIR/junit2.xml" "should create junit2.xml"
|
||||||
|
assert_not_contains "$output" "executable file not found" "nix should now exist"
|
||||||
|
assert_contains "$output" "suite-one" "should contain suite-one"
|
||||||
|
assert_contains "$output" "8/11 (1 SKIPPED)" "should be 8/11 total"
|
||||||
|
assert_contains "$output" "ERROR" "should contain an error"
|
||||||
|
assert_contains "$output" "SKIP" "should contain a skip"
|
||||||
|
'';
|
||||||
|
}
|
||||||
|
];
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue