mirror of
https://github.com/ilyakooo0/nixpkgs.git
synced 2024-12-27 05:43:50 +03:00
lib/tests: Add tests for module-builtin assertions
This commit is contained in:
parent
900c4a5abd
commit
3e39d6efdf
@ -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
|
||||
|
8
lib/tests/modules/assertions/enable-false.nix
Normal file
8
lib/tests/modules/assertions/enable-false.nix
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
|
||||
_module.assertions.test = {
|
||||
enable = false;
|
||||
message = "Assertion failed";
|
||||
};
|
||||
|
||||
}
|
17
lib/tests/modules/assertions/enable-lazy.nix
Normal file
17
lib/tests/modules/assertions/enable-lazy.nix
Normal file
@ -0,0 +1,17 @@
|
||||
{ lib, ... }: {
|
||||
|
||||
options.foo = lib.mkOption {
|
||||
default = true;
|
||||
};
|
||||
|
||||
options.bar = lib.mkOption {
|
||||
default = true;
|
||||
};
|
||||
|
||||
config._module.assertions.test = {
|
||||
enable = throw "enable evaluated";
|
||||
message = "Assertion failed";
|
||||
triggerPath = [ "foo" ];
|
||||
};
|
||||
|
||||
}
|
23
lib/tests/modules/assertions/multi.nix
Normal file
23
lib/tests/modules/assertions/multi.nix
Normal file
@ -0,0 +1,23 @@
|
||||
{
|
||||
|
||||
_module.assertions = {
|
||||
test1 = {
|
||||
enable = true;
|
||||
message = "Assertion 1 failed";
|
||||
};
|
||||
test2 = {
|
||||
enable = true;
|
||||
message = "Assertion 2 failed";
|
||||
};
|
||||
test3 = {
|
||||
enable = true;
|
||||
message = "Warning 3 failed";
|
||||
type = "warning";
|
||||
};
|
||||
test4 = {
|
||||
enable = true;
|
||||
message = "Warning 4 failed";
|
||||
type = "warning";
|
||||
};
|
||||
};
|
||||
}
|
17
lib/tests/modules/assertions/non-cascading.nix
Normal file
17
lib/tests/modules/assertions/non-cascading.nix
Normal file
@ -0,0 +1,17 @@
|
||||
{ lib, config, ... }: {
|
||||
|
||||
options.foo = lib.mkOption {
|
||||
default = true;
|
||||
};
|
||||
|
||||
options.bar = lib.mkOption {
|
||||
default = config.foo;
|
||||
};
|
||||
|
||||
config._module.assertions.foo = {
|
||||
enable = true;
|
||||
message = "Foo assertion";
|
||||
triggerPath = [ "foo" ];
|
||||
};
|
||||
|
||||
}
|
6
lib/tests/modules/assertions/simple.nix
Normal file
6
lib/tests/modules/assertions/simple.nix
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
_module.assertions.test = {
|
||||
enable = true;
|
||||
message = "Assertion failed";
|
||||
};
|
||||
}
|
13
lib/tests/modules/assertions/submodule-attrsOf-attrsOf.nix
Normal file
13
lib/tests/modules/assertions/submodule-attrsOf-attrsOf.nix
Normal file
@ -0,0 +1,13 @@
|
||||
{ lib, ... }: {
|
||||
|
||||
options.foo = lib.mkOption {
|
||||
default = { bar.baz = {}; };
|
||||
type = lib.types.attrsOf (lib.types.attrsOf (lib.types.submodule {
|
||||
_module.assertions.test = {
|
||||
enable = true;
|
||||
message = "Assertion failed";
|
||||
};
|
||||
}));
|
||||
};
|
||||
|
||||
}
|
13
lib/tests/modules/assertions/submodule-attrsOf.nix
Normal file
13
lib/tests/modules/assertions/submodule-attrsOf.nix
Normal file
@ -0,0 +1,13 @@
|
||||
{ lib, ... }: {
|
||||
|
||||
options.foo = lib.mkOption {
|
||||
default = { bar = {}; };
|
||||
type = lib.types.attrsOf (lib.types.submodule {
|
||||
_module.assertions.test = {
|
||||
enable = true;
|
||||
message = "Assertion failed";
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
}
|
13
lib/tests/modules/assertions/submodule.nix
Normal file
13
lib/tests/modules/assertions/submodule.nix
Normal file
@ -0,0 +1,13 @@
|
||||
{ lib, ... }: {
|
||||
|
||||
options.foo = lib.mkOption {
|
||||
default = {};
|
||||
type = lib.types.submodule {
|
||||
_module.assertions.test = {
|
||||
enable = true;
|
||||
message = "Assertion failed";
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
}
|
15
lib/tests/modules/assertions/trigger-lazy.nix
Normal file
15
lib/tests/modules/assertions/trigger-lazy.nix
Normal file
@ -0,0 +1,15 @@
|
||||
{ lib, ... }: {
|
||||
options.foo = lib.mkOption {
|
||||
default = true;
|
||||
};
|
||||
|
||||
options.bar = lib.mkOption {
|
||||
default = true;
|
||||
};
|
||||
|
||||
config._module.assertions.test = {
|
||||
enable = true;
|
||||
message = "Assertion failed";
|
||||
triggerPath = [ "foo" ];
|
||||
};
|
||||
}
|
18
lib/tests/modules/assertions/trigger-submodule.nix
Normal file
18
lib/tests/modules/assertions/trigger-submodule.nix
Normal file
@ -0,0 +1,18 @@
|
||||
{ lib, ... }: {
|
||||
|
||||
options.foo = lib.mkOption {
|
||||
default = { bar = {}; };
|
||||
type = lib.types.attrsOf (lib.types.submodule {
|
||||
options.baz = lib.mkOption {
|
||||
default = true;
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
config._module.assertions.test = {
|
||||
enable = true;
|
||||
message = "Assertion failed";
|
||||
triggerPath = [ "foo" "bar" "baz" ];
|
||||
};
|
||||
|
||||
}
|
8
lib/tests/modules/assertions/underscore-attributes.nix
Normal file
8
lib/tests/modules/assertions/underscore-attributes.nix
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
|
||||
_module.assertions._test = {
|
||||
enable = true;
|
||||
message = "Assertion failed";
|
||||
};
|
||||
|
||||
}
|
9
lib/tests/modules/assertions/warning.nix
Normal file
9
lib/tests/modules/assertions/warning.nix
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
|
||||
_module.assertions.test = {
|
||||
enable = true;
|
||||
type = "warning";
|
||||
message = "Warning message";
|
||||
};
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user