Skip to content

Commit

Permalink
feat(runners): add ability to pass in function as args
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil committed Sep 18, 2024
1 parent 2d6b5c7 commit 2b266db
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 deletions.
24 changes: 20 additions & 4 deletions lua/neotest-golang/lib/cmd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ end

function M.golist_command()
local cmd = { "go", "list", "-json" }
vim.list_extend(cmd, options.get().go_list_args or {})
local go_list_args = options.get().go_list_args
if type(go_list_args) == "function" then
go_list_args = go_list_args()
end
vim.list_extend(cmd, go_list_args or {})
vim.list_extend(cmd, { "./..." })
return cmd
end
Expand Down Expand Up @@ -81,16 +85,28 @@ 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)
local args = options.get().go_test_args
if type(args) == "function" then
args = args()
end
cmd = vim.list_extend(vim.deepcopy(cmd), 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)
local gotestsum_args = options.get().gotestsum_args
if type(gotestsum_args) == "function" then
gotestsum_args = gotestsum_args()
end
local go_test_args = options.get().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), options.get().go_test_args)
cmd = vim.list_extend(vim.deepcopy(cmd), go_test_args)
cmd = vim.list_extend(vim.deepcopy(cmd), go_test_required_args)
return cmd
end
Expand Down
6 changes: 3 additions & 3 deletions lua/neotest-golang/options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ local logger = require("neotest-golang.logging")
local M = {}

local opts = {
go_test_args = { "-v", "-race", "-count=1" },
go_list_args = {},
go_test_args = { "-v", "-race", "-count=1" }, -- 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" },
gotestsum_args = { "--format=standard-verbose" }, -- NOTE: can also be a function
dev_notifications = false,
}

Expand Down
29 changes: 29 additions & 0 deletions tests/unit/options_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,33 @@ describe("Options are set up", function()
options.setup(expected_options)
assert.are_same(expected_options, options.get())
end)

it("With args as functions", function()
local expected_options = {
go_test_args = function()
return {
"-v",
"-race",
"-count=1",
"-parallel=1",
}
end,
go_list_args = function()
return {}
end,
dap_go_opts = {},
testify_enabled = false,
warn_test_name_dupes = true,
warn_test_not_executed = true,

-- experimental
runner = "go",
gotestsum_args = function()
return { "--format=standard-verbose" }
end,
dev_notifications = false,
}
options.setup(expected_options)
assert.are_same(expected_options, options.get())
end)
end)

0 comments on commit 2b266db

Please sign in to comment.