diff --git a/lua/go/gotest.lua b/lua/go/gotest.lua index c8f6900..6437d98 100644 --- a/lua/go/gotest.lua +++ b/lua/go/gotest.lua @@ -136,13 +136,13 @@ local function run_test(path, args) if _GO_NVIM_CFG.run_in_floaterm then local term = require("go.term").run term({ cmd = cmd, autoclose = false }) - return + return cmd end vim.cmd([[setl makeprg=]] .. _GO_NVIM_CFG.go .. [[\ test]]) utils.log("test cmd", cmd) - require("go.asyncmake").make(unpack(cmd)) + return require("go.asyncmake").make(unpack(cmd)) end M.test = function(...) @@ -177,12 +177,11 @@ M.test_package = function(...) local args = { ... } log(args) - local repath = utils.rel_path() or "." - local fpath = repath .. utils.sep() .. "..." + local fpath = "." .. sep .. vim.fn.fnamemodify(vim.fn.expand("%:h"), ":~:.") .. sep .. "..." utils.log("fpath: " .. fpath) -- args[#args + 1] = fpath - run_test(fpath, args) + return run_test(fpath, args) end M.test_fun = function(...) @@ -259,7 +258,8 @@ M.test_file = function(...) -- require sed -- local testcases = [[sed -n 's/func.*\(Test.*\)(.*/\1/p' | xargs | sed 's/ /\\\|/g']] - local fpath = vim.fn.expand("%:p") + -- local fpath = vim.fn.expand("%:p") + local fpath = "." .. sep .. vim.fn.fnamemodify(vim.fn.expand("%:p"), ":~:.") -- utils.log(args) local cmd = [[cat ]] .. fpath .. [[| sed -n 's/func.*\(Test.*\)(.*/\1/p' | xargs | sed 's/ /\|/g']] -- TODO maybe with treesitter or lsp list all functions in current file and regex with Test @@ -321,8 +321,11 @@ M.test_file = function(...) end vim.cmd([[setl makeprg=]] .. _GO_NVIM_CFG.go .. [[\ test]]) - require("go.asyncmake").make(unpack(cmd_args)) + + local cmdret = require("go.asyncmake").make(unpack(cmd_args)) + utils.log("test cmd: ", cmd, " finished") + return cmdret end -- TS based run func @@ -348,6 +351,8 @@ M.run_file = function() end) end + +-- GUI to select test? M.select_tests = function() local bufnr = vim.api.nvim_get_current_buf() local tree = vim.treesitter.get_parser(bufnr):parse()[1] diff --git a/lua/go/utils.lua b/lua/go/utils.lua index b20b22f..774222e 100644 --- a/lua/go/utils.lua +++ b/lua/go/utils.lua @@ -421,7 +421,7 @@ function util.rel_path() if workfolders ~= nil and next(workfolders) then fpath = "." .. fpath:sub(#workfolders[1] + 1) end - return fpath + return "." .. util.sep() .. vim.fn.fnamemodify(vim.fn.expand("%:p"), ":~:.") end function util.rtrim(s) diff --git a/lua/tests/go_test_spec.lua b/lua/tests/go_test_spec.lua index 62d92ca..cc97851 100644 --- a/lua/tests/go_test_spec.lua +++ b/lua/tests/go_test_spec.lua @@ -20,8 +20,53 @@ describe("should run test", function() vim.cmd("silent exe 'e " .. path .. "'") vim.fn.setpos(".", { 0, 5, 11, 0 }) local cmd = require("go.gotest").test_fun() - local lines = vim.fn.readfile(path) eq({ "go", "test", "-v", "-run", "^Test_branch", "./lua/tests/fixtures/coverage" }, cmd) end) end) + +describe("should run test file", function() + -- vim.fn.readfile('minimal.vim') + -- vim.fn.writefile(vim.fn.readfile('fixtures/fmt/hello.go'), name) + status = require("plenary.reload").reload_module("go.nvim") + it("should test function", function() + -- + -- go.nvim may not auto loaded + vim.cmd([[packadd go.nvim]]) + + local path = cur_dir .. "/lua/tests/fixtures/coverage/branch_test.go" -- %:p:h ? %:p + require("go").setup({ + trace = true, + lsp_cfg = true, + log_path = vim.fn.expand("$HOME") .. "/tmp/gonvim.log", + }) + vim.cmd("silent exe 'e " .. path .. "'") + vim.fn.setpos(".", { 0, 5, 11, 0 }) + local cmd = require("go.gotest").test_file() + + eq({ "go", "test", "-v", "-run", "Test_branch", "./lua/tests/fixtures/coverage/branch_test.go" }, cmd) + end) +end) + +describe("should run test package", function() + -- vim.fn.readfile('minimal.vim') + -- vim.fn.writefile(vim.fn.readfile('fixtures/fmt/hello.go'), name) + status = require("plenary.reload").reload_module("go.nvim") + it("should test function", function() + -- + -- go.nvim may not auto loaded + vim.cmd([[packadd go.nvim]]) + + local path = cur_dir .. "/lua/tests/fixtures/coverage/branch_test.go" -- %:p:h ? %:p + require("go").setup({ + trace = true, + lsp_cfg = true, + log_path = vim.fn.expand("$HOME") .. "/tmp/gonvim.log", + }) + vim.cmd("silent exe 'e " .. path .. "'") + vim.fn.setpos(".", { 0, 1, 1, 0 }) + local cmd = require("go.gotest").test_package() + + eq({ "go", "test", "-v", "./lua/tests/fixtures/coverage/..." }, cmd) + end) +end)