Skip to content

Commit

Permalink
fix: escape regex characters in test name (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil authored Jul 5, 2024
1 parent e31de98 commit a9042b6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
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

0 comments on commit a9042b6

Please sign in to comment.