|
| 1 | +-- Headless treesitter validation script. |
| 2 | +-- Exit code 1 on any failure so CI catches it. |
| 3 | +local errors = {} |
| 4 | + |
| 5 | +local function test_parser(lang, filepath) |
| 6 | + local lines = vim.fn.readfile(filepath) |
| 7 | + if #lines == 0 then |
| 8 | + table.insert(errors, string.format('[%s] fixture not found or empty: %s', lang, filepath)) |
| 9 | + return |
| 10 | + end |
| 11 | + |
| 12 | + local buf = vim.api.nvim_create_buf(false, true) |
| 13 | + vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines) |
| 14 | + |
| 15 | + local ok, result = pcall(vim.treesitter.get_parser, buf, lang) |
| 16 | + if not ok then |
| 17 | + table.insert(errors, string.format('[%s] parser load failed: %s', lang, tostring(result))) |
| 18 | + vim.api.nvim_buf_delete(buf, { force = true }) |
| 19 | + return |
| 20 | + end |
| 21 | + |
| 22 | + local ok2, trees = pcall(function() return result:parse() end) |
| 23 | + if not ok2 then |
| 24 | + table.insert(errors, string.format('[%s] parse failed: %s', lang, tostring(trees))) |
| 25 | + vim.api.nvim_buf_delete(buf, { force = true }) |
| 26 | + return |
| 27 | + end |
| 28 | + |
| 29 | + if not trees or #trees == 0 then |
| 30 | + table.insert(errors, string.format('[%s] no parse trees returned', lang)) |
| 31 | + vim.api.nvim_buf_delete(buf, { force = true }) |
| 32 | + return |
| 33 | + end |
| 34 | + |
| 35 | + vim.api.nvim_buf_delete(buf, { force = true }) |
| 36 | + print(string.format('OK [%s]: treesitter parser works', lang)) |
| 37 | +end |
| 38 | + |
| 39 | +local cfg = '/home/dev/.config/nvim' |
| 40 | +test_parser('go', cfg .. '/test/fixtures/hello.go') |
| 41 | +test_parser('terraform', cfg .. '/test/fixtures/main.tf') |
| 42 | + |
| 43 | +if #errors > 0 then |
| 44 | + for _, err in ipairs(errors) do |
| 45 | + io.stderr:write('FAIL ' .. err .. '\n') |
| 46 | + end |
| 47 | + vim.cmd('cquit 1') |
| 48 | +end |
0 commit comments