diff --git a/lib/tests/modules.sh b/lib/tests/modules.sh new file mode 100755 index 00000000000..58231a35636 --- /dev/null +++ b/lib/tests/modules.sh @@ -0,0 +1,94 @@ +#!/bin/sh +# +# This script is used to test that the module system is working as expected. +# By default it test the version of nixpkgs which is defined in the NIX_PATH. + +cd ./modules + +pass=0 +fail=0 + +evalConfig() { + local attr=$1 + shift; + local script="import ./default.nix { modules = [ $@ ];}" + nix-instantiate --timeout 1 -E "$script" -A "$attr" --eval-only +} + +reportFailure() { + local attr=$1 + shift; + local script="import ./default.nix { modules = [ $@ ];}" + echo 2>&1 "$ nix-instantiate -E '$script' -A '$attr' --eval-only" + evalConfig "$attr" "$@" + fail=$((fail + 1)) +} + +checkConfigOutput() { + local outputContains=$1 + shift; + if evalConfig "$@" 2>/dev/null | grep --silent "$outputContains" ; then + pass=$((pass + 1)) + return 0; + else + echo 2>&1 "error: Expected result matching '$outputContains', while evaluating" + reportFailure "$@" + return 1 + fi +} + +checkConfigError() { + local errorContains=$1 + local err="" + shift; + if err==$(evalConfig "$@" 2>&1 >/dev/null); then + echo 2>&1 "error: Expected error code, got exit code 0, while evaluating" + reportFailure "$@" + return 1 + else + if echo "$err" | grep --silent "$errorContains" ; then + pass=$((pass + 1)) + return 0; + else + echo 2>&1 "error: Expected error matching '$errorContains', while evaluating" + reportFailure "$@" + return 1 + fi + fi +} + +checkConfigOutput "false" config.enable ./declare-enable.nix +checkConfigError 'The option .* defined in .* does not exist.' config.enable ./define-enable.nix +set -- config.enable ./declare-enable.nix ./define-enable.nix +checkConfigOutput "true" "$@" +checkConfigOutput "false" "$@" ./define-force-enable.nix +checkConfigOutput "false" "$@" ./define-enable-force.nix + +checkConfigError 'attribute .*foo.* .* not found' config.loaOfSub.foo.enable ./declare-loaOfSub-any-enable.nix +checkConfigOutput 'false' config.loaOfSub.foo.enable ./declare-loaOfSub-any-enable.nix ./define-loaOfSub-foo.nix +set -- config.loaOfSub.foo.enable ./declare-loaOfSub-any-enable.nix ./define-loaOfSub-foo-enable.nix +checkConfigOutput 'true' "$@" +checkConfigOutput 'false' "$@" ./define-force-loaOfSub-foo-enable.nix +checkConfigOutput 'false' "$@" ./define-loaOfSub-force-foo-enable.nix +checkConfigOutput 'false' "$@" ./define-loaOfSub-foo-force-enable.nix +checkConfigOutput 'false' "$@" ./define-loaOfSub-foo-enable-force.nix + +checkConfigError 'attribute .*bar.* .* not found' config.loaOfSub.bar.enable ./declare-loaOfSub-any-enable.nix ./define-loaOfSub-foo.nix +checkConfigOutput 'false' config.loaOfSub.bar.enable ./declare-loaOfSub-any-enable.nix ./define-loaOfSub-foo.nix ./define-loaOfSub-bar.nix +set -- config.loaOfSub.bar.enable ./declare-loaOfSub-any-enable.nix ./define-loaOfSub-foo.nix ./define-loaOfSub-bar-enable.nix +checkConfigOutput 'true' "$@" +checkConfigError 'attribute .*bar.* .* not found' "$@" ./define-force-loaOfSub-foo-enable.nix +checkConfigError 'attribute .*bar.* .* not found' "$@" ./define-loaOfSub-force-foo-enable.nix +checkConfigOutput 'true' "$@" ./define-loaOfSub-foo-force-enable.nix +checkConfigOutput 'true' "$@" ./define-loaOfSub-foo-enable-force.nix + +cat <, modules ? [] }: + +{ + inherit (lib.evalModules { + inherit modules; + }) config options; +} diff --git a/lib/tests/modules/define-enable-force.nix b/lib/tests/modules/define-enable-force.nix new file mode 100644 index 00000000000..f4990a32863 --- /dev/null +++ b/lib/tests/modules/define-enable-force.nix @@ -0,0 +1,5 @@ +{ lib, ... }: + +{ + enable = lib.mkForce false; +} diff --git a/lib/tests/modules/define-enable.nix b/lib/tests/modules/define-enable.nix new file mode 100644 index 00000000000..7dc26010ae5 --- /dev/null +++ b/lib/tests/modules/define-enable.nix @@ -0,0 +1,3 @@ +{ + enable = true; +} diff --git a/lib/tests/modules/define-force-enable.nix b/lib/tests/modules/define-force-enable.nix new file mode 100644 index 00000000000..978caa2a8c0 --- /dev/null +++ b/lib/tests/modules/define-force-enable.nix @@ -0,0 +1,5 @@ +{ lib, ... }: + +lib.mkForce { + enable = false; +} diff --git a/lib/tests/modules/define-force-loaOfSub-foo-enable.nix b/lib/tests/modules/define-force-loaOfSub-foo-enable.nix new file mode 100644 index 00000000000..bfd8e084b59 --- /dev/null +++ b/lib/tests/modules/define-force-loaOfSub-foo-enable.nix @@ -0,0 +1,5 @@ +{ lib, ... }: + +lib.mkForce { + loaOfSub.foo.enable = false; +} diff --git a/lib/tests/modules/define-loaOfSub-bar-enable.nix b/lib/tests/modules/define-loaOfSub-bar-enable.nix new file mode 100644 index 00000000000..422bb0a600b --- /dev/null +++ b/lib/tests/modules/define-loaOfSub-bar-enable.nix @@ -0,0 +1,3 @@ +{ + loaOfSub.bar.enable = true; +} diff --git a/lib/tests/modules/define-loaOfSub-bar.nix b/lib/tests/modules/define-loaOfSub-bar.nix new file mode 100644 index 00000000000..c24315e09b6 --- /dev/null +++ b/lib/tests/modules/define-loaOfSub-bar.nix @@ -0,0 +1,3 @@ +{ + loaOfSub.bar = {}; +} diff --git a/lib/tests/modules/define-loaOfSub-foo-enable-force.nix b/lib/tests/modules/define-loaOfSub-foo-enable-force.nix new file mode 100644 index 00000000000..c1d7b198be5 --- /dev/null +++ b/lib/tests/modules/define-loaOfSub-foo-enable-force.nix @@ -0,0 +1,5 @@ +{ lib, ... }: + +{ + loaOfSub.foo.enable = lib.mkForce false; +} diff --git a/lib/tests/modules/define-loaOfSub-foo-enable.nix b/lib/tests/modules/define-loaOfSub-foo-enable.nix new file mode 100644 index 00000000000..822425c71bb --- /dev/null +++ b/lib/tests/modules/define-loaOfSub-foo-enable.nix @@ -0,0 +1,3 @@ +{ + loaOfSub.foo.enable = true; +} diff --git a/lib/tests/modules/define-loaOfSub-foo-force-enable.nix b/lib/tests/modules/define-loaOfSub-foo-force-enable.nix new file mode 100644 index 00000000000..dce0ef547b3 --- /dev/null +++ b/lib/tests/modules/define-loaOfSub-foo-force-enable.nix @@ -0,0 +1,7 @@ +{ lib, ... }: + +{ + loaOfSub.foo = lib.mkForce { + enable = false; + }; +} diff --git a/lib/tests/modules/define-loaOfSub-foo.nix b/lib/tests/modules/define-loaOfSub-foo.nix new file mode 100644 index 00000000000..e9b2e631f2e --- /dev/null +++ b/lib/tests/modules/define-loaOfSub-foo.nix @@ -0,0 +1,3 @@ +{ + loaOfSub.foo = {}; +} diff --git a/lib/tests/modules/define-loaOfSub-force-foo-enable.nix b/lib/tests/modules/define-loaOfSub-force-foo-enable.nix new file mode 100644 index 00000000000..df5722274ee --- /dev/null +++ b/lib/tests/modules/define-loaOfSub-force-foo-enable.nix @@ -0,0 +1,7 @@ +{ lib, ... }: + +{ + loaOfSub = lib.mkForce { + foo.enable = false; + }; +} diff --git a/lib/tests/release.nix b/lib/tests/release.nix new file mode 100644 index 00000000000..e7fb52f6766 --- /dev/null +++ b/lib/tests/release.nix @@ -0,0 +1,31 @@ +{ nixpkgs }: + +with import ./../.. { }; +with lib; + +stdenv.mkDerivation { + name = "nixpkgs-lib-tests"; + buildInputs = [ nix ]; + NIX_PATH="nixpkgs=${nixpkgs}"; + + buildCommand = '' + datadir="${nix}/share" + export TEST_ROOT=$(pwd)/test-tmp + export NIX_STORE_DIR=$TEST_ROOT/store + export NIX_LOCALSTATE_DIR=$TEST_ROOT/var + export NIX_LOG_DIR=$TEST_ROOT/var/log/nix + export NIX_STATE_DIR=$TEST_ROOT/var/nix + export NIX_DB_DIR=$TEST_ROOT/db + export NIX_CONF_DIR=$TEST_ROOT/etc + export NIX_MANIFESTS_DIR=$TEST_ROOT/var/nix/manifests + export NIX_BUILD_HOOK= + export PAGER=cat + cacheDir=$TEST_ROOT/binary-cache + nix-store --init + + cd ${nixpkgs}/lib/tests + ./modules.sh + + touch $out + ''; +} diff --git a/pkgs/top-level/release.nix b/pkgs/top-level/release.nix index 47026336403..534080668ec 100644 --- a/pkgs/top-level/release.nix +++ b/pkgs/top-level/release.nix @@ -23,6 +23,7 @@ let { tarball = import ./make-tarball.nix { inherit nixpkgs officialRelease; }; manual = import ../../doc; + lib.tests = import ../../lib/tests/release.nix { inherit nixpkgs; }; unstable = pkgs.releaseTools.aggregate { name = "nixpkgs-${jobs.tarball.version}"; @@ -30,6 +31,7 @@ let constituents = [ jobs.tarball jobs.manual + jobs.lib.tests jobs.stdenv.x86_64-linux jobs.stdenv.i686-linux jobs.stdenv.x86_64-darwin