Files
cozystack/.github/workflows/pre-commit.yml
Myasnikov Daniil 062207fd7b test(hack): run the unit bats suite under bats(1)
hack/cozytest.sh serves two populations that want opposite things from a
runner: 32 hermetic unit tests of hack/*.sh, and 3 live-cluster imperative
tests. This moves the unit half to real bats and leaves the live half where
it is; the hack/e2e-*.bats filter already in BATS_UNIT_FILES is what keeps
them apart.

The unit files need no edits. They pass under vanilla bats 1.13 unchanged
because their authors wrote to the intersection of both runners -- no run,
no $status, no skip, repo-root-relative paths. Measured on this tree: 328
tests, 0 failures, 26s under `bats -j 8` against cozytest's 63s serial.

What this buys, none of which cozytest offers: --filter-status failed to
rerun only the previous failures, JUnit so CI annotates the failing test
instead of requiring a log read, -j, and deletion of a bespoke awk parser
whose rewrite rules void helper exit codes, corrupt heredoc fixtures on a
column-0 brace, and collide test names after sanitization.

Two consequences worth naming, neither covered by the issue:

bats is a new dependency. cozytest is pure bash+awk, so no runner image or
contributor machine has ever needed anything else. The checks job installs
it from a pinned tag rather than the distro package, whose version varies
by base image, and the make target says so plainly when it is absent. GNU
parallel stays optional -- BATS_JOBS resolves to 1 without it, since
`bats -j` exits non-zero rather than degrading.

The pre-commit lane is the second consumer and does NOT get bats. That job
lints changed YAML and Markdown on a runner with no bats, and the new
hook's path filter covers .github/workflows/, so a workflow edit would drag
the whole 328-test shell suite into a lint lane and duplicate the checks
job that already runs `make unit-tests`. It is skipped there by id, keeping
the hook a local-developer gate.

The tested shell changes. cozytest is #!/bin/sh, which is dash on the CI
runner, and bats runs bash. Seven POSIX scripts that tests source rather
than execute -- seaweedfs-naming-audit.sh, promote-rewrite-tags.sh,
lib/image-refs.sh, the three e2e-capture scripts and cozystack-version.sh
-- therefore lose a runtime dash check they had by accident. Four
shell=bash files under e2e-chainsaw/_lib gain correct fidelity for the same
reason. The static replacement is the shellcheck gate in item 5 of #3453,
which does not exist yet.

set -u is not restored here. bats enforces -e but not -u, and there is no
runner-level way to inject it: --setup-suite-file does not reach test
files, and SHELLOPTS=nounset is exported, so it leaks into the scripts
under test and drops two tests. Restoring it needs a per-file load and is
the layer above this one.

Refs: #3453
Signed-off-by: Myasnikov Daniil <myasnikovdaniil2001@gmail.com>
2026-07-31 12:08:21 +05:00

82 lines
2.8 KiB
YAML

name: Pre-Commit Checks
on:
pull_request:
types: [opened, synchronize, reopened]
concurrency:
group: pre-commit-${{ github.workflow }}-${{ github.event.pull_request.number }}
cancel-in-progress: true
# Top-level token defaults to read-only; jobs request the minimum extra scopes.
permissions:
contents: read
jobs:
pre-commit:
runs-on: ubuntu-22.04
permissions:
contents: read
steps:
- name: Checkout code
uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3
with:
fetch-depth: 0
fetch-tags: true
- name: Set up Python
uses: actions/setup-python@7f4fc3e22c37d6ff65e88745f38bd3157c663f7c # v4
with:
python-version: '3.11'
- name: Install pre-commit
run: pip install pre-commit
- name: Set up Go
uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5
with:
go-version-file: go.mod
cache: true
- name: Pre-fetch k8s.io/code-generator module
# The repo-root pre-commit hook (run-make-generate-root) shells out to
# hack/update-codegen.sh, which sources kube_codegen.sh from the Go
# module cache. The module is not declared in go.mod, so fetch it
# explicitly at the version pinned in the script - mirrors the
# equivalent step in codegen-drift.yml.
run: |
version=$(grep -oP 'code-generator@\Kv[0-9.]+' hack/update-codegen.sh)
tmpdir=$(mktemp -d)
cd "$tmpdir"
go mod init codegen-fetch
go get "k8s.io/code-generator@${version}"
- name: Install generate
run: |
curl -sSL https://github.com/cozystack/cozyvalues-gen/releases/download/v1.6.0/cozyvalues-gen-linux-amd64.tar.gz | tar -xzvf- -C /usr/local/bin/ cozyvalues-gen
- name: Run pre-commit hooks
# bats-unit-tests is a local-developer gate, skipped here. This job
# lints changed YAML/Markdown, and the hook's path filter covers
# .github/workflows/, so a workflow edit would otherwise drag the whole
# 328-test shell suite into a lint lane -- on a runner with no bats,
# and duplicating the `checks` job in pull-requests.yaml that already
# runs `make unit-tests`.
env:
SKIP: bats-unit-tests
run: |
git fetch origin main || git fetch origin master
base_commit=$(git rev-parse --verify origin/main || git rev-parse --verify origin/master || echo "")
if [ -z "$base_commit" ]; then
files=$(git ls-files '*.yaml' '*.md')
else
files=$(git diff --name-only "$base_commit" -- '*.yaml' '*.md')
fi
if [ -n "$files" ]; then
echo "$files" | xargs pre-commit run --files
else
echo "No YAML or Markdown files to lint"
fi