|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Characterization tests for nix-env, nix-build, and nix-instantiate. |
| 4 | +# Captures error messages, output formats, and edge-case behavior. |
| 5 | +# |
| 6 | +# Expected output files in: cli-characterisation/ |
| 7 | +# Regenerate with: _NIX_TEST_ACCEPT=1 meson test -C build cli-characterisation |
| 8 | + |
| 9 | +source common.sh |
| 10 | + |
| 11 | +source characterisation/framework.sh |
| 12 | + |
| 13 | +set +x |
| 14 | + |
| 15 | +badDiff=0 |
| 16 | +badExitCode=0 |
| 17 | + |
| 18 | +# Normalize store paths, hashes, source paths, and system |
| 19 | +normalize() { |
| 20 | + sed -i \ |
| 21 | + -e "s|${NIX_STORE_DIR:-/nix/store}/[a-z0-9]\{32\}|/test-root/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx|g" \ |
| 22 | + -e "s|$TEST_ROOT|/test-root|g" \ |
| 23 | + -e "s|$(pwd)|/pwd|g" \ |
| 24 | + -e "s|$system|SYSTEM|g" \ |
| 25 | + -e "s|/nix/store/[a-z0-9]\{32\}|/nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx|g" \ |
| 26 | + -e "s|/test-root/store/[a-z0-9]\{32\}|/test-root/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx|g" \ |
| 27 | + -e "s|'[a-z0-9]\{32\}-|'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-|g" \ |
| 28 | + "$@" |
| 29 | +} |
| 30 | + |
| 31 | +diffAndAccept() { |
| 32 | + local testName="$1" |
| 33 | + local got="cli-characterisation/$testName.$2" |
| 34 | + local expected="cli-characterisation/$testName.$3" |
| 35 | + diffAndAcceptInner "$testName" "$got" "$expected" |
| 36 | +} |
| 37 | + |
| 38 | +clearProfiles |
| 39 | + |
| 40 | +# Each test case has a descriptor: cli-characterisation/<name>.cmd |
| 41 | +# containing: <expected-exit-code> <command...> |
| 42 | +# |
| 43 | +# stdout -> <name>.out, stderr -> <name>.err |
| 44 | +# Both are compared against <name>.out.exp / <name>.err.exp |
| 45 | + |
| 46 | +for cmdFile in cli-characterisation/*.cmd; do |
| 47 | + testName=$(basename "$cmdFile" .cmd) |
| 48 | + echo "testing $testName" |
| 49 | + |
| 50 | + # read returns 1 on EOF without trailing newline; handle it |
| 51 | + read -r expectedExit cmd < "$cmdFile" || [[ -n "$expectedExit" ]] |
| 52 | + |
| 53 | + if |
| 54 | + # shellcheck disable=SC2086 # word splitting of cmd is intended |
| 55 | + expect "$expectedExit" $cmd \ |
| 56 | + 1> "cli-characterisation/$testName.out" \ |
| 57 | + 2> "cli-characterisation/$testName.err" |
| 58 | + then |
| 59 | + normalize "cli-characterisation/$testName.out" "cli-characterisation/$testName.err" |
| 60 | + diffAndAccept "$testName" out out.exp |
| 61 | + diffAndAccept "$testName" err err.exp |
| 62 | + else |
| 63 | + echo "FAIL: $testName exited with wrong code" |
| 64 | + badExitCode=1 |
| 65 | + fi |
| 66 | +done |
| 67 | + |
| 68 | +# --- Multi-step tests that need a real .drv path in the store --- |
| 69 | + |
| 70 | +drvPath=$(nix-instantiate ./cli-characterisation/simple.nix) |
| 71 | + |
| 72 | +# Constructor: "building more than one derivation output is not supported" |
| 73 | +testName=nix-build-multi-output |
| 74 | +echo "testing $testName" |
| 75 | +if |
| 76 | + expect 1 nix-build "$drvPath!out,bin" \ |
| 77 | + 1> "cli-characterisation/$testName.out" \ |
| 78 | + 2> "cli-characterisation/$testName.err" |
| 79 | +then |
| 80 | + normalize "cli-characterisation/$testName.out" "cli-characterisation/$testName.err" |
| 81 | + diffAndAccept "$testName" out out.exp |
| 82 | + diffAndAccept "$testName" err err.exp |
| 83 | +else |
| 84 | + echo "FAIL: $testName exited with wrong code" |
| 85 | + badExitCode=1 |
| 86 | +fi |
| 87 | + |
| 88 | +# Constructor: "derivation does not have output" |
| 89 | +testName=nix-build-bad-output |
| 90 | +echo "testing $testName" |
| 91 | +if |
| 92 | + expect 1 nix-build "$drvPath!nonexistent" \ |
| 93 | + 1> "cli-characterisation/$testName.out" \ |
| 94 | + 2> "cli-characterisation/$testName.err" |
| 95 | +then |
| 96 | + normalize "cli-characterisation/$testName.out" "cli-characterisation/$testName.err" |
| 97 | + diffAndAccept "$testName" out out.exp |
| 98 | + diffAndAccept "$testName" err err.exp |
| 99 | +else |
| 100 | + echo "FAIL: $testName exited with wrong code" |
| 101 | + badExitCode=1 |
| 102 | +fi |
| 103 | + |
| 104 | +characterisationTestExit |
0 commit comments