diff --git a/README.md b/README.md index e417c25..229c8fb 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,17 @@ require("neotest").setup({ }) ``` +By default `go test` runs for currecnt package only. If you want to run it recursively you need to set: +```lua +require("neotest").setup({ + adapters = { + require("neotest-go")({ + recursive_run = true + }) + } +}) +``` + ## Usage _NOTE_: all usages of `require('neotest').run.run` can be mapped to a command in your config (this is not included and should be done by the user) diff --git a/lua/neotest-go/init.lua b/lua/neotest-go/init.lua index 2c8f16a..8c97dc4 100644 --- a/lua/neotest-go/init.lua +++ b/lua/neotest-go/init.lua @@ -17,6 +17,10 @@ local get_args = function() return {} end +local recursive_run = function() + return false +end + ---@type neotest.Adapter local adapter = { name = "neotest-go" } @@ -149,25 +153,17 @@ end function adapter.build_spec(args) local results_path = async.fn.tempname() local position = args.tree:data() - local dir = position.path - -- The path for the position is not a directory, ensure the directory variable refers to one + local dir = "./" + if recursive_run() then + dir = "./..." + end + local location = position.path if fn.isdirectory(position.path) ~= 1 then - dir = fn.fnamemodify(position.path, ":h") + location = fn.fnamemodify(position.path, ":h") end - local package = utils.get_go_package_name(position.path) - - local cmd_args = ({ - dir = { "./..." }, - -- file is the same as dir because running a single test file - -- fails if it has external dependencies - file = { "./..." }, - namespace = { package }, - test = { "-run", utils.get_prefix(args.tree, position.name) .. "\\$", dir }, - })[position.type] - local command = vim.tbl_flatten({ "cd", - dir, + location, "&&", "go", "test", @@ -175,7 +171,7 @@ function adapter.build_spec(args) "-json", utils.get_build_tags(), vim.list_extend(get_args(), args.extra_args or {}), - unpack(cmd_args), + dir, }) return { command = table.concat(command, " "), @@ -283,6 +279,14 @@ setmetatable(adapter, { return opts.args end end + + if is_callable(opts.recursive_run) then + recursive_run = opts.recursive_run + elseif opts.recursive_run then + recursive_run = function() + return opts.recursive_run + end + end return adapter end, }) diff --git a/lua/spec/neotest-go/init_spec.lua b/lua/spec/neotest-go/init_spec.lua index a56093e..a406c60 100644 --- a/lua/spec/neotest-go/init_spec.lua +++ b/lua/spec/neotest-go/init_spec.lua @@ -350,9 +350,22 @@ describe("build_spec", function() local args = { tree = tree } local expected_command = "cd " .. vim.loop.cwd() - .. "/neotest_go && go test -v -json -count=1 -timeout=60s ./..." + .. "/neotest_go && go test -v -json -count=1 -timeout=60s ./" local result = plugin.build_spec(args) assert.are.same(expected_command, result.command) assert.are.same(path, result.context.file) end) + async.it("build specification for many_table_test.go recuresive run", function() + local plugin_with_recursive_run = require("neotest-go")({ recursive_run = true }) + local path = vim.loop.cwd() .. "/neotest_go/many_table_test.go" + local tree = plugin.discover_positions(path) + + local args = { tree = tree } + local expected_command = "cd " + .. vim.loop.cwd() + .. "/neotest_go && go test -v -json -count=1 -timeout=60s ./..." + local result = plugin_with_recursive_run.build_spec(args) + assert.are.same(expected_command, result.command) + assert.are.same(path, result.context.file) + end) end)