Skip to content

Commit

Permalink
refactor: use lua
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil committed Jul 5, 2024
1 parent e844b8f commit e1a9755
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
28 changes: 11 additions & 17 deletions lua/neotest-golang/cmd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions lua/neotest-golang/runspec_file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e1a9755

Please sign in to comment.