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

Adds dap support #13

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
20 changes: 20 additions & 0 deletions lua/neotest-go/init.lua
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = {
Expand Down
62 changes: 62 additions & 0 deletions lua/neotest-go/utils.lua
Original file line number Diff line number Diff line change
@@ -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
42 changes: 42 additions & 0 deletions tests/unit/utils_spec.lua
Original file line number Diff line number Diff line change
@@ -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)