diff --git a/lua/neotest-golang/cmd.lua b/lua/neotest-golang/cmd.lua index 11465cc9..1f5d8c73 100644 --- a/lua/neotest-golang/cmd.lua +++ b/lua/neotest-golang/cmd.lua @@ -1,5 +1,7 @@ --- Helper functions building the command to execute. +local async = require("neotest.async") + local options = require("neotest-golang.options") local M = {} @@ -17,6 +19,56 @@ function M.build_golist_cmd(cwd) return go_list_command_result end +function M.build_test_command_for_individual_test(cwd, test_name) + --- The runner to use for running tests. + --- @type string + local runner = options.get().runner + + -- TODO: if gotestsum, check if it is on $PATH, or fall back onto `go test` + + --- The filepath to write test output JSON to, if using `gotestsum`. + --- @type string | nil + local json_filepath = nil + + --- The final test command to execute. + --- @type table + local test_cmd = {} + + if runner == "go" then + test_cmd = M.build_gotest_cmd_for_test(cwd, test_name) + elseif runner == "gotestsum" then + json_filepath = vim.fs.normalize(async.fn.tempname()) + test_cmd = M.build_gotestsum_cmd_for_test(cwd, test_name, json_filepath) + end + + return test_cmd, json_filepath +end + +function M.build_test_command_for_dir(module_name) + --- The runner to use for running tests. + --- @type string + local runner = options.get().runner + + -- TODO: if gotestsum, check if it is on $PATH, or fall back onto `go test` + + --- The filepath to write test output JSON to, if using `gotestsum`. + --- @type string | nil + local json_filepath = nil + + --- The final test command to execute. + --- @type table + local test_cmd = {} + + if runner == "go" then + test_cmd = M.build_gotest_cmd_for_dir(module_name) + elseif runner == "gotestsum" then + json_filepath = vim.fs.normalize(async.fn.tempname()) + test_cmd = M.build_gotestsum_cmd_for_dir(module_name, json_filepath) + end + + return test_cmd, json_filepath +end + function M.build_gotest_cmd_for_dir(module_name) local gotest = { "go", diff --git a/lua/neotest-golang/parse.lua b/lua/neotest-golang/parse.lua index 5180e769..b5ba00ca 100644 --- a/lua/neotest-golang/parse.lua +++ b/lua/neotest-golang/parse.lua @@ -43,12 +43,16 @@ function M.results(spec, result, tree) --- @type neotest.Position local pos = tree:data() + --- The runner to use for running tests. + --- @type string + local runner = options.get().runner + --- The raw output from the test command. --- @type table local raw_output = {} - if options.get().runner == "go" then + if runner == "go" then raw_output = async.fn.readfile(result.output) - elseif options.get().runner == "gotestsum" then + elseif runner == "gotestsum" then raw_output = async.fn.readfile(spec.context.jsonfile) end diff --git a/lua/neotest-golang/runspec_dir.lua b/lua/neotest-golang/runspec_dir.lua index bfd5b771..cf9d46a5 100644 --- a/lua/neotest-golang/runspec_dir.lua +++ b/lua/neotest-golang/runspec_dir.lua @@ -50,26 +50,7 @@ function M.build(pos) end end - --- The runner to use for running tests. - --- @type string - local runner = options.get().runner - - -- TODO: if gotestsum, check if it is on $PATH, or fall back onto `go test` - - --- The filepath to write test output JSON to, if using `gotestsum`. - --- @type string | nil - local json_filepath = nil - - --- The final test command to execute. - --- @type table - local test_cmd = {} - - if runner == "go" then - test_cmd = cmd.build_gotest_cmd_for_dir(module_name) - elseif runner == "gotestsum" then - json_filepath = vim.fs.normalize(async.fn.tempname()) - test_cmd = cmd.build_gotestsum_cmd_for_dir(module_name, json_filepath) - end + local test_cmd, json_filepath = cmd.build_test_command_for_dir(module_name) return M.build_runspec( pos, @@ -127,6 +108,7 @@ end --- @param cwd string --- @param test_cmd table --- @param golist_output table +--- @param json_filepath string | nil --- @return neotest.RunSpec | neotest.RunSpec[] | nil function M.build_runspec(pos, cwd, test_cmd, golist_output, json_filepath) --- @type neotest.RunSpec diff --git a/lua/neotest-golang/runspec_test.lua b/lua/neotest-golang/runspec_test.lua index 7240c890..3bff38b0 100644 --- a/lua/neotest-golang/runspec_test.lua +++ b/lua/neotest-golang/runspec_test.lua @@ -23,31 +23,10 @@ function M.build(pos, strategy) local test_name = convert.to_gotest_test_name(pos.id) test_name = convert.to_gotest_regex_pattern(test_name) - --- The runner to use for running tests. - --- @type string - local runner = options.get().runner - - -- TODO: if gotestsum, check if it is on $PATH, or fall back onto `go test` - - --- The filepath to write test output JSON to, if using `gotestsum`. - --- @type string | nil - local json_filepath = nil - - --- The final test command to execute. - --- @type table - local test_cmd = {} - - if runner == "go" then - test_cmd = - cmd.build_gotest_cmd_for_test(test_folder_absolute_path, test_name) - elseif runner == "gotestsum" then - json_filepath = vim.fs.normalize(async.fn.tempname()) - test_cmd = cmd.build_gotestsum_cmd_for_test( - test_folder_absolute_path, - test_name, - json_filepath - ) - end + local test_cmd, json_filepath = cmd.build_test_command_for_individual_test( + test_folder_absolute_path, + test_name + ) local runspec_strategy = nil if strategy == "dap" then