Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dap): debug files #203

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions lua/neotest-golang/features/dap/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,23 @@ function M.setup_debugging(cwd)
end
end

--- @param test_name_regex string
--- @param test_path string
--- @param test_name_regex string?
--- @return table | nil
function M.get_dap_config(test_name_regex)
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 = "${fileDirname}",
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
Expand All @@ -51,4 +55,14 @@ function M.get_dap_config(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
4 changes: 3 additions & 1 deletion lua/neotest-golang/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down Expand Up @@ -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.
Expand Down
21 changes: 18 additions & 3 deletions lua/neotest-golang/runspec/file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lua/neotest-golang/runspec/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ local M = {}
--- @param strategy string
--- @return neotest.RunSpec | neotest.RunSpec[] | nil
function M.build(pos, strategy)
local test_folder_absolute_path =
string.match(pos.path, "(.+)" .. lib.find.os_path_sep)
local pos_path_foldername = vim.fn.fnamemodify(pos.path, ":h")
local test_folder_absolute_path = pos_path_foldername

local golist_data, golist_error =
lib.cmd.golist_data(test_folder_absolute_path)
Expand All @@ -36,7 +36,7 @@ function M.build(pos, strategy)
local runspec_strategy = nil
if strategy == "dap" then
M.assert_dap_prerequisites()
runspec_strategy = dap.get_dap_config(test_name_regex)
runspec_strategy = dap.get_dap_config(pos_path_foldername, test_name_regex)
logger.debug("DAP strategy used: " .. vim.inspect(runspec_strategy))
dap.setup_debugging(test_folder_absolute_path)
end
Expand Down
Loading