| 1 |
#!/usr/bin/env bash |
| 2 |
|
| 3 |
set -euo pipefail |
| 4 |
|
| 5 |
repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" |
| 6 |
script="$repo_root/scripts/check-thoth-critical.sh" |
| 7 |
tmpdir="$(mktemp -d)" |
| 8 |
trap 'rm -rf "$tmpdir"' EXIT |
| 9 |
|
| 10 |
fake_thoth="$tmpdir/thoth" |
| 11 |
cat > "$fake_thoth" <<'SH' |
| 12 |
#!/usr/bin/env bash |
| 13 |
set -euo pipefail |
| 14 |
case "${THOTH_FAKE_CASE:-ok}" in |
| 15 |
ok) |
| 16 |
cat <<'JSON' |
| 17 |
{"files":[{"path":"ok.go","risk":0.649,"sloc":100,"funcs":4,"worst":"ok"}]} |
| 18 |
JSON |
| 19 |
;; |
| 20 |
critical) |
| 21 |
cat <<'JSON' |
| 22 |
{"files":[{"path":"critical.go","risk":0.650,"sloc":101,"funcs":5,"worst":"critical"}]} |
| 23 |
JSON |
| 24 |
;; |
| 25 |
invalid) |
| 26 |
printf '{not json' |
| 27 |
;; |
| 28 |
esac |
| 29 |
SH |
| 30 |
chmod +x "$fake_thoth" |
| 31 |
|
| 32 |
output="$(THOTH_BIN="$fake_thoth" THOTH_FAKE_CASE=ok "$script" "$repo_root")" |
| 33 |
[[ "$output" == "thoth critical check passed: 0 files at or above risk 0.650" ]] |
| 34 |
|
| 35 |
if THOTH_BIN="$fake_thoth" THOTH_FAKE_CASE=critical "$script" "$repo_root" >"$tmpdir/out" 2>"$tmpdir/err"; then |
| 36 |
echo "expected critical fixture to fail" >&2 |
| 37 |
exit 1 |
| 38 |
fi |
| 39 |
grep -F "thoth critical check failed: 1 file(s) at or above risk 0.650" "$tmpdir/err" >/dev/null |
| 40 |
grep -F "critical.go: risk=0.650" "$tmpdir/err" >/dev/null |
| 41 |
|
| 42 |
if THOTH_BIN="$fake_thoth" THOTH_FAKE_CASE=invalid "$script" "$repo_root" >"$tmpdir/out" 2>"$tmpdir/err"; then |
| 43 |
echo "expected invalid JSON fixture to fail" >&2 |
| 44 |
exit 1 |
| 45 |
fi |
| 46 |
grep -F "error: thoth --json did not return valid JSON" "$tmpdir/err" >/dev/null |
| 47 |
|
| 48 |
echo "thoth critical check script tests passed" |