Skip to content

Commit d9f5de7

Browse files
author
Nathan Gillett
committed
Fix CRITICAL_PREFIXES guard syntax in coverage script
Signed-off-by: Nathan Gillett <nathan@intentproof.io>
1 parent 917afbd commit d9f5de7

2 files changed

Lines changed: 47 additions & 28 deletions

File tree

scripts/check-coverage.sh

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,16 @@ fi
4747

4848
if [[ -n "${CRITICAL_RULES:-}" ]]; then
4949
printf '%s\n' "${CRITICAL_RULES[@]}" >"$rules_file"
50-
elif [[ -n "${CRITICAL_MIN:-}" && ${#CRITICAL_PREFIXES[@]:-0} -gt 0 ]]; then
51-
while IFS= read -r prefix; do
52-
[[ -n "$prefix" ]] || continue
53-
printf '%s:%s\n' "$CRITICAL_MIN" "$prefix"
54-
done < <(printf '%s\n' "${CRITICAL_PREFIXES[@]}") >"$rules_file"
50+
elif [[ -n "${CRITICAL_MIN:-}" ]]; then
51+
if declare -p CRITICAL_PREFIXES &>/dev/null && [[ ${#CRITICAL_PREFIXES[@]} -gt 0 ]]; then
52+
while IFS= read -r prefix; do
53+
[[ -n "$prefix" ]] || continue
54+
printf '%s:%s\n' "$CRITICAL_MIN" "$prefix"
55+
done < <(printf '%s\n' "${CRITICAL_PREFIXES[@]}") >"$rules_file"
56+
else
57+
echo "coverage-tiers.conf must set CRITICAL_RULES or CRITICAL_MIN + CRITICAL_PREFIXES" >&2
58+
exit 2
59+
fi
5560
else
5661
echo "coverage-tiers.conf must set CRITICAL_RULES or CRITICAL_MIN + CRITICAL_PREFIXES" >&2
5762
exit 2

scripts/check-coverage_config_test.sh

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,50 @@
22
# Regression test: missing critical tier config yields a clear error.
33
set -euo pipefail
44

5-
root="$(mktemp -d)"
6-
trap 'rm -rf "$root"' EXIT
5+
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
76

8-
mkdir -p "$root/scripts"
9-
cp "$(dirname "$0")/check-coverage.sh" "$(dirname "$0")/check-coverage-aggregate.awk" \
10-
"$root/scripts/"
7+
run_config_case() {
8+
local conf_body="$1"
9+
local label="$2"
1110

12-
cat >"$root/scripts/coverage-tiers.conf" <<'EOF'
13-
TOTAL_MIN=90
11+
local root
12+
root="$(mktemp -d)"
13+
# shellcheck disable=SC2064
14+
trap "rm -rf '$root'" RETURN
15+
16+
mkdir -p "$root/scripts"
17+
cp "$script_dir/check-coverage.sh" "$script_dir/check-coverage-aggregate.awk" \
18+
"$root/scripts/"
19+
20+
cat >"$root/scripts/coverage-tiers.conf" <<EOF
21+
${conf_body}
1422
EOF
1523

16-
cat >"$root/coverage.out" <<'EOF'
24+
cat >"$root/coverage.out" <<'EOF'
1725
mode: atomic
1826
example.com/pkg/main.go:1.1,2.1 2 2
1927
EOF
2028

21-
output=""
22-
code=0
23-
output="$(bash "$root/scripts/check-coverage.sh" "$root/coverage.out" 2>&1)" || code=$?
24-
25-
if [[ "$code" -ne 2 ]]; then
26-
echo "FAIL: expected exit 2 for incomplete config, got ${code}" >&2
27-
echo "$output" >&2
28-
exit 1
29-
fi
30-
31-
if ! grep -q "must set CRITICAL_RULES or CRITICAL_MIN + CRITICAL_PREFIXES" <<<"$output"; then
32-
echo "FAIL: expected descriptive config error, got:" >&2
33-
echo "$output" >&2
34-
exit 1
35-
fi
29+
local output=""
30+
local code=0
31+
output="$(bash "$root/scripts/check-coverage.sh" "$root/coverage.out" 2>&1)" || code=$?
32+
33+
if [[ "$code" -ne 2 ]]; then
34+
echo "FAIL (${label}): expected exit 2, got ${code}" >&2
35+
echo "$output" >&2
36+
return 1
37+
fi
38+
39+
if ! grep -q "must set CRITICAL_RULES or CRITICAL_MIN + CRITICAL_PREFIXES" <<<"$output"; then
40+
echo "FAIL (${label}): expected descriptive config error, got:" >&2
41+
echo "$output" >&2
42+
return 1
43+
fi
44+
45+
return 0
46+
}
47+
48+
run_config_case "TOTAL_MIN=90" "no critical tier keys" || exit 1
49+
run_config_case $'TOTAL_MIN=90\nCRITICAL_MIN=95' "CRITICAL_MIN without CRITICAL_PREFIXES" || exit 1
3650

3751
echo "PASS: incomplete critical tier config reports clear error"

0 commit comments

Comments
 (0)