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

fix: escape regex characters in test name #96

Merged
merged 2 commits into from
Jul 5, 2024
Merged
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
16 changes: 0 additions & 16 deletions lua/neotest-golang/cmd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,6 @@ function M.golist_data(cwd)
return json.process_golist_output(output)
end

function M.get_regexp(filepath)
local regexp = nil
local lines = {}
for line in io.lines(filepath) do
if line:match("func Test") then
line = line:gsub("func ", "")
line = line:gsub("%(.*", "")
table.insert(lines, line)
end
end
if #lines > 0 then
regexp = "^(" .. table.concat(lines, "|") .. ")$"
end
return regexp
end

function M.test_command_in_package(package_or_path)
local go_test_required_args = { package_or_path }
local cmd, json_filepath = M.test_command(go_test_required_args)
Expand Down
19 changes: 18 additions & 1 deletion lua/neotest-golang/runspec_file.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
--- Helpers to build the command and context around running all tests of a file.

local cmd = require("neotest-golang.cmd")
local convert = require("neotest-golang.convert")
local runspec_dir = require("neotest-golang.runspec_dir")

local M = {}
Expand Down Expand Up @@ -39,7 +40,7 @@ function M.build(pos, tree)
-- find all top-level tests in pos.path
local test_cmd = nil
local json_filepath = nil
local regexp = cmd.get_regexp(pos.path)
local regexp = M.get_regexp(pos.path)
if regexp ~= nil then
test_cmd, json_filepath =
cmd.test_command_in_package_with_regexp(package_name, regexp)
Expand Down Expand Up @@ -86,4 +87,20 @@ function M.fail_fast(pos)
return run_spec
end

function M.get_regexp(filepath)
local regexp = nil
local lines = {}
for line in io.lines(filepath) do
if line:match("func Test") then
line = line:gsub("func ", "")
line = line:gsub("%(.*", "")
table.insert(lines, convert.to_gotest_regex_pattern(line))
end
end
if #lines > 0 then
regexp = "^(" .. table.concat(lines, "|") .. ")$"
end
return regexp
end

return M
Loading