1#!/bin/sh
2#
3# This script is used to test that the module system is working as expected.
4# By default it test the version of nixpkgs which is defined in the NIX_PATH.
5
6cd ./modules
7
8pass=0
9fail=0
10
11evalConfig() {
12 local attr=$1
13 shift;
14 local script="import ./default.nix { modules = [ $@ ];}"
15 nix-instantiate --timeout 1 -E "$script" -A "$attr" --eval-only --show-trace
16}
17
18reportFailure() {
19 local attr=$1
20 shift;
21 local script="import ./default.nix { modules = [ $@ ];}"
22 echo 2>&1 "$ nix-instantiate -E '$script' -A '$attr' --eval-only"
23 evalConfig "$attr" "$@"
24 fail=$((fail + 1))
25}
26
27checkConfigOutput() {
28 local outputContains=$1
29 shift;
30 if evalConfig "$@" 2>/dev/null | grep --silent "$outputContains" ; then
31 pass=$((pass + 1))
32 return 0;
33 else
34 echo 2>&1 "error: Expected result matching '$outputContains', while evaluating"
35 reportFailure "$@"
36 return 1
37 fi
38}
39
40checkConfigError() {
41 local errorContains=$1
42 local err=""
43 shift;
44 if err==$(evalConfig "$@" 2>&1 >/dev/null); then
45 echo 2>&1 "error: Expected error code, got exit code 0, while evaluating"
46 reportFailure "$@"
47 return 1
48 else
49 if echo "$err" | grep --silent "$errorContains" ; then
50 pass=$((pass + 1))
51 return 0;
52 else
53 echo 2>&1 "error: Expected error matching '$errorContains', while evaluating"
54 reportFailure "$@"
55 return 1
56 fi
57 fi
58}
59
60# Check boolean option.
61checkConfigOutput "false" config.enable ./declare-enable.nix
62checkConfigError 'The option .* defined in .* does not exist.' config.enable ./define-enable.nix
63
64# Check mkForce without submodules.
65set -- config.enable ./declare-enable.nix ./define-enable.nix
66checkConfigOutput "true" "$@"
67checkConfigOutput "false" "$@" ./define-force-enable.nix
68checkConfigOutput "false" "$@" ./define-enable-force.nix
69
70# Check mkForce with option and submodules.
71checkConfigError 'attribute .*foo.* .* not found' config.loaOfSub.foo.enable ./declare-loaOfSub-any-enable.nix
72checkConfigOutput 'false' config.loaOfSub.foo.enable ./declare-loaOfSub-any-enable.nix ./define-loaOfSub-foo.nix
73set -- config.loaOfSub.foo.enable ./declare-loaOfSub-any-enable.nix ./define-loaOfSub-foo-enable.nix
74checkConfigOutput 'true' "$@"
75checkConfigOutput 'false' "$@" ./define-force-loaOfSub-foo-enable.nix
76checkConfigOutput 'false' "$@" ./define-loaOfSub-force-foo-enable.nix
77checkConfigOutput 'false' "$@" ./define-loaOfSub-foo-force-enable.nix
78checkConfigOutput 'false' "$@" ./define-loaOfSub-foo-enable-force.nix
79
80# Check overriding effect of mkForce on submodule definitions.
81checkConfigError 'attribute .*bar.* .* not found' config.loaOfSub.bar.enable ./declare-loaOfSub-any-enable.nix ./define-loaOfSub-foo.nix
82checkConfigOutput 'false' config.loaOfSub.bar.enable ./declare-loaOfSub-any-enable.nix ./define-loaOfSub-foo.nix ./define-loaOfSub-bar.nix
83set -- config.loaOfSub.bar.enable ./declare-loaOfSub-any-enable.nix ./define-loaOfSub-foo.nix ./define-loaOfSub-bar-enable.nix
84checkConfigOutput 'true' "$@"
85checkConfigError 'attribute .*bar.* .* not found' "$@" ./define-force-loaOfSub-foo-enable.nix
86checkConfigError 'attribute .*bar.* .* not found' "$@" ./define-loaOfSub-force-foo-enable.nix
87checkConfigOutput 'true' "$@" ./define-loaOfSub-foo-force-enable.nix
88checkConfigOutput 'true' "$@" ./define-loaOfSub-foo-enable-force.nix
89
90# Check mkIf with submodules.
91checkConfigError 'attribute .*foo.* .* not found' config.loaOfSub.foo.enable ./declare-enable.nix ./declare-loaOfSub-any-enable.nix
92set -- config.loaOfSub.foo.enable ./declare-enable.nix ./declare-loaOfSub-any-enable.nix
93checkConfigError 'attribute .*foo.* .* not found' "$@" ./define-if-loaOfSub-foo-enable.nix
94checkConfigError 'attribute .*foo.* .* not found' "$@" ./define-loaOfSub-if-foo-enable.nix
95checkConfigError 'attribute .*foo.* .* not found' "$@" ./define-loaOfSub-foo-if-enable.nix
96checkConfigOutput 'false' "$@" ./define-loaOfSub-foo-enable-if.nix
97checkConfigOutput 'true' "$@" ./define-enable.nix ./define-if-loaOfSub-foo-enable.nix
98checkConfigOutput 'true' "$@" ./define-enable.nix ./define-loaOfSub-if-foo-enable.nix
99checkConfigOutput 'true' "$@" ./define-enable.nix ./define-loaOfSub-foo-if-enable.nix
100checkConfigOutput 'true' "$@" ./define-enable.nix ./define-loaOfSub-foo-enable-if.nix
101
102# Check _module.args.
103set -- config.enable ./declare-enable.nix ./define-enable-with-custom-arg.nix
104checkConfigError 'while evaluating the module argument .*custom.* in .*define-enable-with-custom-arg.nix.*:' "$@"
105checkConfigOutput "true" "$@" ./define-_module-args-custom.nix
106
107# Check that using _module.args on imports cause infinite recursions, with
108# the proper error context.
109set -- "$@" ./define-_module-args-custom.nix ./import-custom-arg.nix
110checkConfigError 'while evaluating the module argument .*custom.* in .*import-custom-arg.nix.*:' "$@"
111checkConfigError 'infinite recursion encountered' "$@"
112
113# Check _module.check.
114set -- config.enable ./declare-enable.nix ./define-enable.nix ./define-loaOfSub-foo.nix
115checkConfigError 'The option .* defined in .* does not exist.' "$@"
116checkConfigOutput "true" "$@" ./define-module-check.nix
117
118cat <<EOF
119====== module tests ======
120$pass Pass
121$fail Fail
122EOF
123
124if test $fail -ne 0; then
125 exit 1
126fi
127exit 0