@@ -0,0 +1,94 @@
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 --timeout 1 -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+ 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+checkConfigOutput "false" config.enable ./declare-enable.nix
61+checkConfigError 'The option .* defined in .* does not exist.' config.enable ./define-enable.nix
62+set -- config.enable ./declare-enable.nix ./define-enable.nix
63+checkConfigOutput "true" "$@"
64+checkConfigOutput "false" "$@" ./define-force-enable.nix
65+checkConfigOutput "false" "$@" ./define-enable-force.nix
66+
67+checkConfigError 'attribute .*foo.* .* not found' config.loaOfSub.foo.enable ./declare-loaOfSub-any-enable.nix
68+checkConfigOutput 'false' config.loaOfSub.foo.enable ./declare-loaOfSub-any-enable.nix ./define-loaOfSub-foo.nix
69+set -- config.loaOfSub.foo.enable ./declare-loaOfSub-any-enable.nix ./define-loaOfSub-foo-enable.nix
70+checkConfigOutput 'true' "$@"
71+checkConfigOutput 'false' "$@" ./define-force-loaOfSub-foo-enable.nix
72+checkConfigOutput 'false' "$@" ./define-loaOfSub-force-foo-enable.nix
73+checkConfigOutput 'false' "$@" ./define-loaOfSub-foo-force-enable.nix
74+checkConfigOutput 'false' "$@" ./define-loaOfSub-foo-enable-force.nix
75+
76+checkConfigError 'attribute .*bar.* .* not found' config.loaOfSub.bar.enable ./declare-loaOfSub-any-enable.nix ./define-loaOfSub-foo.nix
77+checkConfigOutput 'false' config.loaOfSub.bar.enable ./declare-loaOfSub-any-enable.nix ./define-loaOfSub-foo.nix ./define-loaOfSub-bar.nix
78+set -- config.loaOfSub.bar.enable ./declare-loaOfSub-any-enable.nix ./define-loaOfSub-foo.nix ./define-loaOfSub-bar-enable.nix
79+checkConfigOutput 'true' "$@"
80+checkConfigError 'attribute .*bar.* .* not found' "$@" ./define-force-loaOfSub-foo-enable.nix
81+checkConfigError 'attribute .*bar.* .* not found' "$@" ./define-loaOfSub-force-foo-enable.nix
82+checkConfigOutput 'true' "$@" ./define-loaOfSub-foo-force-enable.nix
83+checkConfigOutput 'true' "$@" ./define-loaOfSub-foo-enable-force.nix
84+
85+cat <<EOF
86+====== module tests ======
87+$pass Pass
88+$fail Fail
89+EOF
90+
91+if test $fail -ne 0; then
92+ exit 1
93+fi
94+exit 0