diff --git a/lua/neotest-golang/options.lua b/lua/neotest-golang/options.lua index 9e8f7f21..84053aa7 100644 --- a/lua/neotest-golang/options.lua +++ b/lua/neotest-golang/options.lua @@ -5,11 +5,9 @@ local M = {} local opts = { - go_test_args = { - "-v", - "-race", - "-count=1", - }, + runner = "go", -- or "gotestsum" + go_test_args = { "-v", "-race", "-count=1" }, + gotestsum_args = { "--format=standard-verbose" }, dap_go_enabled = false, dap_go_opts = {}, warn_test_name_dupes = true, diff --git a/lua/neotest-golang/parse.lua b/lua/neotest-golang/parse.lua index 5fe03fb4..5180e769 100644 --- a/lua/neotest-golang/parse.lua +++ b/lua/neotest-golang/parse.lua @@ -43,9 +43,14 @@ function M.results(spec, result, tree) --- @type neotest.Position local pos = tree:data() - --- The raw output from the 'go test -json' command. + --- The raw output from the test command. --- @type table - local raw_output = async.fn.readfile(result.output) + local raw_output = {} + if options.get().runner == "go" then + raw_output = async.fn.readfile(result.output) + elseif options.get().runner == "gotestsum" then + raw_output = async.fn.readfile(spec.context.jsonfile) + end --- The 'go test' JSON output, converted into a lua table. --- @type table diff --git a/lua/neotest-golang/runspec_dir.lua b/lua/neotest-golang/runspec_dir.lua index 7e8974d9..515e5c33 100644 --- a/lua/neotest-golang/runspec_dir.lua +++ b/lua/neotest-golang/runspec_dir.lua @@ -3,6 +3,7 @@ local options = require("neotest-golang.options") local json = require("neotest-golang.json") +local async = require("neotest.async") local M = {} @@ -114,22 +115,45 @@ end --- @param module_name string --- @return neotest.RunSpec | neotest.RunSpec[] | nil function M.build_dir_test_runspec(pos, cwd, golist_output, module_name) - local gotest = { - "go", - "test", - "-json", - } - --- @type table local required_go_test_args = { module_name, } - local combined_args = vim.list_extend( - vim.deepcopy(options.get().go_test_args), - required_go_test_args - ) - local gotest_command = vim.list_extend(vim.deepcopy(gotest), combined_args) + local gotest_command = {} + local jsonfile = "" + + if options.get().runner == "go" then + local gotest = { + "go", + "test", + "-json", + } + + local combined_args = vim.list_extend( + vim.deepcopy(options.get().go_test_args), + required_go_test_args + ) + gotest_command = vim.list_extend(vim.deepcopy(gotest), combined_args) + elseif options.get().runner == "gotestsum" then + jsonfile = vim.fs.normalize(async.fn.tempname()) + local gotest = { "gotestsum" } + local gotestsum_json = { + "--jsonfile=" .. jsonfile, + "--", + } + local gotest_args = vim.list_extend( + vim.deepcopy(options.get().go_test_args), + required_go_test_args + ) + + local cmd = + vim.list_extend(vim.deepcopy(gotest), options.get().gotestsum_args) + cmd = vim.list_extend(vim.deepcopy(cmd), gotestsum_json) + cmd = vim.list_extend(vim.deepcopy(cmd), gotest_args) + + gotest_command = cmd + end --- @type neotest.RunSpec local run_spec = { @@ -143,6 +167,10 @@ function M.build_dir_test_runspec(pos, cwd, golist_output, module_name) }, } + if jsonfile ~= nil then + run_spec.context.jsonfile = jsonfile + end + return run_spec end diff --git a/lua/neotest-golang/runspec_test.lua b/lua/neotest-golang/runspec_test.lua index 0fdeacad..f922b957 100644 --- a/lua/neotest-golang/runspec_test.lua +++ b/lua/neotest-golang/runspec_test.lua @@ -3,6 +3,7 @@ local convert = require("neotest-golang.convert") local options = require("neotest-golang.options") local json = require("neotest-golang.json") +local async = require("neotest.async") local M = {} @@ -33,20 +34,43 @@ 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 gotest = { - "go", - "test", - "-json", - } - --- @type table local required_go_test_args = { test_folder_absolute_path, "-run", test_name } - local combined_args = vim.list_extend( - vim.deepcopy(options.get().go_test_args), - required_go_test_args - ) - local gotest_command = vim.list_extend(vim.deepcopy(gotest), combined_args) + local gotest_command = {} + local jsonfile = "" + + if options.get().runner == "go" then + local gotest = { + "go", + "test", + "-json", + } + + local combined_args = vim.list_extend( + vim.deepcopy(options.get().go_test_args), + required_go_test_args + ) + gotest_command = vim.list_extend(vim.deepcopy(gotest), combined_args) + elseif options.get().runner == "gotestsum" then + jsonfile = vim.fs.normalize(async.fn.tempname()) + local gotest = { "gotestsum" } + local gotestsum_json = { + "--jsonfile=" .. jsonfile, + "--", + } + local gotest_args = vim.list_extend( + vim.deepcopy(options.get().go_test_args), + required_go_test_args + ) + + local cmd = + vim.list_extend(vim.deepcopy(gotest), options.get().gotestsum_args) + cmd = vim.list_extend(vim.deepcopy(cmd), gotestsum_json) + cmd = vim.list_extend(vim.deepcopy(cmd), gotest_args) + + gotest_command = cmd + end --- @type neotest.RunSpec local run_spec = { @@ -60,6 +84,10 @@ function M.build(pos, strategy) }, } + if jsonfile ~= nil then + run_spec.context.jsonfile = jsonfile + end + -- set up for debugging of test if strategy == "dap" then run_spec.strategy = M.get_dap_config(test_name) diff --git a/tests/unit/options_spec.lua b/tests/unit/options_spec.lua index 9ca4ca35..acf3d502 100644 --- a/tests/unit/options_spec.lua +++ b/tests/unit/options_spec.lua @@ -5,11 +5,13 @@ describe("Options are set up", function() local expected_options = { dap_go_enabled = false, dap_go_opts = {}, + runner = "go", go_test_args = { "-v", "-race", "-count=1", }, + gotestsum_args = { "--format=standard-verbose" }, warn_test_name_dupes = true, warn_test_not_executed = true, dev_notifications = false, @@ -22,12 +24,14 @@ describe("Options are set up", function() local expected_options = { dap_go_enabled = false, dap_go_opts = {}, + runner = "go", go_test_args = { "-v", "-race", "-count=1", "-parallel=1", -- non-default }, + gotestsum_args = { "--format=standard-verbose" }, warn_test_name_dupes = true, warn_test_not_executed = true, dev_notifications = false,