Skip to content

Commit

Permalink
feat: pass 'go list' errors onto generic test processing
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil committed Sep 8, 2024
1 parent 53cc69f commit 963d6e0
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 14 deletions.
14 changes: 12 additions & 2 deletions lua/neotest-golang/lib/cmd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,21 @@ function M.golist_data(cwd)
local go_list_command_concat = table.concat(cmd, " ")
logger.debug("Running Go list: " .. go_list_command_concat .. " in " .. cwd)
local result = vim.system(cmd, { cwd = cwd, text = true }):wait()

local err = nil
if result.code == 1 then
logger.error({ "execution of 'go list' failed, output:", result.stderr })
err = "go list:"
if result.stdout ~= nil and result.stdout ~= "" then
err = err .. " " .. result.stdout
end
if result.stdout ~= nil and result.stderr ~= "" then
err = err .. " " .. result.stderr
end
logger.debug({ "Go list error: ", err })
end

local output = result.stdout or ""
return json.decode_from_string(output)
return json.decode_from_string(output), err
end

function M.test_command_in_package(package_or_path)
Expand Down
34 changes: 26 additions & 8 deletions lua/neotest-golang/process.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ local lib = require("neotest-golang.lib")
--- @field pos_id string Neotest tree position id.
--- @field pos_type neotest.PositionType Neotest tree position type.
--- @field golist_data table<string, string> Filepath to 'go list' JSON data (lua table). -- TODO: rename to golist_data
--- @field golist_error? string Error message from 'go list' command.
--- @field parse_test_results boolean If true, parsing of test output will occur.
--- @field test_output_json_filepath? string Gotestsum JSON filepath.

Expand Down Expand Up @@ -42,14 +43,35 @@ function M.test_results(spec, result, tree)
--- @type RunspecContext
local context = spec.context

--- Final Neotest results, the way Neotest wants it returned.
--- @type table<string, neotest.Result>
local neotest_result = {}

-- return early if we're not supposed to parse test results
if context.parse_test_results == false then
---@type table<string, neotest.Result>
local results = {}
results[context.pos_id] = {
neotest_result[context.pos_id] = {
---@type neotest.ResultStatus
status = "skipped",
}
return results -- return early, used by e.g. debugging
return neotest_result -- return early, used by e.g. debugging
end

-- handle 'go list' errors and fail/return early
if context.golist_error ~= nil then
local test_command_output_path = vim.fs.normalize(async.fn.tempname())
async.fn.writefile({ context.golist_error }, test_command_output_path)

neotest_result[context.pos_id] = {
status = "failed",
output = test_command_output_path,
errors = {
{
message = context.golist_error,
},
},
}
return neotest_result -- return early when 'go list' fail
end

--- The Neotest position tree node for this execution.
Expand Down Expand Up @@ -79,9 +101,6 @@ function M.test_results(spec, result, tree)
end
logger.debug({ "Raw 'go test' output: ", raw_output })

--- Final Neotest results, the way Neotest wants it returned.
--- @type table<string, neotest.Result>
local neotest_result = {}
--- Test command (e.g. 'go test') status.
--- @type neotest.ResultStatus
local test_command_status = "skipped"
Expand All @@ -99,7 +118,6 @@ function M.test_results(spec, result, tree)
if result.code == 0 then
gotest_output = lib.json.decode_from_table(raw_output, false)
else
-- also capture the output from stderr, such as compilation errors
gotest_output = lib.json.decode_from_table(raw_output, true)
end

Expand Down Expand Up @@ -142,7 +160,7 @@ function M.test_results(spec, result, tree)
elseif runner == "gotestsum" then
-- gotestsum unfortunately does not write the stderr output into its JSON output.
async.fn.writefile(
{ "Failed to run test. Compilation error?" },
{ "Failed to run 'go test'. Compilation error?" },
test_command_output_path
)
else
Expand Down
3 changes: 2 additions & 1 deletion lua/neotest-golang/runspec/dir.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function M.build(pos)
end

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

-- find the go package that corresponds to the go_mod_folderpath
local package_name = "./..."
Expand All @@ -46,6 +46,7 @@ function M.build(pos)
pos_id = pos.id,
pos_type = "dir",
golist_data = golist_data,
golist_error = golist_error,
parse_test_results = true,
test_output_json_filepath = json_filepath,
}
Expand Down
3 changes: 2 additions & 1 deletion lua/neotest-golang/runspec/file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function M.build(pos, tree)
end

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

-- find the go package that corresponds to the pos.path
local package_name = "./..."
Expand Down Expand Up @@ -60,6 +60,7 @@ function M.build(pos, tree)
pos_id = pos.id,
pos_type = "file",
golist_data = golist_data,
golist_error = golist_error,
parse_test_results = true,
test_output_json_filepath = json_filepath,
}
Expand Down
4 changes: 3 additions & 1 deletion lua/neotest-golang/runspec/namespace.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ function M.build(pos)
--- @type string
local test_folder_absolute_path =
string.match(pos.path, "(.+)" .. lib.find.os_path_sep)
local golist_data = lib.cmd.golist_data(test_folder_absolute_path)
local golist_data, golist_error =
lib.cmd.golist_data(test_folder_absolute_path)

--- @type string
local test_name = lib.convert.to_gotest_test_name(pos.id)
Expand All @@ -27,6 +28,7 @@ function M.build(pos)
pos_id = pos.id,
pos_type = "namespace",
golist_data = golist_data,
golist_error = golist_error,
parse_test_results = true,
test_output_json_filepath = json_filepath,
}
Expand Down
4 changes: 3 additions & 1 deletion lua/neotest-golang/runspec/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ function M.build(pos, strategy)
--- @type string
local test_folder_absolute_path =
string.match(pos.path, "(.+)" .. lib.find.os_path_sep)
local golist_data = lib.cmd.golist_data(test_folder_absolute_path)
local golist_data, golist_error =
lib.cmd.golist_data(test_folder_absolute_path)

--- @type string
local test_name = lib.convert.to_gotest_test_name(pos.id)
Expand All @@ -38,6 +39,7 @@ function M.build(pos, strategy)
pos_id = pos.id,
pos_type = "test",
golist_data = golist_data,
golist_error = golist_error,
parse_test_results = true,
test_output_json_filepath = json_filepath,
}
Expand Down

0 comments on commit 963d6e0

Please sign in to comment.