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