From bad873ef0b528178b150c0ca22cfe58bf4151ef6 Mon Sep 17 00:00:00 2001 From: Fredrik Averpil Date: Wed, 3 Jul 2024 23:29:04 +0200 Subject: [PATCH] refactor: cmds --- lua/neotest-golang/cmd.lua | 157 +++++++---------------------- lua/neotest-golang/runspec_dir.lua | 1 - 2 files changed, 36 insertions(+), 122 deletions(-) diff --git a/lua/neotest-golang/cmd.lua b/lua/neotest-golang/cmd.lua index 219a63a5..c8655395 100644 --- a/lua/neotest-golang/cmd.lua +++ b/lua/neotest-golang/cmd.lua @@ -20,43 +20,22 @@ function M.golist_output(cwd) return json.process_golist_output(output) end -function M.fallback_to_go_test(executable) - if vim.fn.executable(executable) == 0 then - vim.notify( - "Runner " .. executable .. " not found. Falling back to 'go'.", - vim.log.levels.WARN - ) - options.set({ runner = "go" }) - return options.get().runner - end - return options.get().runner +function M.test_command_for_individual_test( + test_folder_absolute_path, + test_name +) + local go_test_required_args = { test_folder_absolute_path, "-run", test_name } + local cmd, json_filepath = M.test_command(go_test_required_args) + return cmd, json_filepath end -function M.test_command_for_individual_test(cwd, test_name) - --- The runner to use for running tests. - --- @type string - local runner = M.fallback_to_go_test(options.get().runner) - - --- 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.gotest_with_args_for_individual_test(cwd, test_name) - elseif runner == "gotestsum" then - json_filepath = vim.fs.normalize(async.fn.tempname()) - test_cmd = - M.gotestsum_with_args_for_individual_test(cwd, test_name, json_filepath) - end - - return test_cmd, json_filepath +function M.test_command_for_dir(module_name) + local go_test_required_args = { module_name } + local cmd, json_filepath = M.test_command(go_test_required_args) + return cmd, json_filepath end -function M.test_command_for_dir(module_name) +function M.test_command(go_test_required_args) --- The runner to use for running tests. --- @type string local runner = M.fallback_to_go_test(options.get().runner) @@ -67,108 +46,44 @@ function M.test_command_for_dir(module_name) --- The final test command to execute. --- @type table - local test_cmd = {} + local cmd = {} if runner == "go" then - test_cmd = M.gotest_with_args_for_dir(module_name) + cmd = M.go_test(go_test_required_args) elseif runner == "gotestsum" then json_filepath = vim.fs.normalize(async.fn.tempname()) - test_cmd = M.gotestsum_with_args_cmd_for_dir(module_name, json_filepath) + cmd = M.gotestsum(go_test_required_args, json_filepath) end - return test_cmd, json_filepath + return cmd, json_filepath end -function M.gotest_with_args_for_dir(module_name) - local gotest = { - "go", - "test", - "-json", - } - - 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 cmd = vim.list_extend(vim.deepcopy(gotest), combined_args) - - return cmd -end - -function M.gotestsum_with_args_cmd_for_dir(module_name, json_filepath) - local gotest = { "gotestsum" } - local gotestsum_json = { - "--jsonfile=" .. json_filepath, - "--", - } - - local required_go_test_args = { - module_name, - } - - 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) - +function M.go_test(go_test_required_args) + local cmd = { "go", "test", "-json" } + cmd = vim.list_extend(vim.deepcopy(cmd), options.get().go_test_args) + cmd = vim.list_extend(vim.deepcopy(cmd), go_test_required_args) return cmd end -function M.gotest_with_args_for_individual_test( - test_folder_absolute_path, - test_name -) - --- @type table - local required_go_test_args = { test_folder_absolute_path, "-run", test_name } - - local gotest = { - "go", - "test", - "-json", - } - - local combined_args = vim.list_extend( - vim.deepcopy(options.get().go_test_args), - required_go_test_args - ) - local cmd = vim.list_extend(vim.deepcopy(gotest), combined_args) +function M.gotestsum(go_test_required_args, json_filepath) + local cmd = { "gotestsum", "--jsonfile=" .. json_filepath } + cmd = vim.list_extend(vim.deepcopy(cmd), options.get().gotestsum_args) + cmd = vim.list_extend(vim.deepcopy(cmd), { "--" }) + cmd = vim.list_extend(vim.deepcopy(cmd), options.get().go_test_args) + cmd = vim.list_extend(vim.deepcopy(cmd), go_test_required_args) return cmd end -function M.gotestsum_with_args_for_individual_test( - test_folder_absolute_path, - test_name, - json_filepath -) - --- @type table - local required_go_test_args = { test_folder_absolute_path, "-run", test_name } - - local gotest = { "gotestsum" } - local gotestsum_json = { - "--jsonfile=" .. json_filepath, - "--", - } - - 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) - - return cmd +function M.fallback_to_go_test(executable) + if vim.fn.executable(executable) == 0 then + vim.notify( + "Runner " .. executable .. " not found. Falling back to 'go'.", + vim.log.levels.WARN + ) + options.set({ runner = "go" }) + return options.get().runner + end + return options.get().runner end return M diff --git a/lua/neotest-golang/runspec_dir.lua b/lua/neotest-golang/runspec_dir.lua index de9e8c0e..0cd72099 100644 --- a/lua/neotest-golang/runspec_dir.lua +++ b/lua/neotest-golang/runspec_dir.lua @@ -1,7 +1,6 @@ --- Helpers to build the command and context around running all tests of --- a Go module. -local json = require("neotest-golang.json") local cmd = require("neotest-golang.cmd") local M = {}