Skip to content

Commit

Permalink
refactor: make context errors optional
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil committed Sep 8, 2024
1 parent 8384547 commit 6a7b064
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 18 deletions.
19 changes: 11 additions & 8 deletions lua/neotest-golang/process.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ local lib = require("neotest-golang.lib")
--- @class RunspecContext
--- @field pos_id string Neotest tree position id.
--- @field golist_data table<string, string> The 'go list' JSON data (lua table).
--- @field errors table<string> Error messages.
--- @field parse_test_results boolean If true, parsing of test output will occur.
--- @field errors? table<string> Error messages.
--- @field process_test_results boolean If true, parsing of test output will occur.
--- @field test_output_json_filepath? string Gotestsum JSON filepath.

--- @class TestData
Expand Down Expand Up @@ -41,28 +41,28 @@ function M.test_results(spec, result, tree)
--- Test command (e.g. 'go test') status.
--- @type neotest.ResultStatus
local result_status = nil
if context.parse_test_results == false then
result_status = "skipped"
elseif #context.errors > 0 then
if context.errors ~= nil and #context.errors > 0 then
result_status = "failed"
elseif result.code == 0 then
result_status = "passed"
else
elseif result.code > 0 then
result_status = "failed"
else
result_status = "skipped"
end

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

-- return early...
if context.parse_test_results == false then
if context.process_test_results == false then
-- if we're not supposed to parse test results.
neotest_result[context.pos_id] = {
status = result_status,
}
return neotest_result
elseif #context.errors > 0 then
elseif context.errors ~= nil and #context.errors > 0 then
-- if there are errors passed with the context.
local test_command_output_path = vim.fs.normalize(async.fn.tempname())
async.fn.writefile(context.errors, test_command_output_path)
Expand Down Expand Up @@ -129,6 +129,9 @@ function M.test_results(spec, result, tree)
end
local cmd_output_path = vim.fs.normalize(async.fn.tempname())
async.fn.writefile(cmd_output, cmd_output_path)
if neotest_result[pos.id] == nil then
neotest_result[pos.id] = {}
end
neotest_result[pos.id].status = result_status
neotest_result[pos.id].output = cmd_output_path

Expand Down
7 changes: 5 additions & 2 deletions lua/neotest-golang/runspec/dir.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ function M.build(pos)
local go_mod_folderpath = vim.fn.fnamemodify(go_mod_filepath, ":h")
local golist_data, golist_error = lib.cmd.golist_data(go_mod_folderpath)

local errors = {}
local errors = nil
if golist_error ~= nil then
if errors == nil then
errors = {}
end
table.insert(errors, golist_error)
end

Expand All @@ -51,7 +54,7 @@ function M.build(pos)
pos_id = pos.id,
golist_data = golist_data,
errors = errors,
parse_test_results = true,
process_test_results = true,
test_output_json_filepath = json_filepath,
}

Expand Down
9 changes: 6 additions & 3 deletions lua/neotest-golang/runspec/file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ function M.build(pos, tree)
local go_mod_folderpath = vim.fn.fnamemodify(go_mod_filepath, ":h")
local golist_data, golist_error = lib.cmd.golist_data(go_mod_folderpath)

local errors = {}
local errors = nil
if golist_error ~= nil then
if errors == nil then
errors = {}
end
table.insert(errors, golist_error)
end

Expand Down Expand Up @@ -65,7 +68,7 @@ function M.build(pos, tree)
pos_id = pos.id,
golist_data = golist_data,
errors = errors,
parse_test_results = true,
process_test_results = true,
test_output_json_filepath = json_filepath,
}

Expand All @@ -85,7 +88,7 @@ function M.return_skipped(pos)
local context = {
pos_id = pos.id,
golist_data = {}, -- no golist output
parse_test_results = false,
process_test_results = false,
}

--- Runspec designed for files that contain no tests.
Expand Down
8 changes: 6 additions & 2 deletions lua/neotest-golang/runspec/namespace.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
--- Helpers to build the command and context around running all tests in a namespace.

local logger = require("neotest-golang.logging")
local lib = require("neotest-golang.lib")

local M = {}
Expand All @@ -14,8 +15,11 @@ function M.build(pos)
local golist_data, golist_error =
lib.cmd.golist_data(test_folder_absolute_path)

local errors = {}
local errors = nil
if golist_error ~= nil then
if errors == nil then
errors = {}
end
table.insert(errors, golist_error)
end

Expand All @@ -32,7 +36,7 @@ function M.build(pos)
pos_id = pos.id,
golist_data = golist_data,
errors = errors,
parse_test_results = true,
process_test_results = true,
test_output_json_filepath = json_filepath,
}

Expand Down
9 changes: 6 additions & 3 deletions lua/neotest-golang/runspec/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ function M.build(pos, strategy)
local golist_data, golist_error =
lib.cmd.golist_data(test_folder_absolute_path)

local errors = {}
local errors = nil
if golist_error ~= nil then
if errors == nil then
errors = {}
end
table.insert(errors, golist_error)
end

Expand All @@ -43,7 +46,7 @@ function M.build(pos, strategy)
pos_id = pos.id,
golist_data = golist_data,
errors = errors,
parse_test_results = true,
process_test_results = true,
test_output_json_filepath = json_filepath,
}

Expand All @@ -56,7 +59,7 @@ function M.build(pos, strategy)

if runspec_strategy ~= nil then
run_spec.strategy = runspec_strategy
run_spec.context.parse_test_results = false
run_spec.context.process_test_results = false
end

logger.debug({ "RunSpec:", run_spec })
Expand Down

0 comments on commit 6a7b064

Please sign in to comment.