diff --git a/lua/neotest-golang/features/dap/init.lua b/lua/neotest-golang/features/dap/init.lua index 55e2670d..f6e36537 100644 --- a/lua/neotest-golang/features/dap/init.lua +++ b/lua/neotest-golang/features/dap/init.lua @@ -31,7 +31,7 @@ function M.setup_debugging(cwd) end --- @param test_path string ---- @param test_name_regex string +--- @param test_name_regex string? --- @return table | nil function M.get_dap_config(test_path, test_name_regex) -- :help dap-configuration @@ -40,10 +40,13 @@ function M.get_dap_config(test_path, test_name_regex) name = "Neotest-golang", request = "launch", mode = "test", - args = { "-test.run", test_name_regex }, program = test_path, } + if test_name_regex ~= nil then + dap_config.args = { "-test.run", test_name_regex } + end + local dap_go_opts = options.get().dap_go_opts or {} if dap_go_opts.delve ~= nil and dap_go_opts.delve.build_flags ~= nil then dap_config.buildFlags = dap_go_opts.delve.build_flags @@ -52,4 +55,14 @@ function M.get_dap_config(test_path, test_name_regex) return dap_config end +function M.assert_dap_prerequisites() + local dap_go_found = pcall(require, "dap-go") + if not dap_go_found then + local msg = "You must have leoluz/nvim-dap-go installed to use DAP strategy. " + .. "See the neotest-golang README for more information." + logger.error(msg) + error(msg) + end +end + return M diff --git a/lua/neotest-golang/init.lua b/lua/neotest-golang/init.lua index fae98ed5..c762a53d 100644 --- a/lua/neotest-golang/init.lua +++ b/lua/neotest-golang/init.lua @@ -76,6 +76,8 @@ function M.Adapter.build_spec(args) return end + logger.info({ "Tree:", pos }) + -- Below is the main logic of figuring out how to execute tests. In short, -- a "runspec" is defined for each command to execute. -- Neotest also distinguishes between different "position types": @@ -110,7 +112,7 @@ function M.Adapter.build_spec(args) elseif pos.type == "file" then -- A runspec is to be created, based on on running all tests in the given -- file. - return runspec.file.build(pos, tree) + return runspec.file.build(pos, tree, args.strategy) elseif pos.type == "namespace" then -- A runspec is to be created, based on running all tests in the given -- namespace. diff --git a/lua/neotest-golang/runspec/file.lua b/lua/neotest-golang/runspec/file.lua index be9a7957..ef57269d 100644 --- a/lua/neotest-golang/runspec/file.lua +++ b/lua/neotest-golang/runspec/file.lua @@ -2,14 +2,16 @@ local logger = require("neotest-golang.logging") local lib = require("neotest-golang.lib") +local dap = require("neotest-golang.features.dap") local M = {} ---- Build runspec for a directory. +--- Build runspec for a file. --- @param pos neotest.Position --- @param tree neotest.Tree +--- @param strategy string --- @return neotest.RunSpec | neotest.RunSpec[] | nil -function M.build(pos, tree) +function M.build(pos, tree, strategy) if vim.tbl_isempty(tree:children()) then return M.return_skipped(pos) end @@ -49,7 +51,7 @@ function M.build(pos, tree) end end end - + -- -- find all top-level tests in pos.path local test_cmd = nil local json_filepath = nil @@ -63,6 +65,14 @@ function M.build(pos, tree) -- NOTE: could also fall back to running on a per-test basis by using a bare return end + local runspec_strategy = nil + if strategy == "dap" then + dap.assert_dap_prerequisites() + runspec_strategy = dap.get_dap_config(pos_path_foldername, regexp) + logger.debug("DAP strategy used: " .. vim.inspect(runspec_strategy)) + dap.setup_debugging(pos_path_foldername) + end + --- @type RunspecContext local context = { pos_id = pos.id, @@ -78,6 +88,11 @@ function M.build(pos, tree) context = context, } + if runspec_strategy ~= nil then + run_spec.strategy = runspec_strategy + run_spec.context.is_dap_active = true + end + logger.debug({ "RunSpec:", run_spec }) return run_spec end