From 6a7b06439f0ba7806eaa30f1624689a7097d946e Mon Sep 17 00:00:00 2001 From: Fredrik Averpil Date: Mon, 9 Sep 2024 00:05:03 +0200 Subject: [PATCH] refactor: make context errors optional --- lua/neotest-golang/process.lua | 19 +++++++++++-------- lua/neotest-golang/runspec/dir.lua | 7 +++++-- lua/neotest-golang/runspec/file.lua | 9 ++++++--- lua/neotest-golang/runspec/namespace.lua | 8 ++++++-- lua/neotest-golang/runspec/test.lua | 9 ++++++--- 5 files changed, 34 insertions(+), 18 deletions(-) diff --git a/lua/neotest-golang/process.lua b/lua/neotest-golang/process.lua index e78eb95a..a34258b0 100644 --- a/lua/neotest-golang/process.lua +++ b/lua/neotest-golang/process.lua @@ -10,8 +10,8 @@ local lib = require("neotest-golang.lib") --- @class RunspecContext --- @field pos_id string Neotest tree position id. --- @field golist_data table The 'go list' JSON data (lua table). ---- @field errors table Error messages. ---- @field parse_test_results boolean If true, parsing of test output will occur. +--- @field errors? table 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 @@ -41,14 +41,14 @@ 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. @@ -56,13 +56,13 @@ function M.test_results(spec, result, tree) 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) @@ -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 diff --git a/lua/neotest-golang/runspec/dir.lua b/lua/neotest-golang/runspec/dir.lua index 0a1a7c3c..fe5dee23 100644 --- a/lua/neotest-golang/runspec/dir.lua +++ b/lua/neotest-golang/runspec/dir.lua @@ -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 @@ -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, } diff --git a/lua/neotest-golang/runspec/file.lua b/lua/neotest-golang/runspec/file.lua index 434b9c5e..b3890f88 100644 --- a/lua/neotest-golang/runspec/file.lua +++ b/lua/neotest-golang/runspec/file.lua @@ -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 @@ -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, } @@ -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. diff --git a/lua/neotest-golang/runspec/namespace.lua b/lua/neotest-golang/runspec/namespace.lua index 7ae8bbd4..59e3d856 100644 --- a/lua/neotest-golang/runspec/namespace.lua +++ b/lua/neotest-golang/runspec/namespace.lua @@ -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 = {} @@ -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 @@ -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, } diff --git a/lua/neotest-golang/runspec/test.lua b/lua/neotest-golang/runspec/test.lua index 2f15133e..df0e4584 100644 --- a/lua/neotest-golang/runspec/test.lua +++ b/lua/neotest-golang/runspec/test.lua @@ -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 @@ -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, } @@ -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 })