From b4a3ca4841b75102316465d6e0d1408763a2c8f2 Mon Sep 17 00:00:00 2001 From: Fredrik Averpil Date: Sun, 16 Jun 2024 22:00:30 +0200 Subject: [PATCH] fix: incorrect runspec type --- lua/neotest-golang/results_dir.lua | 107 ++++++++++++++-------------- lua/neotest-golang/runspec_dir.lua | 22 ++++-- lua/neotest-golang/runspec_file.lua | 2 +- lua/neotest-golang/runspec_test.lua | 2 +- 4 files changed, 70 insertions(+), 63 deletions(-) diff --git a/lua/neotest-golang/results_dir.lua b/lua/neotest-golang/results_dir.lua index d0f72149..91583a0a 100644 --- a/lua/neotest-golang/results_dir.lua +++ b/lua/neotest-golang/results_dir.lua @@ -26,6 +26,10 @@ local M = {} --- @param tree neotest.Tree --- @return table function M.results(spec, result, tree) + --- The Neotest position tree node for this execution. + --- @type neotest.Position + local pos = tree:data() + --- The raw output from the 'go test -json' command. --- @type table local raw_output = async.fn.readfile(result.output) @@ -34,21 +38,62 @@ function M.results(spec, result, tree) --- @type table local gotest_output = json.process_json(raw_output) + --- @type table + local neotest_result = {} + + --- Test command (e.g. 'go test') status. + --- @type neotest.ResultStatus + local test_command_status = "skipped" + if spec.context.skip == true then + test_command_status = "skipped" + elseif result.code == 0 then + test_command_status = "passed" + else + test_command_status = "failed" + end + + --- Full 'go test' output (parsed from JSON). + --- @type table + local o = {} + local test_command_output_path = vim.fs.normalize(async.fn.tempname()) + for _, line in ipairs(gotest_output) do + if line.Action == "output" then + table.insert(o, line.Output) + end + end + async.fn.writefile(o, test_command_output_path) + + -- register properties on the directory node that was run + neotest_result[pos.id] = { + status = test_command_status, + output = test_command_output_path, + } + + -- if the test execution was skipped, return early + if spec.context.skip == true then + return neotest_result + end + --- Internal data structure to store test result data. --- @type table local res = M.aggregate_data(spec, tree, gotest_output) - M.show_warnings(res) - -- DEBUG: enable the following to see the internal test result data. -- vim.notify(vim.inspect(res), vim.log.levels.DEBUG) - local neotest_results = M.to_neotest_results(spec, result, res, gotest_output) + -- show various warnings + M.show_warnings(res) + + -- Convert internal test result data into final Neotest result. + local test_results = M.to_neotest_result(spec, result, res, gotest_output) + for k, v in pairs(test_results) do + neotest_result[k] = v + end -- DEBUG: enable the following to see the final Neotest result. -- vim.notify(vim.inspect(neotest_results), vim.log.levels.DEBUG) - return neotest_results + return neotest_result end --- Aggregate neotest data and 'go test' output data. @@ -113,7 +158,6 @@ end --- @param gotest_output table --- @return table function M.decorate_with_go_package_and_test_name(spec, res, gotest_output) - -- Look for tests not in the "main" package. for pos_id, test_data in pairs(res) do for _, line in ipairs(gotest_output) do if line.Action == "run" and line.Test ~= nil then @@ -236,68 +280,23 @@ end --- @param res table --- @param gotest_output table --- @return table -function M.to_neotest_results(spec, result, res, gotest_output) +function M.to_neotest_result(spec, result, res, gotest_output) --- Neotest results. --- @type table - local neotest_results = {} + local neotest_result = {} -- populate all test results onto the Neotest format. for pos_id, test_data in pairs(res) do local test_output_path = vim.fs.normalize(async.fn.tempname()) async.fn.writefile(test_data.gotest_data.output, test_output_path) - neotest_results[pos_id] = { + neotest_result[pos_id] = { status = test_data.status, errors = test_data.errors, output = test_output_path, -- NOTE: could be slow when running many tests? } end - neotest_results = - M.decorate_with_command_data(spec, result, gotest_output, neotest_results) - - return neotest_results -end - ---- Decorate the final Neotest results with the data from the test command that ---- was executed. ---- @param spec neotest.RunSpec ---- @param result neotest.StrategyResult ---- @param gotest_output table ---- @param neotest_results table ---- @return table -function M.decorate_with_command_data( - spec, - result, - gotest_output, - neotest_results -) - --- Test command (e.g. 'go test') status. - --- @type neotest.ResultStatus - local test_command_status = "skipped" - if result.code == 0 then - test_command_status = "passed" - else - test_command_status = "failed" - end - - --- Full 'go test' output (parsed from JSON). - --- @type table - local full_output = {} - local test_command_output_path = vim.fs.normalize(async.fn.tempname()) - for _, line in ipairs(gotest_output) do - if line.Action == "output" then - table.insert(full_output, line.Output) - end - end - async.fn.writefile(full_output, test_command_output_path) - - -- register properties on the directory node that was run - neotest_results[spec.context.id] = { - status = test_command_status, - output = test_command_output_path, - } - - return neotest_results + return neotest_result end return M diff --git a/lua/neotest-golang/runspec_dir.lua b/lua/neotest-golang/runspec_dir.lua index d1dc1382..309dadac 100644 --- a/lua/neotest-golang/runspec_dir.lua +++ b/lua/neotest-golang/runspec_dir.lua @@ -12,13 +12,21 @@ local M = {} --- @return neotest.RunSpec | nil function M.build(pos) local go_mod_filepath = M.find_file_upwards("go.mod", pos.path) + + -- if no go.mod file was found up the directory tree, until reaching $CWD, + -- then we cannot determine the Go project root. if go_mod_filepath == nil then - vim.notify( - "The selected folder cannot be correlated to a Go project. " - .. "Will now attempt to run tests on a per-test basis instead.", - vim.log.levels.WARN - ) - return nil -- Deletgates away from the 'dir' position type + local msg = "The selected folder cannot be correlated to a Go project." + vim.notify(msg, vim.log.levels.WARN) + local run_spec = { + command = { "echo", msg }, + context = { + id = pos.id, + skip = true, + pos_type = "dir", + }, + } + return run_spec end local go_mod_folderpath = vim.fn.fnamemodify(go_mod_filepath, ":h") @@ -68,7 +76,7 @@ end --- @param pos neotest.Position --- @param cwd string --- @param test_pattern string ---- @return neotest.RunSpec +--- @return neotest.RunSpec | neotest.RunSpec[] | nil function M.build_dir_test_runspec(pos, cwd, test_pattern) local gotest = { "go", diff --git a/lua/neotest-golang/runspec_file.lua b/lua/neotest-golang/runspec_file.lua index 10c72193..ac4373b7 100644 --- a/lua/neotest-golang/runspec_file.lua +++ b/lua/neotest-golang/runspec_file.lua @@ -5,7 +5,7 @@ local M = {} --- Build runspec for a directory. --- @param pos neotest.Position --- @param tree neotest.Tree ---- @return neotest.RunSpec | nil +--- @return neotest.RunSpec | neotest.RunSpec[] | nil function M.build(pos, tree) if utils.table_is_empty(tree:children()) then --- Runspec designed for files that contain no tests. diff --git a/lua/neotest-golang/runspec_test.lua b/lua/neotest-golang/runspec_test.lua index 0f9ce87f..9ba34012 100644 --- a/lua/neotest-golang/runspec_test.lua +++ b/lua/neotest-golang/runspec_test.lua @@ -6,7 +6,7 @@ local M = {} --- Build runspec for a single test --- @param pos neotest.Position --- @param strategy string ---- @return neotest.RunSpec +--- @return neotest.RunSpec | neotest.RunSpec[] | nil function M.build(pos, strategy) --- @type string local test_name = convert.to_gotest_test_name(pos.id)