|
| 1 | +#!/usr/bin/env bats |
| 2 | +# `make bats-unit-tests` must run every bats file even when an early one fails, |
| 3 | +# and still exit non-zero. Each file is its own make target, and without |
| 4 | +# --keep-going make stops scheduling targets at the first failure, so the files |
| 5 | +# sorting after it never run and the output reads like a complete suite. |
| 6 | +# |
| 7 | +# The tests drive the real root Makefile against a miniature tree: two bats |
| 8 | +# files and a stub hack/cozytest.sh that reports which file it was given. |
| 9 | +# |
| 10 | +# Requires: make. |
| 11 | + |
| 12 | +REPO_ROOT="$(cd "$(dirname "${BATS_TEST_FILENAME:-$0}")/.." && pwd)" |
| 13 | + |
| 14 | +# kg_run <stub body line> [makefiles]: builds the fixture, runs the target with |
| 15 | +# MAKEFILES set to the second argument, and leaves the output in $fixture/out |
| 16 | +# and the exit status in $rc. |
| 17 | +kg_run() { |
| 18 | + fixture="$(mktemp -d)" |
| 19 | + mkdir "$fixture/hack" |
| 20 | + : >"$fixture/hack/common-envs.mk" |
| 21 | + : >"$fixture/hack/a.bats" |
| 22 | + : >"$fixture/hack/b.bats" |
| 23 | + printf '%s\n' '#!/bin/sh' 'echo "stub ran $1"' "$1" >"$fixture/hack/cozytest.sh" |
| 24 | + chmod +x "$fixture/hack/cozytest.sh" |
| 25 | + |
| 26 | + # Under `make unit-tests -k` this suite inherits MAKEFLAGS carrying the |
| 27 | + # parent's --keep-going, which would let the old behaviour pass here. |
| 28 | + rc=0 |
| 29 | + env -u MAKEFLAGS -u MFLAGS -u MAKELEVEL MAKEFILES="${2:-}" \ |
| 30 | + make --file="$REPO_ROOT/Makefile" --directory="$fixture" bats-unit-tests >"$fixture/out" 2>&1 || rc=$? |
| 31 | + cat "$fixture/out" |
| 32 | +} |
| 33 | + |
| 34 | +@test "bats-unit-tests runs every file after a failure and still fails" { |
| 35 | + # Fail whichever file runs first, whatever order make picks, so the exit |
| 36 | + # status cannot come from the last file alone. |
| 37 | + kg_run 'mkdir "$(dirname "$0")/.failed" 2>/dev/null && exit 1; exit 0' |
| 38 | + ran_a=$(grep -c '^stub ran hack/a.bats$' "$fixture/out" || true) |
| 39 | + ran_b=$(grep -c '^stub ran hack/b.bats$' "$fixture/out" || true) |
| 40 | + rm -rf "$fixture" |
| 41 | + |
| 42 | + [ "$ran_a" = 1 ] |
| 43 | + [ "$ran_b" = 1 ] |
| 44 | + [ "$rc" -ne 0 ] |
| 45 | +} |
| 46 | + |
| 47 | +@test "bats-unit-tests still finds the root Makefile when MAKEFILES reads another first" { |
| 48 | + extra="$(mktemp)" |
| 49 | + printf '%s\n' 'unrelated:' ' @true' >"$extra" |
| 50 | + kg_run 'exit 0' "$extra" |
| 51 | + ran_b=$(grep -c '^stub ran hack/b.bats$' "$fixture/out" || true) |
| 52 | + rm -rf "$fixture" "$extra" |
| 53 | + [ "$ran_b" = 1 ] |
| 54 | + [ "$rc" -eq 0 ] |
| 55 | +} |
| 56 | + |
| 57 | +@test "bats-unit-tests exits zero when every file passes" { |
| 58 | + kg_run 'exit 0' |
| 59 | + rm -rf "$fixture" |
| 60 | + [ "$rc" -eq 0 ] |
| 61 | +} |
0 commit comments