tree-sitter-bats
v0.2.0
Published
Bats (Bash Automated Testing System) grammar for tree-sitter, extending tree-sitter-bash.
Maintainers
Readme
tree-sitter-bats
Bats (Bash Automated Testing System) grammar for tree-sitter, extending tree-sitter-bash.
Status
Early development. Until 1.0, a minor release may change node types; CHANGELOG.md notes each change.
Install
npm install tree-sitter-bats tree-sitter # Node.js
cargo add tree-sitter-bats tree-sitter # Rust
pip install tree-sitter-bats tree-sitter # PythonGo and Swift users depend on a release tag, such as v0.2.0. Each GitHub Release attaches a Wasm build, tree-sitter-bats.wasm.
Tools that load a compiled grammar, such as ast-grep, need a build from source:
git clone --branch v0.2.0 https://github.com/nertzy/tree-sitter-bats
cd tree-sitter-bats
tree-sitter build --output bats.dylib # bats.so on LinuxParse Bats files
Every Bash construct parses as it does in tree-sitter-bash. Bats test definitions become test_block nodes with a name field and a body field (a compound_statement). The name is a test_name node spanning everything between @test and {, as Bats reads it, so it holds a string, a raw_string, or the words of an unquoted name. This test
@test "addition using bc" {
result="$(echo 2+2 | bc)"
[ "$result" -eq 4 ]
}parses as (abbreviated):
(program
(test_block
name: (test_name (string (string_content)))
body: (compound_statement
(variable_assignment ...)
(test_command ...))))Bats also runs a function as a test when a # @test comment ends the line that opens its body, as in name() { # @test. That comment becomes a test_marker_comment node, the first child of the function's compound_statement, instead of a comment. Its @test is a separate anonymous "@test" token, the same one a test_block starts with, so highlights can color it as a keyword. A # @test anywhere else stays a comment, since Bats ignores it.
setup, teardown, setup_file, and teardown_file are ordinary function_definition nodes, and run, load, and bats_require_minimum_version are ordinary command nodes.
With ast-grep
Register the compiled library as an ast-grep custom language. Bash reads $NAME as an expansion, so set an expandoChar for metavariables:
# sgconfig.yml
customLanguages:
bats:
libraryPath: path/to/bats.dylib
extensions: [bats]
expandoChar: _ast-grep run --lang bats --pattern '@test $NAME { $$$BODY; }'
ast-grep scan --inline-rules '{ id: test-names, language: bats, rule: { kind: test_block } }'Highlighting
queries/highlights.scm covers the Bash constructs plus Bats syntax: @test, test names, the setup and teardown hooks, helpers such as run, load, and skip, and the variables Bats sets ($status, $output, $lines, $stderr, $stderr_lines, and $BATS_*). tree-sitter.json points editors and tree-sitter highlight at it.
No two patterns capture the same node, so editors that prefer the first matching pattern (tree-sitter-highlight) and those that prefer the last (Zed) highlight the same way. The Rust binding test enforces this on test/highlight/ and, once script/fetch-examples has fetched them, on the example suites; CI runs it on both. tree-sitter test runs the assertions in test/highlight/.
Neovim
queries/neovim/ holds highlight, fold, indent, injection, and locals queries for nvim-treesitter. Each extends nvim-treesitter's Bash queries with Bats test blocks, hooks, helpers, and variables. Register the parser as a custom language:
vim.api.nvim_create_autocmd('User', {
pattern = 'TSUpdate',
callback = function()
require('nvim-treesitter.parsers').bats = {
install_info = {
url = 'https://github.com/nertzy/tree-sitter-bats',
revision = 'v0.2.0',
queries = 'queries/neovim',
},
requires = { 'bash' },
}
end,
})Then run :TSInstall bats, which installs the bash parser too, and enable highlighting, folds, and indentation for the bats filetype. Neovim 0.13 detects .bats files as bats; Neovim 0.12 detects them as sh, so add:
vim.filetype.add({ extension = { bats = 'bats' } })Scope and limitations
Bats test files are Bash scripts with extra syntax. This grammar parses them by extending tree-sitter-bash 0.25.1, so anything tree-sitter-bash misparses is misparsed here too unless this grammar works around it.
Worked around:
- A
[ ... ]test, later commands with a redirect, and a following[ ... ]merge into onetest_commandwithout anERRORnode (tree-sitter-bash#316). Here a redirect inside single brackets, as in[ ! command -v go &>/dev/null ], must be a command with single-word redirect targets followed by], and the separate-statements parse wins any remaining ambiguity. - A herestring after a file or heredoc redirect, as in
cat > file <<<"text", produces anERROR(tree-sitter-bash#232, #282). Here a herestring may follow the first file or heredoc redirect; a herestring before it still belongs to the command.
Not yet worked around:
- A line with a redirect after a pipeline of three or more commands merges into the pipeline's last command (same issue, comment).
- A heredoc whose line continues past the opener with
;,&, or more than one further statement, as incat <<EOF; echo nextorcat <<EOF | grep x; echo y, produces anERROR. Proposed fixes: tree-sitter-bash#355 and #356. - More than one heredoc opened on a line, as in
cat <<A <<Borcat <<A; cat <<B, produces anERROR. - An argument after a herestring that follows a file redirect, as in
cat > out <<< x -, produces anERROR. - The read-write redirect
<>produces anERROR(tree-sitter-bash#352). - A quoted string containing a space inside a glob after
=in[[ ... ]], as in[[ $output = *"a b"* ]], produces anERROR(tree-sitter-bash#321);==parses. - A double-quoted string followed by a group in a
=~regex, as in[[ $output =~ "a "([^ ]+) ]], produces anERROR. - An escaped backtick inside a quoted glob in
[[ ... ]], as in[[ $output = *"\`cmd\`"* ]], produces anERROR.
CI parses the .bats files of several public Bats suites at pinned commits. script/known-failures.txt lists the files that still produce ERROR nodes; each traces to one of the limitations above or to a deliberately malformed fixture.
Development
Requires the tree-sitter CLI 0.27.1 (pinned in package.json and CI), Node.js, and a C compiler. See CONTRIBUTING.md.
References
License
MIT. See LICENSE.
