From 12ab6ac0fdd03b1ec9ee4e5378dd204f744a55bf Mon Sep 17 00:00:00 2001 From: ray-x Date: Mon, 21 Oct 2024 11:44:17 +1100 Subject: [PATCH] update for treesitter get_parser --- lua/go/gotest.lua | 38 +++++++++------- lua/go/runner.lua | 5 ++- lua/go/ts/go.lua | 4 +- lua/tests/go_test_spec.lua | 92 +++++++++++++++----------------------- 4 files changed, 65 insertions(+), 74 deletions(-) diff --git a/lua/go/gotest.lua b/lua/go/gotest.lua index df0d29a94..932b01700 100644 --- a/lua/go/gotest.lua +++ b/lua/go/gotest.lua @@ -118,6 +118,7 @@ local function get_test_filebufnr() fn = vfn.fnamemodify(fn, ':p') -- expand to full path -- check if file exists if vfn.filereadable(fn) == 0 then + vim.notify('no test file found for ' .. fn, vim.log.levels.WARN) return 0, 'no test file' end local uri = vim.uri_from_fname(fn) @@ -501,19 +502,21 @@ end M.test_func = function(...) local args = { ... } or {} log(args) - - local ns = M.get_test_func_name() - if empty(ns) then - return M.select_tests() - end - - if not vim.api.nvim_get_runtime_file('parser' .. sep .. 'go.so', false)[1] then + local bufnr = get_test_filebufnr() + local p = vim.treesitter.get_parser(bufnr, 'go') + if not p then -- require('nvim-treesitter.install').commands.TSInstallSync['run!']('go') vim.notify( - 'go treesitter parser not found, please Run `:TSInstallSync go`', + 'go treesitter parser not found for file ' + .. vim.fn.bufname() + .. ' please Run `:TSInstallSync go` ', vim.log.levels.WARN ) end + local ns = M.get_test_func_name() + if empty(ns) then + return M.select_tests() + end return run_tests_with_ts_node(args, ns) end @@ -666,11 +669,16 @@ end -- https://github.com/rentziass/dotfiles/blob/master/vim/.config/nvim/lua/rentziass/lsp/go_tests.lua M.run_file = function() local bufnr = vim.api.nvim_get_current_buf() - local tree = vim.treesitter.get_parser(bufnr, 'go'):parse()[1] + local parser = vim.treesitter.get_parser(bufnr, 'go') + if not parser then + vim.notify('go treesitter parser not found for ' .. vim.fn.bufname(), vim.log.levels.WARN) + return log('no ts parser found') + end + local tree = parser:parse()[1] local query = parse('go', require('go.ts.textobjects').query_test_func) local test_names = {} - local get_node_text=vim.treesitter.get_node_text + local get_node_text = vim.treesitter.get_node_text for id, node in query:iter_captures(tree:root(), bufnr, 0, -1) do local name = query.captures[id] -- name of the capture in the query if name == 'test_name' then @@ -690,17 +698,17 @@ M.get_testfunc = function() local bufnr = get_test_filebufnr() -- Note: the buffer may not be loaded yet - local ok, parser = vim.treesitter.get_parser(bufnr, 'go') - if not ok or not parser then + local parser = vim.treesitter.get_parser(bufnr, 'go') + if not parser then + vim.notify('go treesitter parser not found for ' .. vim.fn.bufname(), vim.log.levels.WARN) return log('no parser found') end - local tree = parser:parse() - tree = tree[1] + local tree = parser:parse()[1] local query = parse('go', require('go.ts.go').query_test_func) local test_names = {} - local get_node_text=vim.treesitter.get_node_text + local get_node_text = vim.treesitter.get_node_text for id, node in query:iter_captures(tree:root(), bufnr, 0, -1) do local name = query.captures[id] -- name of the capture in the query log(node) diff --git a/lua/go/runner.lua b/lua/go/runner.lua index 8c8cabc12..3be534c1b 100644 --- a/lua/go/runner.lua +++ b/lua/go/runner.lua @@ -129,9 +129,10 @@ local run = function(cmd, opts, uvopts) end) end if code ~= 0 then - log('failed to run', code, output_buf) + log('failed to run', code, output_buf, output_stderr) vim.schedule(function() - util.error( cmd_str .. ' failed exit code ' .. tostring(code or 0) .. (output_buf or '')) + util.error( cmd_str .. ' failed exit with code: ' .. tostring(code or 0) .. (output_buf or '') .. + (output_stderr or '')) end) end diff --git a/lua/go/ts/go.lua b/lua/go/ts/go.lua index 056746f2f..d589fe497 100644 --- a/lua/go/ts/go.lua +++ b/lua/go/ts/go.lua @@ -194,7 +194,7 @@ M.get_tbl_testcase_node_name = function(bufnr) local bufn = bufnr or vim.api.nvim_get_current_buf() local parser = vim.treesitter.get_parser(bufn, 'go') if not parser then - return warn('treesitter parser not found') + return warn('treesitter parser not found for' .. vim.fn.bufname(bufn)) end local tree = parser:parse() tree = tree[1] @@ -226,7 +226,7 @@ M.get_sub_testcase_name = function(bufnr) local bufn = bufnr or vim.api.nvim_get_current_buf() local parser = vim.treesitter.get_parser(bufn, 'go') if not parser then - return warn('treesitter parser not found') + return warn('treesitter parser not found for ' .. vim.fn.bufname(bufn)) end local sub_case_query = vim.treesitter.query.parse('go', M.query_sub_testcase_node) diff --git a/lua/tests/go_test_spec.lua b/lua/tests/go_test_spec.lua index a63f06fab..e6647ea40 100644 --- a/lua/tests/go_test_spec.lua +++ b/lua/tests/go_test_spec.lua @@ -4,17 +4,20 @@ local busted = require('plenary/busted') local godir = cur_dir .. '/lua/tests/fixtures' -- hack latest nvim treestitter get_node_text bug +-- TODO remove this after nvim-treesitter is fixed local nvim11 = vim.fn.has('nvim-0.11') == 1 describe('should run func test', function() -- vim.fn.readfile('minimal.vim') -- vim.fn.writefile(vim.fn.readfile('fixtures/fmt/hello.go'), name) - local status = require('plenary.reload').reload_module('go.nvim') + vim.cmd([[packadd go.nvim]]) + require('plenary.reload').reload_module('go.nvim') + require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter') + + vim.wait(400, function() end) it('should test function', function() -- -- go.nvim may not auto loaded - vim.cmd([[packadd go.nvim]]) - local path = './coverage/branch_test.go' -- %:p:h ? %:p require('go').setup({ trace = true, @@ -31,9 +34,6 @@ describe('should run func test', function() end) it('should test function inside a source code', function() -- - -- go.nvim may not auto loaded - vim.cmd([[packadd go.nvim]]) - local path = './coverage/branch.go' -- %:p:h ? %:p require('go').setup({ trace = true, @@ -51,9 +51,6 @@ describe('should run func test', function() end) it('should test function with additional args to test binary', function() -- - -- go.nvim may not auto loaded - vim.cmd([[packadd go.nvim]]) - local path = 'coverage/branch_test.go' -- %:p:h ? %:p require('go').setup({ trace = true, @@ -80,7 +77,8 @@ end) describe('should run test file', function() -- vim.fn.readfile('minimal.vim') -- vim.fn.writefile(vim.fn.readfile('fixtures/fmt/hello.go'), name) - local status = require('plenary.reload').reload_module('go.nvim') + require('plenary.reload').reload_module('go.nvim') + require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter') it('should test function', function() -- -- go.nvim may not auto loaded @@ -111,12 +109,14 @@ end) describe('should run test file with flags', function() -- vim.fn.readfile('minimal.vim') -- vim.fn.writefile(vim.fn.readfile('fixtures/fmt/hello.go'), name) - local status = require('plenary.reload').reload_module('go.nvim') + require('plenary.reload').reload_module('go.nvim') + require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter') it('should test function', function() -- -- go.nvim may not auto loaded vim.cmd([[packadd go.nvim]]) - + require('plenary.reload').reload_module('go.nvim') + require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter') local path = 'coverage/branch_test.go' -- %:p:h ? %:p require('go').setup({ trace = true, @@ -142,12 +142,15 @@ end) describe('should run test package: ', function() -- vim.fn.readfile('minimal.vim') -- vim.fn.writefile(vim.fn.readfile('fixtures/fmt/hello.go'), name) - local status = require('plenary.reload').reload_module('go.nvim') + require('plenary.reload').reload_module('go.nvim') + require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter') it('should test function', function() -- -- go.nvim may not auto loaded vim.cmd([[packadd go.nvim]]) + require('plenary.reload').reload_module('go.nvim') + require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter') local path = 'coverage/branch_test.go' -- %:p:h ? %:p require('go').setup({ trace = true, @@ -166,12 +169,11 @@ end) describe('should run test: ', function() -- vim.fn.readfile('minimal.vim') -- vim.fn.writefile(vim.fn.readfile('fixtures/fmt/hello.go'), name) - local status = require('plenary.reload').reload_module('go.nvim') + vim.cmd([[packadd go.nvim]]) + require('plenary.reload').reload_module('go.nvim') + require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter') it('should test function', function() -- - -- go.nvim may not auto loaded - vim.cmd([[packadd go.nvim]]) - local path = 'coverage/branch_test.go' -- %:p:h ? %:p require('go').setup({ trace = true, @@ -190,12 +192,10 @@ end) describe('should allow select test func: ', function() -- vim.fn.readfile('minimal.vim') -- vim.fn.writefile(vim.fn.readfile('fixtures/fmt/hello.go'), name) - local status = require('plenary.reload').reload_module('go.nvim') + require('plenary.reload').reload_module('go.nvim') + require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter') it('should test function', function() -- - -- go.nvim may not auto loaded - vim.cmd([[packadd go.nvim]]) - local path = 'coverage/branch_test.go' -- %:p:h ? %:p require('go').setup({ trace = true, @@ -214,12 +214,10 @@ end) describe('should run test file with flags inside file: ', function() -- vim.fn.readfile('minimal.vim') -- vim.fn.writefile(vim.fn.readfile('fixtures/fmt/hello.go'), name) - local status = require('plenary.reload').reload_module('go.nvim') + require('plenary.reload').reload_module('go.nvim') + require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter') it('should test function with tag', function() -- - -- go.nvim may not auto loaded - vim.cmd([[packadd go.nvim]]) - local path = 'coverage/tag_test.go' -- %:p:h ? %:p require('go').setup({ trace = true, @@ -243,16 +241,17 @@ describe('should run test file with flags inside file: ', function() end) describe('should run subcase tests: ', function() + vim.cmd([[packadd go.nvim]]) + require('plenary.reload').reload_module('go.nvim') + require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter') + if nvim11 then + eq(1, 1) + return + end it('should test subcase in table test style', function() - if nvim11 then - eq(1, 1) - return - end -- go.nvim may not auto loaded - vim.cmd([[packadd go.nvim]]) - local path = 'coverage/branch_test.go' require('go').setup({ trace = true, @@ -263,17 +262,11 @@ describe('should run subcase tests: ', function() vim.cmd("silent exe 'e " .. path .. "'") vim.fn.setpos('.', { 1, 18, 11, 0 }) local cmd = require('go.gotest').test_tblcase() - eq({ 'go', 'test', './coverage', '-test.run=\'^\\QTest_branch\\E$\'/\'^\\Qa10\\E$\'' }, cmd) + eq({ 'go', 'test', './coverage', "-test.run='^\\QTest_branch\\E$'/'^\\Qa10\\E$'" }, cmd) end) it('should test subcase in table test style when cursor inside test block', function() - if nvim11 then - eq(1, 1) - return - end -- go.nvim may not auto loaded - vim.cmd([[packadd go.nvim]]) - local path = 'coverage/branch_test.go' require('go').setup({ trace = true, @@ -284,17 +277,13 @@ describe('should run subcase tests: ', function() vim.cmd("silent exe 'e " .. path .. "'") vim.fn.setpos('.', { 1, 29, 12, 0 }) local cmd = require('go.gotest').test_tblcase() - eq({ 'go', 'test', './coverage', '-test.run=\'^\\QTest_branch\\E$\'/\'^\\Qb10 [step 1..3]\\E$\'' }, cmd) + eq( + { 'go', 'test', './coverage', "-test.run='^\\QTest_branch\\E$'/'^\\Qb10 [step 1..3]\\E$'" }, + cmd + ) end) it('should test subcase in subtest style', function() - if nvim11 then - eq(1, 1) - return - end - -- go.nvim may not auto loaded - vim.cmd([[packadd go.nvim]]) - local path = 'coverage/branch_test.go' require('go').setup({ trace = true, @@ -305,17 +294,10 @@ describe('should run subcase tests: ', function() vim.cmd("silent exe 'e " .. path .. "'") vim.fn.setpos('.', { 1, 75, 11, 0 }) local cmd = require('go.gotest').test_tblcase() - eq({ 'go', 'test', './coverage', '-test.run=\'^\\QTestBranchSubTest\\E$\'/\'^\\Qa11\\E$\'' }, cmd) + eq({ 'go', 'test', './coverage', "-test.run='^\\QTestBranchSubTest\\E$'/'^\\Qa11\\E$'" }, cmd) end) it('should test subcase in subtest style when cursor inside test block', function() - if nvim11 then - eq(1, 1) - return - end - -- go.nvim may not auto loaded - vim.cmd([[packadd go.nvim]]) - local path = 'coverage/branch_test.go' require('go').setup({ trace = true, @@ -326,6 +308,6 @@ describe('should run subcase tests: ', function() vim.cmd("silent exe 'e " .. path .. "'") vim.fn.setpos('.', { 1, 82, 7, 0 }) local cmd = require('go.gotest').test_tblcase() - eq({ 'go', 'test', './coverage', '-test.run=\'^\\QTestBranchSubTest\\E$\'/\'^\\Qb11\\E$\'' }, cmd) + eq({ 'go', 'test', './coverage', "-test.run='^\\QTestBranchSubTest\\E$'/'^\\Qb11\\E$'" }, cmd) end) end)