Skip to content

Commit

Permalink
refactor: target package for 'file' position
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil committed Jul 4, 2024
1 parent 7960fcc commit 085a444
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 74 deletions.
45 changes: 4 additions & 41 deletions lua/neotest-golang/cmd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,51 +21,14 @@ function M.golist_data(cwd)
return json.process_golist_output(output)
end

function M.gotest_list_data(go_mod_folderpath, module_name)
local cmd = { "go", "test", "-v", "-json", "-list", "^Test", module_name }
local output = vim.fn.system(
"cd " .. go_mod_folderpath .. " && " .. table.concat(cmd, " ")
)

-- FIXME: weird... would've expected to call process_gotest_output
local json_output = json.process_golist_output(output)

--- @type string[]
local test_names = {}
for _, v in ipairs(json_output) do
if v.Action == "output" then
--- @type string
local test_name = string.gsub(v.Output, "\n", "")
if string.match(test_name, "^Test") then
test_names = vim.list_extend(
test_names,
{ convert.to_gotest_regex_pattern(test_name) }
)
end
end
end

return test_names
end

function M.test_command_for_dir(module_name)
local go_test_required_args = { module_name }
function M.test_command_in_package(package_or_path)
local go_test_required_args = { package_or_path }
local cmd, json_filepath = M.test_command(go_test_required_args)
return cmd, json_filepath
end

function M.test_command_for_file(module_name, test_names_regexp)
local go_test_required_args = { "-run", test_names_regexp, module_name }
local cmd, json_filepath = M.test_command(go_test_required_args)

return cmd, json_filepath
end

function M.test_command_for_individual_test(
test_folder_absolute_path,
test_name
)
local go_test_required_args = { test_folder_absolute_path, "-run", test_name }
function M.test_command_in_package_with_regexp(package_or_path, regexp)
local go_test_required_args = { package_or_path, "-run", regexp }
local cmd, json_filepath = M.test_command(go_test_required_args)
return cmd, json_filepath
end
Expand Down
12 changes: 6 additions & 6 deletions lua/neotest-golang/runspec_dir.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--- Helpers to build the command and context around running all tests of
--- a Go module.
--- a Go package.

local cmd = require("neotest-golang.cmd")

Expand All @@ -24,16 +24,16 @@ function M.build(pos)
local go_mod_folderpath = vim.fn.fnamemodify(go_mod_filepath, ":h")
local golist_data = cmd.golist_data(go_mod_folderpath)

-- find the go module that corresponds to the go_mod_folderpath
local module_name = "./..." -- if no go module, run all tests at the $CWD
-- find the go package that corresponds to the go_mod_folderpath
local package_name = "./..."
for _, golist_item in ipairs(golist_data) do
if pos.path == golist_item.Dir then
module_name = golist_item.ImportPath
package_name = golist_item.ImportPath
break
end
end

local test_cmd, json_filepath = cmd.test_command_for_dir(module_name)
local test_cmd, json_filepath = cmd.test_command_in_package(package_name)

--- @type RunspecContext
local context = {
Expand All @@ -56,7 +56,7 @@ end

function M.fail_fast(pos)
local msg = "The selected folder must contain a go.mod file "
.. "or be a subdirectory of a Go module."
.. "or be a subdirectory of a Go package."
vim.notify(msg, vim.log.levels.ERROR)

--- @type RunspecContext
Expand Down
39 changes: 14 additions & 25 deletions lua/neotest-golang/runspec_file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,21 @@ function M.build(pos, tree)
local go_mod_folderpath = vim.fn.fnamemodify(go_mod_filepath, ":h")
local golist_data = cmd.golist_data(go_mod_folderpath)

-- find the go module that corresponds to the go_mod_folderpath
local module_name = "./..." -- if no go module, run all tests at the $CWD
-- find the go package that corresponds to the pos.path
local package_name = "./..."
local pos_path_filename = vim.fn.fnamemodify(pos.path, ":t")
for _, golist_item in ipairs(golist_data) do
if golist_item.TestGoFiles ~= nil then
if vim.tbl_contains(golist_item.TestGoFiles, pos_path_filename) then
package_name = golist_item.ImportPath
break
end
end
end

-- TODO: provide regexp to only include tests found in file

local test_names_regexp =
M.find_tests_in_file(pos, golist_data, go_mod_folderpath, module_name)
local test_cmd, json_filepath =
cmd.test_command_for_file(module_name, test_names_regexp)
local test_cmd, json_filepath = cmd.test_command_in_package(package_name)

--- @type RunspecContext
local context = {
Expand Down Expand Up @@ -70,23 +78,4 @@ function M.fail_fast(pos)
return run_spec
end

function M.find_tests_in_file(pos, golist_data, go_mod_folderpath, module_name)
local pos_path_filename = vim.fn.fnamemodify(pos.path, ":t")

for _, golist_item in ipairs(golist_data) do
if golist_item.TestGoFiles ~= nil then
if vim.tbl_contains(golist_item.TestGoFiles, pos_path_filename) then
module_name = golist_item.ImportPath
break
end
end
end

-- FIXME: this grabs all test files from the package. We only want the one in the file.
local test_names = cmd.gotest_list_data(go_mod_folderpath, module_name)
local test_names_regexp = "^(" .. table.concat(test_names, "|") .. ")$"

return test_names_regexp
end

return M
6 changes: 4 additions & 2 deletions lua/neotest-golang/runspec_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ function M.build(pos, strategy)
local test_name = convert.to_gotest_test_name(pos.id)
test_name = convert.to_gotest_regex_pattern(test_name)

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

local runspec_strategy = nil
if strategy == "dap" then
Expand Down

0 comments on commit 085a444

Please sign in to comment.