Skip to content

Commit

Permalink
feat: using DAP without nvim-dap-go
Browse files Browse the repository at this point in the history
  • Loading branch information
HeavyPunk committed Nov 9, 2024
1 parent df6e3f3 commit 3f37e73
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 45 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Reliable Neotest adapter for running Go tests in Neovim.
- DAP support with [leoluz/nvim-dap-go](https://github.com/leoluz/nvim-dap-go)
integration for debugging of tests using
[delve](https://github.com/go-delve/delve).
- DAP support for your own DAP configuration.
- Monorepo support (detect, run and debug tests in sub-projects).
- Inline diagnostics.
- Custom `go test` argument support.
Expand Down
68 changes: 68 additions & 0 deletions lua/neotest-golang/features/dap/dap_go.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
--- DAP (dap-go) setup related functions.

local options = require("neotest-golang.options")
local logger = require("neotest-golang.logging")

local M = {}

---This will prepare and setup nvim-dap-go for debugging.
---@param cwd string
function M.setup_debugging(cwd)
local dap_go_opts = options.get().dap_go_opts or {}
if type(dap_go_opts) == "function" then
dap_go_opts = dap_go_opts()
end
local dap_go_opts_original = vim.deepcopy(dap_go_opts)
if dap_go_opts.delve == nil then
dap_go_opts.delve = {}
end
dap_go_opts.delve.cwd = cwd
logger.debug({ "Provided dap_go_opts for DAP: ", dap_go_opts })
require("dap-go").setup(dap_go_opts)

-- reset nvim-dap-go (and cwd) after debugging with nvim-dap
require("dap").listeners.after.event_terminated["neotest-golang-debug"] = function()
logger.debug({
"Resetting provided dap_go_opts for DAP: ",
dap_go_opts_original,
})
require("dap-go").setup(dap_go_opts_original)
end
end

--- @param test_path string
--- @param test_name_regex string?
--- @return table | nil
function M.get_dap_config(test_path, test_name_regex)
-- :help dap-configuration
local dap_config = {
type = "go",
name = "Neotest-golang",
request = "launch",
mode = "test",
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
end

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
45 changes: 45 additions & 0 deletions lua/neotest-golang/features/dap/dap_manual.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
--- DAP (manual dap configuration) setup related functions.

local options = require("neotest-golang.options")
local logger = require("neotest-golang.logging")

local dap = require("dap")

local M = {}

---@param cwd string
function M.setup_debugging(cwd)
local dap_manual_configuration = options.get().dap_manual_configuration or {}
if type(dap_manual_configuration) == "function" then
dap_manual_configuration = dap_manual_configuration()
end

dap_manual_configuration.cwd = cwd
end

---This will setup a dap configuration to run tests
---@param test_path string
---@param test_name_regex string?
---@return table | nil
function M.get_dap_config(test_path, test_name_regex)
local dap_manual_configuration = options.get().dap_manual_configuration or {}
if type(dap_manual_configuration) == "function" then
dap_manual_configuration = dap_manual_configuration()
end

dap_manual_configuration.program = test_path

if test_name_regex ~= nil then
dap_manual_configuration.args = dap_manual_configuration.args or {}
table.insert(dap_manual_configuration.args, "-test.run")
table.insert(dap_manual_configuration.args, test_name_regex)
end
return dap_manual_configuration
end

---Dummy function is needed to be corresponding to dap-go setup (just like trait implementation)
function M.assert_dap_prerequisites()
logger.debug("Nothing to check. Manual DAP configuration in use")
end

return M
67 changes: 22 additions & 45 deletions lua/neotest-golang/features/dap/init.lua
Original file line number Diff line number Diff line change
@@ -1,67 +1,44 @@
--- DAP setup related functions.

local options = require("neotest-golang.options")
local logger = require("neotest-golang.logging")
local dap_manual = require("neotest-golang.features.dap.dap_manual")
local dap_go = require("neotest-golang.features.dap.dap_go")

local M = {}

---This will prepare and setup nvim-dap-go for debugging.
---@param cwd string
function M.setup_debugging(cwd)
local dap_go_opts = options.get().dap_go_opts or {}
if type(dap_go_opts) == "function" then
dap_go_opts = dap_go_opts()
end
local dap_go_opts_original = vim.deepcopy(dap_go_opts)
if dap_go_opts.delve == nil then
dap_go_opts.delve = {}
local function is_dap_manual_enabled()
local dap_manual_enabled = options.get().dap_manual_enabled
if type(dap_manual_enabled) == "function" then
dap_manual_enabled = dap_manual_enabled()
end
dap_go_opts.delve.cwd = cwd
logger.debug({ "Provided dap_go_opts for DAP: ", dap_go_opts })
require("dap-go").setup(dap_go_opts)
return dap_manual_enabled
end

-- reset nvim-dap-go (and cwd) after debugging with nvim-dap
require("dap").listeners.after.event_terminated["neotest-golang-debug"] = function()
logger.debug({
"Resetting provided dap_go_opts for DAP: ",
dap_go_opts_original,
})
require("dap-go").setup(dap_go_opts_original)
---@param cwd string
function M.setup_debugging(cwd)
if is_dap_manual_enabled() then
dap_manual.setup_debugging(cwd)
else
dap_go.setup_debugging(cwd)
end
end

--- @param test_path string
--- @param test_name_regex string?
--- @return table | nil
function M.get_dap_config(test_path, test_name_regex)
-- :help dap-configuration
local dap_config = {
type = "go",
name = "Neotest-golang",
request = "launch",
mode = "test",
program = test_path,
}

if test_name_regex ~= nil then
dap_config.args = { "-test.run", test_name_regex }
if is_dap_manual_enabled() then
return dap_manual.get_dap_config(test_path, test_name_regex)
else
return dap_go.get_dap_config(test_path, 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
end

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)
if is_dap_manual_enabled() then
dap_manual.assert_dap_prerequisites()
else
dap_go.assert_dap_prerequisites()
end
end

Expand Down
2 changes: 2 additions & 0 deletions lua/neotest-golang/options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ local opts = {
gotestsum_args = { "--format=standard-verbose" }, -- NOTE: can also be a function
go_list_args = {}, -- NOTE: can also be a function
dap_go_opts = {}, -- NOTE: can also be a function
dap_manual_enabled = false, -- NOTE: can alse be a function
dap_manual_configuration = {}, -- NOTE: can also be a function
testify_enabled = false,
colorize_test_output = true,
warn_test_name_dupes = true,
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/options_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ describe("Options are set up", function()
go_list_args = {},
gotestsum_args = { "--format=standard-verbose" },
dap_go_opts = {},
dap_manual_enabled = false,
dap_manual_configuration = {},
testify_enabled = false,
colorize_test_output = true,
warn_test_name_dupes = true,
Expand All @@ -37,6 +39,8 @@ describe("Options are set up", function()
go_list_args = {},
gotestsum_args = { "--format=standard-verbose" },
dap_go_opts = {},
dap_manual_enabled = false,
dap_manual_configuration = {},
testify_enabled = false,
colorize_test_output = false,
warn_test_name_dupes = true,
Expand Down Expand Up @@ -65,6 +69,12 @@ describe("Options are set up", function()
dap_go_opts = function()
return {}
end,
dap_manual_enabled = function()
return false
end,
dap_manual_configuration = function()
return {}
end,
testify_enabled = false,
colorize_test_output = true,
warn_test_name_dupes = true,
Expand Down

0 comments on commit 3f37e73

Please sign in to comment.