From 79b6de4e4565965a31098f453433df252a989f49 Mon Sep 17 00:00:00 2001 From: Camilo Avelar Date: Sun, 10 Nov 2024 07:58:26 -0300 Subject: [PATCH 1/2] enabling select test to run on floatterm (#520) --- Makefile | 5 ++- lua/go/gotest.lua | 19 +++++++-- lua/tests/go_test_spec.lua | 84 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 91c11d887..0e6c28392 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ localfailed: localtestsetup localtest: localtestsetup nvim --headless --noplugin -u lua/tests/init.vim -c "PlenaryBustedDirectory lua/tests/ {minimal_init = 'lua/tests/init.vim'}" localtestfile: localtestsetup - nvim --headless --noplugin -u lua/tests/init.vim -c "PlenaryBustedFile lua/tests/go_comment_spec.lua" + nvim --headless --noplugin -u lua/tests/init.vim -c "PlenaryBustedFile lua/tests/go_test_spec.lua" lint: luacheck lua/go @@ -21,6 +21,9 @@ localtestsetup: @test -d $(PACKER_DIR)/nvim-lspconfig ||\ git clone --depth 1 https://github.com/neovim/nvim-lspconfig $(PACKER_DIR)/nvim-lspconfig + @test -d $(PACKER_DIR)/guihua ||\ + git clone --depth 1 https://github.com/ray-x/guihua.lua $(PACKER_DIR)/guihua + @test -d $(PACKER_DIR)/nvim-treesitter ||\ git clone --depth 1 https://github.com/nvim-treesitter/nvim-treesitter $(PACKER_DIR)/nvim-treesitter diff --git a/lua/go/gotest.lua b/lua/go/gotest.lua index 0988dd836..e69e82a06 100644 --- a/lua/go/gotest.lua +++ b/lua/go/gotest.lua @@ -442,7 +442,7 @@ local function run_tests_with_ts_node(args, func_node, tblcase_ns) end if optarg['s'] then - return M.select_tests() + return M.select_tests(args) end if func_node == nil or func_node.name == nil then return @@ -515,7 +515,7 @@ M.test_func = function(...) end local ns = M.get_test_func_name() if empty(ns) then - return M.select_tests() + return M.select_tests(args) end return run_tests_with_ts_node(args, ns) end @@ -721,7 +721,7 @@ M.get_testfunc = function() end -- GUI to select test? -M.select_tests = function() +M.select_tests = function(args) local original_select = vim.ui.select vim.ui.select = _GO_NVIM_CFG.go_select() @@ -734,8 +734,21 @@ M.select_tests = function() if not item then return end + local uri = vim.uri_from_bufnr(0) + local fpath = M.get_test_path() + local cmd_args, optarg = cmd_builder(fpath, args) log(uri, item, idx) + + if optarg['F'] or _GO_NVIM_CFG.run_in_floaterm then + table.insert(cmd_args, '-test.run=' .. format_test_name(item)) + + local term = require('go.term').run + log(cmd_args) + term({ cmd = cmd_args, autoclose = false }) + return + end + vim.schedule(function() vim.lsp.buf.execute_command({ command = 'gopls.run_tests', diff --git a/lua/tests/go_test_spec.lua b/lua/tests/go_test_spec.lua index e6647ea40..a9dba6440 100644 --- a/lua/tests/go_test_spec.lua +++ b/lua/tests/go_test_spec.lua @@ -72,6 +72,90 @@ describe('should run func test', function() "-test.run='^\\QTest_branch\\E$'", }, cmd) end) + it('should test function on float term', function() + local path = 'coverage/branch_test.go' -- %:p:h ? %:p + require('go').setup({ + trace = true, + lsp_cfg = true, + log_path = vim.fn.expand('$HOME') .. '/tmp/gonvim.log', + test_runner = 'go', + }) + vim.cmd('cd ' .. godir) + vim.cmd("silent exe 'e " .. path .. "'") + vim.fn.setpos('.', { 0, 5, 11, 0 }) + local expCmd = {} + require('go.term').run = function(tbl) + expCmd = tbl.cmd + end + require('go.gotest').test_func('-F') + + eq({ + 'go', + 'test', + './coverage', + "-test.run='^\\QTest_branch\\E$'", + }, expCmd) + end) + it('should test function selecting tests', function() + local path = 'coverage/branch_test.go' -- %:p:h ? %:p + require('go').setup({ + trace = true, + lsp_cfg = true, + log_path = vim.fn.expand('$HOME') .. '/tmp/gonvim.log', + test_runner = 'go', + }) + vim.cmd('cd ' .. godir) + vim.cmd("silent exe 'e " .. path .. "'") + vim.fn.setpos('.', { 0, 5, 11, 0 }) + _GO_NVIM_CFG.go_select = function() + return function(_, _, func) + func('TestBranch', 2) + end + end + local expCmd = "" + local expArgs = {} + vim.lsp.buf.execute_command = function (tbl) + expCmd = tbl.command + expArgs = tbl.arguments + end + require('go.gotest').test_func('-s') + + vim.wait(500) + + eq('gopls.run_tests', expCmd) + eq({'TestBranch'}, expArgs[1].Tests) + end) + it('should test function on floating term selecting tests', function() + local path = 'coverage/branch_test.go' -- %:p:h ? %:p + require('go').setup({ + trace = true, + lsp_cfg = true, + log_path = vim.fn.expand('$HOME') .. '/tmp/gonvim.log', + test_runner = 'go', + }) + vim.cmd('cd ' .. godir) + vim.cmd("silent exe 'e " .. path .. "'") + vim.fn.setpos('.', { 0, 5, 11, 0 }) + _GO_NVIM_CFG.go_select = function() + return function(_, _, func) + func('TestBranch', 2) + end + end + local expCmd = {} + require('go.term').run = function(tbl) + expCmd = tbl.cmd + end + require('go.gotest').test_func('-sF') + + vim.wait(500) + + eq({ + 'go', + 'test', + './coverage', + "-test.run='^\\QTestBranch\\E$'", + }, expCmd) + end) end) describe('should run test file', function() From 4dedae4cd4d95c0edf1a9889bf25a7de894daaf3 Mon Sep 17 00:00:00 2001 From: Giulio Collura Date: Mon, 11 Nov 2024 15:31:20 -0800 Subject: [PATCH 2/2] fix forcing test godebug on `GoDebug` without params (#521) --- lua/go/dap.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/go/dap.lua b/lua/go/dap.lua index 54623d149..8ea4ec0c6 100644 --- a/lua/go/dap.lua +++ b/lua/go/dap.lua @@ -474,7 +474,7 @@ M.run = function(...) log(testfunc) if testfunc then - if testfunc.name ~= 'main' then + if testfunc.name:lower():find('test') and vim.tbl_isempty(optarg) then optarg['t'] = true end end