summaryrefslogtreecommitdiff
path: root/lib/tests/debug.sh
blob: c71791551ded4ca6a11ec947885999da05741f4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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