From 91dabb01aef5ba3e0e7db86ff9d6dc66c58c65af Mon Sep 17 00:00:00 2001 From: Fredrik Averpil Date: Mon, 15 Jul 2024 21:34:19 +0200 Subject: [PATCH] fix(dap): show config error, remove excessive regex characters from test name (#141) --- lua/neotest-golang/features/dap/init.lua | 8 +++++--- lua/neotest-golang/runspec/test.lua | 24 ++++++++++++++++++------ 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/lua/neotest-golang/features/dap/init.lua b/lua/neotest-golang/features/dap/init.lua index 59d6d80f..8066180f 100644 --- a/lua/neotest-golang/features/dap/init.lua +++ b/lua/neotest-golang/features/dap/init.lua @@ -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) @@ -19,9 +21,9 @@ 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", @@ -29,7 +31,7 @@ function M.get_dap_config(test_name) request = "launch", mode = "test", program = "${fileDirname}", - args = { "-test.run", "^" .. test_name .. "$" }, + args = { "-test.run", test_name_regex }, } return dap_config diff --git a/lua/neotest-golang/runspec/test.lua b/lua/neotest-golang/runspec/test.lua index 8ad0b2df..31d5bdd4 100644 --- a/lua/neotest-golang/runspec/test.lua +++ b/lua/neotest-golang/runspec/test.lua @@ -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") @@ -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 @@ -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