mirror of
https://github.com/NixOS/nixpkgs.git
synced 2024-11-19 00:08:32 +01:00
48965506a1
`assert` has the annoying property that it dumps a lot of code at the user without the built in capability to display a nicer message. We have worked around this using `assertMsg` which would *additionally* display a nice message. We can do even better: By using `throw` we can make evaluation fail before assert draws its conclusions and prevent it from displaying the code making up the assert condition, so we get the nicer message of `throw` and the syntactical convenience of `assert`. Before: nix-repl> python.override { reproducibleBuild = true; stripBytecode = false; } trace: Deterministic builds require stripping bytecode. error: assertion (((lib).assertMsg (reproducibleBuild -> stripBytecode)) "Deterministic builds require stripping bytecode.") failed at /home/lukas/src/nix/nixpkgs/pkgs/development/interpreters/python/cpython/2.7/default.nix:45:1 After: nix-repl> python.override { reproducibleBuild = true; stripBytecode = false; } error: Deterministic builds require stripping bytecode.
42 lines
1.1 KiB
Nix
42 lines
1.1 KiB
Nix
{ lib }:
|
|
|
|
rec {
|
|
|
|
/* Throw if pred is false, else return pred.
|
|
Intended to be used to augment asserts with helpful error messages.
|
|
|
|
Example:
|
|
assertMsg false "nope"
|
|
stderr> error: nope
|
|
|
|
assert assertMsg ("foo" == "bar") "foo is not bar, silly"; ""
|
|
stderr> error: foo is not bar, silly
|
|
|
|
Type:
|
|
assertMsg :: Bool -> String -> Bool
|
|
*/
|
|
# TODO(Profpatsch): add tests that check stderr
|
|
assertMsg = pred: msg:
|
|
pred || builtins.throw msg;
|
|
|
|
/* Specialized `assertMsg` for checking if val is one of the elements
|
|
of a list. Useful for checking enums.
|
|
|
|
Example:
|
|
let sslLibrary = "libressl";
|
|
in assertOneOf "sslLibrary" sslLibrary [ "openssl" "bearssl" ]
|
|
stderr> error: sslLibrary must be one of [
|
|
stderr> "openssl"
|
|
stderr> "bearssl"
|
|
stderr> ], but is: "libressl"
|
|
|
|
Type:
|
|
assertOneOf :: String -> ComparableVal -> List ComparableVal -> Bool
|
|
*/
|
|
assertOneOf = name: val: xs: assertMsg
|
|
(lib.elem val xs)
|
|
"${name} must be one of ${
|
|
lib.generators.toPretty {} xs}, but is: ${
|
|
lib.generators.toPretty {} val}";
|
|
|
|
}
|