Skip to content

Commit

Permalink
refactor(runners): allow for custom runners
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil committed Sep 17, 2024
1 parent 2d6b5c7 commit b615300
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 33 deletions.
34 changes: 6 additions & 28 deletions lua/neotest-golang/lib/cmd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,45 +54,23 @@ function M.test_command_in_package_with_regexp(package_or_path, regexp)
return cmd, json_filepath
end

function M.test_command(go_test_required_args)
function M.test_command(required_go_test_args)
--- The runner to use for running tests.
--- @type string
local runner = M.runner_fallback(options.get().runner)

--- The filepath to write test output JSON to, if using `gotestsum`.
--- Optional and custom filepath for writing test output.
--- @type string | nil
local json_filepath = nil
local test_output_filepath = nil

--- The final test command to execute.
--- @type table<string>
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, test_output_filepath =
options.get().runners[runner].cmd(required_go_test_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
return cmd, test_output_filepath
end

function M.runner_fallback(executable)
Expand Down
50 changes: 45 additions & 5 deletions lua/neotest-golang/options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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_go_test_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_go_test_args)
return cmd, nil
end,
},
gotestsum = {
cmd = function(required_go_test_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_go_test_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
Expand Down

0 comments on commit b615300

Please sign in to comment.