Files
cozystack/hack/code-changed_test.bats
T
Marian Koreniuk ceb2063f34 ci: widen the docs-only gate to Markdown, images and repository metadata
The PR pipeline skipped build, Talos and E2E only when every changed path
was under docs/. A pull request that touched README.md, GOVERNANCE.md, an
issue template or LICENSE still built every image, booted a sandbox and
ran the suite, for files nothing in that pipeline reads.

Move the classifier into hack/code-changed.sh, the one source of truth for
both lanes: the in-tree `plan` job feeds it the --no-renames merge diff and
the fork `resolve` job feeds it the base-repo PR file list (filename and
previous_filename, so a packages/… -> docs/… rename still reads as code).
Non-code is the docs/ tree, Markdown outside packages/, img/, LICENSE and
the GitHub and review-bot metadata.

Markdown under packages/ stays code: Go controller tests read chart
READMEs. TestDocs_StateTheTimingsTheBinaryAndTheChartUse checks
packages/system/flux-plunger/README.md and runs only under
make test-controllers, i.e. only when hack/code-changed.sh prints true, so
a README-only PR there would skip the test and turn the next code PR on
main red. The classifier uses awk rather than grep to express that
exception. Other chart READMEs are generated by `make generate`, which
pre-commit re-runs, and contract-tested by hack/*.bats, which the `checks`
job still runs for every path except the two CI-only tools.

The push triggers of build-main, build-release and CodeQL mirror the
classifier so a docs-only merge no longer rebuilds main. They use a
`paths` filter (`**`, one `!` glob per non-code path, then `packages/**.md`)
because only `paths` documents re-including after a negation.
hack/code-changed_test.bats checks that the three filters are equal, that
every `!` glob is non-code, that every re-include is code, and that
CodeQL's pull_request trigger carries no path filter at all: it is a
required check on main.

Over the 200 merged PRs returned by `gh pr list --state merged --limit
200` on 2026-11-18 the gate on main (docs/ plus the two CI-only tools)
skipped 8, the new one 10 (#5168 and #5188 are newly skipped).

Assisted-by: LLM
Signed-off-by: Marian Koreniuk <moriarti@cp.if.ua>
2026-12-02 10:53:49 +01:00

114 lines
4.8 KiB
Bash

#!/usr/bin/env bats
# Unit tests for hack/code-changed.sh, the PR docs-only gate, and for the
# paths-ignore lists that mirror it in the push-triggered workflows.
#
# Run from the repo root through `make bats-unit-tests`; the relative paths
# below resolve against that cwd. Negated assertions use `if cmd; then false`
# rather than `! cmd`; see hack/bats-no-negated-assert.bats.
load test_helper
classify() {
list=$(mktemp)
printf '%s\n' "$@" > "$list"
hack/code-changed.sh "$list"
rm -f "$list"
}
@test "docs tree, Markdown and repo metadata are not code" {
[ "$(classify docs/release.md docs/img/arch.png)" = "false" ]
[ "$(classify README.md ADOPTERS.md CLAUDE.md hack/README.md)" = "false" ]
[ "$(classify examples/backups/etcd/README.md examples/backups/etcd/91-scenario-user-backup.md)" = "false" ]
[ "$(classify img/logo.svg LICENSE security-insights.yml .coderabbit.yaml .gemini/styleguide.md)" = "false" ]
[ "$(classify .github/CODEOWNERS .github/ISSUE_TEMPLATE/bug_report.md .github/ISSUE_TEMPLATE/config.yml .github/PULL_REQUEST_TEMPLATE.md .github/labels.yml .github/renovate.json)" = "false" ]
}
@test "Markdown under packages/ is code: Go tests read chart READMEs" {
# internal/controller/fluxplunger/hook_contract_test.go reads this README.
[ "$(classify packages/system/flux-plunger/README.md)" = "true" ]
[ "$(classify packages/apps/mariadb/README.md docs/release.md)" = "true" ]
[ "$(classify hack/README.md examples/backups/etcd/README.md)" = "false" ]
}
@test "CI-only Go tools are not code, the rest of cmd/ is" {
[ "$(classify cmd/backport-batch/main.go cmd/api-gate/main.go)" = "false" ]
[ "$(classify cmd/backport-batch/main.go cmd/cozystack-api/main.go)" = "true" ]
}
@test "any product path makes the list code" {
[ "$(classify docs/release.md packages/apps/mariadb/values.yaml)" = "true" ]
[ "$(classify README.md hack/code-changed.sh)" = "true" ]
[ "$(classify .github/workflows/pull-requests.yaml)" = "true" ]
[ "$(classify .github/workflows/README.md .github/actionlint.yaml)" = "true" ]
[ "$(classify examples/backups/etcd/run-all.sh)" = "true" ]
[ "$(classify packages/tests/fixture/values.yaml)" = "true" ]
[ "$(classify .gitignore)" = "true" ]
[ "$(classify .dockerignore)" = "true" ]
}
@test "a Markdown suffix on a directory does not hide the files under it" {
[ "$(classify packages/apps/foo.md/templates/deploy.yaml)" = "true" ]
[ "$(classify docs.md/x.go)" = "true" ]
}
@test "a rename listed as both paths reads as code when either side is code" {
# git diff --no-renames and the fork lane both list old and new path.
[ "$(classify packages/apps/foo/templates/deploy.yaml docs/deploy.yaml)" = "true" ]
[ "$(classify docs/old.md docs/new.md)" = "false" ]
}
@test "an empty list is not code and a blank line fails closed" {
list=$(mktemp)
: > "$list"
[ "$(hack/code-changed.sh "$list")" = "false" ]
printf 'docs/a.md\n\n' > "$list"
[ "$(hack/code-changed.sh "$list")" = "true" ]
rm -f "$list"
}
@test "usage errors exit 2" {
run hack/code-changed.sh
[ "$status" -eq 2 ]
run hack/code-changed.sh /nonexistent/changed.txt
[ "$status" -eq 2 ]
}
# The push-triggered workflows cannot call the script, so they carry a `paths`
# filter: `**`, then one `!` glob per non-code path, then re-includes. Every `!`
# glob must name something the script calls non-code, or a push of real code
# would skip its build; every re-include must name code; and the three filters
# must be identical, so one copy cannot drift from the others.
push_paths() {
yq '.on.push.paths[]' "$1"
}
# A representative path for a glob: `**` spans directories, `*` a name.
samples() {
sed -e 's#/\*\*$#/sub/dir/file.txt#' -e 's#^\*\*\.md$#examples/x/README.md#' -e 's#\*\*#sub/dir#g' -e 's#\*#name#g'
}
@test "push path filters mirror the classifier and each other" {
main=$(push_paths .github/workflows/build-main.yaml)
[ "$(printf '%s\n' "$main" | head -n 1)" = "**" ]
[ "$(push_paths .github/workflows/build-release.yaml)" = "$main" ]
[ "$(push_paths .github/workflows/codeql.yml)" = "$main" ]
# Chart READMEs are code (see code-changed.sh), so `!**.md` needs this back.
printf '%s\n' "$main" | command grep -qxF 'packages/**.md'
list=$(mktemp)
printf '%s\n' "$main" | sed -n 's/^!//p' | samples > "$list"
cat "$list"
[ -s "$list" ]
[ "$(hack/code-changed.sh "$list")" = "false" ]
for glob in $(printf '%s\n' "$main" | sed -e '1d' -e '/^!/d'); do
printf '%s\n' "$glob" | samples > "$list"
cat "$list"
[ "$(hack/code-changed.sh "$list")" = "true" ]
done
rm -f "$list"
}
@test "CodeQL's pull_request trigger has no path filter: it is a required check" {
# Only `branches`: any paths or paths-ignore key would leave PRs on "Expected".
[ "$(yq '.on.pull_request | keys | join(",")' .github/workflows/codeql.yml)" = "branches" ]
}