From ec5fee37c589f914ec6b11a9cb2a695913da0fa5 Mon Sep 17 00:00:00 2001 From: monkopedia-coder Date: Thu, 3 Sep 2026 00:13:39 -0400 Subject: [PATCH] Share one anchoring helper across the grep gates (#597) Three gates shipped the same defect: a post-filter meant for ONE field of `grep`'s `path:line:content` output, applied unanchored to the whole line, so real violations dropped out of the results while the gate reported clean (#577/#588, #590/#593, #594/#598). `scripts/lib/gate-filters.sh` now holds the two anchors as named functions with the reasoning written down once: skip_comment_continuations content field, `^[^:]*:[0-9]+:[[:space:]]*\*` exempt_path path field, `^([^:]*/)?:[0-9]+:` They stay two functions rather than one parameterised regex on purpose. The three instances were not three hand-rolls of one pattern -- they were two different anchor shapes -- and the bug each time was a category error, applying a field-specific filter to the whole line. Naming the field is the remedy for that; de-duplication is not. The load-bearing part of this change is a comment, not the code. The helper's header says, first thing, that every `scripts/check-*.sh` MUST ship a matching `scripts/tests/-test.sh` wired into CI, and says why the helper is the lesser mechanism: nothing forces the next author to call it, whereas the self-test pattern caught two of the three defects and, on #598's harness alone, rejected four distinct wrong fixes including the regex that issue had itself recommended. The same pointer sits above the `source` line in each converted gate, since a fourth gate will most likely start life as a copy of a third. Converted: check-no-production-runblocking.sh and check-no-manual-json.sh. check-no-sensitive-logging.sh is deliberately NOT converted here -- #692 / PR #695 is live in that file, and a refactor should not race a fix. It is tracked as a follow-up. Not converted, and noted as such in the helper: check-zombie-screens.sh and check-apk-distribution.sh have no post-filter of this shape. Nor is the `// allow-manual-json` marker filter, which is genuinely safe unanchored because a path cannot contain `//`; its self-test already guards that, so a later "anchor everything consistently" sweep cannot break it quietly. Two small hardening choices in `exempt_path`, both tested. The basename is validated against `[A-Za-z0-9_][A-Za-z0-9_.-]*` and its dots escaped: that check is what makes escaping only `.` provably sufficient, and a basename carrying an unescaped ERE metacharacter would silently WIDEN an exemption, which is the class of bug this file exists to prevent. On misuse it prints the diagnostic to stdout as well as stderr and passes its input through unfiltered, so the calling gate goes RED carrying the message instead of quietly exempting the wrong set of files. Callers moved from a trailing `|| true` to the `set +e` bracket the detection greps already use. Behaviourally identical -- both discard a status that is 1 in the good case -- but a function in an `||` condition turns `set -e` off inside it (SC2310), and the shell-lint gate enables that check. Detection patterns are untouched. Proof that nothing else moved: * Byte-identical gate output, per converted gate, on the real tree and on a seeded corpus. The clean tree only proves the trivial case, so an identical probe set was planted in a worktree at `ac1b0284` and in this branch -- KDoc continuations, violations containing `: *` in a string and in a trailing comment, files merely NAMING the exempt file, and `MyAuditDetailScreen.kt` -- and each gate's stdout+stderr+exit status captured on both sides. `md5sum` agrees on both gates: the runBlocking gate reports the same 4 findings, the manual-json gate the same 5, and both exempt the same lines. * Every pre-existing `scripts/tests/*-test.sh` log is byte-identical before and after, `diff`-ed file by file. Suite: 91 -> 111 `ok:` lines, the delta being this change's own 20. * `check-shell-lint-test.sh` is red before and after, identically (same 19 failures, same 7 `ok:`), because actionlint is not installed locally. Its phase 1 was run by hand over all 22 tracked `*.sh`: clean. `scripts/tests/gate-filters-test.sh` asserts BOTH directions for both functions -- the line that must be dropped and the line that must survive -- because a filter that drops everything and one that drops nothing are both green if only one side is tested. Verified by mutation rather than asserted: six deliberate breakages of the helper were each required to go red, and did -- the unanchored content filter (2 kept-cases fail), the unanchored path filter (4), the `^[^:]*Foo\.kt:` near-miss that #598 rejected (1), dropping the dot escaping (1), dropping the basename guard (3), and both filters replaced by `cat` (5). Closes #597 Co-authored-by: Claude Opus 5 --- .github/workflows/ci.yml | 7 + scripts/check-no-manual-json.sh | 37 +++- scripts/check-no-production-runblocking.sh | 26 ++- scripts/lib/gate-filters.sh | 105 ++++++++++ scripts/tests/check-no-manual-json-test.sh | 7 +- .../check-no-production-runblocking-test.sh | 5 +- scripts/tests/gate-filters-test.sh | 198 ++++++++++++++++++ 7 files changed, 367 insertions(+), 18 deletions(-) create mode 100644 scripts/lib/gate-filters.sh create mode 100644 scripts/tests/gate-filters-test.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a0862896..94471bca 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -113,6 +113,13 @@ jobs: run: bash scripts/tests/check-no-sensitive-logging-test.sh - name: "runBlocking gate self-test (issue #590)" run: bash scripts/tests/check-no-production-runblocking-test.sh + # The anchors those two post-filters share now live in one library (#597), + # which makes a mistake in them three gates' worth of expensive. Asserts + # both directions for both filters -- the line that must be dropped AND + # the line that must survive -- since a filter that drops everything and + # one that drops nothing are both green if only one side is tested. + - name: "Gate post-filter library tests (issue #597)" + run: bash scripts/tests/gate-filters-test.sh # A linter wrapper is the easiest gate of all to make vacuous: hand # `shellcheck` an empty file list and it reads stdin, prints nothing and # exits 0 -- identical output to a clean tree. actionlint is worse, and diff --git a/scripts/check-no-manual-json.sh b/scripts/check-no-manual-json.sh index 08df04af..cf5ad1d5 100755 --- a/scripts/check-no-manual-json.sh +++ b/scripts/check-no-manual-json.sh @@ -15,21 +15,36 @@ set -euo pipefail # # Usage: bash scripts/check-no-manual-json.sh -# git grep -n emits `path:line:content`. The allowlist below is a PATH filter, -# so it is anchored to the path field: `^([^:]*/)?` cannot cross the first colon, -# and the trailing `:[0-9]+:` pins that colon to the path/line separator. Without -# that anchoring it also matched the content field, so any production line that -# merely mentioned the exempt file in a comment or string silently dropped out of -# the results (#594 — third instance of #577/#590). Requiring the basename to be -# exactly AuditDetailScreen.kt also keeps longer names that contain it -# (AuditDetailScreenTest.kt, MyAuditDetailScreen.kt) inside the gate. +# ADDING A NEW GATE? It MUST ship a matching scripts/tests/-test.sh, wired +# into CI. The harness -- not the shared filter below -- is what catches an +# unanchored post-filter; see the header of scripts/lib/gate-filters.sh. +# +# Resolved from this script's own location rather than from the working +# directory, so the gate can be invoked by path from anywhere -- including from +# the throwaway repo its self-test builds. +gate_lib_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +# shellcheck source=scripts/lib/gate-filters.sh +source "$gate_lib_dir/lib/gate-filters.sh" + +# git grep -n emits `path:line:content`. The AuditDetailScreen.kt allowlist is a +# PATH filter, so it goes through `exempt_path`, which anchors it to the path +# field and requires the basename to match exactly. scripts/lib/gate-filters.sh +# documents both halves and what the unanchored form let through (#594 -- third +# instance of #577/#590). # # The `allow-manual-json` filter above it is a genuine content-field marker and -# needs no anchoring: a path cannot contain `//`. +# needs no anchoring — a path cannot contain `//` — so it deliberately stays a +# plain `grep -v`. The self-test asserts it still works. +# +# `set +e` rather than a trailing `|| true`: both discard the status, and every +# stage here exits 1 when it simply has nothing left to emit (the clean case), +# but the `||` form puts a function in a condition and turns set -e off inside it +# (SC2310). +set +e hits=$(git grep -nE '"""\{' -- '*/src/main/*.kt' \ | grep -v -E '//\s*allow-manual-json' \ - | grep -v -E '^([^:]*/)?AuditDetailScreen\.kt:[0-9]+:' \ - || true) + | exempt_path AuditDetailScreen.kt) +set -e if [ -n "$hits" ]; then cat <&2 diff --git a/scripts/check-no-production-runblocking.sh b/scripts/check-no-production-runblocking.sh index 2578a1ed..01cf4fba 100755 --- a/scripts/check-no-production-runblocking.sh +++ b/scripts/check-no-production-runblocking.sh @@ -17,6 +17,17 @@ set -euo pipefail +# ADDING A NEW GATE? It MUST ship a matching scripts/tests/-test.sh, wired +# into CI. The harness -- not the shared filter below -- is what catches an +# unanchored post-filter; see the header of scripts/lib/gate-filters.sh. +# +# Resolved from this script's own location rather than from the working +# directory, so the gate can be invoked by path from anywhere -- including from +# the throwaway repo its self-test builds. +gate_lib_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +# shellcheck source=scripts/lib/gate-filters.sh +source "$gate_lib_dir/lib/gate-filters.sh" + # Assigned, then cd'd, rather than `cd "$(git rev-parse ...)"`. Measured, so the # reason is the real one and not the obvious guess: the masked form does NOT # silently pass. `cd ""` is an error in bash, not a no-op, so a failing @@ -51,11 +62,16 @@ if [ "$status" -gt 1 ]; then fi # Drop block-comment continuation lines (` * ...`) so prose about runBlocking does -# not fail the build. Anchored to the start of grep's `file:line:content` output -# and applied only to the content field: an unanchored `grep -v ':[[:space:]]*\*'` -# also swallowed real violations whose content happened to contain `: *` anywhere -# (#590, the same defect as #577 in the sibling sensitive-logging gate). -matches=$(printf '%s\n' "$raw" | grep -vE '^[^:]*:[0-9]+:[[:space:]]*\*' || true) +# not fail the build. The anchoring lives in scripts/lib/gate-filters.sh, which +# documents why it is a CONTENT-field filter and what the unanchored form +# swallowed (#590, the same defect as #577 in the sibling sensitive-logging +# gate). `grep -v` exits 1 when it emits nothing, which for this gate is the good +# case, so the status is discarded -- via the same `set +e` bracket the detection +# grep above uses, rather than a trailing `|| true`, which would put a function in +# an `||` condition and turn set -e off inside it (SC2310). +set +e +matches=$(printf '%s\n' "$raw" | skip_comment_continuations) +set -e if [ -n "$matches" ]; then echo "Found production runBlocking (issue #136 regression):" diff --git a/scripts/lib/gate-filters.sh b/scripts/lib/gate-filters.sh new file mode 100644 index 00000000..b40ee807 --- /dev/null +++ b/scripts/lib/gate-filters.sh @@ -0,0 +1,105 @@ +#!/usr/bin/env bash +# Shared post-filters for this repo's grep-based CI gates (#597). +# +# Sourced, not executed: +# +# source "$(git rev-parse --show-toplevel)/scripts/lib/gate-filters.sh" +# hits=$(git grep -nE "$pattern" -- "${paths[@]}" \ +# | skip_comment_continuations \ +# | exempt_path AuditDetailScreen.kt \ +# || true) +# +# ============================================================================= +# READ THIS FIRST IF YOU ARE ADDING A GATE +# ============================================================================= +# Every `scripts/check-*.sh` MUST ship a matching `scripts/tests/-test.sh` +# and MUST have that self-test wired into CI. That requirement -- not this file -- +# is the part that catches the bug. +# +# Be clear about what this library is and is not. It removes ONE way to get a +# post-filter wrong: re-deriving the anchor by hand. It cannot help a gate that +# does not call it, and nothing forces the next author to call it. What actually +# caught two of the three defects below was the self-test pattern: a throwaway +# git repo, the REAL gate run end-to-end, and red cases asserted red -- so the +# harness catches a hand-rolled filter whether or not it used this file. On +# #598's harness alone, four distinct wrong fixes were rejected, including the +# regex the issue itself had recommended; a shared helper would have caught none +# of them on its own merits. +# +# So: a new gate without a self-test is not finished, however it filters. +# +# ============================================================================= +# WHY THE ANCHORS LOOK LIKE THIS +# ============================================================================= +# `grep -n` / `git grep -n` emit `path:line:content`. A post-filter is always +# about exactly ONE of those three fields, and the recurring defect is applying +# a field-specific filter to the whole line: the pattern then also matches in a +# field it was never meant to see, and REAL violations drop silently out of the +# results while the gate reports clean. +# +# Three separate gates shipped that same defect -- and they were not three +# hand-rolls of one regex, they were two DIFFERENT anchor shapes, which is why +# each is a named function here rather than one shared pattern. Naming the field +# is the point: the bug is a category error, not a typo. +# +# #577 / #588 check-no-sensitive-logging.sh content field +# #590 / #593 check-no-production-runblocking.sh content field +# #594 / #598 check-no-manual-json.sh path field +# +# Not every post-filter needs anchoring, and adding it where it is not needed is +# its own hazard. `check-no-manual-json.sh` also filters on the marker +# `// allow-manual-json`, which is safe unanchored because a path cannot contain +# `//`; its self-test asserts that, so a later "consistently anchor everything" +# sweep cannot quietly break it. Only 3 of this repo's `check-*.sh` gates have a +# filter of this shape at all -- `check-zombie-screens.sh` and +# `check-apk-distribution.sh` have none and deliberately do not source this file. +# +# Both functions read stdin and write stdout, so they compose as pipeline +# stages. Neither swallows `grep`'s exit status: `grep -v` exits 1 when it emits +# nothing, which for a gate is the GOOD case, so the caller keeps the `|| true` +# on the pipeline as a whole exactly as before. + +# Drop lines whose CONTENT field is a block-comment continuation (` * ...`), so +# prose about the banned construct -- a KDoc paragraph, a pattern list written +# out in a comment -- does not fail the build. +# +# `^[^:]*:[0-9]+:` consumes the path and line fields, and `[^:]*` cannot cross +# the first colon, so `[[:space:]]*\*` is matched only at the START of the +# content. The unanchored form (`grep -v ':[[:space:]]*\*'`) also matched `: *` +# ANYWHERE -- inside a string literal, inside a trailing comment -- and threw +# away genuine violations that happened to contain it (#577, #590). +skip_comment_continuations() { + grep -vE '^[^:]*:[0-9]+:[[:space:]]*\*' +} + +# Drop lines whose PATH field is exactly , in any directory. For a +# whole-file allowlist entry ("this one file is exempt from this gate"). +# +# `^([^:]*/)?` is an optional directory prefix that cannot cross the first colon, +# and the trailing `:[0-9]+:` pins that colon to the path/line separator, so the +# match is confined to the path field. Unanchored it also matched the content +# field, and then ANY file could evade the gate just by naming the exempt file in +# a comment or a string (#594). +# +# Requiring the basename to be EXACTLY matters as much as the field +# anchoring. `^[^:]*Foo\.kt:[0-9]+:` looks anchored and is not: `[^:]*` happily +# absorbs `some/dir/My`, so `MyFoo.kt` and `dir/NotFoo.kt` become exempt too. +# That precise regex was recommended in #594 and rejected by #598's harness. +# +# The argument is a bare basename, checked rather than trusted: the check is what +# makes escaping only `.` provably sufficient. A basename carrying an unescaped +# ERE metacharacter would silently widen the exemption, which is the class of bug +# this whole file exists to prevent. Misuse is reported on stdout as well as +# stderr and the input is passed through UNFILTERED, so the calling gate goes RED +# carrying the diagnostic instead of quietly exempting the wrong set of files. +exempt_path() { + local base=${1-} + if [[ ! "$base" =~ ^[A-Za-z0-9_][A-Za-z0-9_.-]*$ ]]; then + echo "gate-filters: exempt_path: '$base' is not a bare basename" >&2 + echo "gate-filters: exempt_path: '$base' is not a bare basename (gate not filtered)" + cat + return 0 + fi + local escaped=${base//./\\.} + grep -vE "^([^:]*/)?${escaped}:[0-9]+:" +} diff --git a/scripts/tests/check-no-manual-json-test.sh b/scripts/tests/check-no-manual-json-test.sh index 3b518eeb..960b453f 100755 --- a/scripts/tests/check-no-manual-json-test.sh +++ b/scripts/tests/check-no-manual-json-test.sh @@ -33,8 +33,13 @@ setup_sandbox() { git init -q . git config user.email t@example.com git config user.name test - mkdir -p scripts app/src/main + mkdir -p scripts/lib app/src/main + # The gate resolves scripts/lib/gate-filters.sh relative to its own location + # (#597), so the sandbox mirrors that layout. Copied, not symlinked, so the + # test still runs the REAL library rather than reaching back into the source + # tree by accident. cp "$repo_root/$gate" scripts/ + cp "$repo_root/scripts/lib/gate-filters.sh" scripts/lib/ echo 'package p' > app/src/main/Main.kt git add -A } diff --git a/scripts/tests/check-no-production-runblocking-test.sh b/scripts/tests/check-no-production-runblocking-test.sh index c2e1a3ef..b575c160 100755 --- a/scripts/tests/check-no-production-runblocking-test.sh +++ b/scripts/tests/check-no-production-runblocking-test.sh @@ -28,9 +28,12 @@ setup_sandbox() { git init -q . git config user.email t@example.com git config user.name test - mkdir -p scripts app/src/main app/src/debug integrations/src/main \ + mkdir -p scripts/lib app/src/main app/src/debug integrations/src/main \ core/mcp/src/jvmMain relay/src/tls cp "$repo_root/$gate" "$repo_root/scripts/production-source-dirs.sh" scripts/ + # The gate resolves scripts/lib/gate-filters.sh relative to its own location + # (#597), so the sandbox mirrors that layout. + cp "$repo_root/scripts/lib/gate-filters.sh" scripts/lib/ echo 'package p' > app/src/main/Main.kt echo 'package p' > app/src/debug/Debug.kt echo 'package p' > integrations/src/main/Main.kt diff --git a/scripts/tests/gate-filters-test.sh b/scripts/tests/gate-filters-test.sh new file mode 100644 index 00000000..6639696e --- /dev/null +++ b/scripts/tests/gate-filters-test.sh @@ -0,0 +1,198 @@ +#!/usr/bin/env bash +# Tests for scripts/lib/gate-filters.sh (#597). +# +# Three gates shipped the same defect -- a post-filter meant for ONE field of +# `grep`'s `path:line:content` output, applied unanchored to the whole line +# (#577, #590, #594). Those anchors now live in one shared library, which makes +# a mistake in them three times as expensive: every gate that sources it +# inherits it. So this file asserts both directions for both functions -- the +# line that MUST be filtered, and the line that must NOT be -- because a filter +# that drops everything and a filter that drops nothing are both green if you +# only test one side. +# +# This does not replace the per-gate harnesses. Those run the REAL gate over a +# throwaway repo and are what actually caught the three defects; a gate that +# hand-rolls its filter is caught there and is invisible here. + +set -euo pipefail + +repo_root=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd) +# shellcheck source=scripts/lib/gate-filters.sh +source "$repo_root/scripts/lib/gate-filters.sh" + +failures=0 + +# kept [args...] -- the line must SURVIVE the filter named by the +# remaining arguments. This is the direction that matters most: a line that is +# wrongly dropped here is a violation the gate reports as clean. +kept() { + local name=$1 line=$2 + shift 2 + local out + set +e + out=$(printf '%s\n' "$line" | "$@") + set -e + if [ "$out" != "$line" ]; then + echo "FAIL: $name -- line was dropped (or altered) but must be kept" + printf ' in: %s\n out: %s\n' "$line" "${out:-}" + failures=$((failures + 1)) + return + fi + echo "ok: $name (kept)" +} + +# dropped [args...] -- the line must be REMOVED by the filter. +dropped() { + local name=$1 line=$2 + shift 2 + local out + set +e + out=$(printf '%s\n' "$line" | "$@") + set -e + if [ -n "$out" ]; then + echo "FAIL: $name -- line survived the filter but must be dropped" + printf ' out: %s\n' "$out" + failures=$((failures + 1)) + return + fi + echo "ok: $name (dropped)" +} + +# --- skip_comment_continuations: a CONTENT-field filter ---------------------- + +dropped 'KDoc continuation is dropped' \ + 'app/src/main/A.kt:12: * Callers must not wrap this in runBlocking.' \ + skip_comment_continuations + +dropped 'continuation with no leading space is dropped' \ + 'app/src/main/A.kt:12:* prose' \ + skip_comment_continuations + +dropped 'continuation with deep indentation is dropped' \ + 'app/src/main/A.kt:3: * prose' \ + skip_comment_continuations + +# The #577/#590 direction. Unanchored, `: *` anywhere in the line matched, so +# these real violations were silently discarded and the gates reported clean. +kept 'violation with ": *" inside a string literal survives' \ + 'app/src/main/A.kt:12:fun go() = runBlocking { println("total: *done") }' \ + skip_comment_continuations + +kept 'violation with ": *" in a trailing comment survives' \ + 'app/src/main/A.kt:12:fun go() = runBlocking { work() } // note: *temporary*' \ + skip_comment_continuations + +kept 'ordinary violation survives' \ + 'app/src/main/A.kt:12:fun go() = runBlocking { work() }' \ + skip_comment_continuations + +kept 'a "*" later in the content does not count as a continuation' \ + 'app/src/main/A.kt:12:val n = a * b' \ + skip_comment_continuations + +# A path is never a continuation, whatever it is called. +kept 'a path with a leading star-ish name survives' \ + 'app/src/main/Star.kt:12:val n = 1' \ + skip_comment_continuations + +# --- exempt_path: a PATH-field filter ---------------------------------------- + +dropped 'exempt file in a directory is dropped' \ + 'app/src/main/java/com/x/AuditDetailScreen.kt:5:val body = """{}"""' \ + exempt_path AuditDetailScreen.kt + +dropped 'exempt file at the repo root is dropped' \ + 'AuditDetailScreen.kt:5:val body = """{}"""' \ + exempt_path AuditDetailScreen.kt + +# The #594 direction: the exemption must not reach the content field, or any +# file can leave the gate just by naming the exempt file. +kept 'another file that mentions the exempt name in a comment survives' \ + 'app/src/main/Probe.kt:5:val body = """{}""" // shape mirrors AuditDetailScreen.kt' \ + exempt_path AuditDetailScreen.kt + +kept 'another file that mentions the exempt name in a string survives' \ + 'app/src/main/Probe.kt:5:val body = """{"src": "AuditDetailScreen.kt"}"""' \ + exempt_path AuditDetailScreen.kt + +# The basename must match EXACTLY. `^[^:]*AuditDetailScreen\.kt:[0-9]+:` looks +# anchored to the path field and is not -- `[^:]*` absorbs `.../My` -- which is +# the regex #594 recommended and #598's harness rejected. +kept 'a longer name ending in the exempt name survives' \ + 'app/src/main/MyAuditDetailScreen.kt:5:val body = """{}"""' \ + exempt_path AuditDetailScreen.kt + +kept 'a longer name starting with the exempt name survives' \ + 'app/src/main/AuditDetailScreenTest.kt:5:val body = """{}"""' \ + exempt_path AuditDetailScreen.kt + +# The `.` in a basename is escaped, so it is a literal dot and not "any char". +kept 'a dot in the basename is not a wildcard' \ + 'app/src/main/AuditDetailScreenXkt:5:val body = """{}"""' \ + exempt_path AuditDetailScreen.kt + +# A line number is required between the path and the content, so a path-shaped +# prefix in the content field cannot be mistaken for the path field. +kept 'the exempt name followed by a non-numeric field survives' \ + 'app/src/main/Probe.kt:5:see AuditDetailScreen.kt:notaline: for the fixture' \ + exempt_path AuditDetailScreen.kt + +# --- exempt_path misuse ------------------------------------------------------ +# A basename carrying an ERE metacharacter would silently widen the exemption. +# The guard makes that a RED gate instead: input passes through unfiltered, and +# the diagnostic goes to stdout so the caller's `[ -n "$hits" ]` fires. + +misuse() { + local name=$1 line=$2 arg=$3 out + set +e + out=$(printf '%s\n' "$line" | exempt_path "$arg" 2>/dev/null) + set -e + if ! printf '%s\n' "$out" | grep -q 'not a bare basename'; then + echo "FAIL: $name -- no diagnostic on stdout, so the gate would stay green" + printf ' out: %s\n' "${out:-}" + failures=$((failures + 1)) + return + fi + if ! printf '%s\n' "$out" | grep -qF -- "$line"; then + echo "FAIL: $name -- input was filtered instead of passed through" + printf ' out: %s\n' "${out:-}" + failures=$((failures + 1)) + return + fi + echo "ok: $name (diagnostic on stdout, input passed through)" +} + +misuse 'a glob in the basename is refused' \ + 'app/src/main/AuditDetailScreen.kt:5:x' 'Audit*.kt' + +misuse 'a path rather than a basename is refused' \ + 'app/src/main/AuditDetailScreen.kt:5:x' 'app/src/main/AuditDetailScreen.kt' + +misuse 'an empty basename is refused' \ + 'app/src/main/AuditDetailScreen.kt:5:x' '' + +# --- composition ------------------------------------------------------------- +# The gates chain these as pipeline stages, so assert the chain, not just the +# stages: a violation must survive both filters and a doubly-exempt line must +# not. +set +e +composed=$(printf '%s\n' \ + 'app/src/main/A.kt:1: * prose mentioning AuditDetailScreen.kt' \ + 'app/src/main/AuditDetailScreen.kt:2:val body = """{}"""' \ + 'app/src/main/A.kt:3:val body = """{}""" // like AuditDetailScreen.kt: *see above*' \ + | skip_comment_continuations | exempt_path AuditDetailScreen.kt) +set -e + +if [ "$composed" = 'app/src/main/A.kt:3:val body = """{}""" // like AuditDetailScreen.kt: *see above*' ]; then + echo "ok: chained filters keep exactly the real violation" +else + echo "FAIL: chained filters -- unexpected result" + printf ' out: %s\n' "${composed:-}" + failures=$((failures + 1)) +fi + +if [ "$failures" -gt 0 ]; then + echo "$failures test(s) failed" + exit 1 +fi +echo "All scripts/lib/gate-filters.sh tests passed"