From e1a97556d255d4b72f1dde96b90cd4116ef24400 Mon Sep 17 00:00:00 2001 From: Fredrik Averpil Date: Fri, 5 Jul 2024 02:04:43 +0200 Subject: [PATCH] refactor: use lua --- lua/neotest-golang/cmd.lua | 28 +++++++++++----------------- lua/neotest-golang/runspec_file.lua | 5 +++-- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/lua/neotest-golang/cmd.lua b/lua/neotest-golang/cmd.lua index 6b368006..c16968d4 100644 --- a/lua/neotest-golang/cmd.lua +++ b/lua/neotest-golang/cmd.lua @@ -20,25 +20,19 @@ function M.golist_data(cwd) return json.process_golist_output(output) end -function M.sed_regexp(filepath) - if M.system_has("sed") == false then - return nil - end - local sed_command = - string.format("sed -n '/func Test/p' %s", vim.fn.shellescape(filepath)) - local handle = io.popen(sed_command) - local result = nil - if handle ~= nil then - result = handle:read("*a") - handle:close() - end +function M.get_regexp(filepath) + local regexp = nil local lines = {} - for line in string.gmatch(result, "([^\n]*)\n") do - line = string.gsub(line, "func ", "") - line = string.gsub(line, "%(.*", "") - table.insert(lines, line) + 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 - local regexp = "^(" .. table.concat(lines, "|") .. ")$" return regexp end diff --git a/lua/neotest-golang/runspec_file.lua b/lua/neotest-golang/runspec_file.lua index 3e64159b..f52d5cb3 100644 --- a/lua/neotest-golang/runspec_file.lua +++ b/lua/neotest-golang/runspec_file.lua @@ -36,16 +36,17 @@ function M.build(pos, tree) end end - -- use sed to find all top-level tests in pos.path + -- find all top-level tests in pos.path local test_cmd = nil local json_filepath = nil - local regexp = cmd.sed_regexp(pos.path) + local regexp = cmd.get_regexp(pos.path) if regexp ~= nil then test_cmd, json_filepath = cmd.test_command_in_package_with_regexp(package_name, regexp) else -- fallback: run all tests in the package test_cmd, json_filepath = cmd.test_command_in_package(package_name) + -- NOTE: could also fall back to running on a per-test basis by using a bare return end --- @type RunspecContext