feat(tests)!: new infrastructure based on makefile
Problem: Not easy to run all checks and tests locally. Redundant CI
workflows.
Solution: Separate CI into two workflows:
* lint: Lua files (stylua, luals), query files (valid captures,
predicates, directives using tsqueryls), docs
(SUPPORTED_LANGUAGES.md) -- does not need parser installation
* tests: parsers (ABI compatibility), query files (tsqueryls on
Linux/macOS; nvim on Windows), highlight and indent tests (separated
for better readability) -- needs parser installation (but only once)
Switch to https://github.com/nvim-treesitter/highlight-assertions fork
with ABI 15 support.
Run all tests (on Linux and macOS) through `make` (`formatlua`,
`checklua`, `lintquery`, `formatquery`, `checkquery`, `docs`, `tests`),
which downloads and caches all necessary dependencies.
Remove `update-readme` workflow (replaced by lint job on PRs).
2025-04-29 19:40:18 +02:00
|
|
|
local config = require('nvim-treesitter.config')
|
2023-02-12 18:18:48 +00:00
|
|
|
local ts = vim.treesitter
|
2021-11-20 15:09:19 +01:00
|
|
|
|
2022-01-07 20:04:21 +01:00
|
|
|
local COMMENT_NODES = {
|
2023-06-12 09:54:30 -06:00
|
|
|
markdown = 'html_block',
|
|
|
|
|
haskell = 'haddock',
|
2022-01-07 20:04:21 +01:00
|
|
|
}
|
|
|
|
|
|
2021-11-20 15:09:19 +01:00
|
|
|
local function check_assertions(file)
|
2023-09-04 16:19:33 +09:00
|
|
|
local buf = vim.fn.bufadd(file)
|
|
|
|
|
vim.fn.bufload(file)
|
|
|
|
|
local ft = vim.bo[buf].filetype
|
|
|
|
|
local lang = vim.treesitter.language.get_lang(ft) or ft
|
2023-06-12 09:54:30 -06:00
|
|
|
local comment_node = COMMENT_NODES[lang] or 'comment'
|
feat(tests)!: new infrastructure based on makefile
Problem: Not easy to run all checks and tests locally. Redundant CI
workflows.
Solution: Separate CI into two workflows:
* lint: Lua files (stylua, luals), query files (valid captures,
predicates, directives using tsqueryls), docs
(SUPPORTED_LANGUAGES.md) -- does not need parser installation
* tests: parsers (ABI compatibility), query files (tsqueryls on
Linux/macOS; nvim on Windows), highlight and indent tests (separated
for better readability) -- needs parser installation (but only once)
Switch to https://github.com/nvim-treesitter/highlight-assertions fork
with ABI 15 support.
Run all tests (on Linux and macOS) through `make` (`formatlua`,
`checklua`, `lintquery`, `formatquery`, `checkquery`, `docs`, `tests`),
which downloads and caches all necessary dependencies.
Remove `update-readme` workflow (replaced by lint job on PRs).
2025-04-29 19:40:18 +02:00
|
|
|
local assertions = vim.fn.json_decode(vim.fn.system({
|
|
|
|
|
os.getenv('HLASSERT'),
|
|
|
|
|
'-p',
|
|
|
|
|
config.get_install_dir('parser') .. '/' .. lang .. '.so',
|
|
|
|
|
'-s',
|
|
|
|
|
file,
|
|
|
|
|
'-c',
|
|
|
|
|
comment_node,
|
|
|
|
|
}))
|
2023-09-04 16:19:33 +09:00
|
|
|
assert.True(#assertions > 0, 'No assertions detected!')
|
2021-11-20 15:09:19 +01:00
|
|
|
|
2023-09-04 16:19:33 +09:00
|
|
|
local parser = ts.get_parser(buf)
|
|
|
|
|
|
|
|
|
|
parser:parse(true)
|
2021-11-20 15:09:19 +01:00
|
|
|
|
|
|
|
|
for _, assertion in ipairs(assertions) do
|
|
|
|
|
local row = assertion.position.row
|
|
|
|
|
local col = assertion.position.column
|
2023-09-04 16:19:33 +09:00
|
|
|
assert.is.number(row)
|
|
|
|
|
assert.is.number(col)
|
2021-11-20 15:09:19 +01:00
|
|
|
|
|
|
|
|
local captures = {}
|
2023-09-04 16:19:33 +09:00
|
|
|
parser:for_each_tree(function(tstree, tree)
|
|
|
|
|
if not tstree then
|
2021-11-20 15:09:19 +01:00
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
2023-06-12 09:54:30 -06:00
|
|
|
local root = tstree:root()
|
2021-11-20 15:09:19 +01:00
|
|
|
local root_start_row, _, root_end_row, _ = root:range()
|
|
|
|
|
|
|
|
|
|
-- Only worry about trees within the line range
|
|
|
|
|
if root_start_row > row or root_end_row < row then
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
2023-09-04 16:19:33 +09:00
|
|
|
local query = ts.query.get(tree:lang(), 'highlights')
|
|
|
|
|
if not query then
|
2021-11-20 15:09:19 +01:00
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
2023-09-04 16:19:33 +09:00
|
|
|
for id, node, _ in query:iter_captures(root, buf, row, row + 1) do
|
|
|
|
|
if ts.is_in_node_range(node, row, col) then
|
|
|
|
|
local capture = query.captures[id]
|
|
|
|
|
if capture ~= nil and capture ~= 'conceal' and capture ~= 'spell' then
|
|
|
|
|
captures[capture] = true
|
2021-11-20 15:09:19 +01:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2023-06-12 09:54:30 -06:00
|
|
|
end)
|
|
|
|
|
if assertion.expected_capture_name:match('^!') then
|
2023-01-01 15:16:04 +01:00
|
|
|
assert.Falsy(
|
2023-09-04 16:19:33 +09:00
|
|
|
captures[assertion.expected_capture_name:sub(2)],
|
|
|
|
|
'Error in '
|
2023-01-01 15:16:04 +01:00
|
|
|
.. file
|
2023-06-12 09:54:30 -06:00
|
|
|
.. ':'
|
2023-01-01 15:16:04 +01:00
|
|
|
.. (row + 1)
|
2023-06-12 09:54:30 -06:00
|
|
|
.. ':'
|
2023-01-01 15:16:04 +01:00
|
|
|
.. (col + 1)
|
|
|
|
|
.. ': expected "'
|
|
|
|
|
.. assertion.expected_capture_name
|
|
|
|
|
.. '", captures: '
|
|
|
|
|
.. vim.inspect(vim.tbl_keys(captures))
|
|
|
|
|
)
|
|
|
|
|
else
|
|
|
|
|
assert.True(
|
2023-09-04 16:19:33 +09:00
|
|
|
captures[assertion.expected_capture_name],
|
|
|
|
|
'Error in '
|
2023-01-01 15:16:04 +01:00
|
|
|
.. file
|
2023-06-12 09:54:30 -06:00
|
|
|
.. ':'
|
2023-01-01 15:16:04 +01:00
|
|
|
.. (row + 1)
|
2023-06-12 09:54:30 -06:00
|
|
|
.. ':'
|
2023-01-01 15:16:04 +01:00
|
|
|
.. (col + 1)
|
|
|
|
|
.. ': expected "'
|
|
|
|
|
.. assertion.expected_capture_name
|
|
|
|
|
.. '", captures: '
|
|
|
|
|
.. vim.inspect(vim.tbl_keys(captures))
|
|
|
|
|
)
|
|
|
|
|
end
|
2021-11-20 15:09:19 +01:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2023-06-12 09:54:30 -06:00
|
|
|
describe('highlight queries', function()
|
|
|
|
|
local files = vim.fn.split(vim.fn.glob('tests/query/highlights/**/*.*'))
|
2021-11-20 15:09:19 +01:00
|
|
|
for _, file in ipairs(files) do
|
|
|
|
|
it(file, function()
|
|
|
|
|
check_assertions(file)
|
|
|
|
|
end)
|
|
|
|
|
end
|
|
|
|
|
end)
|