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