Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

254 строки
8.2 KiB

  1. /* Collection of functions useful for debugging
  2. broken nix expressions.
  3. * `trace`-like functions take two values, print
  4. the first to stderr and return the second.
  5. * `traceVal`-like functions take one argument
  6. which both printed and returned.
  7. * `traceSeq`-like functions fully evaluate their
  8. traced value before printing (not just to “weak
  9. head normal form” like trace does by default).
  10. * Functions that end in `-Fn` take an additional
  11. function as their first argument, which is applied
  12. to the traced value before it is printed.
  13. */
  14. { lib }:
  15. let
  16. inherit (builtins) trace isAttrs isList isInt
  17. head substring attrNames;
  18. inherit (lib) id elem isFunction;
  19. in
  20. rec {
  21. # -- TRACING --
  22. /* Conditionally trace the supplied message, based on a predicate.
  23. Type: traceIf :: bool -> string -> a -> a
  24. Example:
  25. traceIf true "hello" 3
  26. trace: hello
  27. => 3
  28. */
  29. traceIf =
  30. # Predicate to check
  31. pred:
  32. # Message that should be traced
  33. msg:
  34. # Value to return
  35. x: if pred then trace msg x else x;
  36. /* Trace the supplied value after applying a function to it, and
  37. return the original value.
  38. Type: traceValFn :: (a -> b) -> a -> a
  39. Example:
  40. traceValFn (v: "mystring ${v}") "foo"
  41. trace: mystring foo
  42. => "foo"
  43. */
  44. traceValFn =
  45. # Function to apply
  46. f:
  47. # Value to trace and return
  48. x: trace (f x) x;
  49. /* Trace the supplied value and return it.
  50. Type: traceVal :: a -> a
  51. Example:
  52. traceVal 42
  53. # trace: 42
  54. => 42
  55. */
  56. traceVal = traceValFn id;
  57. /* `builtins.trace`, but the value is `builtins.deepSeq`ed first.
  58. Type: traceSeq :: a -> b -> b
  59. Example:
  60. trace { a.b.c = 3; } null
  61. trace: { a = <CODE>; }
  62. => null
  63. traceSeq { a.b.c = 3; } null
  64. trace: { a = { b = { c = 3; }; }; }
  65. => null
  66. */
  67. traceSeq =
  68. # The value to trace
  69. x:
  70. # The value to return
  71. y: trace (builtins.deepSeq x x) y;
  72. /* Like `traceSeq`, but only evaluate down to depth n.
  73. This is very useful because lots of `traceSeq` usages
  74. lead to an infinite recursion.
  75. Example:
  76. traceSeqN 2 { a.b.c = 3; } null
  77. trace: { a = { b = {…}; }; }
  78. => null
  79. */
  80. traceSeqN = depth: x: y: with lib;
  81. let snip = v: if isList v then noQuotes "[…]" v
  82. else if isAttrs v then noQuotes "{…}" v
  83. else v;
  84. noQuotes = str: v: { __pretty = const str; val = v; };
  85. modify = n: fn: v: if (n == 0) then fn v
  86. else if isList v then map (modify (n - 1) fn) v
  87. else if isAttrs v then mapAttrs
  88. (const (modify (n - 1) fn)) v
  89. else v;
  90. in trace (generators.toPretty { allowPrettyValues = true; }
  91. (modify depth snip x)) y;
  92. /* A combination of `traceVal` and `traceSeq` that applies a
  93. provided function to the value to be traced after `deepSeq`ing
  94. it.
  95. */
  96. traceValSeqFn =
  97. # Function to apply
  98. f:
  99. # Value to trace
  100. v: traceValFn f (builtins.deepSeq v v);
  101. /* A combination of `traceVal` and `traceSeq`. */
  102. traceValSeq = traceValSeqFn id;
  103. /* A combination of `traceVal` and `traceSeqN` that applies a
  104. provided function to the value to be traced. */
  105. traceValSeqNFn =
  106. # Function to apply
  107. f:
  108. depth:
  109. # Value to trace
  110. v: traceSeqN depth (f v) v;
  111. /* A combination of `traceVal` and `traceSeqN`. */
  112. traceValSeqN = traceValSeqNFn id;
  113. # -- TESTING --
  114. /* Evaluate a set of tests. A test is an attribute set `{expr,
  115. expected}`, denoting an expression and its expected result. The
  116. result is a list of failed tests, each represented as `{name,
  117. expected, actual}`, denoting the attribute name of the failing
  118. test and its expected and actual results.
  119. Used for regression testing of the functions in lib; see
  120. tests.nix for an example. Only tests having names starting with
  121. "test" are run.
  122. Add attr { tests = ["testName"]; } to run these tests only.
  123. */
  124. runTests =
  125. # Tests to run
  126. tests: lib.concatLists (lib.attrValues (lib.mapAttrs (name: test:
  127. let testsToRun = if tests ? tests then tests.tests else [];
  128. in if (substring 0 4 name == "test" || elem name testsToRun)
  129. && ((testsToRun == []) || elem name tests.tests)
  130. && (test.expr != test.expected)
  131. then [ { inherit name; expected = test.expected; result = test.expr; } ]
  132. else [] ) tests));
  133. /* Create a test assuming that list elements are `true`.
  134. Example:
  135. { testX = allTrue [ true ]; }
  136. */
  137. testAllTrue = expr: { inherit expr; expected = map (x: true) expr; };
  138. # -- DEPRECATED --
  139. traceShowVal = x: trace (showVal x) x;
  140. traceShowValMarked = str: x: trace (str + showVal x) x;
  141. attrNamesToStr = a:
  142. trace ( "Warning: `attrNamesToStr` is deprecated "
  143. + "and will be removed in the next release. "
  144. + "Please use more specific concatenation "
  145. + "for your uses (`lib.concat(Map)StringsSep`)." )
  146. (lib.concatStringsSep "; " (map (x: "${x}=") (attrNames a)));
  147. showVal = with lib;
  148. trace ( "Warning: `showVal` is deprecated "
  149. + "and will be removed in the next release, "
  150. + "please use `traceSeqN`" )
  151. (let
  152. modify = v:
  153. let pr = f: { __pretty = f; val = v; };
  154. in if isDerivation v then pr
  155. (drv: "<δ:${drv.name}:${concatStringsSep ","
  156. (attrNames drv)}>")
  157. else if [] == v then pr (const "[]")
  158. else if isList v then pr (l: "[ ${go (head l)}, … ]")
  159. else if isAttrs v then pr
  160. (a: "{ ${ concatStringsSep ", " (attrNames a)} }")
  161. else v;
  162. go = x: generators.toPretty
  163. { allowPrettyValues = true; }
  164. (modify x);
  165. in go);
  166. traceXMLVal = x:
  167. trace ( "Warning: `traceXMLVal` is deprecated "
  168. + "and will be removed in the next release. "
  169. + "Please use `traceValFn builtins.toXML`." )
  170. (trace (builtins.toXML x) x);
  171. traceXMLValMarked = str: x:
  172. trace ( "Warning: `traceXMLValMarked` is deprecated "
  173. + "and will be removed in the next release. "
  174. + "Please use `traceValFn (x: str + builtins.toXML x)`." )
  175. (trace (str + builtins.toXML x) x);
  176. # trace the arguments passed to function and its result
  177. # maybe rewrite these functions in a traceCallXml like style. Then one function is enough
  178. traceCall = n: f: a: let t = n2: x: traceShowValMarked "${n} ${n2}:" x; in t "result" (f (t "arg 1" a));
  179. 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));
  180. 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));
  181. traceValIfNot = c: x:
  182. trace ( "Warning: `traceValIfNot` is deprecated "
  183. + "and will be removed in the next release. "
  184. + "Please use `if/then/else` and `traceValSeq 1`.")
  185. (if c x then true else traceSeq (showVal x) false);
  186. addErrorContextToAttrs = attrs:
  187. trace ( "Warning: `addErrorContextToAttrs` is deprecated "
  188. + "and will be removed in the next release. "
  189. + "Please use `builtins.addErrorContext` directly." )
  190. (lib.mapAttrs (a: v: lib.addErrorContext "while evaluating ${a}" v) attrs);
  191. # example: (traceCallXml "myfun" id 3) will output something like
  192. # calling myfun arg 1: 3 result: 3
  193. # this forces deep evaluation of all arguments and the result!
  194. # note: if result doesn't evaluate you'll get no trace at all (FIXME)
  195. # args should be printed in any case
  196. traceCallXml = a:
  197. trace ( "Warning: `traceCallXml` is deprecated "
  198. + "and will be removed in the next release. "
  199. + "Please complain if you use the function regularly." )
  200. (if !isInt a then
  201. traceCallXml 1 "calling ${a}\n"
  202. else
  203. let nr = a;
  204. in (str: expr:
  205. if isFunction expr then
  206. (arg:
  207. traceCallXml (builtins.add 1 nr) "${str}\n arg ${builtins.toString nr} is \n ${builtins.toXML (builtins.seq arg arg)}" (expr arg)
  208. )
  209. else
  210. let r = builtins.seq expr expr;
  211. in trace "${str}\n result:\n${builtins.toXML r}" r
  212. ));
  213. }