Skip to content

Commit

Permalink
refactor: distinguish between path and package
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil committed Sep 21, 2024
1 parent 130a049 commit 9f3ee54
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 17 deletions.
3 changes: 2 additions & 1 deletion lua/neotest-golang/lib/cmd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ function M.golist_command()
end

--- @class TestCommandData
--- @field package_or_path string The Go package or the full path to test.
--- @field absolute_folder_path string | nil The absolute folder path to the test file.
--- @field package_name string | nil The Go package name.
--- @field position neotest.Position The position of the test.
--- @field regexp string | nil The regular expression to filter tests.

Expand Down
36 changes: 28 additions & 8 deletions lua/neotest-golang/options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,21 @@ local default_runners = {
go_test_args = go_test_args()
end
local required_go_test_args = {}
if cmd_data.regexp ~= nil then
if
cmd_data.position.type == "test"
or cmd_data.position.type == "namespace"
then
required_go_test_args =
{ cmd_data.package_or_path, "-run", cmd_data.regexp }
else
required_go_test_args = { cmd_data.package_or_path }
{ cmd_data.absolute_folder_path, "-run", cmd_data.regexp }
elseif cmd_data.position.type == "file" then
if cmd_data.regexp ~= nil then
required_go_test_args =
{ cmd_data.package_name, "-run", cmd_data.regexp }
else
required_go_test_args = { cmd_data.package_name }
end
elseif cmd_data.position.type == "dir" then
required_go_test_args = { cmd_data.package_name }
end
cmd = vim.list_extend(vim.deepcopy(cmd), go_test_args)
cmd = vim.list_extend(vim.deepcopy(cmd), required_go_test_args)
Expand All @@ -55,11 +65,21 @@ local default_runners = {
go_test_args = go_test_args()
end
local required_go_test_args = {}
if cmd_data.regexp ~= nil then
if
cmd_data.position.type == "test"
or cmd_data.position.type == "namespace"
then
required_go_test_args =
{ cmd_data.package_or_path, "-run", cmd_data.regexp }
else
required_go_test_args = { cmd_data.package_or_path }
{ cmd_data.absolute_folder_path, "-run", cmd_data.regexp }
elseif cmd_data.position.type == "file" then
if cmd_data.regexp ~= nil then
required_go_test_args =
{ cmd_data.package_name, "-run", cmd_data.regexp }
else
required_go_test_args = { cmd_data.package_name }
end
elseif cmd_data.position.type == "dir" then
required_go_test_args = { cmd_data.package_name }
end
cmd = vim.list_extend(vim.deepcopy(cmd), gotestsum_args)
cmd = vim.list_extend(vim.deepcopy(cmd), { "--" })
Expand Down
8 changes: 6 additions & 2 deletions lua/neotest-golang/runspec/dir.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ function M.build(pos)
end
end

local cmd_data =
{ package_or_path = package_name, position = pos, regexp = nil }
local cmd_data = {
package_name = package_name,
position = pos,
regexp = nil,
}

local test_cmd, json_filepath = lib.cmd.test_command(cmd_data)

--- @type RunspecContext
Expand Down
14 changes: 10 additions & 4 deletions lua/neotest-golang/runspec/file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,19 @@ function M.build(pos, tree)
local json_filepath = nil
local regexp = M.get_regexp(pos.path)
if regexp ~= nil then
local cmd_data =
{ package_or_path = package_name, position = pos, regexp = regexp }
local cmd_data = {
package_name = package_name,
position = pos,
regexp = regexp,
}
test_cmd, json_filepath = lib.cmd.test_command(cmd_data)
else
-- fallback: run all tests in the package
local cmd_data =
{ package_or_path = package_name, position = pos, regexp = nil }
local cmd_data = {
package_name = package_name,
position = pos,
regexp = nil,
}
test_cmd, json_filepath =
lib.cmd.test_command_in_package_with_regexp(cmd_data)
-- NOTE: could also fall back to running on a per-test basis by using a bare return
Expand Down
2 changes: 1 addition & 1 deletion lua/neotest-golang/runspec/namespace.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function M.build(pos)
local test_name_regexp = lib.convert.to_gotest_regex_pattern(test_name)

local cmd_data = {
package_or_path = test_folder_absolute_path,
absolute_folder_path = test_folder_absolute_path,
position = pos,
regexp = test_name_regexp,
}
Expand Down
2 changes: 1 addition & 1 deletion lua/neotest-golang/runspec/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function M.build(pos, strategy)
local test_name_regexp = lib.convert.to_gotest_regex_pattern(test_name)

local cmd_data = {
package_or_path = test_folder_absolute_path,
absolute_folder_path = test_folder_absolute_path,
position = pos,
regexp = test_name_regexp,
}
Expand Down

0 comments on commit 9f3ee54

Please sign in to comment.