Skip to content

Commit 649a766

Browse files
committed
lib/tests: move throwTestFailures tests to standalone script
The throwTestFailures tests in misc.nix produce confusing trace output that looks like test failures even when tests pass. This commit moves these tests to a new lib/tests/debug.sh script, following the pattern from modules.sh, sources.sh, and filesystem.sh. The confusing trace output does not appear in any logs anymore. Example of the confusing log: trace: FAIL "testDerivation": Expected: <derivation a> Result: <derivation b> evaluation warning: Using `lib.generators.toPlist` without `escape = true` is deprecated [ ]
1 parent efcea0e commit 649a766

File tree

3 files changed

+85
-29
lines changed

3 files changed

+85
-29
lines changed

lib/tests/debug.sh

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/env bash
2+
3+
# Tests lib/debug.nix
4+
# Run:
5+
# [nixpkgs]$ lib/tests/debug.sh
6+
# or:
7+
# [nixpkgs]$ nix-build lib/tests/release.nix
8+
9+
set -euo pipefail
10+
shopt -s inherit_errexit
11+
12+
# Use
13+
# || die
14+
die() {
15+
echo >&2 "test case failed: " "$@"
16+
exit 1
17+
}
18+
19+
if test -n "${TEST_LIB:-}"; then
20+
NIX_PATH=nixpkgs="$(dirname "$TEST_LIB")"
21+
else
22+
NIX_PATH=nixpkgs="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.."; pwd)"
23+
fi
24+
export NIX_PATH
25+
26+
work="$(mktemp -d)"
27+
clean_up() {
28+
rm -rf "$work"
29+
}
30+
trap clean_up EXIT
31+
cd "$work"
32+
33+
expectSuccess() {
34+
local expr=$1
35+
local expectedResultRegex=$2
36+
if ! result=$(nix-instantiate --eval --strict --json \
37+
--expr "with (import <nixpkgs/lib>).debug; $expr" 2>/dev/null); then
38+
die "$expr failed to evaluate, but it was expected to succeed"
39+
fi
40+
if [[ ! "$result" =~ $expectedResultRegex ]]; then
41+
die "$expr == $result, but $expectedResultRegex was expected"
42+
fi
43+
}
44+
45+
expectFailure() {
46+
local expr=$1
47+
local expectedErrorRegex=$2
48+
if result=$(nix-instantiate --eval --strict --json 2>"$work/stderr" \
49+
--expr "with (import <nixpkgs/lib>).debug; $expr"); then
50+
die "$expr evaluated successfully to $result, but it was expected to fail"
51+
fi
52+
if [[ ! "$(<"$work/stderr")" =~ $expectedErrorRegex ]]; then
53+
die "Error was $(<"$work/stderr"), but $expectedErrorRegex was expected"
54+
fi
55+
}
56+
57+
# Test throwTestFailures with empty failures list
58+
expectSuccess 'throwTestFailures { failures = [ ]; }' "null"
59+
60+
# Test throwTestFailures with actual failures
61+
# This should throw with a specific error message format
62+
expectFailure 'throwTestFailures {
63+
failures = [
64+
{
65+
name = "testDerivation";
66+
expected = builtins.derivation {
67+
name = "a";
68+
builder = "bash";
69+
system = "x86_64-linux";
70+
};
71+
result = builtins.derivation {
72+
name = "b";
73+
builder = "bash";
74+
system = "x86_64-linux";
75+
};
76+
}
77+
];
78+
}' "1 tests failed"
79+
80+
echo >&2 tests ok

lib/tests/misc.nix

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
which is `throw`'s and `abort`'s, without error messages.
77
88
If you need to test error messages or more complex evaluations, see
9-
`lib/tests/modules.sh`, `lib/tests/sources.sh` or `lib/tests/filesystem.sh` as examples.
9+
`lib/tests/modules.sh`, `lib/tests/sources.sh`, `lib/tests/filesystem.sh` or
10+
`lib/tests/debug.sh` as examples.
1011
1112
To run these tests:
1213
@@ -4813,32 +4814,4 @@ runTests {
48134814
targetTarget = "prefix-tt";
48144815
};
48154816
};
4816-
4817-
testThrowTestFailuresEmpty = {
4818-
expr = lib.debug.throwTestFailures {
4819-
failures = [ ];
4820-
};
4821-
4822-
expected = null;
4823-
};
4824-
4825-
testThrowTestFailures = testingThrow (
4826-
lib.debug.throwTestFailures {
4827-
failures = [
4828-
{
4829-
name = "testDerivation";
4830-
expected = builtins.derivation {
4831-
name = "a";
4832-
builder = "bash";
4833-
system = "x86_64-linux";
4834-
};
4835-
result = builtins.derivation {
4836-
name = "b";
4837-
builder = "bash";
4838-
system = "x86_64-linux";
4839-
};
4840-
}
4841-
];
4842-
}
4843-
);
48444817
}

lib/tests/test-with-nix.nix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ pkgs.runCommand "nixpkgs-lib-tests-nix-${nix.version}"
6060
echo "Running lib/tests/sources.sh"
6161
TEST_LIB=$PWD/lib bash lib/tests/sources.sh
6262
63+
echo "Running lib/tests/debug.sh"
64+
TEST_LIB=$PWD/lib bash lib/tests/debug.sh
65+
6366
echo "Running lib/tests/network.sh"
6467
TEST_LIB=$PWD/lib bash lib/tests/network.sh
6568

0 commit comments

Comments
 (0)