From a9042b6a601c4123c9f84de5df113cd46735dac3 Mon Sep 17 00:00:00 2001 From: Fredrik Averpil Date: Fri, 5 Jul 2024 08:13:27 +0200 Subject: [PATCH] fix: escape regex characters in test name (#96) --- lua/neotest-golang/cmd.lua | 16 ---------------- lua/neotest-golang/runspec_file.lua | 19 ++++++++++++++++++- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/lua/neotest-golang/cmd.lua b/lua/neotest-golang/cmd.lua index c16968d4..2839f3da 100644 --- a/lua/neotest-golang/cmd.lua +++ b/lua/neotest-golang/cmd.lua @@ -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) diff --git a/lua/neotest-golang/runspec_file.lua b/lua/neotest-golang/runspec_file.lua index f52d5cb3..219dbfe2 100644 --- a/lua/neotest-golang/runspec_file.lua +++ b/lua/neotest-golang/runspec_file.lua @@ -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 = {} @@ -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) @@ -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