mirror of
https://github.com/NixOS/nixpkgs.git
synced 2025-06-09 19:13:26 +03:00
lib/asserts: Factor out NixOS's toplevel assertion / warning logic.
This commit is contained in:
parent
cad590f322
commit
4b0d1225f5
2 changed files with 69 additions and 8 deletions
|
@ -1,5 +1,16 @@
|
||||||
{ lib }:
|
{ lib }:
|
||||||
|
|
||||||
|
let
|
||||||
|
inherit (lib.strings)
|
||||||
|
concatStringsSep
|
||||||
|
;
|
||||||
|
inherit (lib.lists)
|
||||||
|
filter
|
||||||
|
;
|
||||||
|
inherit (lib.trivial)
|
||||||
|
showWarnings
|
||||||
|
;
|
||||||
|
in
|
||||||
rec {
|
rec {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -131,4 +142,61 @@ rec {
|
||||||
"each element in ${name} must be one of ${lib.generators.toPretty { } xs}, but is: ${
|
"each element in ${name} must be one of ${lib.generators.toPretty { } xs}, but is: ${
|
||||||
lib.generators.toPretty { } vals
|
lib.generators.toPretty { } vals
|
||||||
}";
|
}";
|
||||||
|
|
||||||
|
/**
|
||||||
|
Wrap a value with logic that throws an error when assertions
|
||||||
|
fail and emits any warnings.
|
||||||
|
|
||||||
|
# Inputs
|
||||||
|
|
||||||
|
`assertions`
|
||||||
|
|
||||||
|
: A list of assertions. If any of their `assertion` attrs is `false`, their `message` attrs will be emitted in a `throw`.
|
||||||
|
|
||||||
|
`warnings`
|
||||||
|
|
||||||
|
: A list of strings to emit as warnings. This function does no filtering on this list.
|
||||||
|
|
||||||
|
`val`
|
||||||
|
|
||||||
|
: A value to return, wrapped in `warn`, if a `throw` is not necessary.
|
||||||
|
|
||||||
|
# Type
|
||||||
|
|
||||||
|
```
|
||||||
|
checkAssertWarn :: [ { assertion :: Bool; message :: String } ] -> [ String ] -> Any -> Any
|
||||||
|
```
|
||||||
|
|
||||||
|
# Examples
|
||||||
|
:::{.example}
|
||||||
|
## `lib.asserts.checkAssertWarn` usage example
|
||||||
|
```nix
|
||||||
|
checkAssertWarn
|
||||||
|
[ { assertion = false; message = "Will fail"; } ]
|
||||||
|
[ ]
|
||||||
|
null
|
||||||
|
stderr> error:
|
||||||
|
stderr> Failed assertions:
|
||||||
|
stderr> - Will fail
|
||||||
|
|
||||||
|
checkAssertWarn
|
||||||
|
[ { assertion = true; message = "Will not fail"; } ]
|
||||||
|
[ "Will warn" ]
|
||||||
|
null
|
||||||
|
stderr> evaluation warning: Will warn
|
||||||
|
null
|
||||||
|
```
|
||||||
|
|
||||||
|
:::
|
||||||
|
*/
|
||||||
|
checkAssertWarn =
|
||||||
|
assertions: warnings: val:
|
||||||
|
let
|
||||||
|
failedAssertions = map (x: x.message) (filter (x: !x.assertion) assertions);
|
||||||
|
in
|
||||||
|
if failedAssertions != [ ] then
|
||||||
|
throw "\nFailed assertions:\n${concatStringsSep "\n" (map (x: "- ${x}") failedAssertions)}"
|
||||||
|
else
|
||||||
|
showWarnings warnings val;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -77,14 +77,7 @@ let
|
||||||
);
|
);
|
||||||
|
|
||||||
# Handle assertions and warnings
|
# Handle assertions and warnings
|
||||||
|
baseSystemAssertWarn = lib.asserts.checkAssertWarn config.assertions config.warnings baseSystem;
|
||||||
failedAssertions = map (x: x.message) (filter (x: !x.assertion) config.assertions);
|
|
||||||
|
|
||||||
baseSystemAssertWarn =
|
|
||||||
if failedAssertions != [ ] then
|
|
||||||
throw "\nFailed assertions:\n${concatStringsSep "\n" (map (x: "- ${x}") failedAssertions)}"
|
|
||||||
else
|
|
||||||
showWarnings config.warnings baseSystem;
|
|
||||||
|
|
||||||
# Replace runtime dependencies
|
# Replace runtime dependencies
|
||||||
system =
|
system =
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue