at 22.05-pre 18 kB view raw
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# https://stackoverflow.com/a/246128/6605742 7DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" 8 9cd "$DIR"/modules 10 11pass=0 12fail=0 13 14evalConfig() { 15 local attr=$1 16 shift; 17 local script="import ./default.nix { modules = [ $@ ];}" 18 nix-instantiate --timeout 1 -E "$script" -A "$attr" --eval-only --show-trace --read-write-mode 19} 20 21reportFailure() { 22 local attr=$1 23 shift; 24 local script="import ./default.nix { modules = [ $@ ];}" 25 echo 2>&1 "$ nix-instantiate -E '$script' -A '$attr' --eval-only" 26 evalConfig "$attr" "$@" 27 fail=$((fail + 1)) 28} 29 30checkConfigOutput() { 31 local outputContains=$1 32 shift; 33 if evalConfig "$@" 2>/dev/null | grep --silent "$outputContains" ; then 34 pass=$((pass + 1)) 35 return 0; 36 else 37 echo 2>&1 "error: Expected result matching '$outputContains', while evaluating" 38 reportFailure "$@" 39 return 1 40 fi 41} 42 43checkConfigError() { 44 local errorContains=$1 45 local err="" 46 shift; 47 if err==$(evalConfig "$@" 2>&1 >/dev/null); then 48 echo 2>&1 "error: Expected error code, got exit code 0, while evaluating" 49 reportFailure "$@" 50 return 1 51 else 52 if echo "$err" | grep -zP --silent "$errorContains" ; then 53 pass=$((pass + 1)) 54 return 0; 55 else 56 echo 2>&1 "error: Expected error matching '$errorContains', while evaluating" 57 reportFailure "$@" 58 return 1 59 fi 60 fi 61} 62 63# Check boolean option. 64checkConfigOutput "false" config.enable ./declare-enable.nix 65checkConfigError 'The option .* does not exist. Definition values:\n\s*- In .*: true' config.enable ./define-enable.nix 66 67# Check integer types. 68# unsigned 69checkConfigOutput "42" config.value ./declare-int-unsigned-value.nix ./define-value-int-positive.nix 70checkConfigError '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 71# positive 72checkConfigError '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 73# between 74checkConfigOutput "42" config.value ./declare-int-between-value.nix ./define-value-int-positive.nix 75checkConfigError '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 76 77# Check either types 78# types.either 79checkConfigOutput "42" config.value ./declare-either.nix ./define-value-int-positive.nix 80checkConfigOutput "\"24\"" config.value ./declare-either.nix ./define-value-string.nix 81# types.oneOf 82checkConfigOutput "42" config.value ./declare-oneOf.nix ./define-value-int-positive.nix 83checkConfigOutput "[ ]" config.value ./declare-oneOf.nix ./define-value-list.nix 84checkConfigOutput "\"24\"" config.value ./declare-oneOf.nix ./define-value-string.nix 85 86# Check mkForce without submodules. 87set -- config.enable ./declare-enable.nix ./define-enable.nix 88checkConfigOutput "true" "$@" 89checkConfigOutput "false" "$@" ./define-force-enable.nix 90checkConfigOutput "false" "$@" ./define-enable-force.nix 91 92# Check mkForce with option and submodules. 93checkConfigError 'attribute .*foo.* .* not found' config.attrsOfSub.foo.enable ./declare-attrsOfSub-any-enable.nix 94checkConfigOutput 'false' config.attrsOfSub.foo.enable ./declare-attrsOfSub-any-enable.nix ./define-attrsOfSub-foo.nix 95set -- config.attrsOfSub.foo.enable ./declare-attrsOfSub-any-enable.nix ./define-attrsOfSub-foo-enable.nix 96checkConfigOutput 'true' "$@" 97checkConfigOutput 'false' "$@" ./define-force-attrsOfSub-foo-enable.nix 98checkConfigOutput 'false' "$@" ./define-attrsOfSub-force-foo-enable.nix 99checkConfigOutput 'false' "$@" ./define-attrsOfSub-foo-force-enable.nix 100checkConfigOutput 'false' "$@" ./define-attrsOfSub-foo-enable-force.nix 101 102# Check overriding effect of mkForce on submodule definitions. 103checkConfigError 'attribute .*bar.* .* not found' config.attrsOfSub.bar.enable ./declare-attrsOfSub-any-enable.nix ./define-attrsOfSub-foo.nix 104checkConfigOutput 'false' config.attrsOfSub.bar.enable ./declare-attrsOfSub-any-enable.nix ./define-attrsOfSub-foo.nix ./define-attrsOfSub-bar.nix 105set -- config.attrsOfSub.bar.enable ./declare-attrsOfSub-any-enable.nix ./define-attrsOfSub-foo.nix ./define-attrsOfSub-bar-enable.nix 106checkConfigOutput 'true' "$@" 107checkConfigError 'attribute .*bar.* .* not found' "$@" ./define-force-attrsOfSub-foo-enable.nix 108checkConfigError 'attribute .*bar.* .* not found' "$@" ./define-attrsOfSub-force-foo-enable.nix 109checkConfigOutput 'true' "$@" ./define-attrsOfSub-foo-force-enable.nix 110checkConfigOutput 'true' "$@" ./define-attrsOfSub-foo-enable-force.nix 111 112# Check mkIf with submodules. 113checkConfigError 'attribute .*foo.* .* not found' config.attrsOfSub.foo.enable ./declare-enable.nix ./declare-attrsOfSub-any-enable.nix 114set -- config.attrsOfSub.foo.enable ./declare-enable.nix ./declare-attrsOfSub-any-enable.nix 115checkConfigError 'attribute .*foo.* .* not found' "$@" ./define-if-attrsOfSub-foo-enable.nix 116checkConfigError 'attribute .*foo.* .* not found' "$@" ./define-attrsOfSub-if-foo-enable.nix 117checkConfigError 'attribute .*foo.* .* not found' "$@" ./define-attrsOfSub-foo-if-enable.nix 118checkConfigOutput 'false' "$@" ./define-attrsOfSub-foo-enable-if.nix 119checkConfigOutput 'true' "$@" ./define-enable.nix ./define-if-attrsOfSub-foo-enable.nix 120checkConfigOutput 'true' "$@" ./define-enable.nix ./define-attrsOfSub-if-foo-enable.nix 121checkConfigOutput 'true' "$@" ./define-enable.nix ./define-attrsOfSub-foo-if-enable.nix 122checkConfigOutput 'true' "$@" ./define-enable.nix ./define-attrsOfSub-foo-enable-if.nix 123 124# Check disabledModules with config definitions and option declarations. 125set -- config.enable ./define-enable.nix ./declare-enable.nix 126checkConfigOutput "true" "$@" 127checkConfigOutput "false" "$@" ./disable-define-enable.nix 128checkConfigError "The option .*enable.* does not exist. Definition values:\n\s*- In .*: true" "$@" ./disable-declare-enable.nix 129checkConfigError "attribute .*enable.* in selection path .*config.enable.* not found" "$@" ./disable-define-enable.nix ./disable-declare-enable.nix 130checkConfigError "attribute .*enable.* in selection path .*config.enable.* not found" "$@" ./disable-enable-modules.nix 131 132# Check _module.args. 133set -- config.enable ./declare-enable.nix ./define-enable-with-custom-arg.nix 134checkConfigError 'while evaluating the module argument .*custom.* in .*define-enable-with-custom-arg.nix.*:' "$@" 135checkConfigOutput "true" "$@" ./define-_module-args-custom.nix 136 137# Check that using _module.args on imports cause infinite recursions, with 138# the proper error context. 139set -- "$@" ./define-_module-args-custom.nix ./import-custom-arg.nix 140checkConfigError 'while evaluating the module argument .*custom.* in .*import-custom-arg.nix.*:' "$@" 141checkConfigError 'infinite recursion encountered' "$@" 142 143# Check _module.check. 144set -- config.enable ./declare-enable.nix ./define-enable.nix ./define-attrsOfSub-foo.nix 145checkConfigError 'The option .* does not exist. Definition values:\n\s*- In .*' "$@" 146checkConfigOutput "true" "$@" ./define-module-check.nix 147 148# Check coerced value. 149checkConfigOutput "\"42\"" config.value ./declare-coerced-value.nix 150checkConfigOutput "\"24\"" config.value ./declare-coerced-value.nix ./define-value-string.nix 151checkConfigError '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 152 153# Check coerced value with unsound coercion 154checkConfigOutput "12" config.value ./declare-coerced-value-unsound.nix 155checkConfigError '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 156checkConfigError 'json.exception.parse_error' config.value ./declare-coerced-value-unsound.nix ./define-value-string-arbitrary.nix 157 158# Check mkAliasOptionModule. 159checkConfigOutput "true" config.enable ./alias-with-priority.nix 160checkConfigOutput "true" config.enableAlias ./alias-with-priority.nix 161checkConfigOutput "false" config.enable ./alias-with-priority-can-override.nix 162checkConfigOutput "false" config.enableAlias ./alias-with-priority-can-override.nix 163 164# submoduleWith 165 166## specialArgs should work 167checkConfigOutput "foo" config.submodule.foo ./declare-submoduleWith-special.nix 168 169## shorthandOnlyDefines config behaves as expected 170checkConfigOutput "true" config.submodule.config ./declare-submoduleWith-shorthand.nix ./define-submoduleWith-shorthand.nix 171checkConfigError 'is not of type `boolean' config.submodule.config ./declare-submoduleWith-shorthand.nix ./define-submoduleWith-noshorthand.nix 172checkConfigError "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 173checkConfigOutput "true" config.submodule.config ./declare-submoduleWith-noshorthand.nix ./define-submoduleWith-noshorthand.nix 174 175## submoduleWith should merge all modules in one swoop 176checkConfigOutput "true" config.submodule.inner ./declare-submoduleWith-modules.nix 177checkConfigOutput "true" config.submodule.outer ./declare-submoduleWith-modules.nix 178# Should also be able to evaluate the type name (which evaluates freeformType, 179# which evaluates all the modules defined by the type) 180checkConfigOutput "submodule" options.submodule.type.description ./declare-submoduleWith-modules.nix 181 182## submodules can be declared using (evalModules {...}).type 183checkConfigOutput "true" config.submodule.inner ./declare-submodule-via-evalModules.nix 184checkConfigOutput "true" config.submodule.outer ./declare-submodule-via-evalModules.nix 185# Should also be able to evaluate the type name (which evaluates freeformType, 186# which evaluates all the modules defined by the type) 187checkConfigOutput "submodule" options.submodule.type.description ./declare-submodule-via-evalModules.nix 188 189## Paths should be allowed as values and work as expected 190checkConfigOutput "true" config.submodule.enable ./declare-submoduleWith-path.nix 191 192# Check that disabledModules works recursively and correctly 193checkConfigOutput "true" config.enable ./disable-recursive/main.nix 194checkConfigOutput "true" config.enable ./disable-recursive/{main.nix,disable-foo.nix} 195checkConfigOutput "true" config.enable ./disable-recursive/{main.nix,disable-bar.nix} 196checkConfigError 'The option .* does not exist. Definition values:\n\s*- In .*: true' config.enable ./disable-recursive/{main.nix,disable-foo.nix,disable-bar.nix} 197 198# Check that imports can depend on derivations 199checkConfigOutput "true" config.enable ./import-from-store.nix 200 201# Check that configs can be conditional on option existence 202checkConfigOutput true config.enable ./define-option-dependently.nix ./declare-enable.nix ./declare-int-positive-value.nix 203checkConfigOutput 360 config.value ./define-option-dependently.nix ./declare-enable.nix ./declare-int-positive-value.nix 204checkConfigOutput 7 config.value ./define-option-dependently.nix ./declare-int-positive-value.nix 205checkConfigOutput true config.set.enable ./define-option-dependently-nested.nix ./declare-enable-nested.nix ./declare-int-positive-value-nested.nix 206checkConfigOutput 360 config.set.value ./define-option-dependently-nested.nix ./declare-enable-nested.nix ./declare-int-positive-value-nested.nix 207checkConfigOutput 7 config.set.value ./define-option-dependently-nested.nix ./declare-int-positive-value-nested.nix 208 209# Check attrsOf and lazyAttrsOf. Only lazyAttrsOf should be lazy, and only 210# attrsOf should work with conditional definitions 211# In addition, lazyAttrsOf should honor an options emptyValue 212checkConfigError "is not lazy" config.isLazy ./declare-attrsOf.nix ./attrsOf-lazy-check.nix 213checkConfigOutput "true" config.isLazy ./declare-lazyAttrsOf.nix ./attrsOf-lazy-check.nix 214checkConfigOutput "true" config.conditionalWorks ./declare-attrsOf.nix ./attrsOf-conditional-check.nix 215checkConfigOutput "false" config.conditionalWorks ./declare-lazyAttrsOf.nix ./attrsOf-conditional-check.nix 216checkConfigOutput "empty" config.value.foo ./declare-lazyAttrsOf.nix ./attrsOf-conditional-check.nix 217 218 219# Even with multiple assignments, a type error should be thrown if any of them aren't valid 220checkConfigError 'A definition for option .* is not of type .*' \ 221 config.value ./declare-int-unsigned-value.nix ./define-value-list.nix ./define-value-int-positive.nix 222 223## Freeform modules 224# Assigning without a declared option should work 225checkConfigOutput 24 config.value ./freeform-attrsOf.nix ./define-value-string.nix 226# No freeform assigments shouldn't make it error 227checkConfigOutput '{ }' config ./freeform-attrsOf.nix 228# but only if the type matches 229checkConfigError 'A definition for option .* is not of type .*' config.value ./freeform-attrsOf.nix ./define-value-list.nix 230# and properties should be applied 231checkConfigOutput yes config.value ./freeform-attrsOf.nix ./define-value-string-properties.nix 232# Options should still be declarable, and be able to have a type that doesn't match the freeform type 233checkConfigOutput false config.enable ./freeform-attrsOf.nix ./define-value-string.nix ./declare-enable.nix 234checkConfigOutput 24 config.value ./freeform-attrsOf.nix ./define-value-string.nix ./declare-enable.nix 235# and this should work too with nested values 236checkConfigOutput false config.nest.foo ./freeform-attrsOf.nix ./freeform-nested.nix 237checkConfigOutput bar config.nest.bar ./freeform-attrsOf.nix ./freeform-nested.nix 238# Check whether a declared option can depend on an freeform-typed one 239checkConfigOutput null config.foo ./freeform-attrsOf.nix ./freeform-str-dep-unstr.nix 240checkConfigOutput 24 config.foo ./freeform-attrsOf.nix ./freeform-str-dep-unstr.nix ./define-value-string.nix 241# Check whether an freeform-typed value can depend on a declared option, this can only work with lazyAttrsOf 242checkConfigError 'infinite recursion encountered' config.foo ./freeform-attrsOf.nix ./freeform-unstr-dep-str.nix 243checkConfigError 'The option .* is used but not defined' config.foo ./freeform-lazyAttrsOf.nix ./freeform-unstr-dep-str.nix 244checkConfigOutput 24 config.foo ./freeform-lazyAttrsOf.nix ./freeform-unstr-dep-str.nix ./define-value-string.nix 245 246## types.anything 247# Check that attribute sets are merged recursively 248checkConfigOutput null config.value.foo ./types-anything/nested-attrs.nix 249checkConfigOutput null config.value.l1.foo ./types-anything/nested-attrs.nix 250checkConfigOutput null config.value.l1.l2.foo ./types-anything/nested-attrs.nix 251checkConfigOutput null config.value.l1.l2.l3.foo ./types-anything/nested-attrs.nix 252# Attribute sets that are coercible to strings shouldn't be recursed into 253checkConfigOutput foo config.value.outPath ./types-anything/attrs-coercible.nix 254# Multiple lists aren't concatenated together 255checkConfigError 'The option .* has conflicting definitions' config.value ./types-anything/lists.nix 256# Check that all equalizable atoms can be used as long as all definitions are equal 257checkConfigOutput 0 config.value.int ./types-anything/equal-atoms.nix 258checkConfigOutput false config.value.bool ./types-anything/equal-atoms.nix 259checkConfigOutput '""' config.value.string ./types-anything/equal-atoms.nix 260checkConfigOutput / config.value.path ./types-anything/equal-atoms.nix 261checkConfigOutput null config.value.null ./types-anything/equal-atoms.nix 262checkConfigOutput 0.1 config.value.float ./types-anything/equal-atoms.nix 263# Functions can't be merged together 264checkConfigError "The option .value.multiple-lambdas.<function body>. has conflicting option types" config.applied.multiple-lambdas ./types-anything/functions.nix 265checkConfigOutput '<LAMBDA>' config.value.single-lambda ./types-anything/functions.nix 266checkConfigOutput 'null' config.applied.merging-lambdas.x ./types-anything/functions.nix 267checkConfigOutput 'null' config.applied.merging-lambdas.y ./types-anything/functions.nix 268# Check that all mk* modifiers are applied 269checkConfigError 'attribute .* not found' config.value.mkiffalse ./types-anything/mk-mods.nix 270checkConfigOutput '{ }' config.value.mkiftrue ./types-anything/mk-mods.nix 271checkConfigOutput 1 config.value.mkdefault ./types-anything/mk-mods.nix 272checkConfigOutput '{ }' config.value.mkmerge ./types-anything/mk-mods.nix 273checkConfigOutput true config.value.mkbefore ./types-anything/mk-mods.nix 274checkConfigOutput 1 config.value.nested.foo ./types-anything/mk-mods.nix 275checkConfigOutput baz config.value.nested.bar.baz ./types-anything/mk-mods.nix 276 277## types.functionTo 278checkConfigOutput "input is input" config.result ./functionTo/trivial.nix 279checkConfigOutput "a b" config.result ./functionTo/merging-list.nix 280checkConfigError '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 281checkConfigOutput "b a" config.result ./functionTo/list-order.nix 282checkConfigOutput "a c" config.result ./functionTo/merging-attrs.nix 283 284# moduleType 285checkConfigOutput "a b" config.resultFoo ./declare-variants.nix ./define-variant.nix 286checkConfigOutput "a y z" config.resultFooBar ./declare-variants.nix ./define-variant.nix 287checkConfigOutput "a b c" config.resultFooFoo ./declare-variants.nix ./define-variant.nix 288 289cat <<EOF 290====== module tests ====== 291$pass Pass 292$fail Fail 293EOF 294 295if test $fail -ne 0; then 296 exit 1 297fi 298exit 0