Skip to content

Commit

Permalink
refactor: adapter options
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil committed Jun 17, 2024
1 parent 70d98ee commit 706ff6f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 46 deletions.
2 changes: 1 addition & 1 deletion lua/neotest-golang/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ end
--- Adapter options.
setmetatable(M.Adapter, {
__call = function(_, opts)
return options.setup(opts)
M.Adapter.options = options.setup(opts)
end,
})

Expand Down
66 changes: 29 additions & 37 deletions lua/neotest-golang/options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,42 @@
--- providing them as arguments to the Adapter function. See the README for mode
--- details and examples.

local M = {}
local Opts = {}

--- Arguments to pass into `go test`. Will be combined with arguments required
--- for neotest-golang to work and execute the expected test(s).
--- @type table
M._go_test_args = {
"-v",
"-race",
"-count=1",
"-timeout=60s",
}
--- Create a new options object.
function Opts:new(opts)
self.go_test_args = opts.go_test_args
or {
"-v",
"-race",
"-count=1",
"-timeout=60s",
}
self.dap_go_enabled = opts.dap_go_enabled or false
self.dap_go_opts = opts.dap_go_opts or {}
end

--- Whether to enable nvim-dap-go.
--- @type boolean
M._dap_go_enabled = false
--- A convenience function to get the current options.
function Opts:get()
return {
go_test_args = self.go_test_args,
dap_go_enabled = self.dap_go_enabled,
dap_go_opts = self.dap_go_opts,
}
end

--- Options to pass into dap-go.setup.
--- @type table
M._dap_go_opts = {}
local M = {}

--- Option setup function. This is what you call when setting up the adapter.
--- @param opts table
--- Set up the adapter.
function M.setup(opts)
opts = opts or {}
if opts.args or opts.dap_go_args then
-- temporary warning
vim.notify(
"Please update your config, the arguments/opts have changed for neotest-golang.",
vim.log.levels.WARN
)
end
if opts.go_test_args then
if opts.go_test_args then
M._go_test_args = opts.go_test_args
end
if opts.dap_go_enabled then
M._dap_go_enabled = opts.dap_go_enabled
if opts.dap_go_opts then
M._dap_go_opts = opts.dap_go_opts
end
end
end
Opts:new(opts)
return M.options
end

return M.Adapter
--- Get the adapter configuration.
function M.get()
return Opts:get()
end

return M
8 changes: 5 additions & 3 deletions lua/neotest-golang/runspec_dir.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,14 @@ function M.build_dir_test_runspec(pos, cwd, test_pattern)
}

--- @type table
local go_test_args = {
local required_go_test_args = {
test_pattern,
}

local combined_args =
vim.list_extend(vim.deepcopy(options._go_test_args), go_test_args)
local combined_args = vim.list_extend(
vim.deepcopy(options.get().go_test_args),
required_go_test_args
)
local gotest_command = vim.list_extend(vim.deepcopy(gotest), combined_args)

--- @type neotest.RunSpec
Expand Down
12 changes: 7 additions & 5 deletions lua/neotest-golang/runspec_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ function M.build(pos, strategy)
}

--- @type table
local go_test_args = {
local required_go_test_args = {
test_folder_absolute_path,
"-run",
"^" .. test_name .. "$",
}

local combined_args =
vim.list_extend(vim.deepcopy(options._go_test_args), go_test_args)
local combined_args = vim.list_extend(
vim.deepcopy(options.get().go_test_args),
required_go_test_args
)
local gotest_command = vim.list_extend(vim.deepcopy(gotest), combined_args)

--- @type neotest.RunSpec
Expand All @@ -47,8 +49,8 @@ function M.build(pos, strategy)
run_spec.context.skip = true -- do not attempt to parse test output

-- nvim-dap and nvim-dap-go cwd
if options._dap_go_enabled then
local dap_go_opts = options._dap_go_opts or {}
if options.get().dap_go_enabled then
local dap_go_opts = options.get().dap_go_opts or {}
local dap_go_opts_original = vim.deepcopy(dap_go_opts)
if dap_go_opts.delve == nil then
dap_go_opts.delve = {}
Expand Down

0 comments on commit 706ff6f

Please sign in to comment.