`.bats` files are dropped from the corpus: Bash test suites are invisible to the graph
What happens
.bats (Bats, Bash Automated Testing System) appears in no extension set in
detect.py. classify_file() returns None, so the files are not merely
classified as non-code - they are dropped from the corpus entirely.
>>> from graphify.detect import classify_file
>>> from pathlib import Path
>>> classify_file(Path('tests/some_suite.bats'))
None
>>> from graphify.extract import extract
>>> extract([Path('tests/some_suite.bats')], cache_root=Path('.'))['nodes']
[]
.sh and .bash are both in CODE_EXTENSIONS; .bats is not. Passing the
path straight to extract() fails the same way, because dispatch is by
extension there too.
Reproduced on graphifyy 0.9.54 with Bats 1.13.0.
Why it matters
In a shell-first repository the Bats suite is the whole test layer. In the one that surfaced this it is 10 files, roughly 100 KB of source, holding 180 tests. Documents in the same repo reference those tests by name, so the graph ends up with edge endpoints that have no node behind them: 59 dangling-endpoint edges in a 547-node graph. The Step 4.5 health check reports that as possible corruption, when in fact the extractor was never given the files.
The fix has two parts
Adding .bats to CODE_EXTENSIONS is necessary but not sufficient. Copying one
suite to a .bash name and extracting yields only the file node, the script
node, and the plain helper functions - every test in it produces nothing,
because the Bash grammar has no rule for the Bats test forms.
Bats has two ways to declare a test, and both need a rule:
# native form
@test "addition using bc" {
result="$(echo 2+2 | bc)"
[ "$result" -eq 4 ]
}
# bash-compliant form, for shellcheck and shfmt
function invoking_foo_without_arguments_prints_usage { #@test
run foo
[ "$status" -eq 1 ]
}
Both are documented upstream (docs/source/writing-tests.md, and the linting
entry in docs/source/gotchas.md). A rule that only matches @test at line
start would silently miss every suite written for linter compatibility.
Suggested change
- Add
.batstoCODE_EXTENSIONSand route it to the Bash extractor. - Emit a node per test in both forms - named from the quoted description for the native form, and from the function name for the comment form - so tests become linkable targets for the documents that describe them.
Workaround
None that survives. .graphifyrc accepts only viz_node_limit, there is no
extra-extensions option and no env var, and the direct-path route fails as shown
above.
Related, not duplicates
- #2825 - recognize
.zshin shell AST extraction. Same family (a shell extension missing from the sets), but.zshneeds no new grammar rule;.batsdoes, which is the second half of this report. - #2848 - systemd units are in no extension set. Same failure shape: the file is dropped rather than classified, and nothing warns.
- #2349 - Gherkin
.featurefiles are invisible to the graph. Closest analogue among test-spec formats. - #1689 - R is in
CODE_EXTENSIONSbut has no AST extractor. That is precisely the half-fixed state this issue asks to avoid: adding.batsto the set without a test-block rule would land there.
Hey! I've opened a PR for this.
I've added .bats to the parser's recognized extensions and upgraded the extract_bash logic to natively support both the @test and #@test Bats syntax forms so they emit as linkable bash_test nodes. I've also added a test suite to ensure no regressions.
Another case for this, still on graphifyy 0.9.122 (newest on PyPI), unpatched: a shell-first repo with 296 .bats files, holding 3,356 @test blocks, 308 load lines and 4,385 run lines.
None of them is in the graph, so every script they test looks untested, and every helper they load looks unused.
When .bats support lands, the two Bats commands that link files would help most, beyond the test nodes:
load helpers/commonloadshelpers/common.bash(Bats adds.bash), relative to the test file's folder: animports_fromedge, and calls into it resolved likesource.run ../bin/toolruns a script: an edge to it, likebash x.sh(#1756).
A minimal sample, in a new git repo:
# test/tool.bats
#!/usr/bin/env bats
load helpers/common
@test "the tool greets" {
setup_tool
run ../bin/tool
}
# test/helpers/common.bash
setup_tool() { export TOOL_HOME=/tmp; }
# bin/tool
#!/usr/bin/env bash
echo hello
graphify extract "$PWD" --code-only --out /tmp/out, edges other than contains/defines:
Expected:
imports_from test/tool.bats -> test/helpers/common.bash
calls test/tool.bats -> test/helpers/common.bash setup_tool
(an edge) test/tool.bats -> bin/tool
Got:
(none)