From a98ff707fb4ad7e0d2fb838e45ffffb10486a97e Mon Sep 17 00:00:00 2001 From: Lukas Hoehl Date: Sun, 13 Oct 2024 20:18:30 +0200 Subject: [PATCH 1/2] fix(dap): use test filepath from neotest positions Signed-off-by: Lukas Hoehl --- lua/neotest-golang/features/dap/init.lua | 5 +++-- lua/neotest-golang/runspec/test.lua | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lua/neotest-golang/features/dap/init.lua b/lua/neotest-golang/features/dap/init.lua index bebc5f13..55e2670d 100644 --- a/lua/neotest-golang/features/dap/init.lua +++ b/lua/neotest-golang/features/dap/init.lua @@ -30,17 +30,18 @@ function M.setup_debugging(cwd) end end +--- @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, } local dap_go_opts = options.get().dap_go_opts or {} diff --git a/lua/neotest-golang/runspec/test.lua b/lua/neotest-golang/runspec/test.lua index 1831178d..17df70f5 100644 --- a/lua/neotest-golang/runspec/test.lua +++ b/lua/neotest-golang/runspec/test.lua @@ -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) @@ -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 From 8b96f54042ffad0fa8de0ff07510e9ecc69b3ac4 Mon Sep 17 00:00:00 2001 From: Lukas Hoehl Date: Sun, 13 Oct 2024 21:22:38 +0200 Subject: [PATCH 2/2] feat: debug files Signed-off-by: Lukas Hoehl --- lua/neotest-golang/features/dap/init.lua | 17 +++++++++++++++-- lua/neotest-golang/init.lua | 4 +++- lua/neotest-golang/runspec/file.lua | 21 ++++++++++++++++++--- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/lua/neotest-golang/features/dap/init.lua b/lua/neotest-golang/features/dap/init.lua index 55e2670d..f6e36537 100644 --- a/lua/neotest-golang/features/dap/init.lua +++ b/lua/neotest-golang/features/dap/init.lua @@ -31,7 +31,7 @@ function M.setup_debugging(cwd) end --- @param test_path string ---- @param test_name_regex string +--- @param test_name_regex string? --- @return table | nil function M.get_dap_config(test_path, test_name_regex) -- :help dap-configuration @@ -40,10 +40,13 @@ function M.get_dap_config(test_path, test_name_regex) name = "Neotest-golang", request = "launch", mode = "test", - 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 @@ -52,4 +55,14 @@ function M.get_dap_config(test_path, 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 diff --git a/lua/neotest-golang/init.lua b/lua/neotest-golang/init.lua index fae98ed5..c762a53d 100644 --- a/lua/neotest-golang/init.lua +++ b/lua/neotest-golang/init.lua @@ -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": @@ -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. diff --git a/lua/neotest-golang/runspec/file.lua b/lua/neotest-golang/runspec/file.lua index be9a7957..ef57269d 100644 --- a/lua/neotest-golang/runspec/file.lua +++ b/lua/neotest-golang/runspec/file.lua @@ -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 @@ -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 @@ -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, @@ -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