git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #6242 from nbp/issue-6161

Issue #6161 - Add tests for NixOS modules.

commit7699f34c524a50959c475425587f6b07d443a9e9 authorNicolas B. Pierron <nicolas.b.pierron@gmail.com> dateMon 23 Feb 2015, 12:12 (12 years ago) parent1cddb5bde0f122
17 files changed+226
lib/tests/modules.sh+94 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/release.nix+31 pkgs/top-level/release.nix+2
lib/tests/modules.shnew file+94
@@ -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
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+}
lib/tests/release.nixnew file+31
@@ -0,0 +1,31 @@
1+{ nixpkgs }:
2+
3+with import ./../.. { };
4+with lib;
5+
6+stdenv.mkDerivation {
7+ name = "nixpkgs-lib-tests";
8+ buildInputs = [ nix ];
9+ NIX_PATH="nixpkgs=${nixpkgs}";
10+
11+ buildCommand = ''
12+ datadir="${nix}/share"
13+ export TEST_ROOT=$(pwd)/test-tmp
14+ export NIX_STORE_DIR=$TEST_ROOT/store
15+ export NIX_LOCALSTATE_DIR=$TEST_ROOT/var
16+ export NIX_LOG_DIR=$TEST_ROOT/var/log/nix
17+ export NIX_STATE_DIR=$TEST_ROOT/var/nix
18+ export NIX_DB_DIR=$TEST_ROOT/db
19+ export NIX_CONF_DIR=$TEST_ROOT/etc
20+ export NIX_MANIFESTS_DIR=$TEST_ROOT/var/nix/manifests
21+ export NIX_BUILD_HOOK=
22+ export PAGER=cat
23+ cacheDir=$TEST_ROOT/binary-cache
24+ nix-store --init
25+
26+ cd ${nixpkgs}/lib/tests
27+ ./modules.sh
28+
29+ touch $out
30+ '';
31+}
pkgs/top-level/release.nix+2
@@ -23,6 +23,7 @@ let
2323 { tarball = import ./make-tarball.nix { inherit nixpkgs officialRelease; };
2424
2525 manual = import ../../doc;
26+ lib.tests = import ../../lib/tests/release.nix { inherit nixpkgs; };
2627
2728 unstable = pkgs.releaseTools.aggregate
2829 { name = "nixpkgs-${jobs.tarball.version}";
@@ -30,6 +31,7 @@ let
3031 constituents =
3132 [ jobs.tarball
3233 jobs.manual
34+ jobs.lib.tests
3335 jobs.stdenv.x86_64-linux
3436 jobs.stdenv.i686-linux
3537 jobs.stdenv.x86_64-darwin