From 9c199758c8c1d8f109d6c95e35b741db1501343b Mon Sep 17 00:00:00 2001 From: Fredrik Averpil Date: Tue, 17 Sep 2024 21:35:12 +0200 Subject: [PATCH] refactor(runners): allow for custom runners --- lua/neotest-golang/lib/cmd.lua | 25 +---------------- lua/neotest-golang/options.lua | 50 ++++++++++++++++++++++++++++++---- 2 files changed, 46 insertions(+), 29 deletions(-) diff --git a/lua/neotest-golang/lib/cmd.lua b/lua/neotest-golang/lib/cmd.lua index a1e31c3..c177ce3 100644 --- a/lua/neotest-golang/lib/cmd.lua +++ b/lua/neotest-golang/lib/cmd.lua @@ -67,34 +67,11 @@ function M.test_command(go_test_required_args) --- @type table local cmd = {} - if runner == "go" then - cmd = M.go_test(go_test_required_args) - elseif runner == "gotestsum" then - json_filepath = vim.fs.normalize(async.fn.tempname()) - cmd = M.gotestsum(go_test_required_args, json_filepath) - end - + cmd, json_filepath = options.get().runners[runner].cmd(go_test_required_args) logger.info("Test command: " .. table.concat(cmd, " ")) - return cmd, json_filepath end -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.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.runner_fallback(executable) if M.system_has(executable) == false then options.set({ runner = "go" }) diff --git a/lua/neotest-golang/options.lua b/lua/neotest-golang/options.lua index f94f87d..a76247b 100644 --- a/lua/neotest-golang/options.lua +++ b/lua/neotest-golang/options.lua @@ -3,23 +3,63 @@ --- details and examples. local logger = require("neotest-golang.logging") +local async = require("neotest.async") local M = {} -local opts = { - go_test_args = { "-v", "-race", "-count=1" }, - go_list_args = {}, +local defaults = { + runner = "go", -- corresponds to a key in the 'runners' table + go_test_args = { "-v", "-race", "-count=1" }, -- NOTE: can also be a function + gotestsum_args = { "--format=standard-verbose" }, -- NOTE: can also be a function + go_list_args = {}, -- NOTE: can also be a function dap_go_opts = {}, testify_enabled = false, warn_test_name_dupes = true, warn_test_not_executed = true, -- experimental, for now undocumented, options - runner = "go", -- or "gotestsum" - gotestsum_args = { "--format=standard-verbose" }, dev_notifications = false, } +local default_runners = { + runners = { + go = { + cmd = function(required_args) + local cmd = { "go", "test", "-json" } + local go_test_args = defaults.go_test_args + if type(go_test_args) == "function" then + go_test_args = go_test_args() + end + cmd = vim.list_extend(vim.deepcopy(cmd), go_test_args) + cmd = vim.list_extend(vim.deepcopy(cmd), required_args) + return cmd, nil + end, + }, + gotestsum = { + cmd = function(required_args) + local json_filepath = vim.fs.normalize(async.fn.tempname()) + local cmd = { "gotestsum", "--jsonfile=" .. json_filepath } + local gotestsum_args = defaults.gotestsum_args + if type(gotestsum_args) == "function" then + gotestsum_args = gotestsum_args() + end + local gotest_args = defaults.go_test_args + local go_test_args = defaults.go_test_args + if type(go_test_args) == "function" then + go_test_args = go_test_args() + end + cmd = vim.list_extend(vim.deepcopy(cmd), gotestsum_args) + cmd = vim.list_extend(vim.deepcopy(cmd), { "--" }) + cmd = vim.list_extend(vim.deepcopy(cmd), gotest_args) + cmd = vim.list_extend(vim.deepcopy(cmd), required_args) + return cmd, json_filepath + end, + }, + }, +} + +local opts = vim.tbl_extend("force", defaults, default_runners) + function M.setup(user_opts) if type(user_opts) == "table" and not vim.tbl_isempty(user_opts) then for k, v in pairs(user_opts) do