mirror of
https://github.com/NixOS/nixpkgs.git
synced 2026-06-05 21:03:40 +00:00
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 [ ]
This commit is contained in:
80
lib/tests/debug.sh
Executable file
80
lib/tests/debug.sh
Executable file
@@ -0,0 +1,80 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# Tests lib/debug.nix
|
||||
# Run:
|
||||
# [nixpkgs]$ lib/tests/debug.sh
|
||||
# or:
|
||||
# [nixpkgs]$ nix-build lib/tests/release.nix
|
||||
|
||||
set -euo pipefail
|
||||
shopt -s inherit_errexit
|
||||
|
||||
# Use
|
||||
# || die
|
||||
die() {
|
||||
echo >&2 "test case failed: " "$@"
|
||||
exit 1
|
||||
}
|
||||
|
||||
if test -n "${TEST_LIB:-}"; then
|
||||
NIX_PATH=nixpkgs="$(dirname "$TEST_LIB")"
|
||||
else
|
||||
NIX_PATH=nixpkgs="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.."; pwd)"
|
||||
fi
|
||||
export NIX_PATH
|
||||
|
||||
work="$(mktemp -d)"
|
||||
clean_up() {
|
||||
rm -rf "$work"
|
||||
}
|
||||
trap clean_up EXIT
|
||||
cd "$work"
|
||||
|
||||
expectSuccess() {
|
||||
local expr=$1
|
||||
local expectedResultRegex=$2
|
||||
if ! result=$(nix-instantiate --eval --strict --json \
|
||||
--expr "with (import <nixpkgs/lib>).debug; $expr" 2>/dev/null); then
|
||||
die "$expr failed to evaluate, but it was expected to succeed"
|
||||
fi
|
||||
if [[ ! "$result" =~ $expectedResultRegex ]]; then
|
||||
die "$expr == $result, but $expectedResultRegex was expected"
|
||||
fi
|
||||
}
|
||||
|
||||
expectFailure() {
|
||||
local expr=$1
|
||||
local expectedErrorRegex=$2
|
||||
if result=$(nix-instantiate --eval --strict --json 2>"$work/stderr" \
|
||||
--expr "with (import <nixpkgs/lib>).debug; $expr"); then
|
||||
die "$expr evaluated successfully to $result, but it was expected to fail"
|
||||
fi
|
||||
if [[ ! "$(<"$work/stderr")" =~ $expectedErrorRegex ]]; then
|
||||
die "Error was $(<"$work/stderr"), but $expectedErrorRegex was expected"
|
||||
fi
|
||||
}
|
||||
|
||||
# Test throwTestFailures with empty failures list
|
||||
expectSuccess 'throwTestFailures { failures = [ ]; }' "null"
|
||||
|
||||
# Test throwTestFailures with actual failures
|
||||
# This should throw with a specific error message format
|
||||
expectFailure 'throwTestFailures {
|
||||
failures = [
|
||||
{
|
||||
name = "testDerivation";
|
||||
expected = builtins.derivation {
|
||||
name = "a";
|
||||
builder = "bash";
|
||||
system = "x86_64-linux";
|
||||
};
|
||||
result = builtins.derivation {
|
||||
name = "b";
|
||||
builder = "bash";
|
||||
system = "x86_64-linux";
|
||||
};
|
||||
}
|
||||
];
|
||||
}' "1 tests failed"
|
||||
|
||||
echo >&2 tests ok
|
||||
@@ -6,7 +6,8 @@
|
||||
which is `throw`'s and `abort`'s, without error messages.
|
||||
|
||||
If you need to test error messages or more complex evaluations, see
|
||||
`lib/tests/modules.sh`, `lib/tests/sources.sh` or `lib/tests/filesystem.sh` as examples.
|
||||
`lib/tests/modules.sh`, `lib/tests/sources.sh`, `lib/tests/filesystem.sh` or
|
||||
`lib/tests/debug.sh` as examples.
|
||||
|
||||
To run these tests:
|
||||
|
||||
@@ -4813,32 +4814,4 @@ runTests {
|
||||
targetTarget = "prefix-tt";
|
||||
};
|
||||
};
|
||||
|
||||
testThrowTestFailuresEmpty = {
|
||||
expr = lib.debug.throwTestFailures {
|
||||
failures = [ ];
|
||||
};
|
||||
|
||||
expected = null;
|
||||
};
|
||||
|
||||
testThrowTestFailures = testingThrow (
|
||||
lib.debug.throwTestFailures {
|
||||
failures = [
|
||||
{
|
||||
name = "testDerivation";
|
||||
expected = builtins.derivation {
|
||||
name = "a";
|
||||
builder = "bash";
|
||||
system = "x86_64-linux";
|
||||
};
|
||||
result = builtins.derivation {
|
||||
name = "b";
|
||||
builder = "bash";
|
||||
system = "x86_64-linux";
|
||||
};
|
||||
}
|
||||
];
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@@ -60,6 +60,9 @@ pkgs.runCommand "nixpkgs-lib-tests-nix-${nix.version}"
|
||||
echo "Running lib/tests/sources.sh"
|
||||
TEST_LIB=$PWD/lib bash lib/tests/sources.sh
|
||||
|
||||
echo "Running lib/tests/debug.sh"
|
||||
TEST_LIB=$PWD/lib bash lib/tests/debug.sh
|
||||
|
||||
echo "Running lib/tests/network.sh"
|
||||
TEST_LIB=$PWD/lib bash lib/tests/network.sh
|
||||
|
||||
|
||||
Reference in New Issue
Block a user