Skip to content

test(bats): refuse teardown_file assertions in the BATS suite - #1720

Merged
c-vigo merged 2 commits into
devfrom
bugfix/1699-teardown-file-shape-pin
Sep 25, 2026
Merged

c-vigo merged 2 commits into
devfrom
bugfix/1699-teardown-file-shape-pin

Conversation

@c-vigo

@c-vigo c-vigo commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

Description

#1699 reported that a failing teardown_file prints not ok but leaves bats exiting 0. That does not reproduce. The two-line file from the issue exits 1 on the flake's bats 1.12.0 in every mode tried — plain, -j 4, directory target, both formatters, and the exact nix develop --command bats t.bats; echo $? form — and on bats 1.14.0 from the nixpkgs-unstable input. The exit code is set in bats-exec-file and propagated by bats-exec-suite on both the serial and the parallel path; every upstream teardown_file exit-code issue is closed. The pasted output in the issue lacks the bats warning: Executed 2 instead of expected 1 tests line bats always prints here, so the spike most likely read $? after a pipeline.

What is real: three paths swallow a teardown_file failure whole — exit 0 and no not ok line either — so neither a pin advance nor a TAP-grep guard (the issue's options 1 and 2) has anything to catch:

  1. setup_file calls skip → the file's teardown_file status is discarded (deliberate upstream, bats-core#695)
  2. --filter / --filter-status selects zero tests in the file → the file body never runs
  3. a command fails inside a pipeline in teardown_file (false | cat) → plain bash pipeline status, no pipefail

So this lands only option 3: a shape pin in tests/test_bats_shape.py refusing teardown_file in tests/bats/*.bats, with an empty allowlist and the three swallow paths documented where the next reader will look. No file defines teardown_file today, so the gate starts green.

Type of Change

  • feat -- New feature
  • fix -- Bug fix
  • docs -- Documentation only
  • chore -- Maintenance task (deps, config, etc.)
  • refactor -- Code restructuring (no behavior change)
  • test -- Adding or updating tests
  • ci -- CI/CD pipeline changes
  • build -- Build system or dependency changes
  • revert -- Reverts a previous commit
  • style -- Code style (formatting, whitespace)

Modifiers

  • Breaking change (!) -- This change breaks backward compatibility

Changes Made

tests/test_bats_shape.py

  • TEARDOWN_FILE_ALLOWLIST: frozenset[str] = frozenset() and a definition matcher covering teardown_file() {, teardown_file () { and both function spellings, ignoring comment lines
  • test_bats_file_defines_no_teardown_file, parametrized over the suite like the existing minimum-version pin
  • test_teardown_file_matcher: 7 positive spellings (including a definition after a ;, a review finding) and 7 non-matches (commented-out definitions, a quoted mention, teardown, teardown_file_helper)
  • Module docstring explains the pin and corrects the exit-code claim; Refs: #1697, #1699

Changelog Entry

No changelog needed — test-only, per the issue.

Testing

  • Tests pass locally (just test)
  • Manual testing performed (describe below)

Manual Testing Details

  • uv run pytest tests/test_bats_shape.py -q → 57 passed (1 discovery + 21 min-version + 21 no-teardown-file + 14 matcher)
  • RED proof: temporarily appending teardown_file() { true; } to tests/bats/clean.bats → FAILED …[clean.bats], reverted; nothing temporary left in the tree
  • Reproduction of the issue's claim on bats 1.12.0: not ok 2 teardown_file failed and exit 1, in every mode listed above
  • ruff check / ruff format --check clean; prek run --all-files → all hooks passed

Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have updated the documentation accordingly (edit docs/templates/, then run just docs)
  • I have updated CHANGELOG.md in the [Unreleased] section (and pasted the entry above)
  • My changes generate no new warnings or errors
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published

Additional Notes

The issue is closed with this PR as "not reproducible as filed; shape pin landed", with the reproduction evidence on the issue.

Refs: #1699

The exit-code claim in #1699 did not reproduce: a plainly failing
teardown_file exits 1 on bats 1.12.0 and on 1.14.0, in serial, parallel
and both formatters, so there is nothing to guard in the runners.

Three paths do swallow such a failure whole (exit 0, no `not ok` line):
a `skip` in setup_file (deliberate upstream, bats-core#695), a
`--filter`/`--filter-status` that selects zero tests in the file, and a
command failing inside a pipeline (bats sets no `pipefail`). Pin the
shape instead: no .bats file defines teardown_file, allowlist empty, so
no future invariant is asserted where it can go unchecked while the run
stays green.

Refs: #1699
`setup() { :; }; teardown_file() { false; }` slipped past the
line-anchored pattern. Search for the definition at line start or after
a `;`; the comment skip now does real work.

Refs: #1699
@c-vigo
c-vigo merged commit ac90505 into dev Sep 25, 2026
15 checks passed
@c-vigo
c-vigo deleted the bugfix/1699-teardown-file-shape-pin branch September 25, 2026 16:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant