2009-05-24 14:57:46 +04:00
|
|
|
let lib = import ./default.nix;
|
|
|
|
|
|
|
|
inherit (builtins) trace attrNamesToStr isAttrs isFunction isList head substring attrNames;
|
|
|
|
|
|
|
|
in
|
2009-03-07 02:21:31 +03:00
|
|
|
|
2009-02-09 19:51:03 +03:00
|
|
|
rec {
|
|
|
|
|
|
|
|
|
|
|
|
# Wrapper aroung the primop `addErrorContext', which shouldn't used
|
|
|
|
# directly. It evaluates and returns `val', but if an evaluation
|
|
|
|
# error occurs, the text in `msg' is added to the error context
|
|
|
|
# (stack trace) printed by Nix.
|
|
|
|
addErrorContext =
|
|
|
|
if builtins ? addErrorContext
|
|
|
|
then builtins.addErrorContext
|
|
|
|
else msg: val: val;
|
|
|
|
|
2009-03-07 02:21:31 +03:00
|
|
|
addErrorContextToAttrs = lib.mapAttrs (a : v : lib.addErrorContext "while evaluating ${a}" v);
|
2009-02-09 19:51:03 +03:00
|
|
|
|
2009-03-31 17:03:50 +04:00
|
|
|
|
2009-03-25 16:48:55 +03:00
|
|
|
traceVal = if builtins ? trace then x: (builtins.trace x x) else x: x;
|
|
|
|
traceXMLVal = if builtins ? trace then x: (builtins.trace (builtins.toXML x) x) else x: x;
|
2009-02-09 19:51:03 +03:00
|
|
|
|
2009-03-31 17:03:50 +04:00
|
|
|
|
2009-03-07 02:21:35 +03:00
|
|
|
# this can help debug your code as well - designed to not produce thousands of lines
|
2009-05-24 14:57:46 +04:00
|
|
|
traceShowVal = x : trace (showVal x) x;
|
|
|
|
traceShowValMarked = str: x: trace (str + showVal x) x;
|
|
|
|
attrNamesToStr = a : lib.concatStringsSep "; " (map (x : "${x}=") (attrNames a));
|
2009-03-25 16:48:55 +03:00
|
|
|
showVal = x :
|
2009-05-24 14:57:46 +04:00
|
|
|
if isAttrs x then
|
2009-03-25 16:48:55 +03:00
|
|
|
if x ? outPath then "x is a derivation, name ${if x ? name then x.name else "<no name>"}, { ${attrNamesToStr x} }"
|
2009-03-07 02:21:35 +03:00
|
|
|
else "x is attr set { ${attrNamesToStr x} }"
|
2009-05-24 14:57:46 +04:00
|
|
|
else if isFunction x then "x is a function"
|
2009-03-25 16:48:55 +03:00
|
|
|
else if x == [] then "x is an empty list"
|
2009-07-17 16:04:34 +04:00
|
|
|
else if isList x then "x is a list, first element is: ${showVal (head x)}"
|
2009-03-25 16:48:55 +03:00
|
|
|
else if x == true then "x is boolean true"
|
|
|
|
else if x == false then "x is boolean false"
|
|
|
|
else if x == null then "x is null"
|
2009-07-17 16:04:34 +04:00
|
|
|
else "x is probably a string `${substring 0 50 x}...'";
|
2009-03-07 02:21:35 +03:00
|
|
|
# trace the arguments passed to function and its result
|
2009-03-25 16:48:55 +03:00
|
|
|
traceCall = n : f : a : let t = n2 : x : traceShowValMarked "${n} ${n2}:" x; in t "result" (f (t "arg 1" a));
|
|
|
|
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));
|
|
|
|
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));
|
2009-03-07 02:21:35 +03:00
|
|
|
|
2009-06-26 16:42:00 +04:00
|
|
|
traceValIfNot = c: x:
|
|
|
|
if c x then true else trace (showVal x) false;
|
2009-03-31 17:03:50 +04:00
|
|
|
|
|
|
|
/* Evaluate a set of tests. A test is an attribute set {expr,
|
|
|
|
expected}, denoting an expression and its expected result. The
|
|
|
|
result is a list of failed tests, each represented as {name,
|
|
|
|
expected, actual}, denoting the attribute name of the failing
|
|
|
|
test and its expected and actual results. Used for regression
|
|
|
|
testing of the functions in lib; see tests.nix for an example.
|
|
|
|
*/
|
|
|
|
runTests = tests: lib.concatLists (lib.attrValues (lib.mapAttrs (name: test:
|
|
|
|
if ! lib.eqStrict test.expr test.expected
|
|
|
|
then [ { inherit name; expected = test.expected; result = test.expr; } ]
|
|
|
|
else [] ) tests));
|
|
|
|
|
2009-03-07 02:21:35 +03:00
|
|
|
}
|