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

fix(dap): show config error, remove excessive regex characters from test name #141

Merged
merged 2 commits into from
Jul 15, 2024
Merged
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
8 changes: 5 additions & 3 deletions lua/neotest-golang/features/dap/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ local options = require("neotest-golang.options")

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 {}
local dap_go_opts_original = vim.deepcopy(dap_go_opts)
Expand All @@ -19,17 +21,17 @@ function M.setup_debugging(cwd)
end
end

--- @param test_name string
--- @param test_name_regex string
--- @return table | nil
function M.get_dap_config(test_name)
function M.get_dap_config(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 .. "$" },
args = { "-test.run", test_name_regex },
}

return dap_config
Expand Down
24 changes: 18 additions & 6 deletions lua/neotest-golang/runspec/test.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
--- Helpers to build the command and context around running a single test.

local logger = require("neotest-golang.logging")
local lib = require("neotest-golang.lib")
local options = require("neotest-golang.options")
local dap = require("neotest-golang.features.dap")
Expand All @@ -17,19 +18,19 @@ function M.build(pos, strategy)

--- @type string
local test_name = lib.convert.to_gotest_test_name(pos.id)
test_name = lib.convert.to_gotest_regex_pattern(test_name)
local test_name_regex = lib.convert.to_gotest_regex_pattern(test_name)

local test_cmd, json_filepath = lib.cmd.test_command_in_package_with_regexp(
test_folder_absolute_path,
test_name
test_name_regex
)

local runspec_strategy = nil
if strategy == "dap" then
if options.get().dap_go_enabled then
runspec_strategy = dap.get_dap_config(test_name)
dap.setup_debugging(test_folder_absolute_path)
end
M.assert_dap_prerequisites()
runspec_strategy = dap.get_dap_config(test_name_regex)
logger.debug("DAP strategy used: " .. vim.inspect(runspec_strategy))
dap.setup_debugging(test_folder_absolute_path)
end

--- @type RunspecContext
Expand All @@ -56,4 +57,15 @@ function M.build(pos, strategy)
return run_spec
end

function M.assert_dap_prerequisites()
local dap_go_enabled = options.get().dap_go_enabled
local dap_go_found = pcall(require, "dap-go")
if not dap_go_enabled or not dap_go_found then
local msg = "You must set {dap_go_enabled=true} and 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
Loading