返回 CodeWhale
check-behaviour-line.test.sh
根目录 / scripts / check-behaviour-line.test.sh
1 #!/usr/bin/env bash
2 set -euo pipefail
3
4 repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
5 checker="${repo_root}/scripts/check-behaviour-line.py"
6 python_bin="${PYTHON:-python3}"
7 fixture="$(mktemp -d)"
8 trap 'rm -rf "${fixture}"' EXIT
9
10 git -C "${fixture}" init -q
11 git -C "${fixture}" config user.name "Behaviour Test"
12 git -C "${fixture}" config user.email "behaviour-test@example.com"
13
14 printf 'seed\n' >"${fixture}/seed.txt"
15 git -C "${fixture}" add seed.txt
16 git -C "${fixture}" commit -qm "chore: seed"
17 base="$(git -C "${fixture}" rev-parse HEAD)"
18
19 # A fix without a statement fails.
20 printf 'a\n' >"${fixture}/a.txt"
21 git -C "${fixture}" add a.txt
22 git -C "${fixture}" commit -qm "fix(engine): drop a default"
23
24 if (cd "${fixture}" && "${python_bin}" "${checker}" "${base}" HEAD >missing.out 2>&1); then
25 echo "missing behaviour statement unexpectedly passed" >&2
26 exit 1
27 fi
28 grep -Fq '1 of 1 behaviour-changing commit(s)' "${fixture}/missing.out"
29 grep -Fq 'fix(engine): drop a default' "${fixture}/missing.out"
30
31 # --warn-only reports the same finding and exits 0.
32 (cd "${fixture}" && "${python_bin}" "${checker}" --warn-only "${base}" HEAD >warn.out 2>&1)
33 grep -Fq '1 of 1 behaviour-changing commit(s)' "${fixture}/warn.out"
34
35 # A Behaviour: line passes.
36 printf 'b\n' >"${fixture}/b.txt"
37 git -C "${fixture}" add b.txt
38 git -C "${fixture}" commit -qm "fix(engine): restore the default" \
39 -m "Behaviour: retry budget 2 -> 4 attempts"
40
41 # A No-behaviour-change: line passes.
42 printf 'c\n' >"${fixture}/c.txt"
43 git -C "${fixture}" add c.txt
44 git -C "${fixture}" commit -qm "feat(engine): reorganise the queue" \
45 -m "No-behaviour-change: no default, limit or rendered block changed"
46
47 # A docs commit needs neither.
48 printf 'd\n' >"${fixture}/d.txt"
49 git -C "${fixture}" add d.txt
50 git -C "${fixture}" commit -qm "docs: explain the queue"
51
52 # The range with one missing commit still fails...
53 if (cd "${fixture}" && "${python_bin}" "${checker}" "${base}" HEAD >all.out 2>&1); then
54 echo "range with a missing statement unexpectedly passed" >&2
55 exit 1
56 fi
57 grep -Fq '1 of 3 behaviour-changing commit(s)' "${fixture}/all.out"
58
59 # ...and the clean range passes.
60 base2="$(git -C "${fixture}" rev-parse HEAD~3)"
61 (cd "${fixture}" && "${python_bin}" "${checker}" "${base2}" HEAD >clean.out 2>&1)
62 grep -Fq '2 behaviour-changing commit(s) carry a statement' "${fixture}/clean.out"
63
64 guards_base="$(git -C "${fixture}" rev-parse HEAD)"
65 mkdir -p "${fixture}/crates/engine/tests"
66 printf 'g\n' >"${fixture}/crates/engine/tests/behavioural_guards.rs"
67 git -C "${fixture}" add crates
68 git -C "${fixture}" commit -qm "test: add a guard" -m "No-behaviour-change: guard only"
69
70 (cd "${fixture}" && "${python_bin}" "${checker}" "${guards_base}" HEAD >plain.out 2>&1)
71
72 if (cd "${fixture}" && "${python_bin}" "${checker}" --strict-guards "${guards_base}" HEAD >strict.out 2>&1); then
73 echo "unstated guard change unexpectedly passed --strict-guards" >&2
74 exit 1
75 fi
76 grep -Fq 'behavioural_guards.rs' "${fixture}/strict.out"
77
78 if (cd "${fixture}" && "${python_bin}" "${checker}" --guards-only "${guards_base}" HEAD >only.out 2>&1); then
79 echo "unstated guard change unexpectedly passed --guards-only" >&2
80 exit 1
81 fi
82
83 printf 'h\n' >>"${fixture}/crates/engine/tests/behavioural_guards.rs"
84 git -C "${fixture}" add crates
85 git -C "${fixture}" commit -qm "test: tighten a guard" -m "Behaviour: guard now rejects empty output"
86 stated_base="$(git -C "${fixture}" rev-parse HEAD~0)"
87 printf 'i\n' >"${fixture}/i.txt"
88 git -C "${fixture}" add i.txt
89 git -C "${fixture}" commit -qm "fix(engine): unrelated" -m "No-behaviour-change: none"
90 (cd "${fixture}" && "${python_bin}" "${checker}" --strict-guards "${stated_base}" HEAD >ok.out 2>&1)
91 (cd "${fixture}" && "${python_bin}" "${checker}" --guards-only "${stated_base}" HEAD >ok2.out 2>&1)
92
93 echo "check-behaviour-line tests passed"
94
94 lines BASH