Skip to content

Commit

Permalink
feat: mega hack to make testify tests run
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil committed Jul 6, 2024
1 parent 2e51f6b commit e8e2797
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 7 deletions.
2 changes: 0 additions & 2 deletions lua/neotest-golang/ast.lua
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,6 @@ function M.detect_tests(file_path)
testify.add(file_path, function_name, receiver) -- FIXME: accumulates forever
end

vim.notify(vim.inspect(testify.get()))

return tree_with_merged_namespaces
end

Expand Down
4 changes: 3 additions & 1 deletion lua/neotest-golang/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ function M.Adapter.build_spec(args)
elseif pos.type == "namespace" then
-- A runspec is to be created, based on running all tests in the given
-- namespace.
return runspec_namespace.build(pos)

-- return runspec_namespace.build(pos)
return -- delegate to type 'test'
elseif pos.type == "test" then
-- A runspec is to be created, based on on running the given test.
return runspec_test.build(pos, args.strategy)
Expand Down
21 changes: 20 additions & 1 deletion lua/neotest-golang/parse.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local async = require("neotest.async")
local options = require("neotest-golang.options")
local convert = require("neotest-golang.convert")
local json = require("neotest-golang.json")
local testify = require("neotest-golang.testify")

-- TODO: remove pos_type when properly supporting all position types.
-- and instead get this from the pos.type field.
Expand Down Expand Up @@ -192,6 +193,22 @@ function M.gather_neotest_data_and_set_defaults(tree)
return res
end

local function hack(test_name)
-- HACK: replace receiver with suite for testify.
-- TODO: place this under opt-in option.
-- TODO: could make more efficient by matching on filename first?
for filename, data in pairs(testify.get()) do
for _, entry in ipairs(data) do
-- TODO: better, more reliable matching needed
if string.match(test_name, "^" .. entry.suite .. "/") then
test_name = string.gsub(test_name, entry.suite, entry.receiver)
return test_name
end
end
end
return test_name
end

--- Decorate the internal test result data with go package and test name.
--- This is an important step, in which we figure out exactly which test output
--- belongs to which test in the Neotest position tree.
Expand Down Expand Up @@ -225,14 +242,16 @@ function M.decorate_with_go_package_and_test_name(
if gotestline.Package == golistline.ImportPath then
local pattern = convert.to_lua_pattern(folderpath)
.. "/(.-)/"
.. convert.to_lua_pattern(gotestline.Test)
.. convert.to_lua_pattern(hack(gotestline.Test))
.. "$"
match = tweaked_pos_id:find(pattern, 1, false)
if match ~= nil then
test_data.gotest_data.pkg = gotestline.Package
test_data.gotest_data.name = gotestline.Test
break
end

-- HACK: testify suites
end
if match ~= nil then
break
Expand Down
15 changes: 14 additions & 1 deletion lua/neotest-golang/runspec_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ local convert = require("neotest-golang.convert")
local options = require("neotest-golang.options")
local cmd = require("neotest-golang.cmd")
local dap = require("neotest-golang.dap")
local testify = require("neotest-golang.testify")

local M = {}

Expand All @@ -16,8 +17,20 @@ function M.build(pos, strategy)
local test_folder_absolute_path = string.match(pos.path, "(.+)/")
local golist_data = cmd.golist_data(test_folder_absolute_path)

local pos_id = pos.id

-- HACK: replace receiver with suite for testify.
-- TODO: place this under opt-in option.
for filename, data in pairs(testify.get()) do
for _, entry in ipairs(data) do
if string.match(pos_id, "::" .. entry.receiver .. "::") then
pos_id = string.gsub(pos_id, entry.receiver, entry.suite)
end
end
end

--- @type string
local test_name = convert.to_gotest_test_name(pos.id)
local test_name = convert.to_gotest_test_name(pos_id)
test_name = convert.to_gotest_regex_pattern(test_name)

local test_cmd, json_filepath = cmd.test_command_in_package_with_regexp(
Expand Down
7 changes: 5 additions & 2 deletions lua/neotest-golang/testify.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ function M.get()
return lookup_map
end

function M.add(file_name, suite_name, test_name)
function M.add(file_name, suite_name, receiver_name)
if not lookup_map[file_name] then
lookup_map[file_name] = {}
end
table.insert(lookup_map[file_name], { suite = suite_name, test = test_name })
table.insert(
lookup_map[file_name],
{ suite = suite_name, receiver = receiver_name }
)
end

function M.clear()
Expand Down

0 comments on commit e8e2797

Please sign in to comment.