nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
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
9set -euo pipefail
10shopt -s inherit_errexit
11
12# Use
13# || die
14die() {
15 echo >&2 "test case failed: " "$@"
16 exit 1
17}
18
19if test -n "${TEST_LIB:-}"; then
20 NIX_PATH=nixpkgs="$(dirname "$TEST_LIB")"
21else
22 NIX_PATH=nixpkgs="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.."; pwd)"
23fi
24export NIX_PATH
25
26work="$(mktemp -d)"
27clean_up() {
28 rm -rf "$work"
29}
30trap clean_up EXIT
31cd "$work"
32
33expectSuccess() {
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
45expectFailure() {
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
58expectSuccess 'throwTestFailures { failures = [ ]; }' "null"
59
60# Test throwTestFailures with actual failures
61# This should throw with a specific error message format
62expectFailure '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
80echo >&2 tests ok