diff --git a/lua/neotest-go/init.lua b/lua/neotest-go/init.lua index 0af99ec..b0fff1d 100644 --- a/lua/neotest-go/init.lua +++ b/lua/neotest-go/init.lua @@ -1,6 +1,7 @@ local async = require('neotest.async') local Path = require('plenary.path') local lib = require('neotest.lib') +local utils = require('neotest-go.utils') local api = vim.api local fn = vim.fn @@ -249,10 +250,15 @@ local function get_prefix(tree, name) return parent_name .. '/' .. name end +local get_function_name = function(args, position) + return get_prefix(args.tree, position.name) +end + ---@async ---@param args neotest.RunArgs ---@return neotest.RunSpec function adapter.build_spec(args) + local strategy = args.strategy local results_path = async.fn.tempname() local position = args.tree:data() local dir = position.path @@ -281,6 +287,20 @@ function adapter.build_spec(args) unpack(cmd_args), }) + if strategy == 'dap' then + local func_name = get_function_name(args, position) + local debug_args = utils.get_test_function_debug_args(func_name) + local config = utils.get_strategy_config(strategy, debug_args) + return { + command = table.concat(command, ' '), + context = { + results_path = results_path, + file = position.path, + }, + strategy = config, + } + end + return { command = table.concat(command, ' '), context = { diff --git a/lua/neotest-go/utils.lua b/lua/neotest-go/utils.lua new file mode 100644 index 0000000..41e450a --- /dev/null +++ b/lua/neotest-go/utils.lua @@ -0,0 +1,62 @@ +local M = {} + +M.benchmark_regex = '[^Benchmark$|^Benchmark\\P{Ll}.*]' + +---@param strategy string +---@param args table +---@return table | nil +M.get_strategy_config = function(strategy, args) + local config = { + dap = function() + return { + type = 'go', + name = 'Neotest Debugger', + request = 'launch', + mode = 'test', + program = './${relativeFileDirname}', + args = args, + } + end, + } + if config[strategy] then + return config[strategy]() + end + + return nil +end + +--@param test_func_name string +--@return boolean +M.is_benchmark_test = function(test_func_name) + local rg = vim.regex(M.benchmark_regex) + + local match = rg:match_str(test_func_name) + if match == 0 or match == nil then + return false + end + return true +end + +---@param func_name string +---@return table +M.get_test_function_debug_args = function(func_name) + if M.is_benchmark_test(func_name) then + return { + '-test.bench', + '^' .. func_name .. '$', + '-test.run', + 'a^', + } + end + + -- TODO: add testify support + -- see vscode-go implementation at https://github.com/golang/vscode-go/blob/8dfd39349da7a523b4ed0f781c9d10c753be76bf/src/testUtils.ts#L191-L191 + local args = { + '-test.run', + '^' .. func_name .. '$', + } + + return args +end + +return M diff --git a/tests/unit/utils_spec.lua b/tests/unit/utils_spec.lua new file mode 100644 index 0000000..a901449 --- /dev/null +++ b/tests/unit/utils_spec.lua @@ -0,0 +1,42 @@ +local utils = require('neotest-go.utils') + +describe('utils', function() + describe('is_benchmark_test', function() + it('returns false on test functions', function() + is_bench = utils.is_benchmark_test('TestExample') + assert.is.False(is_bench) + end) + + it('returns true on benchmark functions', function() + is_bench = utils.is_benchmark_test('BenchmarkExample') + assert.is.True(is_bench) + end) + + it('returns false on empty function name', function() + is_bench = utils.is_benchmark_test('') + assert.is.False(is_bench) + end) + end) + + describe('get_test_function_debug_args', function() + it('with test function', function() + local args = utils.get_test_function_debug_args('TestExample') + + assert.are.same(args, { + '-test.run', + '^TestExample$', + }) + end) + + it('with benchmark function', function() + local args = utils.get_test_function_debug_args('BenchmarkExample') + + assert.are.same(args, { + '-test.bench', + '^BenchmarkExample$', + '-test.run', + 'a^', + }) + end) + end) +end)