2015-02-08 23:48:38 +03:00
#!/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 = [ $@ ];} "
2015-05-13 23:44:04 +03:00
nix-instantiate --timeout 1 -E " $script " -A " $attr " --eval-only --show-trace
2015-02-08 23:48:38 +03:00
}
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
2015-02-09 00:51:20 +03:00
local err = ""
2015-02-08 23:48:38 +03:00
shift;
2015-02-09 00:51:20 +03:00
if err = = $( evalConfig " $@ " 2>& 1 >/dev/null) ; then
2015-02-08 23:48:38 +03:00
echo 2>& 1 "error: Expected error code, got exit code 0, while evaluating"
reportFailure " $@ "
return 1
else
2015-02-09 00:51:20 +03:00
if echo " $err " | grep --silent " $errorContains " ; then
pass = $(( pass + 1 ))
return 0;
else
echo 2>& 1 " error: Expected error matching ' $errorContains ', while evaluating "
reportFailure " $@ "
return 1
fi
2015-02-08 23:48:38 +03:00
fi
}
2015-03-17 00:38:41 +03:00
# Check boolean option.
2015-02-09 00:51:20 +03:00
checkConfigOutput "false" config.enable ./declare-enable.nix
2015-02-08 23:48:38 +03:00
checkConfigError 'The option .* defined in .* does not exist.' config.enable ./define-enable.nix
2015-03-17 00:38:41 +03:00
2017-10-31 16:23:05 +03:00
# Check integer types.
# unsigned
checkConfigOutput "42" config.value ./declare-int-unsigned-value.nix ./define-value-int-positive.nix
checkConfigError 'The option value .* in .* is not of type.*unsigned integer.*' config.value ./declare-int-unsigned-value.nix ./define-value-int-negative.nix
2017-10-31 19:30:15 +03:00
# positive
checkConfigError 'The option value .* in .* is not of type.*positive integer.*' config.value ./declare-int-positive-value.nix ./define-value-int-zero.nix
2017-10-31 16:23:05 +03:00
# between
checkConfigOutput "42" config.value ./declare-int-between-value.nix ./define-value-int-positive.nix
checkConfigError 'The option value .* in .* is not of type.*between.*-21 and 43.*inclusive.*' config.value ./declare-int-between-value.nix ./define-value-int-negative.nix
2019-08-01 16:55:33 +03:00
# Check either types
# types.either
checkConfigOutput "42" config.value ./declare-either.nix ./define-value-int-positive.nix
checkConfigOutput "\"24\"" config.value ./declare-either.nix ./define-value-string.nix
# types.oneOf
checkConfigOutput "42" config.value ./declare-oneOf.nix ./define-value-int-positive.nix
checkConfigOutput "[ ]" config.value ./declare-oneOf.nix ./define-value-list.nix
checkConfigOutput "\"24\"" config.value ./declare-oneOf.nix ./define-value-string.nix
2015-03-17 00:38:41 +03:00
# Check mkForce without submodules.
2015-02-08 23:48:38 +03:00
set -- config.enable ./declare-enable.nix ./define-enable.nix
checkConfigOutput "true" " $@ "
checkConfigOutput "false" " $@ " ./define-force-enable.nix
checkConfigOutput "false" " $@ " ./define-enable-force.nix
2015-03-17 00:38:41 +03:00
# Check mkForce with option and submodules.
2015-02-09 00:51:20 +03:00
checkConfigError 'attribute .*foo.* .* not found' config.loaOfSub.foo.enable ./declare-loaOfSub-any-enable.nix
2015-02-08 23:48:38 +03:00
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
2015-03-17 00:38:41 +03:00
# Check overriding effect of mkForce on submodule definitions.
2015-02-09 00:51:20 +03:00
checkConfigError 'attribute .*bar.* .* not found' config.loaOfSub.bar.enable ./declare-loaOfSub-any-enable.nix ./define-loaOfSub-foo.nix
2015-02-08 23:48:38 +03:00
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' " $@ "
2015-02-09 00:51:20 +03:00
checkConfigError 'attribute .*bar.* .* not found' " $@ " ./define-force-loaOfSub-foo-enable.nix
checkConfigError 'attribute .*bar.* .* not found' " $@ " ./define-loaOfSub-force-foo-enable.nix
2015-02-08 23:48:38 +03:00
checkConfigOutput 'true' " $@ " ./define-loaOfSub-foo-force-enable.nix
checkConfigOutput 'true' " $@ " ./define-loaOfSub-foo-enable-force.nix
2015-03-17 00:38:41 +03:00
# Check mkIf with submodules.
checkConfigError 'attribute .*foo.* .* not found' config.loaOfSub.foo.enable ./declare-enable.nix ./declare-loaOfSub-any-enable.nix
set -- config.loaOfSub.foo.enable ./declare-enable.nix ./declare-loaOfSub-any-enable.nix
checkConfigError 'attribute .*foo.* .* not found' " $@ " ./define-if-loaOfSub-foo-enable.nix
checkConfigError 'attribute .*foo.* .* not found' " $@ " ./define-loaOfSub-if-foo-enable.nix
checkConfigError 'attribute .*foo.* .* not found' " $@ " ./define-loaOfSub-foo-if-enable.nix
checkConfigOutput 'false' " $@ " ./define-loaOfSub-foo-enable-if.nix
checkConfigOutput 'true' " $@ " ./define-enable.nix ./define-if-loaOfSub-foo-enable.nix
checkConfigOutput 'true' " $@ " ./define-enable.nix ./define-loaOfSub-if-foo-enable.nix
checkConfigOutput 'true' " $@ " ./define-enable.nix ./define-loaOfSub-foo-if-enable.nix
checkConfigOutput 'true' " $@ " ./define-enable.nix ./define-loaOfSub-foo-enable-if.nix
2017-02-15 01:18:44 +03:00
# Check disabledModules with config definitions and option declarations.
set -- config.enable ./define-enable.nix ./declare-enable.nix
checkConfigOutput "true" " $@ "
checkConfigOutput "false" " $@ " ./disable-define-enable.nix
checkConfigError "The option .*enable.* defined in .* does not exist" " $@ " ./disable-declare-enable.nix
checkConfigError "attribute .*enable.* in selection path .*config.enable.* not found" " $@ " ./disable-define-enable.nix ./disable-declare-enable.nix
checkConfigError "attribute .*enable.* in selection path .*config.enable.* not found" " $@ " ./disable-enable-modules.nix
2015-03-17 00:38:41 +03:00
# Check _module.args.
2015-05-13 23:44:04 +03:00
set -- config.enable ./declare-enable.nix ./define-enable-with-custom-arg.nix
checkConfigError 'while evaluating the module argument .*custom.* in .*define-enable-with-custom-arg.nix.*:' " $@ "
checkConfigOutput "true" " $@ " ./define-_module-args-custom.nix
# Check that using _module.args on imports cause infinite recursions, with
# the proper error context.
set -- " $@ " ./define-_module-args-custom.nix ./import-custom-arg.nix
checkConfigError 'while evaluating the module argument .*custom.* in .*import-custom-arg.nix.*:' " $@ "
checkConfigError 'infinite recursion encountered' " $@ "
2015-03-17 00:38:41 +03:00
# Check _module.check.
set -- config.enable ./declare-enable.nix ./define-enable.nix ./define-loaOfSub-foo.nix
checkConfigError 'The option .* defined in .* does not exist.' " $@ "
checkConfigOutput "true" " $@ " ./define-module-check.nix
2017-02-02 01:52:40 +03:00
# Check coerced value.
checkConfigOutput "\"42\"" config.value ./declare-coerced-value.nix
checkConfigOutput "\"24\"" config.value ./declare-coerced-value.nix ./define-value-string.nix
2018-04-04 17:55:45 +03:00
checkConfigError 'The option value .* in .* is not.*string or signed integer convertible to it' config.value ./declare-coerced-value.nix ./define-value-list.nix
2017-02-02 01:52:40 +03:00
2018-04-04 17:51:46 +03:00
# Check coerced value with unsound coercion
checkConfigOutput "12" config.value ./declare-coerced-value-unsound.nix
2018-04-04 17:55:45 +03:00
checkConfigError 'The option value .* in .* is not.*8 bit signed integer.* or string convertible to it' config.value ./declare-coerced-value-unsound.nix ./define-value-string-bigint.nix
2018-04-04 17:51:46 +03:00
checkConfigError 'unrecognised JSON value' config.value ./declare-coerced-value-unsound.nix ./define-value-string-arbitrary.nix
2018-03-24 19:38:45 +03:00
# Check loaOf with long list.
checkConfigOutput "1 2 3 4 5 6 7 8 9 10" config.result ./loaOf-with-long-list.nix
# Check loaOf with many merges of lists.
checkConfigOutput "1 2 3 4 5 6 7 8 9 10" config.result ./loaOf-with-many-list-merges.nix
2019-01-24 06:58:33 +03:00
# Check mkAliasOptionModule.
2019-01-04 10:34:59 +03:00
checkConfigOutput "true" config.enable ./alias-with-priority.nix
2019-01-06 12:10:03 +03:00
checkConfigOutput "true" config.enableAlias ./alias-with-priority.nix
checkConfigOutput "false" config.enable ./alias-with-priority-can-override.nix
checkConfigOutput "false" config.enableAlias ./alias-with-priority-can-override.nix
2019-01-04 10:34:59 +03:00
2015-02-08 23:48:38 +03:00
cat <<EOF
= = = = = = module tests = = = = = =
$pass Pass
$fail Fail
EOF
if test $fail -ne 0; then
exit 1
fi
exit 0