git.iko.soy/typeable/nixpkgs
on master help sign in
Issue #6161 - Add tests for NixOS modules.
commit6d15e32536abbcef919b706f073c7fea46cf9229 authorNicolas B. Pierron <nicolas.b.pierron@gmail.com> dateSun 8 Feb 2015, 21:48 (12 years ago) parentd54611b
15 files changed+192
lib/tests/modules.sh+93 lib/tests/modules/declare-enable.nix+14 lib/tests/modules/declare-loaOfSub-any-enable.nix+29 lib/tests/modules/default.nix+7 lib/tests/modules/define-enable-force.nix+5 lib/tests/modules/define-enable.nix+3 lib/tests/modules/define-force-enable.nix+5 lib/tests/modules/define-force-loaOfSub-foo-enable.nix+5 lib/tests/modules/define-loaOfSub-bar-enable.nix+3 lib/tests/modules/define-loaOfSub-bar.nix+3 lib/tests/modules/define-loaOfSub-foo-enable-force.nix+5 lib/tests/modules/define-loaOfSub-foo-enable.nix+3 lib/tests/modules/define-loaOfSub-foo-force-enable.nix+7 lib/tests/modules/define-loaOfSub-foo.nix+3 lib/tests/modules/define-loaOfSub-force-foo-enable.nix+7
lib/tests/modules.shnew file+93
@@ -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
lib/tests/modules/declare-enable.nixnew file+14
@@ -0,0 +1,14 @@
1+{ lib, ... }:
2+
3+{
4+ options = {
5+ enable = lib.mkOption {
6+ default = false;
7+ example = true;
8+ type = lib.types.bool;
9+ description = ''
10+ Some descriptive text
11+ '';
12+ };
13+ };
14+}
lib/tests/modules/declare-loaOfSub-any-enable.nixnew file+29
@@ -0,0 +1,29 @@
1+{ lib, ... }:
2+
3+let
4+ submod = { ... }: {
5+ options = {
6+ enable = lib.mkOption {
7+ default = false;
8+ example = true;
9+ type = lib.types.bool;
10+ description = ''
11+ Some descriptive text
12+ '';
13+ };
14+ };
15+ };
16+in
17+
18+{
19+ options = {
20+ loaOfSub = lib.mkOption {
21+ default = {};
22+ example = {};
23+ type = lib.types.loaOf (lib.types.submodule [ submod ]);
24+ description = ''
25+ Some descriptive text
26+ '';
27+ };
28+ };
29+}
lib/tests/modules/default.nixnew file+7
@@ -0,0 +1,7 @@
1+{ lib ? import <nixpkgs/lib>, modules ? [] }:
2+
3+{
4+ inherit (lib.evalModules {
5+ inherit modules;
6+ }) config options;
7+}
lib/tests/modules/define-enable-force.nixnew file+5
@@ -0,0 +1,5 @@
1+{ lib, ... }:
2+
3+{
4+ enable = lib.mkForce false;
5+}
lib/tests/modules/define-enable.nixnew file+3
@@ -0,0 +1,3 @@
1+{
2+ enable = true;
3+}
lib/tests/modules/define-force-enable.nixnew file+5
@@ -0,0 +1,5 @@
1+{ lib, ... }:
2+
3+lib.mkForce {
4+ enable = false;
5+}
lib/tests/modules/define-force-loaOfSub-foo-enable.nixnew file+5
@@ -0,0 +1,5 @@
1+{ lib, ... }:
2+
3+lib.mkForce {
4+ loaOfSub.foo.enable = false;
5+}
lib/tests/modules/define-loaOfSub-bar-enable.nixnew file+3
@@ -0,0 +1,3 @@
1+{
2+ loaOfSub.bar.enable = true;
3+}
lib/tests/modules/define-loaOfSub-bar.nixnew file+3
@@ -0,0 +1,3 @@
1+{
2+ loaOfSub.bar = {};
3+}
lib/tests/modules/define-loaOfSub-foo-enable-force.nixnew file+5
@@ -0,0 +1,5 @@
1+{ lib, ... }:
2+
3+{
4+ loaOfSub.foo.enable = lib.mkForce false;
5+}
lib/tests/modules/define-loaOfSub-foo-enable.nixnew file+3
@@ -0,0 +1,3 @@
1+{
2+ loaOfSub.foo.enable = true;
3+}
lib/tests/modules/define-loaOfSub-foo-force-enable.nixnew file+7
@@ -0,0 +1,7 @@
1+{ lib, ... }:
2+
3+{
4+ loaOfSub.foo = lib.mkForce {
5+ enable = false;
6+ };
7+}
lib/tests/modules/define-loaOfSub-foo.nixnew file+3
@@ -0,0 +1,3 @@
1+{
2+ loaOfSub.foo = {};
3+}
lib/tests/modules/define-loaOfSub-force-foo-enable.nixnew file+7
@@ -0,0 +1,7 @@
1+{ lib, ... }:
2+
3+{
4+ loaOfSub = lib.mkForce {
5+ foo.enable = false;
6+ };
7+}