Open fredrikaverpil opened 4 months ago
For anyone having the same issue, as a workaround I wrote a function that leverages Treesitter to get the nearest function name, then I add -run=xxx
as extra arg to a dedicate keymap. It only works when the cursor is within a function definition.
local function get_nearest_function_name()
local ts_utils = require("nvim-treesitter.ts_utils")
local node = ts_utils.get_node_at_cursor()
while node do
if node:type() == "function_declaration" then
return ts_utils.get_node_text(node:child(1))[1]
end
node = node:parent()
end
end
vim.keymap.set({"n", "<leader>tf",
function()
local name = get_nearest_function_name()
if not name then
return
end
require("neotest").run.run({
extra_args = { "-run", name ) },
})
end,
desc = "Test nearest function",
})
When I "run nearest test" (
require("neotest").run.run()
) when positioning my cursor on the test function, I can see how neotest-go executes all tests.Inspecting the
go test
command generated by neotest-goI inspected the
go test
command produced by printing thecommand
variable value ininit.lua
:...which gave me this command:
Note that I'm adding
coverprofile
myself here.I would've expected something different, such as having added the
-run
flag togo test
along with a regex matching the test name to the command. For example:go test -run ^Test_Level_1$
to the command.Inspecting the
tests
map returned bymarshal_gotest_output.tests
I then inspected the value of the
tests
returned frommarshal_gotest_output
which again shows how a bunch of tests in other files are being executed.