diff --git a/pkgs/lib/debug.nix b/pkgs/lib/debug.nix index ce38259725e..a58539ee3c4 100644 --- a/pkgs/lib/debug.nix +++ b/pkgs/lib/debug.nix @@ -14,9 +14,11 @@ rec { addErrorContextToAttrs = lib.mapAttrs (a : v : lib.addErrorContext "while evaluating ${a}" v); + 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; + # this can help debug your code as well - designed to not produce thousands of lines traceShowVal = x : __trace (showVal x) x; traceShowValMarked = str: x: __trace (str + showVal x) x; @@ -37,4 +39,17 @@ rec { 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)); + + /* 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)); + } diff --git a/pkgs/lib/misc-tests.nix b/pkgs/lib/misc-tests.nix deleted file mode 100644 index f8deb1b1a8a..00000000000 --- a/pkgs/lib/misc-tests.nix +++ /dev/null @@ -1,53 +0,0 @@ -let lib = import ./default.nix; - - eqStrictTest = - let inherit(lib) eqStrict; in - assert eqStrict 2 2; - assert !(eqStrict 3 2); - assert eqStrict [2 1] [2 1]; - assert !(eqStrict [1 3] [1 2]); - assert eqStrict {a = 7; b = 20;} {b= 20; a = 7;}; - assert eqStrict [{a = 7; b = 20;}] [{b= 20; a = 7;}]; - assert eqStrict {a = [7 8]; b = 20;} {b= 20; a = [7 8];}; - "ok"; - - overridableDelayableArgsTest = - let inherit (lib) defaultOverridableDelayableArgs; - res1 = defaultOverridableDelayableArgs lib.id {}; - res2 = defaultOverridableDelayableArgs lib.id { a = 7; }; - res3 = let x = defaultOverridableDelayableArgs lib.id { a = 7; }; - in (x.merge) { b = 10; }; - res4 = let x = defaultOverridableDelayableArgs lib.id { a = 7; }; - in (x.merge) ( x: { b = 10; }); - res5 = let x = defaultOverridableDelayableArgs lib.id { a = 7; }; - in (x.merge) ( x: { a = __add x.a 3; }); - res6 = let x = defaultOverridableDelayableArgs lib.id { a = 7; mergeAttrBy = { a = __add; }; }; - y = x.merge {}; - in (y.merge) { a = 10; }; - - resRem7 = res6.replace (a : removeAttrs a ["a"]); - - resReplace6 = let x = defaultOverridableDelayableArgs lib.id { a = 7; mergeAttrBy = { a = __add; }; }; - x2 = x.merge { a = 20; }; # now we have 27 - in (x2.replace) { a = 10; }; # and override the value by 10 - - # fixed tests (delayed args): (when using them add some comments, please) - resFixed1 = - let x = defaultOverridableDelayableArgs lib.id ( x : { a = 7; c = x.fixed.b; }); - y = x.merge (x : { name = "name-${builtins.toString x.fixed.c}"; }); - in (y.merge) { b = 10; }; - strip = attrs : removeAttrs attrs ["merge" "replace"]; - - in - assert lib.eqStrict (strip res1) { }; - assert lib.eqStrict (strip res2) { a = 7; }; - assert lib.eqStrict (strip res3) { a = 7; b = 10; }; - assert lib.eqStrict (strip res4) { a = 7; b = 10; }; - assert lib.eqStrict (strip res5) { a = 10; }; - assert lib.eqStrict (strip res6) { a = 17; }; - assert lib.eqStrict (strip resRem7) {}; - assert lib.eqStrict (strip resFixed1) { a = 7; b = 10; c =10; name = "name-10"; }; - "ok"; - - -in [ eqStrictTest overridableDelayableArgsTest ] diff --git a/pkgs/lib/tests.nix b/pkgs/lib/tests.nix index 2e75d7f141f..395b4b3e17b 100644 --- a/pkgs/lib/tests.nix +++ b/pkgs/lib/tests.nix @@ -1,8 +1,102 @@ -let lib = import ./default.nix; +with import ./default.nix; - miscTests = import ./misc-tests.nix; +runTests { -in - if lib.all (a : a == "ok") (lib.concatLists [ miscTests ]) then - throw "all tests have passed" - else "there has been a some lib test failures" + id = { + expr = id 1; + expected = 1; + }; + + const = { + expr = const 2 3; + expected = 2; + }; + + or = { + expr = or true false; + expected = true; + }; + + and = { + expr = and true false; + expected = false; + }; + + fix = { + expr = fix (x: {a = if x ? a then "a" else "b";}); + expected = {a = "a";}; + }; + + concatMapStrings = { + expr = concatMapStrings (x: x + ";") ["a" "b" "c"]; + expected = "a;b;c;"; + }; + + concatStringsSep = { + expr = concatStringsSep "," ["a" "b" "c"]; + expected = "a,b,c"; + }; + + filter = { + expr = filter (x: x != "a") ["a" "b" "c" "a"]; + expected = ["b" "c"]; + }; + + fold = { + expr = fold (builtins.add) 0 (range 0 100); + expected = 5050; + }; + + eqStrict = { + expr = all id [ + (eqStrict 2 2) + (!eqStrict 3 2) + (eqStrict [2 1] [2 1]) + (!eqStrict [1 3] [1 2]) + (eqStrict {a = 7; b = 20;} {b= 20; a = 7;}) + (eqStrict [{a = 7; b = 20;}] [{b= 20; a = 7;}]) + (eqStrict {a = [7 8]; b = 20;} {b= 20; a = [7 8];}) + ]; + expected = true; + }; + + overridableDelayableArgsTest = { + expr = + let res1 = defaultOverridableDelayableArgs id {}; + res2 = defaultOverridableDelayableArgs id { a = 7; }; + res3 = let x = defaultOverridableDelayableArgs id { a = 7; }; + in (x.merge) { b = 10; }; + res4 = let x = defaultOverridableDelayableArgs id { a = 7; }; + in (x.merge) ( x: { b = 10; }); + res5 = let x = defaultOverridableDelayableArgs id { a = 7; }; + in (x.merge) ( x: { a = __add x.a 3; }); + res6 = let x = defaultOverridableDelayableArgs id { a = 7; mergeAttrBy = { a = __add; }; }; + y = x.merge {}; + in (y.merge) { a = 10; }; + + resRem7 = res6.replace (a : removeAttrs a ["a"]); + + resReplace6 = let x = defaultOverridableDelayableArgs id { a = 7; mergeAttrBy = { a = __add; }; }; + x2 = x.merge { a = 20; }; # now we have 27 + in (x2.replace) { a = 10; }; # and override the value by 10 + + # fixed tests (delayed args): (when using them add some comments, please) + resFixed1 = + let x = defaultOverridableDelayableArgs id ( x : { a = 7; c = x.fixed.b; }); + y = x.merge (x : { name = "name-${builtins.toString x.fixed.c}"; }); + in (y.merge) { b = 10; }; + strip = attrs : removeAttrs attrs ["merge" "replace"]; + in all id + [ (eqStrict (strip res1) { }) + (eqStrict (strip res2) { a = 7; }) + (eqStrict (strip res3) { a = 7; b = 10; }) + (eqStrict (strip res4) { a = 7; b = 10; }) + (eqStrict (strip res5) { a = 10; }) + (eqStrict (strip res6) { a = 17; }) + (eqStrict (strip resRem7) {}) + (eqStrict (strip resFixed1) { a = 7; b = 10; c =10; name = "name-10"; }) + ]; + expected = true; + }; + +} diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index 40ca3a7840e..91cec7d28f6 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -9348,6 +9348,7 @@ let inherit libxml2 guile perl intltool libtool pkgconfig; }; + ### SCIENCE/BIOLOGY alliance = import ../applications/science/electronics/alliance { @@ -9392,6 +9393,7 @@ let inherit fetchurl stdenv perl paml; }; + ### SCIENCE/MATH atlas = import ../development/libraries/science/math/atlas { @@ -9402,18 +9404,21 @@ let inherit fetchurl stdenv gfortran; }; */ + ### SCIENCE/LOGIC coq = import ../applications/science/logic/coq { inherit fetchurl stdenv ocaml ncurses; }; + ### SCIENCE / ELECTRONICS ngspice = import ../applications/science/electronics/ngspice { inherit fetchurl stdenv readline; }; + ### SCIENCE / MATH maxima = import ../applications/science/math/maxima { @@ -9432,6 +9437,7 @@ let withX = true; }; + ### MISC atari800 = import ../misc/emulators/atari800 { @@ -9745,5 +9751,5 @@ let inherit (stdenv) mkDerivation; }; - libTests = import ../lib/tests.nix; + }; in pkgs diff --git a/pkgs/top-level/make-tarball.nix b/pkgs/top-level/make-tarball.nix index d6e3a118d44..11c12378ba8 100644 --- a/pkgs/top-level/make-tarball.nix +++ b/pkgs/top-level/make-tarball.nix @@ -39,6 +39,12 @@ releaseTools.makeSourceTarball { doCheck = true; checkPhase = '' + # Run the regression tests in `lib'. + if test "$(nix-instantiate --eval-only --strict tests.nix)" != "List([])"; then + echo "regression tests for `lib' failed" + exit 1 + fi + # Check that we can fully evaluate build-for-release.nix. header "checking pkgs/top-level/build-for-release.nix" nix-env --readonly-mode -f pkgs/top-level/build-for-release.nix \