@@ -0,0 +1,93 @@
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+
6+cd ./modules
7+
8+pass=0
9+fail=0
10+
11+evalConfig() {
12+ local attr=$1
13+ shift;
14+ local script="import ./default.nix { modules = [ $@ ];}"
15+ nix-instantiate -E "$script" -A "$attr" --eval-only
16+}
17+
18+reportFailure() {
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+
27+checkConfigOutput() {
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+
40+checkConfigError() {
41+ local errorContains=$1
42+ shift;
43+ if evalConfig "$@" 1>/dev/null 2>&1; then
44+ echo 2>&1 "error: Expected error code, got exit code 0, while evaluating"
45+ reportFailure "$@"
46+ return 1
47+ fi
48+
49+ if evalConfig "$@" 2>&1 >/dev/null | 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+}
58+
59+checkConfigOutput "false" config.enable
60+checkConfigError 'The option .* defined in .* does not exist.' config.enable ./define-enable.nix
61+set -- config.enable ./declare-enable.nix ./define-enable.nix
62+checkConfigOutput "true" "$@"
63+checkConfigOutput "false" "$@" ./define-force-enable.nix
64+checkConfigOutput "false" "$@" ./define-enable-force.nix
65+
66+checkConfigError 'attribute .foo. .* not found' config.loaOfSub.foo.enable ./declare-loaOfSub-any-enable.nix
67+checkConfigOutput 'false' config.loaOfSub.foo.enable ./declare-loaOfSub-any-enable.nix ./define-loaOfSub-foo.nix
68+set -- config.loaOfSub.foo.enable ./declare-loaOfSub-any-enable.nix ./define-loaOfSub-foo-enable.nix
69+checkConfigOutput 'true' "$@"
70+checkConfigOutput 'false' "$@" ./define-force-loaOfSub-foo-enable.nix
71+checkConfigOutput 'false' "$@" ./define-loaOfSub-force-foo-enable.nix
72+checkConfigOutput 'false' "$@" ./define-loaOfSub-foo-force-enable.nix
73+checkConfigOutput 'false' "$@" ./define-loaOfSub-foo-enable-force.nix
74+
75+checkConfigError 'attribute .bar. .* not found' config.loaOfSub.bar.enable ./declare-loaOfSub-any-enable.nix ./define-loaOfSub-foo.nix
76+checkConfigOutput 'false' config.loaOfSub.bar.enable ./declare-loaOfSub-any-enable.nix ./define-loaOfSub-foo.nix ./define-loaOfSub-bar.nix
77+set -- config.loaOfSub.bar.enable ./declare-loaOfSub-any-enable.nix ./define-loaOfSub-foo.nix ./define-loaOfSub-bar-enable.nix
78+checkConfigOutput 'true' "$@"
79+checkConfigError 'attribute .bar. .* not found' "$@" ./define-force-loaOfSub-foo-enable.nix
80+checkConfigError 'attribute .bar. .* not found' "$@" ./define-loaOfSub-force-foo-enable.nix
81+checkConfigOutput 'true' "$@" ./define-loaOfSub-foo-force-enable.nix
82+checkConfigOutput 'true' "$@" ./define-loaOfSub-foo-enable-force.nix
83+
84+cat <<EOF
85+====== module tests ======
86+$pass Pass
87+$fail Fail
88+EOF
89+
90+if test $fail -ne 0; then
91+ exit 1
92+fi
93+exit 0