1{ lib }:
2
3let
4
5inherit (builtins) trace attrNamesToStr isAttrs isList isInt
6 isString isBool head substring attrNames;
7
8inherit (lib) all id mapAttrsFlatten elem isFunction;
9
10in
11
12rec {
13
14 inherit (builtins) addErrorContext;
15
16 addErrorContextToAttrs = lib.mapAttrs (a: v: lib.addErrorContext "while evaluating ${a}" v);
17
18 traceIf = p: msg: x: if p then trace msg x else x;
19
20 traceVal = x: trace x x;
21 traceXMLVal = x: trace (builtins.toXML x) x;
22 traceXMLValMarked = str: x: trace (str + builtins.toXML x) x;
23
24 # strict trace functions (traced structure is fully evaluated and printed)
25
26 /* `builtins.trace`, but the value is `builtins.deepSeq`ed first. */
27 traceSeq = x: y: trace (builtins.deepSeq x x) y;
28
29 /* Like `traceSeq`, but only down to depth n.
30 * This is very useful because lots of `traceSeq` usages
31 * lead to an infinite recursion.
32 */
33 traceSeqN = depth: x: y: with lib;
34 let snip = v: if isList v then noQuotes "[…]" v
35 else if isAttrs v then noQuotes "{…}" v
36 else v;
37 noQuotes = str: v: { __pretty = const str; val = v; };
38 modify = n: fn: v: if (n == 0) then fn v
39 else if isList v then map (modify (n - 1) fn) v
40 else if isAttrs v then mapAttrs
41 (const (modify (n - 1) fn)) v
42 else v;
43 in trace (generators.toPretty { allowPrettyValues = true; }
44 (modify depth snip x)) y;
45
46 /* `traceSeq`, but the same value is traced and returned */
47 traceValSeq = v: traceVal (builtins.deepSeq v v);
48 /* `traceValSeq` but with fixed depth */
49 traceValSeqN = depth: v: traceSeqN depth v v;
50
51
52 # this can help debug your code as well - designed to not produce thousands of lines
53 traceShowVal = x: trace (showVal x) x;
54 traceShowValMarked = str: x: trace (str + showVal x) x;
55 attrNamesToStr = a: lib.concatStringsSep "; " (map (x: "${x}=") (attrNames a));
56 showVal = x:
57 if isAttrs x then
58 if x ? outPath then "x is a derivation, name ${if x ? name then x.name else "<no name>"}, { ${attrNamesToStr x} }"
59 else "x is attr set { ${attrNamesToStr x} }"
60 else if isFunction x then "x is a function"
61 else if x == [] then "x is an empty list"
62 else if isList x then "x is a list, first element is: ${showVal (head x)}"
63 else if x == true then "x is boolean true"
64 else if x == false then "x is boolean false"
65 else if x == null then "x is null"
66 else if isInt x then "x is an integer `${toString x}'"
67 else if isString x then "x is a string `${substring 0 50 x}...'"
68 else "x is probably a path `${substring 0 50 (toString x)}...'";
69
70 # trace the arguments passed to function and its result
71 # maybe rewrite these functions in a traceCallXml like style. Then one function is enough
72 traceCall = n: f: a: let t = n2: x: traceShowValMarked "${n} ${n2}:" x; in t "result" (f (t "arg 1" a));
73 traceCall2 = n: f: a: b: let t = n2: x: traceShowValMarked "${n} ${n2}:" x; in t "result" (f (t "arg 1" a) (t "arg 2" b));
74 traceCall3 = n: f: a: b: c: let t = n2: x: traceShowValMarked "${n} ${n2}:" x; in t "result" (f (t "arg 1" a) (t "arg 2" b) (t "arg 3" c));
75
76 # FIXME: rename this?
77 traceValIfNot = c: x:
78 if c x then true else trace (showVal x) false;
79
80 /* Evaluate a set of tests. A test is an attribute set {expr,
81 expected}, denoting an expression and its expected result. The
82 result is a list of failed tests, each represented as {name,
83 expected, actual}, denoting the attribute name of the failing
84 test and its expected and actual results. Used for regression
85 testing of the functions in lib; see tests.nix for an example.
86 Only tests having names starting with "test" are run.
87 Add attr { tests = ["testName"]; } to run these test only
88 */
89 runTests = tests: lib.concatLists (lib.attrValues (lib.mapAttrs (name: test:
90 let testsToRun = if tests ? tests then tests.tests else [];
91 in if (substring 0 4 name == "test" || elem name testsToRun)
92 && ((testsToRun == []) || elem name tests.tests)
93 && (test.expr != test.expected)
94
95 then [ { inherit name; expected = test.expected; result = test.expr; } ]
96 else [] ) tests));
97
98 # create a test assuming that list elements are true
99 # usage: { testX = allTrue [ true ]; }
100 testAllTrue = expr: { inherit expr; expected = map (x: true) expr; };
101
102 strict = v:
103 trace "Warning: strict is deprecated and will be removed in the next release"
104 (builtins.seq v v);
105
106 # example: (traceCallXml "myfun" id 3) will output something like
107 # calling myfun arg 1: 3 result: 3
108 # this forces deep evaluation of all arguments and the result!
109 # note: if result doesn't evaluate you'll get no trace at all (FIXME)
110 # args should be printed in any case
111 traceCallXml = a:
112 if !isInt a then
113 traceCallXml 1 "calling ${a}\n"
114 else
115 let nr = a;
116 in (str: expr:
117 if isFunction expr then
118 (arg:
119 traceCallXml (builtins.add 1 nr) "${str}\n arg ${builtins.toString nr} is \n ${builtins.toXML (strict arg)}" (expr arg)
120 )
121 else
122 let r = strict expr;
123 in trace "${str}\n result:\n${builtins.toXML r}" r
124 );
125}