graphify icon indicating copy to clipboard operation
graphify copied to clipboard

`.bats` files are dropped from the corpus: Bash test suites are invisible to the graph

Open Stashub opened this issue 2 months ago • 2 comments

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

  1. Add .bats to CODE_EXTENSIONS and route it to the Bash extractor.
  2. 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 .zsh in shell AST extraction. Same family (a shell extension missing from the sets), but .zsh needs no new grammar rule; .bats does, 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 .feature files are invisible to the graph. Closest analogue among test-spec formats.
  • #1689 - R is in CODE_EXTENSIONS but has no AST extractor. That is precisely the half-fixed state this issue asks to avoid: adding .bats to the set without a test-block rule would land there.

Stashub avatar Sep 05 '26 17:09 Stashub

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.

nikhilsaxena04 avatar Sep 09 '26 22:09 nikhilsaxena04

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/common loads helpers/common.bash (Bats adds .bash), relative to the test file's folder: an imports_from edge, and calls into it resolved like source.
  • run ../bin/tool runs a script: an edge to it, like bash 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)

yashsaxena avatar Nov 28 '26 04:11 yashsaxena