summary refs log tree commit diff
path: root/lib/tests/modules.sh
diff options
context:
space:
mode:
authorSilvan Mosberger <contact@infinisil.com>2020-09-02 22:34:13 +0200
committerSilvan Mosberger <contact@infinisil.com>2020-11-30 23:51:23 +0100
commit3e39d6efdf65ce8fbf18471c0bb1062b28bfe984 (patch)
treeaa417ae5b9d29712d12d6c24c296ed6e980b08df /lib/tests/modules.sh
parent900c4a5abd1061db2390224849cbc5eb8eb07059 (diff)
downloadnixpkgs-3e39d6efdf65ce8fbf18471c0bb1062b28bfe984.tar
nixpkgs-3e39d6efdf65ce8fbf18471c0bb1062b28bfe984.tar.gz
nixpkgs-3e39d6efdf65ce8fbf18471c0bb1062b28bfe984.tar.bz2
nixpkgs-3e39d6efdf65ce8fbf18471c0bb1062b28bfe984.tar.lz
nixpkgs-3e39d6efdf65ce8fbf18471c0bb1062b28bfe984.tar.xz
nixpkgs-3e39d6efdf65ce8fbf18471c0bb1062b28bfe984.tar.zst
nixpkgs-3e39d6efdf65ce8fbf18471c0bb1062b28bfe984.zip
lib/tests: Add tests for module-builtin assertions
Diffstat (limited to 'lib/tests/modules.sh')
-rwxr-xr-xlib/tests/modules.sh37
1 files changed, 37 insertions, 0 deletions
diff --git a/lib/tests/modules.sh b/lib/tests/modules.sh
index 012835e48c1..65eb91c9927 100755
--- a/lib/tests/modules.sh
+++ b/lib/tests/modules.sh
@@ -275,6 +275,43 @@ 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
 
+## Module assertions
+# Check that assertions are triggered by default for just evaluating config
+checkConfigError 'Failed assertions:\n- \[test\] Assertion failed' config ./assertions/simple.nix
+# Check that assertions are only triggered if they have a triggerPath that's evaluated
+checkConfigError 'Failed assertions:\n- \[test\] Assertion failed' config.foo ./assertions/trigger-lazy.nix
+checkConfigOutput true config.bar ./assertions/trigger-lazy.nix
+
+# The assertions enable condition should only be evaluated if the trigger is evaluated
+checkConfigError 'enable evaluated' config.foo ./assertions/enable-lazy.nix
+checkConfigOutput true config.bar ./assertions/enable-lazy.nix
+
+# Assertion is not triggered when enable is false
+checkConfigOutput '{ }' config ./assertions/enable-false.nix
+
+# Warnings should be displayed on standard error
+checkConfigCodeOutErr 0 '{ }' 'warning: \[test\] Warning message' config ./assertions/warning.nix
+
+# A triggerPath can be set to a submodule path
+checkConfigOutput '{ baz = <CODE>; }' config.foo.bar ./assertions/trigger-submodule.nix
+checkConfigError 'Failed assertions:\n- \[test\] Assertion failed' config.foo.bar.baz ./assertions/trigger-submodule.nix
+
+# Check that multiple assertions and warnings can be triggered at once
+checkConfigError 'Failed assertions:\n- \[test1\] Assertion 1 failed\n- \[test2\] Assertion 2 failed' config ./assertions/multi.nix
+checkConfigError 'trace: warning: \[test3\] Warning 3 failed\ntrace: warning: \[test4\] Warning 4 failed' config ./assertions/multi.nix
+
+# Submodules should be able to trigger assertions and display the submodule prefix in their error
+checkConfigError 'Failed assertions:\n- \[foo/test\] Assertion failed' config.foo ./assertions/submodule.nix
+checkConfigError 'Failed assertions:\n- \[foo.bar/test\] Assertion failed' config.foo.bar ./assertions/submodule-attrsOf.nix
+checkConfigError 'Failed assertions:\n- \[foo.bar.baz/test\] Assertion failed' config.foo.bar.baz ./assertions/submodule-attrsOf-attrsOf.nix
+
+# Assertions aren't triggered when the trigger path is only evaluated from within the same module evaluation
+# This behavior is necessary to allow assertions to depend on config values. This could potentially be changed in the future if all of NixOS' assertions are rewritten to not depend on any config values
+checkConfigOutput true config.bar ./assertions/non-cascading.nix
+
+# Assertions with an attribute starting with _ shouldn't have their name displayed
+checkConfigError 'Failed assertions:\n- Assertion failed' config ./assertions/underscore-attributes.nix
+
 cat <<EOF
 ====== module tests ======
 $pass Pass