diff --git a/README.md b/README.md index e417c25..b3fff8a 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 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) diff --git a/neotest_go/nested/n_cases.go b/neotest_go/nested/n_cases.go new file mode 100644 index 0000000..aa5c918 --- /dev/null +++ b/neotest_go/nested/n_cases.go @@ -0,0 +1,9 @@ +package main + +func add(a, b int) int { + return a + b +} + +func subtract(a, b int) int { + return a - b +} diff --git a/neotest_go/nested/n_cases_test.go b/neotest_go/nested/n_cases_test.go new file mode 100644 index 0000000..7c39201 --- /dev/null +++ b/neotest_go/nested/n_cases_test.go @@ -0,0 +1,49 @@ +package main + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestSubtract(t *testing.T) { + testCases := []struct { + desc string + a int + b int + want int + }{ + { + desc: "test one", + a: 1, + b: 2, + want: 3, + }, + { + desc: "test two", + a: 1, + b: 2, + want: 7, + }, + } + for _, tC := range testCases { + t.Run(tC.desc, func(t *testing.T) { + assert.Equal(t, tC.want, subtract(tC.a, tC.b)) + }) + } +} + +func TestAdd(t *testing.T) { + t.Run("test one", func(t *testing.T) { + assert.Equal(t, 3, add(1, 2)) + }) + + t.Run("test two", func(t *testing.T) { + assert.Equal(t, 5, add(1, 2)) + }) + + variable := "string" + t.Run(variable, func(t *testing.T) { + assert.Equal(t, 3, add(1, 2)) + }) +}