Skip to content

Commit

Permalink
perf: more efficient 'go list' $cwd (#246)
Browse files Browse the repository at this point in the history
The 'go list' command used to be executed in the same $cwd as the go.mod
file. But after this change, the $cwd will be set to the parent folder
of the selected file - or the same directory as the selected directory.
  • Loading branch information
fredrikaverpil authored Jan 2, 2025
1 parent ee3f462 commit 770dd49
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lua/neotest-golang/runspec/dir.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function M.build(pos)
end

local go_mod_folderpath = vim.fn.fnamemodify(go_mod_filepath, ":h")
local golist_data, golist_error = lib.cmd.golist_data(go_mod_folderpath)
local golist_data, golist_error = lib.cmd.golist_data(pos.path)

local errors = nil
if golist_error ~= nil then
Expand Down
12 changes: 6 additions & 6 deletions lua/neotest-golang/runspec/file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ function M.build(pos, tree, strategy)
end

local go_mod_folderpath = vim.fn.fnamemodify(go_mod_filepath, ":h")
local golist_data, golist_error = lib.cmd.golist_data(go_mod_folderpath)
local pos_path_folderpath = vim.fn.fnamemodify(pos.path, ":h")
local golist_data, golist_error = lib.cmd.golist_data(pos_path_folderpath)

local errors = nil
if golist_error ~= nil then
Expand All @@ -38,12 +39,11 @@ function M.build(pos, tree, strategy)
-- find the go package that corresponds to the pos.path
local package_name = "./..."
local pos_path_filename = vim.fn.fnamemodify(pos.path, ":t")
local pos_path_foldername = vim.fn.fnamemodify(pos.path, ":h")

for _, golist_item in ipairs(golist_data) do
if golist_item.TestGoFiles ~= nil then
if
pos_path_foldername == golist_item.Dir
pos_path_folderpath == golist_item.Dir
and vim.tbl_contains(golist_item.TestGoFiles, pos_path_filename)
then
package_name = golist_item.ImportPath
Expand All @@ -53,7 +53,7 @@ function M.build(pos, tree, strategy)
if golist_item.XTestGoFiles ~= nil then
-- NOTE: XTestGoFiles are test files that are part of a [packagename]_test package.
if
pos_path_foldername == golist_item.Dir
pos_path_folderpath == golist_item.Dir
and vim.tbl_contains(golist_item.XTestGoFiles, pos_path_filename)
then
package_name = golist_item.ImportPath
Expand All @@ -78,9 +78,9 @@ function M.build(pos, tree, strategy)
local runspec_strategy = nil
if strategy == "dap" then
dap.assert_dap_prerequisites()
runspec_strategy = dap.get_dap_config(pos_path_foldername, regexp)
runspec_strategy = dap.get_dap_config(pos_path_folderpath, regexp)
logger.debug("DAP strategy used: " .. vim.inspect(runspec_strategy))
dap.setup_debugging(pos_path_foldername)
dap.setup_debugging(pos_path_folderpath)
end

--- @type RunspecContext
Expand Down
13 changes: 5 additions & 8 deletions lua/neotest-golang/runspec/namespace.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ local M = {}
--- @param pos neotest.Position
--- @return neotest.RunSpec | neotest.RunSpec[] | nil
function M.build(pos)
local test_folder_absolute_path =
local pos_path_folderpath =
string.match(pos.path, "(.+)" .. lib.find.os_path_sep)

local golist_data, golist_error =
lib.cmd.golist_data(test_folder_absolute_path)
local golist_data, golist_error = lib.cmd.golist_data(pos_path_folderpath)

local errors = nil
if golist_error ~= nil then
Expand All @@ -26,10 +25,8 @@ function M.build(pos)
local test_name = lib.convert.to_gotest_test_name(pos.id)
test_name = lib.convert.to_gotest_regex_pattern(test_name)

local test_cmd, json_filepath = lib.cmd.test_command_in_package_with_regexp(
test_folder_absolute_path,
test_name
)
local test_cmd, json_filepath =
lib.cmd.test_command_in_package_with_regexp(pos_path_folderpath, test_name)

--- @type RunspecContext
local context = {
Expand All @@ -42,7 +39,7 @@ function M.build(pos)
--- @type neotest.RunSpec
local run_spec = {
command = test_cmd,
cwd = test_folder_absolute_path,
cwd = pos_path_folderpath,
context = context,
}

Expand Down
14 changes: 6 additions & 8 deletions lua/neotest-golang/runspec/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ local M = {}
--- @param strategy string
--- @return neotest.RunSpec | neotest.RunSpec[] | nil
function M.build(pos, strategy)
local pos_path_foldername = vim.fn.fnamemodify(pos.path, ":h")
local test_folder_absolute_path = pos_path_foldername
local pos_path_folderpath = vim.fn.fnamemodify(pos.path, ":h")

local golist_data, golist_error =
lib.cmd.golist_data(test_folder_absolute_path)
local golist_data, golist_error = lib.cmd.golist_data(pos_path_folderpath)

local errors = nil
if golist_error ~= nil then
Expand All @@ -29,16 +27,16 @@ function M.build(pos, strategy)
local test_name_regex = lib.convert.to_gotest_regex_pattern(test_name)

local test_cmd, json_filepath = lib.cmd.test_command_in_package_with_regexp(
test_folder_absolute_path,
pos_path_folderpath,
test_name_regex
)

local runspec_strategy = nil
if strategy == "dap" then
dap.assert_dap_prerequisites()
runspec_strategy = dap.get_dap_config(pos_path_foldername, test_name_regex)
runspec_strategy = dap.get_dap_config(pos_path_folderpath, test_name_regex)
logger.debug("DAP strategy used: " .. vim.inspect(runspec_strategy))
dap.setup_debugging(test_folder_absolute_path)
dap.setup_debugging(pos_path_folderpath)
end

--- @type RunspecContext
Expand All @@ -53,7 +51,7 @@ function M.build(pos, strategy)
--- @type neotest.RunSpec
local run_spec = {
command = test_cmd,
cwd = test_folder_absolute_path,
cwd = pos_path_folderpath,
context = context,
}

Expand Down

0 comments on commit 770dd49

Please sign in to comment.