You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

126 lines
5.2 KiB

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