diff options
author | Alyssa Ross <hi@alyssa.is> | 2022-01-03 23:55:00 +0000 |
---|---|---|
committer | Alyssa Ross <hi@alyssa.is> | 2022-02-19 11:03:39 +0000 |
commit | f4cf97a04cd5d0b86aa46baec9fb228a8f671c03 (patch) | |
tree | 28192415ff39a661d0001563bf81cc93fa25d16d /nixpkgs/lib/tests | |
parent | f8422837c9bde058e8f2de37702e7e94b2226040 (diff) | |
parent | 18c84ea816348e2a098390101b92d1e39a9dbd45 (diff) | |
download | nixlib-f4cf97a04cd5d0b86aa46baec9fb228a8f671c03.tar nixlib-f4cf97a04cd5d0b86aa46baec9fb228a8f671c03.tar.gz nixlib-f4cf97a04cd5d0b86aa46baec9fb228a8f671c03.tar.bz2 nixlib-f4cf97a04cd5d0b86aa46baec9fb228a8f671c03.tar.lz nixlib-f4cf97a04cd5d0b86aa46baec9fb228a8f671c03.tar.xz nixlib-f4cf97a04cd5d0b86aa46baec9fb228a8f671c03.tar.zst nixlib-f4cf97a04cd5d0b86aa46baec9fb228a8f671c03.zip |
Merge commit '18c84ea816348e2a098390101b92d1e39a9dbd45'
Conflicts: nixpkgs/nixos/modules/misc/documentation.nix nixpkgs/pkgs/applications/networking/browsers/firefox/packages.nix nixpkgs/pkgs/applications/window-managers/sway/default.nix nixpkgs/pkgs/build-support/rust/build-rust-package/default.nix nixpkgs/pkgs/development/go-modules/generic/default.nix nixpkgs/pkgs/development/interpreters/ruby/default.nix nixpkgs/pkgs/development/interpreters/ruby/patchsets.nix nixpkgs/pkgs/development/libraries/boehm-gc/7.6.6.nix nixpkgs/pkgs/development/python-modules/django-mailman3/default.nix nixpkgs/pkgs/servers/mail/mailman/web.nix nixpkgs/pkgs/top-level/aliases.nix nixpkgs/pkgs/top-level/all-packages.nix nixpkgs/pkgs/top-level/impure.nix
Diffstat (limited to 'nixpkgs/lib/tests')
-rw-r--r-- | nixpkgs/lib/tests/misc.nix | 2 | ||||
-rwxr-xr-x | nixpkgs/lib/tests/modules.sh | 254 | ||||
-rw-r--r-- | nixpkgs/lib/tests/modules/declare-attrsOf.nix | 9 | ||||
-rw-r--r-- | nixpkgs/lib/tests/modules/declare-submodule-via-evalModules.nix | 28 | ||||
-rw-r--r-- | nixpkgs/lib/tests/modules/declare-variants.nix | 9 | ||||
-rw-r--r-- | nixpkgs/lib/tests/modules/define-variant.nix | 22 | ||||
-rw-r--r-- | nixpkgs/lib/tests/modules/freeform-nested.nix | 9 | ||||
-rw-r--r-- | nixpkgs/lib/tests/release.nix | 4 | ||||
-rwxr-xr-x | nixpkgs/lib/tests/sources.sh | 20 |
9 files changed, 223 insertions, 134 deletions
diff --git a/nixpkgs/lib/tests/misc.nix b/nixpkgs/lib/tests/misc.nix index 7b3a6b4e60b8..5fa95828df69 100644 --- a/nixpkgs/lib/tests/misc.nix +++ b/nixpkgs/lib/tests/misc.nix @@ -496,7 +496,7 @@ runTests { testToPretty = let - deriv = derivation { name = "test"; builder = "/bin/sh"; system = builtins.currentSystem; }; + deriv = derivation { name = "test"; builder = "/bin/sh"; system = "aarch64-linux"; }; in { expr = mapAttrs (const (generators.toPretty { multiline = false; })) rec { int = 42; diff --git a/nixpkgs/lib/tests/modules.sh b/nixpkgs/lib/tests/modules.sh index b51db91f6b07..590937da5b8f 100755 --- a/nixpkgs/lib/tests/modules.sh +++ b/nixpkgs/lib/tests/modules.sh @@ -1,8 +1,11 @@ -#!/bin/sh +#!/usr/bin/env bash # # 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. +set -o errexit -o noclobber -o nounset -o pipefail +shopt -s failglob inherit_errexit + # https://stackoverflow.com/a/246128/6605742 DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" @@ -13,101 +16,96 @@ fail=0 evalConfig() { local attr=$1 - shift; - local script="import ./default.nix { modules = [ $@ ];}" + shift + local script="import ./default.nix { modules = [ $* ];}" nix-instantiate --timeout 1 -E "$script" -A "$attr" --eval-only --show-trace --read-write-mode } reportFailure() { local attr=$1 - shift; - local script="import ./default.nix { modules = [ $@ ];}" + shift + local script="import ./default.nix { modules = [ $* ];}" echo 2>&1 "$ nix-instantiate -E '$script' -A '$attr' --eval-only" - evalConfig "$attr" "$@" - fail=$((fail + 1)) + evalConfig "$attr" "$@" || true + ((++fail)) } checkConfigOutput() { local outputContains=$1 - shift; + shift if evalConfig "$@" 2>/dev/null | grep --silent "$outputContains" ; then - pass=$((pass + 1)) - return 0; + ((++pass)) 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 + 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 -zP --silent "$errorContains" ; then - pass=$((pass + 1)) - return 0; + ((++pass)) else echo 2>&1 "error: Expected error matching '$errorContains', while evaluating" reportFailure "$@" - return 1 fi fi } # Check boolean option. -checkConfigOutput "false" config.enable ./declare-enable.nix -checkConfigError 'The option .* does not exist. Definition values:\n- In .*: true' config.enable ./define-enable.nix +checkConfigOutput '^false$' config.enable ./declare-enable.nix +checkConfigError 'The option .* does not exist. Definition values:\n\s*- In .*: true' config.enable ./define-enable.nix # Check integer types. # unsigned -checkConfigOutput "42" config.value ./declare-int-unsigned-value.nix ./define-value-int-positive.nix -checkConfigError 'A definition for option .* is not of type.*unsigned integer.*. Definition values:\n- In .*: -23' config.value ./declare-int-unsigned-value.nix ./define-value-int-negative.nix +checkConfigOutput '^42$' config.value ./declare-int-unsigned-value.nix ./define-value-int-positive.nix +checkConfigError 'A definition for option .* is not of type.*unsigned integer.*. Definition values:\n\s*- In .*: -23' config.value ./declare-int-unsigned-value.nix ./define-value-int-negative.nix # positive -checkConfigError 'A definition for option .* is not of type.*positive integer.*. Definition values:\n- In .*: 0' config.value ./declare-int-positive-value.nix ./define-value-int-zero.nix +checkConfigError 'A definition for option .* is not of type.*positive integer.*. Definition values:\n\s*- In .*: 0' config.value ./declare-int-positive-value.nix ./define-value-int-zero.nix # between -checkConfigOutput "42" config.value ./declare-int-between-value.nix ./define-value-int-positive.nix -checkConfigError 'A definition for option .* is not of type.*between.*-21 and 43.*inclusive.*. Definition values:\n- In .*: -23' config.value ./declare-int-between-value.nix ./define-value-int-negative.nix +checkConfigOutput '^42$' config.value ./declare-int-between-value.nix ./define-value-int-positive.nix +checkConfigError 'A definition for option .* is not of type.*between.*-21 and 43.*inclusive.*. Definition values:\n\s*- In .*: -23' config.value ./declare-int-between-value.nix ./define-value-int-negative.nix # Check either types # types.either -checkConfigOutput "42" config.value ./declare-either.nix ./define-value-int-positive.nix -checkConfigOutput "\"24\"" config.value ./declare-either.nix ./define-value-string.nix +checkConfigOutput '^42$' config.value ./declare-either.nix ./define-value-int-positive.nix +checkConfigOutput '^"24"$' config.value ./declare-either.nix ./define-value-string.nix # types.oneOf -checkConfigOutput "42" config.value ./declare-oneOf.nix ./define-value-int-positive.nix -checkConfigOutput "[ ]" config.value ./declare-oneOf.nix ./define-value-list.nix -checkConfigOutput "\"24\"" config.value ./declare-oneOf.nix ./define-value-string.nix +checkConfigOutput '^42$' config.value ./declare-oneOf.nix ./define-value-int-positive.nix +checkConfigOutput '^\[ \]$' config.value ./declare-oneOf.nix ./define-value-list.nix +checkConfigOutput '^"24"$' config.value ./declare-oneOf.nix ./define-value-string.nix # Check mkForce without submodules. set -- config.enable ./declare-enable.nix ./define-enable.nix -checkConfigOutput "true" "$@" -checkConfigOutput "false" "$@" ./define-force-enable.nix -checkConfigOutput "false" "$@" ./define-enable-force.nix +checkConfigOutput '^true$' "$@" +checkConfigOutput '^false$' "$@" ./define-force-enable.nix +checkConfigOutput '^false$' "$@" ./define-enable-force.nix # Check mkForce with option and submodules. checkConfigError 'attribute .*foo.* .* not found' config.attrsOfSub.foo.enable ./declare-attrsOfSub-any-enable.nix -checkConfigOutput 'false' config.attrsOfSub.foo.enable ./declare-attrsOfSub-any-enable.nix ./define-attrsOfSub-foo.nix +checkConfigOutput '^false$' config.attrsOfSub.foo.enable ./declare-attrsOfSub-any-enable.nix ./define-attrsOfSub-foo.nix set -- config.attrsOfSub.foo.enable ./declare-attrsOfSub-any-enable.nix ./define-attrsOfSub-foo-enable.nix -checkConfigOutput 'true' "$@" -checkConfigOutput 'false' "$@" ./define-force-attrsOfSub-foo-enable.nix -checkConfigOutput 'false' "$@" ./define-attrsOfSub-force-foo-enable.nix -checkConfigOutput 'false' "$@" ./define-attrsOfSub-foo-force-enable.nix -checkConfigOutput 'false' "$@" ./define-attrsOfSub-foo-enable-force.nix +checkConfigOutput '^true$' "$@" +checkConfigOutput '^false$' "$@" ./define-force-attrsOfSub-foo-enable.nix +checkConfigOutput '^false$' "$@" ./define-attrsOfSub-force-foo-enable.nix +checkConfigOutput '^false$' "$@" ./define-attrsOfSub-foo-force-enable.nix +checkConfigOutput '^false$' "$@" ./define-attrsOfSub-foo-enable-force.nix # Check overriding effect of mkForce on submodule definitions. checkConfigError 'attribute .*bar.* .* not found' config.attrsOfSub.bar.enable ./declare-attrsOfSub-any-enable.nix ./define-attrsOfSub-foo.nix -checkConfigOutput 'false' config.attrsOfSub.bar.enable ./declare-attrsOfSub-any-enable.nix ./define-attrsOfSub-foo.nix ./define-attrsOfSub-bar.nix +checkConfigOutput '^false$' config.attrsOfSub.bar.enable ./declare-attrsOfSub-any-enable.nix ./define-attrsOfSub-foo.nix ./define-attrsOfSub-bar.nix set -- config.attrsOfSub.bar.enable ./declare-attrsOfSub-any-enable.nix ./define-attrsOfSub-foo.nix ./define-attrsOfSub-bar-enable.nix -checkConfigOutput 'true' "$@" +checkConfigOutput '^true$' "$@" checkConfigError 'attribute .*bar.* .* not found' "$@" ./define-force-attrsOfSub-foo-enable.nix checkConfigError 'attribute .*bar.* .* not found' "$@" ./define-attrsOfSub-force-foo-enable.nix -checkConfigOutput 'true' "$@" ./define-attrsOfSub-foo-force-enable.nix -checkConfigOutput 'true' "$@" ./define-attrsOfSub-foo-enable-force.nix +checkConfigOutput '^true$' "$@" ./define-attrsOfSub-foo-force-enable.nix +checkConfigOutput '^true$' "$@" ./define-attrsOfSub-foo-enable-force.nix # Check mkIf with submodules. checkConfigError 'attribute .*foo.* .* not found' config.attrsOfSub.foo.enable ./declare-enable.nix ./declare-attrsOfSub-any-enable.nix @@ -115,24 +113,24 @@ set -- config.attrsOfSub.foo.enable ./declare-enable.nix ./declare-attrsOfSub-an checkConfigError 'attribute .*foo.* .* not found' "$@" ./define-if-attrsOfSub-foo-enable.nix checkConfigError 'attribute .*foo.* .* not found' "$@" ./define-attrsOfSub-if-foo-enable.nix checkConfigError 'attribute .*foo.* .* not found' "$@" ./define-attrsOfSub-foo-if-enable.nix -checkConfigOutput 'false' "$@" ./define-attrsOfSub-foo-enable-if.nix -checkConfigOutput 'true' "$@" ./define-enable.nix ./define-if-attrsOfSub-foo-enable.nix -checkConfigOutput 'true' "$@" ./define-enable.nix ./define-attrsOfSub-if-foo-enable.nix -checkConfigOutput 'true' "$@" ./define-enable.nix ./define-attrsOfSub-foo-if-enable.nix -checkConfigOutput 'true' "$@" ./define-enable.nix ./define-attrsOfSub-foo-enable-if.nix +checkConfigOutput '^false$' "$@" ./define-attrsOfSub-foo-enable-if.nix +checkConfigOutput '^true$' "$@" ./define-enable.nix ./define-if-attrsOfSub-foo-enable.nix +checkConfigOutput '^true$' "$@" ./define-enable.nix ./define-attrsOfSub-if-foo-enable.nix +checkConfigOutput '^true$' "$@" ./define-enable.nix ./define-attrsOfSub-foo-if-enable.nix +checkConfigOutput '^true$' "$@" ./define-enable.nix ./define-attrsOfSub-foo-enable-if.nix # Check disabledModules with config definitions and option declarations. set -- config.enable ./define-enable.nix ./declare-enable.nix -checkConfigOutput "true" "$@" -checkConfigOutput "false" "$@" ./disable-define-enable.nix -checkConfigError "The option .*enable.* does not exist. Definition values:\n- In .*: true" "$@" ./disable-declare-enable.nix +checkConfigOutput '^true$' "$@" +checkConfigOutput '^false$' "$@" ./disable-define-enable.nix +checkConfigError "The option .*enable.* does not exist. Definition values:\n\s*- In .*: true" "$@" ./disable-declare-enable.nix checkConfigError "attribute .*enable.* in selection path .*config.enable.* not found" "$@" ./disable-define-enable.nix ./disable-declare-enable.nix checkConfigError "attribute .*enable.* in selection path .*config.enable.* not found" "$@" ./disable-enable-modules.nix # Check _module.args. set -- config.enable ./declare-enable.nix ./define-enable-with-custom-arg.nix checkConfigError 'while evaluating the module argument .*custom.* in .*define-enable-with-custom-arg.nix.*:' "$@" -checkConfigOutput "true" "$@" ./define-_module-args-custom.nix +checkConfigOutput '^true$' "$@" ./define-_module-args-custom.nix # Check that using _module.args on imports cause infinite recursions, with # the proper error context. @@ -142,71 +140,78 @@ checkConfigError 'infinite recursion encountered' "$@" # Check _module.check. set -- config.enable ./declare-enable.nix ./define-enable.nix ./define-attrsOfSub-foo.nix -checkConfigError 'The option .* does not exist. Definition values:\n- In .*' "$@" -checkConfigOutput "true" "$@" ./define-module-check.nix +checkConfigError 'The option .* does not exist. Definition values:\n\s*- In .*' "$@" +checkConfigOutput '^true$' "$@" ./define-module-check.nix # Check coerced value. -checkConfigOutput "\"42\"" config.value ./declare-coerced-value.nix -checkConfigOutput "\"24\"" config.value ./declare-coerced-value.nix ./define-value-string.nix -checkConfigError 'A definition for option .* is not.*string or signed integer convertible to it.*. Definition values:\n- In .*: \[ \]' config.value ./declare-coerced-value.nix ./define-value-list.nix +checkConfigOutput '^"42"$' config.value ./declare-coerced-value.nix +checkConfigOutput '^"24"$' config.value ./declare-coerced-value.nix ./define-value-string.nix +checkConfigError 'A definition for option .* is not.*string or signed integer convertible to it.*. Definition values:\n\s*- In .*: \[ \]' config.value ./declare-coerced-value.nix ./define-value-list.nix # Check coerced value with unsound coercion -checkConfigOutput "12" config.value ./declare-coerced-value-unsound.nix -checkConfigError 'A definition for option .* is not of type .*. Definition values:\n- In .*: "1000"' config.value ./declare-coerced-value-unsound.nix ./define-value-string-bigint.nix -checkConfigError 'unrecognised JSON value' config.value ./declare-coerced-value-unsound.nix ./define-value-string-arbitrary.nix +checkConfigOutput '^12$' config.value ./declare-coerced-value-unsound.nix +checkConfigError 'A definition for option .* is not of type .*. Definition values:\n\s*- In .*: "1000"' config.value ./declare-coerced-value-unsound.nix ./define-value-string-bigint.nix +checkConfigError 'json.exception.parse_error' config.value ./declare-coerced-value-unsound.nix ./define-value-string-arbitrary.nix # Check mkAliasOptionModule. -checkConfigOutput "true" config.enable ./alias-with-priority.nix -checkConfigOutput "true" config.enableAlias ./alias-with-priority.nix -checkConfigOutput "false" config.enable ./alias-with-priority-can-override.nix -checkConfigOutput "false" config.enableAlias ./alias-with-priority-can-override.nix +checkConfigOutput '^true$' config.enable ./alias-with-priority.nix +checkConfigOutput '^true$' config.enableAlias ./alias-with-priority.nix +checkConfigOutput '^false$' config.enable ./alias-with-priority-can-override.nix +checkConfigOutput '^false$' config.enableAlias ./alias-with-priority-can-override.nix # submoduleWith ## specialArgs should work -checkConfigOutput "foo" config.submodule.foo ./declare-submoduleWith-special.nix +checkConfigOutput '^"foo"$' config.submodule.foo ./declare-submoduleWith-special.nix ## shorthandOnlyDefines config behaves as expected -checkConfigOutput "true" config.submodule.config ./declare-submoduleWith-shorthand.nix ./define-submoduleWith-shorthand.nix +checkConfigOutput '^true$' config.submodule.config ./declare-submoduleWith-shorthand.nix ./define-submoduleWith-shorthand.nix checkConfigError 'is not of type `boolean' config.submodule.config ./declare-submoduleWith-shorthand.nix ./define-submoduleWith-noshorthand.nix -checkConfigError "You're trying to declare a value of type \`bool'\nrather than an attribute-set for the option" config.submodule.config ./declare-submoduleWith-noshorthand.nix ./define-submoduleWith-shorthand.nix -checkConfigOutput "true" config.submodule.config ./declare-submoduleWith-noshorthand.nix ./define-submoduleWith-noshorthand.nix +checkConfigError "You're trying to declare a value of type \`bool'\n\s*rather than an attribute-set for the option" config.submodule.config ./declare-submoduleWith-noshorthand.nix ./define-submoduleWith-shorthand.nix +checkConfigOutput '^true$' config.submodule.config ./declare-submoduleWith-noshorthand.nix ./define-submoduleWith-noshorthand.nix ## submoduleWith should merge all modules in one swoop -checkConfigOutput "true" config.submodule.inner ./declare-submoduleWith-modules.nix -checkConfigOutput "true" config.submodule.outer ./declare-submoduleWith-modules.nix +checkConfigOutput '^true$' config.submodule.inner ./declare-submoduleWith-modules.nix +checkConfigOutput '^true$' config.submodule.outer ./declare-submoduleWith-modules.nix # Should also be able to evaluate the type name (which evaluates freeformType, # which evaluates all the modules defined by the type) -checkConfigOutput "submodule" options.submodule.type.description ./declare-submoduleWith-modules.nix +checkConfigOutput '^"submodule"$' options.submodule.type.description ./declare-submoduleWith-modules.nix + +## submodules can be declared using (evalModules {...}).type +checkConfigOutput '^true$' config.submodule.inner ./declare-submodule-via-evalModules.nix +checkConfigOutput '^true$' config.submodule.outer ./declare-submodule-via-evalModules.nix +# Should also be able to evaluate the type name (which evaluates freeformType, +# which evaluates all the modules defined by the type) +checkConfigOutput '^"submodule"$' options.submodule.type.description ./declare-submodule-via-evalModules.nix ## Paths should be allowed as values and work as expected -checkConfigOutput "true" config.submodule.enable ./declare-submoduleWith-path.nix +checkConfigOutput '^true$' config.submodule.enable ./declare-submoduleWith-path.nix # Check that disabledModules works recursively and correctly -checkConfigOutput "true" config.enable ./disable-recursive/main.nix -checkConfigOutput "true" config.enable ./disable-recursive/{main.nix,disable-foo.nix} -checkConfigOutput "true" config.enable ./disable-recursive/{main.nix,disable-bar.nix} -checkConfigError 'The option .* does not exist. Definition values:\n- In .*: true' config.enable ./disable-recursive/{main.nix,disable-foo.nix,disable-bar.nix} +checkConfigOutput '^true$' config.enable ./disable-recursive/main.nix +checkConfigOutput '^true$' config.enable ./disable-recursive/{main.nix,disable-foo.nix} +checkConfigOutput '^true$' config.enable ./disable-recursive/{main.nix,disable-bar.nix} +checkConfigError 'The option .* does not exist. Definition values:\n\s*- In .*: true' config.enable ./disable-recursive/{main.nix,disable-foo.nix,disable-bar.nix} # Check that imports can depend on derivations -checkConfigOutput "true" config.enable ./import-from-store.nix +checkConfigOutput '^true$' config.enable ./import-from-store.nix # Check that configs can be conditional on option existence -checkConfigOutput true config.enable ./define-option-dependently.nix ./declare-enable.nix ./declare-int-positive-value.nix -checkConfigOutput 360 config.value ./define-option-dependently.nix ./declare-enable.nix ./declare-int-positive-value.nix -checkConfigOutput 7 config.value ./define-option-dependently.nix ./declare-int-positive-value.nix -checkConfigOutput true config.set.enable ./define-option-dependently-nested.nix ./declare-enable-nested.nix ./declare-int-positive-value-nested.nix -checkConfigOutput 360 config.set.value ./define-option-dependently-nested.nix ./declare-enable-nested.nix ./declare-int-positive-value-nested.nix -checkConfigOutput 7 config.set.value ./define-option-dependently-nested.nix ./declare-int-positive-value-nested.nix +checkConfigOutput '^true$' config.enable ./define-option-dependently.nix ./declare-enable.nix ./declare-int-positive-value.nix +checkConfigOutput '^360$' config.value ./define-option-dependently.nix ./declare-enable.nix ./declare-int-positive-value.nix +checkConfigOutput '^7$' config.value ./define-option-dependently.nix ./declare-int-positive-value.nix +checkConfigOutput '^true$' config.set.enable ./define-option-dependently-nested.nix ./declare-enable-nested.nix ./declare-int-positive-value-nested.nix +checkConfigOutput '^360$' config.set.value ./define-option-dependently-nested.nix ./declare-enable-nested.nix ./declare-int-positive-value-nested.nix +checkConfigOutput '^7$' config.set.value ./define-option-dependently-nested.nix ./declare-int-positive-value-nested.nix # Check attrsOf and lazyAttrsOf. Only lazyAttrsOf should be lazy, and only # attrsOf should work with conditional definitions # In addition, lazyAttrsOf should honor an options emptyValue checkConfigError "is not lazy" config.isLazy ./declare-attrsOf.nix ./attrsOf-lazy-check.nix -checkConfigOutput "true" config.isLazy ./declare-lazyAttrsOf.nix ./attrsOf-lazy-check.nix -checkConfigOutput "true" config.conditionalWorks ./declare-attrsOf.nix ./attrsOf-conditional-check.nix -checkConfigOutput "false" config.conditionalWorks ./declare-lazyAttrsOf.nix ./attrsOf-conditional-check.nix -checkConfigOutput "empty" config.value.foo ./declare-lazyAttrsOf.nix ./attrsOf-conditional-check.nix +checkConfigOutput '^true$' config.isLazy ./declare-lazyAttrsOf.nix ./attrsOf-lazy-check.nix +checkConfigOutput '^true$' config.conditionalWorks ./declare-attrsOf.nix ./attrsOf-conditional-check.nix +checkConfigOutput '^false$' config.conditionalWorks ./declare-lazyAttrsOf.nix ./attrsOf-conditional-check.nix +checkConfigOutput '^"empty"$' config.value.foo ./declare-lazyAttrsOf.nix ./attrsOf-conditional-check.nix # Even with multiple assignments, a type error should be thrown if any of them aren't valid @@ -215,64 +220,69 @@ checkConfigError 'A definition for option .* is not of type .*' \ ## Freeform modules # Assigning without a declared option should work -checkConfigOutput 24 config.value ./freeform-attrsOf.nix ./define-value-string.nix +checkConfigOutput '^"24"$' config.value ./freeform-attrsOf.nix ./define-value-string.nix # No freeform assigments shouldn't make it error -checkConfigOutput '{ }' config ./freeform-attrsOf.nix +checkConfigOutput '^{ }$' config ./freeform-attrsOf.nix # but only if the type matches checkConfigError 'A definition for option .* is not of type .*' config.value ./freeform-attrsOf.nix ./define-value-list.nix # and properties should be applied -checkConfigOutput yes config.value ./freeform-attrsOf.nix ./define-value-string-properties.nix +checkConfigOutput '^"yes"$' config.value ./freeform-attrsOf.nix ./define-value-string-properties.nix # Options should still be declarable, and be able to have a type that doesn't match the freeform type -checkConfigOutput false config.enable ./freeform-attrsOf.nix ./define-value-string.nix ./declare-enable.nix -checkConfigOutput 24 config.value ./freeform-attrsOf.nix ./define-value-string.nix ./declare-enable.nix +checkConfigOutput '^false$' config.enable ./freeform-attrsOf.nix ./define-value-string.nix ./declare-enable.nix +checkConfigOutput '^"24"$' config.value ./freeform-attrsOf.nix ./define-value-string.nix ./declare-enable.nix # and this should work too with nested values -checkConfigOutput false config.nest.foo ./freeform-attrsOf.nix ./freeform-nested.nix -checkConfigOutput bar config.nest.bar ./freeform-attrsOf.nix ./freeform-nested.nix +checkConfigOutput '^false$' config.nest.foo ./freeform-attrsOf.nix ./freeform-nested.nix +checkConfigOutput '^"bar"$' config.nest.bar ./freeform-attrsOf.nix ./freeform-nested.nix # Check whether a declared option can depend on an freeform-typed one -checkConfigOutput null config.foo ./freeform-attrsOf.nix ./freeform-str-dep-unstr.nix -checkConfigOutput 24 config.foo ./freeform-attrsOf.nix ./freeform-str-dep-unstr.nix ./define-value-string.nix +checkConfigOutput '^null$' config.foo ./freeform-attrsOf.nix ./freeform-str-dep-unstr.nix +checkConfigOutput '^"24"$' config.foo ./freeform-attrsOf.nix ./freeform-str-dep-unstr.nix ./define-value-string.nix # Check whether an freeform-typed value can depend on a declared option, this can only work with lazyAttrsOf checkConfigError 'infinite recursion encountered' config.foo ./freeform-attrsOf.nix ./freeform-unstr-dep-str.nix checkConfigError 'The option .* is used but not defined' config.foo ./freeform-lazyAttrsOf.nix ./freeform-unstr-dep-str.nix -checkConfigOutput 24 config.foo ./freeform-lazyAttrsOf.nix ./freeform-unstr-dep-str.nix ./define-value-string.nix +checkConfigOutput '^"24"$' config.foo ./freeform-lazyAttrsOf.nix ./freeform-unstr-dep-str.nix ./define-value-string.nix ## types.anything # Check that attribute sets are merged recursively -checkConfigOutput null config.value.foo ./types-anything/nested-attrs.nix -checkConfigOutput null config.value.l1.foo ./types-anything/nested-attrs.nix -checkConfigOutput null config.value.l1.l2.foo ./types-anything/nested-attrs.nix -checkConfigOutput null config.value.l1.l2.l3.foo ./types-anything/nested-attrs.nix +checkConfigOutput '^null$' config.value.foo ./types-anything/nested-attrs.nix +checkConfigOutput '^null$' config.value.l1.foo ./types-anything/nested-attrs.nix +checkConfigOutput '^null$' config.value.l1.l2.foo ./types-anything/nested-attrs.nix +checkConfigOutput '^null$' config.value.l1.l2.l3.foo ./types-anything/nested-attrs.nix # Attribute sets that are coercible to strings shouldn't be recursed into -checkConfigOutput foo config.value.outPath ./types-anything/attrs-coercible.nix +checkConfigOutput '^"foo"$' config.value.outPath ./types-anything/attrs-coercible.nix # Multiple lists aren't concatenated together checkConfigError 'The option .* has conflicting definitions' config.value ./types-anything/lists.nix # Check that all equalizable atoms can be used as long as all definitions are equal -checkConfigOutput 0 config.value.int ./types-anything/equal-atoms.nix -checkConfigOutput false config.value.bool ./types-anything/equal-atoms.nix -checkConfigOutput '""' config.value.string ./types-anything/equal-atoms.nix -checkConfigOutput / config.value.path ./types-anything/equal-atoms.nix -checkConfigOutput null config.value.null ./types-anything/equal-atoms.nix -checkConfigOutput 0.1 config.value.float ./types-anything/equal-atoms.nix +checkConfigOutput '^0$' config.value.int ./types-anything/equal-atoms.nix +checkConfigOutput '^false$' config.value.bool ./types-anything/equal-atoms.nix +checkConfigOutput '^""$' config.value.string ./types-anything/equal-atoms.nix +checkConfigOutput '^/$' config.value.path ./types-anything/equal-atoms.nix +checkConfigOutput '^null$' config.value.null ./types-anything/equal-atoms.nix +checkConfigOutput '^0.1$' config.value.float ./types-anything/equal-atoms.nix # Functions can't be merged together checkConfigError "The option .value.multiple-lambdas.<function body>. has conflicting option types" config.applied.multiple-lambdas ./types-anything/functions.nix -checkConfigOutput '<LAMBDA>' config.value.single-lambda ./types-anything/functions.nix -checkConfigOutput 'null' config.applied.merging-lambdas.x ./types-anything/functions.nix -checkConfigOutput 'null' config.applied.merging-lambdas.y ./types-anything/functions.nix +checkConfigOutput '^<LAMBDA>$' config.value.single-lambda ./types-anything/functions.nix +checkConfigOutput '^null$' config.applied.merging-lambdas.x ./types-anything/functions.nix +checkConfigOutput '^null$' config.applied.merging-lambdas.y ./types-anything/functions.nix # Check that all mk* modifiers are applied checkConfigError 'attribute .* not found' config.value.mkiffalse ./types-anything/mk-mods.nix -checkConfigOutput '{ }' config.value.mkiftrue ./types-anything/mk-mods.nix -checkConfigOutput 1 config.value.mkdefault ./types-anything/mk-mods.nix -checkConfigOutput '{ }' config.value.mkmerge ./types-anything/mk-mods.nix -checkConfigOutput true config.value.mkbefore ./types-anything/mk-mods.nix -checkConfigOutput 1 config.value.nested.foo ./types-anything/mk-mods.nix -checkConfigOutput baz config.value.nested.bar.baz ./types-anything/mk-mods.nix +checkConfigOutput '^{ }$' config.value.mkiftrue ./types-anything/mk-mods.nix +checkConfigOutput '^1$' config.value.mkdefault ./types-anything/mk-mods.nix +checkConfigOutput '^{ }$' config.value.mkmerge ./types-anything/mk-mods.nix +checkConfigOutput '^true$' config.value.mkbefore ./types-anything/mk-mods.nix +checkConfigOutput '^1$' config.value.nested.foo ./types-anything/mk-mods.nix +checkConfigOutput '^"baz"$' config.value.nested.bar.baz ./types-anything/mk-mods.nix ## types.functionTo -checkConfigOutput "input is input" config.result ./functionTo/trivial.nix -checkConfigOutput "a b" config.result ./functionTo/merging-list.nix -checkConfigError 'A definition for option .fun.\[function body\]. is not of type .string.. Definition values:\n- In .*wrong-type.nix' config.result ./functionTo/wrong-type.nix -checkConfigOutput "b a" config.result ./functionTo/list-order.nix -checkConfigOutput "a c" config.result ./functionTo/merging-attrs.nix +checkConfigOutput '^"input is input"$' config.result ./functionTo/trivial.nix +checkConfigOutput '^"a b"$' config.result ./functionTo/merging-list.nix +checkConfigError 'A definition for option .fun.\[function body\]. is not of type .string.. Definition values:\n\s*- In .*wrong-type.nix' config.result ./functionTo/wrong-type.nix +checkConfigOutput '^"b a"$' config.result ./functionTo/list-order.nix +checkConfigOutput '^"a c"$' config.result ./functionTo/merging-attrs.nix + +# moduleType +checkConfigOutput '^"a b"$' config.resultFoo ./declare-variants.nix ./define-variant.nix +checkConfigOutput '^"a y z"$' config.resultFooBar ./declare-variants.nix ./define-variant.nix +checkConfigOutput '^"a b c"$' config.resultFooFoo ./declare-variants.nix ./define-variant.nix cat <<EOF ====== module tests ====== @@ -280,7 +290,7 @@ $pass Pass $fail Fail EOF -if test $fail -ne 0; then +if [ "$fail" -ne 0 ]; then exit 1 fi exit 0 diff --git a/nixpkgs/lib/tests/modules/declare-attrsOf.nix b/nixpkgs/lib/tests/modules/declare-attrsOf.nix index b3999de7e5fb..d19964064b21 100644 --- a/nixpkgs/lib/tests/modules/declare-attrsOf.nix +++ b/nixpkgs/lib/tests/modules/declare-attrsOf.nix @@ -1,6 +1,13 @@ -{ lib, ... }: { +{ lib, ... }: +let + deathtrapArgs = lib.mapAttrs + (k: _: throw "The module system is too strict, accessing an unused option's ${k} mkOption-attribute.") + (lib.functionArgs lib.mkOption); +in +{ options.value = lib.mkOption { type = lib.types.attrsOf lib.types.str; default = {}; }; + options.testing-laziness-so-don't-read-me = lib.mkOption deathtrapArgs; } diff --git a/nixpkgs/lib/tests/modules/declare-submodule-via-evalModules.nix b/nixpkgs/lib/tests/modules/declare-submodule-via-evalModules.nix new file mode 100644 index 000000000000..2841c64a073d --- /dev/null +++ b/nixpkgs/lib/tests/modules/declare-submodule-via-evalModules.nix @@ -0,0 +1,28 @@ +{ lib, ... }: { + options.submodule = lib.mkOption { + inherit (lib.evalModules { + modules = [ + { + options.inner = lib.mkOption { + type = lib.types.bool; + default = false; + }; + } + ]; + }) type; + default = {}; + }; + + config.submodule = lib.mkMerge [ + ({ lib, ... }: { + options.outer = lib.mkOption { + type = lib.types.bool; + default = false; + }; + }) + { + inner = true; + outer = true; + } + ]; +} diff --git a/nixpkgs/lib/tests/modules/declare-variants.nix b/nixpkgs/lib/tests/modules/declare-variants.nix new file mode 100644 index 000000000000..3ed6fa689e21 --- /dev/null +++ b/nixpkgs/lib/tests/modules/declare-variants.nix @@ -0,0 +1,9 @@ +{ lib, moduleType, ... }: +let inherit (lib) mkOption types; +in +{ + options.variants = mkOption { + type = types.lazyAttrsOf moduleType; + default = {}; + }; +} diff --git a/nixpkgs/lib/tests/modules/define-variant.nix b/nixpkgs/lib/tests/modules/define-variant.nix new file mode 100644 index 000000000000..423cb0e37cb5 --- /dev/null +++ b/nixpkgs/lib/tests/modules/define-variant.nix @@ -0,0 +1,22 @@ +{ config, lib, ... }: +let inherit (lib) types mkOption attrNames; +in +{ + options = { + attrs = mkOption { type = types.attrsOf lib.types.int; }; + result = mkOption { }; + resultFoo = mkOption { }; + resultFooBar = mkOption { }; + resultFooFoo = mkOption { }; + }; + config = { + attrs.a = 1; + variants.foo.attrs.b = 1; + variants.bar.attrs.y = 1; + variants.foo.variants.bar.attrs.z = 1; + variants.foo.variants.foo.attrs.c = 3; + resultFoo = lib.concatMapStringsSep " " toString (attrNames config.variants.foo.attrs); + resultFooBar = lib.concatMapStringsSep " " toString (attrNames config.variants.foo.variants.bar.attrs); + resultFooFoo = lib.concatMapStringsSep " " toString (attrNames config.variants.foo.variants.foo.attrs); + }; +} diff --git a/nixpkgs/lib/tests/modules/freeform-nested.nix b/nixpkgs/lib/tests/modules/freeform-nested.nix index 5da27f5a8b4f..b81fa7f0d222 100644 --- a/nixpkgs/lib/tests/modules/freeform-nested.nix +++ b/nixpkgs/lib/tests/modules/freeform-nested.nix @@ -1,7 +1,14 @@ -{ lib, ... }: { +{ lib, ... }: +let + deathtrapArgs = lib.mapAttrs + (k: _: throw "The module system is too strict, accessing an unused option's ${k} mkOption-attribute.") + (lib.functionArgs lib.mkOption); +in +{ options.nest.foo = lib.mkOption { type = lib.types.bool; default = false; }; + options.nest.unused = lib.mkOption deathtrapArgs; config.nest.bar = "bar"; } diff --git a/nixpkgs/lib/tests/release.nix b/nixpkgs/lib/tests/release.nix index 77e0e1af7555..815841e0a8f3 100644 --- a/nixpkgs/lib/tests/release.nix +++ b/nixpkgs/lib/tests/release.nix @@ -23,6 +23,10 @@ pkgs.runCommand "nixpkgs-lib-tests" { export NIX_STORE_DIR=$TEST_ROOT/store export PAGER=cat cacheDir=$TEST_ROOT/binary-cache + + mkdir -p $NIX_CONF_DIR + echo "experimental-features = nix-command" >> $NIX_CONF_DIR/nix.conf + nix-store --init cp -r ${../.} lib diff --git a/nixpkgs/lib/tests/sources.sh b/nixpkgs/lib/tests/sources.sh index 71fee719cb21..a7f490a79d74 100755 --- a/nixpkgs/lib/tests/sources.sh +++ b/nixpkgs/lib/tests/sources.sh @@ -1,5 +1,6 @@ #!/usr/bin/env bash set -euo pipefail +shopt -s inherit_errexit # Use # || die @@ -9,27 +10,28 @@ die() { } if test -n "${TEST_LIB:-}"; then - export NIX_PATH=nixpkgs="$(dirname "$TEST_LIB")" + NIX_PATH=nixpkgs="$(dirname "$TEST_LIB")" else - export NIX_PATH=nixpkgs="$(cd $(dirname ${BASH_SOURCE[0]})/../..; pwd)" + NIX_PATH=nixpkgs="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.."; pwd)" fi +export NIX_PATH work="$(mktemp -d)" clean_up() { rm -rf "$work" } trap clean_up EXIT -cd $work +cd "$work" touch {README.md,module.o,foo.bar} # nix-instantiate doesn't write out the source, only computing the hash, so # this uses the experimental nix command instead. -dir="$(nix eval --raw '(with import <nixpkgs/lib>; "${ +dir="$(nix eval --impure --raw --expr '(with import <nixpkgs/lib>; "${ cleanSource ./. }")')" -(cd $dir; find) | sort -f | diff -U10 - <(cat <<EOF +(cd "$dir"; find) | sort -f | diff -U10 - <(cat <<EOF . ./foo.bar ./README.md @@ -37,20 +39,20 @@ EOF ) || die "cleanSource 1" -dir="$(nix eval --raw '(with import <nixpkgs/lib>; "${ +dir="$(nix eval --impure --raw --expr '(with import <nixpkgs/lib>; "${ cleanSourceWith { src = '"$work"'; filter = path: type: ! hasSuffix ".bar" path; } }")')" -(cd $dir; find) | sort -f | diff -U10 - <(cat <<EOF +(cd "$dir"; find) | sort -f | diff -U10 - <(cat <<EOF . ./module.o ./README.md EOF ) || die "cleanSourceWith 1" -dir="$(nix eval --raw '(with import <nixpkgs/lib>; "${ +dir="$(nix eval --impure --raw --expr '(with import <nixpkgs/lib>; "${ cleanSourceWith { src = cleanSource '"$work"'; filter = path: type: ! hasSuffix ".bar" path; } }")')" -(cd $dir; find) | sort -f | diff -U10 - <(cat <<EOF +(cd "$dir"; find) | sort -f | diff -U10 - <(cat <<EOF . ./README.md EOF |