Skip to content

Commit

Permalink
refactor: options and defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil committed Jun 18, 2024
1 parent ee6cd4c commit d03a1ca
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 41 deletions.
2 changes: 1 addition & 1 deletion lua/neotest-golang/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,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 Opts:get()
end

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

return M
6 changes: 3 additions & 3 deletions lua/neotest-golang/runspec_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function M.build(pos, strategy)
}

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

--- @type neotest.RunSpec
Expand All @@ -47,8 +47,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
34 changes: 34 additions & 0 deletions tests/unit/options_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
local options = require("neotest-golang.options")

describe("Options is setup", function()
it("With defaults", function()
local expected_options = {
dap_go_enabled = false,
dap_go_opts = {},
go_test_args = {
"-v",
"-race",
"-count=1",
"-timeout=60s",
},
}
options.setup()
assert.are_same(expected_options, options.get())
end)

it("With non-defaults", function()
local expected_options = {
dap_go_enabled = false,
dap_go_opts = {},
go_test_args = {
"-v",
"-race",
"-count=1",
"-parallel=1",
"-timeout=60s",
},
}
options.setup(expected_options)
assert.are_same(expected_options, options.get())
end)
end)

0 comments on commit d03a1ca

Please sign in to comment.