Skip to content

Commit

Permalink
fix(dap): show config error, remove excessive regex characters from t…
Browse files Browse the repository at this point in the history
…est name (#141)
  • Loading branch information
fredrikaverpil authored Jul 15, 2024
1 parent f0ec2c6 commit 91dabb0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
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

0 comments on commit 91dabb0

Please sign in to comment.