2024-06-30 08:16:52 +00:00
|
|
|
/*
|
|
|
|
Collection of functions useful for debugging
|
|
|
|
broken nix expressions.
|
|
|
|
|
|
|
|
* `trace`-like functions take two values, print
|
|
|
|
the first to stderr and return the second.
|
|
|
|
* `traceVal`-like functions take one argument
|
|
|
|
which both printed and returned.
|
|
|
|
* `traceSeq`-like functions fully evaluate their
|
|
|
|
traced value before printing (not just to “weak
|
|
|
|
head normal form” like trace does by default).
|
|
|
|
* Functions that end in `-Fn` take an additional
|
|
|
|
function as their first argument, which is applied
|
|
|
|
to the traced value before it is printed.
|
2024-05-01 22:14:04 +00:00
|
|
|
*/
|
|
|
|
{ lib }:
|
|
|
|
let
|
|
|
|
inherit (lib)
|
|
|
|
isList
|
|
|
|
isAttrs
|
|
|
|
substring
|
|
|
|
attrValues
|
|
|
|
concatLists
|
|
|
|
const
|
|
|
|
elem
|
|
|
|
generators
|
|
|
|
id
|
|
|
|
mapAttrs
|
2024-06-30 08:16:52 +00:00
|
|
|
trace
|
|
|
|
;
|
2024-05-01 22:14:04 +00:00
|
|
|
in
|
|
|
|
|
|
|
|
rec {
|
|
|
|
|
|
|
|
# -- TRACING --
|
|
|
|
|
2024-06-30 08:16:52 +00:00
|
|
|
/*
|
|
|
|
Conditionally trace the supplied message, based on a predicate.
|
2024-05-01 22:14:04 +00:00
|
|
|
|
2024-06-30 08:16:52 +00:00
|
|
|
Type: traceIf :: bool -> string -> a -> a
|
2024-05-01 22:14:04 +00:00
|
|
|
|
2024-06-30 08:16:52 +00:00
|
|
|
Example:
|
|
|
|
traceIf true "hello" 3
|
|
|
|
trace: hello
|
|
|
|
=> 3
|
2024-05-01 22:14:04 +00:00
|
|
|
*/
|
|
|
|
traceIf =
|
|
|
|
# Predicate to check
|
|
|
|
pred:
|
|
|
|
# Message that should be traced
|
|
|
|
msg:
|
|
|
|
# Value to return
|
2024-06-30 08:16:52 +00:00
|
|
|
x:
|
|
|
|
if pred then trace msg x else x;
|
2024-05-01 22:14:04 +00:00
|
|
|
|
2024-06-30 08:16:52 +00:00
|
|
|
/*
|
|
|
|
Trace the supplied value after applying a function to it, and
|
|
|
|
return the original value.
|
2024-05-01 22:14:04 +00:00
|
|
|
|
2024-06-30 08:16:52 +00:00
|
|
|
Type: traceValFn :: (a -> b) -> a -> a
|
2024-05-01 22:14:04 +00:00
|
|
|
|
2024-06-30 08:16:52 +00:00
|
|
|
Example:
|
|
|
|
traceValFn (v: "mystring ${v}") "foo"
|
|
|
|
trace: mystring foo
|
|
|
|
=> "foo"
|
2024-05-01 22:14:04 +00:00
|
|
|
*/
|
|
|
|
traceValFn =
|
|
|
|
# Function to apply
|
|
|
|
f:
|
|
|
|
# Value to trace and return
|
2024-06-30 08:16:52 +00:00
|
|
|
x:
|
|
|
|
trace (f x) x;
|
2024-05-01 22:14:04 +00:00
|
|
|
|
2024-06-30 08:16:52 +00:00
|
|
|
/*
|
|
|
|
Trace the supplied value and return it.
|
2024-05-01 22:14:04 +00:00
|
|
|
|
2024-06-30 08:16:52 +00:00
|
|
|
Type: traceVal :: a -> a
|
2024-05-01 22:14:04 +00:00
|
|
|
|
2024-06-30 08:16:52 +00:00
|
|
|
Example:
|
|
|
|
traceVal 42
|
|
|
|
# trace: 42
|
|
|
|
=> 42
|
2024-05-01 22:14:04 +00:00
|
|
|
*/
|
|
|
|
traceVal = traceValFn id;
|
|
|
|
|
2024-06-30 08:16:52 +00:00
|
|
|
/*
|
|
|
|
`builtins.trace`, but the value is `builtins.deepSeq`ed first.
|
2024-05-01 22:14:04 +00:00
|
|
|
|
2024-06-30 08:16:52 +00:00
|
|
|
Type: traceSeq :: a -> b -> b
|
2024-05-01 22:14:04 +00:00
|
|
|
|
2024-06-30 08:16:52 +00:00
|
|
|
Example:
|
|
|
|
trace { a.b.c = 3; } null
|
|
|
|
trace: { a = <CODE>; }
|
|
|
|
=> null
|
|
|
|
traceSeq { a.b.c = 3; } null
|
|
|
|
trace: { a = { b = { c = 3; }; }; }
|
|
|
|
=> null
|
2024-05-01 22:14:04 +00:00
|
|
|
*/
|
|
|
|
traceSeq =
|
|
|
|
# The value to trace
|
|
|
|
x:
|
|
|
|
# The value to return
|
2024-06-30 08:16:52 +00:00
|
|
|
y:
|
|
|
|
trace (builtins.deepSeq x x) y;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Like `traceSeq`, but only evaluate down to depth n.
|
|
|
|
This is very useful because lots of `traceSeq` usages
|
|
|
|
lead to an infinite recursion.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
traceSeqN 2 { a.b.c = 3; } null
|
|
|
|
trace: { a = { b = {…}; }; }
|
|
|
|
=> null
|
|
|
|
|
|
|
|
Type: traceSeqN :: Int -> a -> b -> b
|
|
|
|
*/
|
|
|
|
traceSeqN =
|
|
|
|
depth: x: y:
|
|
|
|
let
|
|
|
|
snip =
|
|
|
|
v:
|
|
|
|
if isList v then
|
|
|
|
noQuotes "[…]" v
|
|
|
|
else if isAttrs v then
|
|
|
|
noQuotes "{…}" v
|
|
|
|
else
|
|
|
|
v;
|
|
|
|
noQuotes = str: v: {
|
|
|
|
__pretty = const str;
|
|
|
|
val = v;
|
|
|
|
};
|
|
|
|
modify =
|
|
|
|
n: fn: v:
|
|
|
|
if (n == 0) then
|
|
|
|
fn v
|
|
|
|
else if isList v then
|
|
|
|
map (modify (n - 1) fn) v
|
|
|
|
else if isAttrs v then
|
|
|
|
mapAttrs (const (modify (n - 1) fn)) v
|
|
|
|
else
|
|
|
|
v;
|
|
|
|
in
|
|
|
|
trace (generators.toPretty { allowPrettyValues = true; } (modify depth snip x)) y;
|
|
|
|
|
|
|
|
/*
|
|
|
|
A combination of `traceVal` and `traceSeq` that applies a
|
|
|
|
provided function to the value to be traced after `deepSeq`ing
|
|
|
|
it.
|
2024-05-01 22:14:04 +00:00
|
|
|
*/
|
|
|
|
traceValSeqFn =
|
|
|
|
# Function to apply
|
|
|
|
f:
|
|
|
|
# Value to trace
|
2024-06-30 08:16:52 +00:00
|
|
|
v:
|
|
|
|
traceValFn f (builtins.deepSeq v v);
|
2024-05-01 22:14:04 +00:00
|
|
|
|
2024-06-30 08:16:52 +00:00
|
|
|
# A combination of `traceVal` and `traceSeq`.
|
2024-05-01 22:14:04 +00:00
|
|
|
traceValSeq = traceValSeqFn id;
|
|
|
|
|
2024-06-30 08:16:52 +00:00
|
|
|
/*
|
|
|
|
A combination of `traceVal` and `traceSeqN` that applies a
|
|
|
|
provided function to the value to be traced.
|
|
|
|
*/
|
2024-05-01 22:14:04 +00:00
|
|
|
traceValSeqNFn =
|
|
|
|
# Function to apply
|
2024-06-30 08:16:52 +00:00
|
|
|
f: depth:
|
2024-05-01 22:14:04 +00:00
|
|
|
# Value to trace
|
2024-06-30 08:16:52 +00:00
|
|
|
v:
|
|
|
|
traceSeqN depth (f v) v;
|
2024-05-01 22:14:04 +00:00
|
|
|
|
2024-06-30 08:16:52 +00:00
|
|
|
# A combination of `traceVal` and `traceSeqN`.
|
2024-05-01 22:14:04 +00:00
|
|
|
traceValSeqN = traceValSeqNFn id;
|
|
|
|
|
2024-06-30 08:16:52 +00:00
|
|
|
/*
|
|
|
|
Trace the input and output of a function `f` named `name`,
|
|
|
|
both down to `depth`.
|
2024-05-01 22:14:04 +00:00
|
|
|
|
2024-06-30 08:16:52 +00:00
|
|
|
This is useful for adding around a function call,
|
|
|
|
to see the before/after of values as they are transformed.
|
2024-05-01 22:14:04 +00:00
|
|
|
|
2024-06-30 08:16:52 +00:00
|
|
|
Example:
|
|
|
|
traceFnSeqN 2 "id" (x: x) { a.b.c = 3; }
|
|
|
|
trace: { fn = "id"; from = { a.b = {…}; }; to = { a.b = {…}; }; }
|
|
|
|
=> { a.b.c = 3; }
|
2024-05-01 22:14:04 +00:00
|
|
|
*/
|
2024-06-30 08:16:52 +00:00
|
|
|
traceFnSeqN =
|
|
|
|
depth: name: f: v:
|
|
|
|
let
|
|
|
|
res = f v;
|
|
|
|
in
|
|
|
|
lib.traceSeqN (depth + 1) {
|
|
|
|
fn = name;
|
|
|
|
from = v;
|
|
|
|
to = res;
|
|
|
|
} res;
|
2024-05-01 22:14:04 +00:00
|
|
|
|
|
|
|
# -- TESTING --
|
|
|
|
|
2024-06-30 08:16:52 +00:00
|
|
|
/*
|
|
|
|
Evaluates a set of tests.
|
2024-05-01 22:14:04 +00:00
|
|
|
|
|
|
|
A test is an attribute set `{expr, expected}`,
|
|
|
|
denoting an expression and its expected result.
|
|
|
|
|
|
|
|
The result is a `list` of __failed tests__, each represented as
|
|
|
|
`{name, expected, result}`,
|
|
|
|
|
|
|
|
- expected
|
|
|
|
- What was passed as `expected`
|
|
|
|
- result
|
|
|
|
- The actual `result` of the test
|
|
|
|
|
|
|
|
Used for regression testing of the functions in lib; see
|
|
|
|
tests.nix for more examples.
|
|
|
|
|
|
|
|
Important: Only attributes that start with `test` are executed.
|
|
|
|
|
|
|
|
- If you want to run only a subset of the tests add the attribute `tests = ["testName"];`
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
runTests {
|
|
|
|
testAndOk = {
|
|
|
|
expr = lib.and true false;
|
|
|
|
expected = false;
|
|
|
|
};
|
|
|
|
testAndFail = {
|
|
|
|
expr = lib.and true false;
|
|
|
|
expected = true;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
->
|
|
|
|
[
|
|
|
|
{
|
|
|
|
name = "testAndFail";
|
|
|
|
expected = true;
|
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
Type:
|
|
|
|
runTests :: {
|
|
|
|
tests = [ String ];
|
|
|
|
${testName} :: {
|
|
|
|
expr :: a;
|
|
|
|
expected :: a;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
->
|
|
|
|
[
|
|
|
|
{
|
|
|
|
name :: String;
|
|
|
|
expected :: a;
|
|
|
|
result :: a;
|
|
|
|
}
|
|
|
|
]
|
|
|
|
*/
|
|
|
|
runTests =
|
|
|
|
# Tests to run
|
2024-06-30 08:16:52 +00:00
|
|
|
tests:
|
|
|
|
concatLists (
|
|
|
|
attrValues (
|
|
|
|
mapAttrs (
|
|
|
|
name: test:
|
|
|
|
let
|
|
|
|
testsToRun = if tests ? tests then tests.tests else [ ];
|
|
|
|
in
|
|
|
|
if
|
|
|
|
(substring 0 4 name == "test" || elem name testsToRun)
|
|
|
|
&& ((testsToRun == [ ]) || elem name tests.tests)
|
|
|
|
&& (test.expr != test.expected)
|
|
|
|
|
|
|
|
then
|
|
|
|
[
|
|
|
|
{
|
|
|
|
inherit name;
|
|
|
|
expected = test.expected;
|
|
|
|
result = test.expr;
|
|
|
|
}
|
|
|
|
]
|
|
|
|
else
|
|
|
|
[ ]
|
|
|
|
) tests
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/*
|
|
|
|
Create a test assuming that list elements are `true`.
|
2024-05-01 22:14:04 +00:00
|
|
|
|
2024-06-30 08:16:52 +00:00
|
|
|
Example:
|
|
|
|
{ testX = allTrue [ true ]; }
|
2024-05-01 22:14:04 +00:00
|
|
|
*/
|
2024-06-30 08:16:52 +00:00
|
|
|
testAllTrue = expr: {
|
|
|
|
inherit expr;
|
|
|
|
expected = map (x: true) expr;
|
|
|
|
};
|
2024-05-01 22:14:04 +00:00
|
|
|
}
|