From 20c270ac048586b1677470fbf33ddfd84b3ed314 Mon Sep 17 00:00:00 2001 From: Fredrik Averpil Date: Tue, 18 Jun 2024 10:08:09 +0200 Subject: [PATCH] refactor: options and defaults --- lua/neotest-golang/init.lua | 2 +- lua/neotest-golang/options.lua | 66 +++++++++++++---------------- lua/neotest-golang/runspec_test.lua | 6 +-- tests/unit/options_spec.lua | 34 +++++++++++++++ 4 files changed, 67 insertions(+), 41 deletions(-) create mode 100644 tests/unit/options_spec.lua diff --git a/lua/neotest-golang/init.lua b/lua/neotest-golang/init.lua index 0531a406..6989b639 100644 --- a/lua/neotest-golang/init.lua +++ b/lua/neotest-golang/init.lua @@ -154,7 +154,7 @@ end --- Adapter options. setmetatable(M.Adapter, { __call = function(_, opts) - return options.setup(opts) + M.Adapter.options = options.setup(opts) end, }) diff --git a/lua/neotest-golang/options.lua b/lua/neotest-golang/options.lua index 943252c5..c865cbe7 100644 --- a/lua/neotest-golang/options.lua +++ b/lua/neotest-golang/options.lua @@ -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 diff --git a/lua/neotest-golang/runspec_test.lua b/lua/neotest-golang/runspec_test.lua index 84de5e47..64578b5e 100644 --- a/lua/neotest-golang/runspec_test.lua +++ b/lua/neotest-golang/runspec_test.lua @@ -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 @@ -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 = {} diff --git a/tests/unit/options_spec.lua b/tests/unit/options_spec.lua new file mode 100644 index 00000000..8a85c102 --- /dev/null +++ b/tests/unit/options_spec.lua @@ -0,0 +1,34 @@ +local options = require("neotest-golang.options") + +describe("Options are set up", 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)