Issue #6161 - Add tests for NixOS modules.
This commit is contained in:
parent
d54611bde7
commit
6d15e32536
93
lib/tests/modules.sh
Executable file
93
lib/tests/modules.sh
Executable file
@ -0,0 +1,93 @@
|
||||
#!/bin/sh
|
||||
#
|
||||
# This script is used to test that the module system is working as expected.
|
||||
# By default it test the version of nixpkgs which is defined in the NIX_PATH.
|
||||
|
||||
cd ./modules
|
||||
|
||||
pass=0
|
||||
fail=0
|
||||
|
||||
evalConfig() {
|
||||
local attr=$1
|
||||
shift;
|
||||
local script="import ./default.nix { modules = [ $@ ];}"
|
||||
nix-instantiate -E "$script" -A "$attr" --eval-only
|
||||
}
|
||||
|
||||
reportFailure() {
|
||||
local attr=$1
|
||||
shift;
|
||||
local script="import ./default.nix { modules = [ $@ ];}"
|
||||
echo 2>&1 "$ nix-instantiate -E '$script' -A '$attr' --eval-only"
|
||||
evalConfig "$attr" "$@"
|
||||
fail=$((fail + 1))
|
||||
}
|
||||
|
||||
checkConfigOutput() {
|
||||
local outputContains=$1
|
||||
shift;
|
||||
if evalConfig "$@" 2>/dev/null | grep --silent "$outputContains" ; then
|
||||
pass=$((pass + 1))
|
||||
return 0;
|
||||
else
|
||||
echo 2>&1 "error: Expected result matching '$outputContains', while evaluating"
|
||||
reportFailure "$@"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
checkConfigError() {
|
||||
local errorContains=$1
|
||||
shift;
|
||||
if evalConfig "$@" 1>/dev/null 2>&1; then
|
||||
echo 2>&1 "error: Expected error code, got exit code 0, while evaluating"
|
||||
reportFailure "$@"
|
||||
return 1
|
||||
fi
|
||||
|
||||
if evalConfig "$@" 2>&1 >/dev/null | grep --silent "$errorContains" ; then
|
||||
pass=$((pass + 1))
|
||||
return 0;
|
||||
else
|
||||
echo 2>&1 "error: Expected error matching '$errorContains', while evaluating"
|
||||
reportFailure "$@"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
checkConfigOutput "false" config.enable
|
||||
checkConfigError 'The option .* defined in .* does not exist.' config.enable ./define-enable.nix
|
||||
set -- config.enable ./declare-enable.nix ./define-enable.nix
|
||||
checkConfigOutput "true" "$@"
|
||||
checkConfigOutput "false" "$@" ./define-force-enable.nix
|
||||
checkConfigOutput "false" "$@" ./define-enable-force.nix
|
||||
|
||||
checkConfigError 'attribute .foo. .* not found' config.loaOfSub.foo.enable ./declare-loaOfSub-any-enable.nix
|
||||
checkConfigOutput 'false' config.loaOfSub.foo.enable ./declare-loaOfSub-any-enable.nix ./define-loaOfSub-foo.nix
|
||||
set -- config.loaOfSub.foo.enable ./declare-loaOfSub-any-enable.nix ./define-loaOfSub-foo-enable.nix
|
||||
checkConfigOutput 'true' "$@"
|
||||
checkConfigOutput 'false' "$@" ./define-force-loaOfSub-foo-enable.nix
|
||||
checkConfigOutput 'false' "$@" ./define-loaOfSub-force-foo-enable.nix
|
||||
checkConfigOutput 'false' "$@" ./define-loaOfSub-foo-force-enable.nix
|
||||
checkConfigOutput 'false' "$@" ./define-loaOfSub-foo-enable-force.nix
|
||||
|
||||
checkConfigError 'attribute .bar. .* not found' config.loaOfSub.bar.enable ./declare-loaOfSub-any-enable.nix ./define-loaOfSub-foo.nix
|
||||
checkConfigOutput 'false' config.loaOfSub.bar.enable ./declare-loaOfSub-any-enable.nix ./define-loaOfSub-foo.nix ./define-loaOfSub-bar.nix
|
||||
set -- config.loaOfSub.bar.enable ./declare-loaOfSub-any-enable.nix ./define-loaOfSub-foo.nix ./define-loaOfSub-bar-enable.nix
|
||||
checkConfigOutput 'true' "$@"
|
||||
checkConfigError 'attribute .bar. .* not found' "$@" ./define-force-loaOfSub-foo-enable.nix
|
||||
checkConfigError 'attribute .bar. .* not found' "$@" ./define-loaOfSub-force-foo-enable.nix
|
||||
checkConfigOutput 'true' "$@" ./define-loaOfSub-foo-force-enable.nix
|
||||
checkConfigOutput 'true' "$@" ./define-loaOfSub-foo-enable-force.nix
|
||||
|
||||
cat <<EOF
|
||||
====== module tests ======
|
||||
$pass Pass
|
||||
$fail Fail
|
||||
EOF
|
||||
|
||||
if test $fail -ne 0; then
|
||||
exit 1
|
||||
fi
|
||||
exit 0
|
14
lib/tests/modules/declare-enable.nix
Normal file
14
lib/tests/modules/declare-enable.nix
Normal file
@ -0,0 +1,14 @@
|
||||
{ lib, ... }:
|
||||
|
||||
{
|
||||
options = {
|
||||
enable = lib.mkOption {
|
||||
default = false;
|
||||
example = true;
|
||||
type = lib.types.bool;
|
||||
description = ''
|
||||
Some descriptive text
|
||||
'';
|
||||
};
|
||||
};
|
||||
}
|
29
lib/tests/modules/declare-loaOfSub-any-enable.nix
Normal file
29
lib/tests/modules/declare-loaOfSub-any-enable.nix
Normal file
@ -0,0 +1,29 @@
|
||||
{ lib, ... }:
|
||||
|
||||
let
|
||||
submod = { ... }: {
|
||||
options = {
|
||||
enable = lib.mkOption {
|
||||
default = false;
|
||||
example = true;
|
||||
type = lib.types.bool;
|
||||
description = ''
|
||||
Some descriptive text
|
||||
'';
|
||||
};
|
||||
};
|
||||
};
|
||||
in
|
||||
|
||||
{
|
||||
options = {
|
||||
loaOfSub = lib.mkOption {
|
||||
default = {};
|
||||
example = {};
|
||||
type = lib.types.loaOf (lib.types.submodule [ submod ]);
|
||||
description = ''
|
||||
Some descriptive text
|
||||
'';
|
||||
};
|
||||
};
|
||||
}
|
7
lib/tests/modules/default.nix
Normal file
7
lib/tests/modules/default.nix
Normal file
@ -0,0 +1,7 @@
|
||||
{ lib ? import <nixpkgs/lib>, modules ? [] }:
|
||||
|
||||
{
|
||||
inherit (lib.evalModules {
|
||||
inherit modules;
|
||||
}) config options;
|
||||
}
|
5
lib/tests/modules/define-enable-force.nix
Normal file
5
lib/tests/modules/define-enable-force.nix
Normal file
@ -0,0 +1,5 @@
|
||||
{ lib, ... }:
|
||||
|
||||
{
|
||||
enable = lib.mkForce false;
|
||||
}
|
3
lib/tests/modules/define-enable.nix
Normal file
3
lib/tests/modules/define-enable.nix
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
enable = true;
|
||||
}
|
5
lib/tests/modules/define-force-enable.nix
Normal file
5
lib/tests/modules/define-force-enable.nix
Normal file
@ -0,0 +1,5 @@
|
||||
{ lib, ... }:
|
||||
|
||||
lib.mkForce {
|
||||
enable = false;
|
||||
}
|
5
lib/tests/modules/define-force-loaOfSub-foo-enable.nix
Normal file
5
lib/tests/modules/define-force-loaOfSub-foo-enable.nix
Normal file
@ -0,0 +1,5 @@
|
||||
{ lib, ... }:
|
||||
|
||||
lib.mkForce {
|
||||
loaOfSub.foo.enable = false;
|
||||
}
|
3
lib/tests/modules/define-loaOfSub-bar-enable.nix
Normal file
3
lib/tests/modules/define-loaOfSub-bar-enable.nix
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
loaOfSub.bar.enable = true;
|
||||
}
|
3
lib/tests/modules/define-loaOfSub-bar.nix
Normal file
3
lib/tests/modules/define-loaOfSub-bar.nix
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
loaOfSub.bar = {};
|
||||
}
|
5
lib/tests/modules/define-loaOfSub-foo-enable-force.nix
Normal file
5
lib/tests/modules/define-loaOfSub-foo-enable-force.nix
Normal file
@ -0,0 +1,5 @@
|
||||
{ lib, ... }:
|
||||
|
||||
{
|
||||
loaOfSub.foo.enable = lib.mkForce false;
|
||||
}
|
3
lib/tests/modules/define-loaOfSub-foo-enable.nix
Normal file
3
lib/tests/modules/define-loaOfSub-foo-enable.nix
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
loaOfSub.foo.enable = true;
|
||||
}
|
7
lib/tests/modules/define-loaOfSub-foo-force-enable.nix
Normal file
7
lib/tests/modules/define-loaOfSub-foo-force-enable.nix
Normal file
@ -0,0 +1,7 @@
|
||||
{ lib, ... }:
|
||||
|
||||
{
|
||||
loaOfSub.foo = lib.mkForce {
|
||||
enable = false;
|
||||
};
|
||||
}
|
3
lib/tests/modules/define-loaOfSub-foo.nix
Normal file
3
lib/tests/modules/define-loaOfSub-foo.nix
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
loaOfSub.foo = {};
|
||||
}
|
7
lib/tests/modules/define-loaOfSub-force-foo-enable.nix
Normal file
7
lib/tests/modules/define-loaOfSub-force-foo-enable.nix
Normal file
@ -0,0 +1,7 @@
|
||||
{ lib, ... }:
|
||||
|
||||
{
|
||||
loaOfSub = lib.mkForce {
|
||||
foo.enable = false;
|
||||
};
|
||||
}
|
Loading…
Reference in New Issue
Block a user