Skip to content

Commit

Permalink
fix: set namespace type, merge namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil committed Jul 7, 2024
1 parent 87bba19 commit 7c720b4
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 125 deletions.
4 changes: 1 addition & 3 deletions lua/neotest-golang/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,7 @@ 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 -- delegate to type 'test'
return -- delegate to type 'test' as it requires the same logic
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
38 changes: 38 additions & 0 deletions lua/neotest-golang/inspect.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
local M = {}

function M.deep(t, indent, done)
done = done or {}
indent = indent or ""
local output = {}

for k, v in pairs(t) do
if type(v) == "table" and not done[v] then
done[v] = true
table.insert(
output,
indent .. tostring(k) .. ":\n" .. M.deep(v, indent .. " ", done)
)
else
table.insert(output, indent .. tostring(k) .. ": " .. tostring(v))
end
end

return table.concat(output, "\n")
end

function M.dump(o)
if type(o) == "table" then
local s = "{ "
for k, v in pairs(o) do
if type(k) ~= "number" then
k = '"' .. k .. '"'
end
s = s .. "[" .. k .. "] = " .. M.dump(v) .. ","
end
return s .. "} "
else
return tostring(o)
end
end

return M
15 changes: 0 additions & 15 deletions lua/neotest-golang/runspec_namespace.lua

This file was deleted.

169 changes: 62 additions & 107 deletions lua/neotest-golang/testify.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
--- Opt-in functionality to support testify suites.
--- See https://github.com/stretchr/testify for more info.

local parsers = require("nvim-treesitter.parsers")

local M = {}
Expand Down Expand Up @@ -34,105 +37,17 @@ M.testify_query = [[
---@param tree neotest.Tree
function M.modify_neotest_tree(file_path, tree)
local lookup = M.get_lookup_map()
local file_lookup = lookup[file_path]

if file_lookup then
M.replace_receiver_with_suite(tree, file_lookup)
if not lookup then
return tree
end

-- Now merge the namespaces after applying the name changes
local modified_tree = M.replace_receiver_with_suite(tree:root(), lookup)
local tree_with_merged_namespaces = M.merge_duplicate_namespaces(tree:root())
return tree_with_merged_namespaces
end

function M.get_lookup_map()
-- FIXME: this is what the map looks like now.
local wrong = {
["/Users/fredrik/code/public/neotest-golang/tests/go/testify1_test.go"] = {
{
receiver = "receiverStruct",
suite = "TestSuite",
},
},
["/Users/fredrik/code/public/neotest-golang/tests/go/testify2_test.go"] = {
{
receiver = "receiverStruct2",
suite = "TestSuite2",
},
},
}
-- but we want this:
local right = {
["/Users/fredrik/code/public/neotest-golang/tests/go/testify1_test.go"] = {
{
receiver = "receiverStruct",
suite = "TestSuite",
},
{
receiver = "receiverStruct2",
suite = "TestSuite2",
},
},
["/Users/fredrik/code/public/neotest-golang/tests/go/testify2_test.go"] = {
{
receiver = "receiverStruct",
suite = "TestSuite",
},
{
receiver = "receiverStruct2",
suite = "TestSuite2",
},
},
["/Users/fredrik/code/public/neotest-golang/tests/go/testify_test.go"] = {
{
receiver = "ExampleTestSuite",
suite = "TestExampleTestSuite",
},
},
}
local current = {
["/Users/fredrik/code/public/neotest-golang/tests/go/testify1_test.go"] = {
{
package = "main",
receiver = "receiverStruct",
suite = "TestSuite",
},
},
["/Users/fredrik/code/public/neotest-golang/tests/go/testify2_test.go"] = {
{
package = "main",
receiver = "receiverStruct2",
suite = "TestSuite2",
},
},
}

-- local lookup_map = {
-- ["/Users/fredrik/code/public/neotest-golang/tests/go/testify1_test.go"] = {
-- package = "main",
-- receivers = { "receiverStruct", "receiverStruct2" },
-- suites = {
-- receiverStruct = "TestSuite",
-- receiverStruct2 = "TestSuite2",
-- },
-- },
-- ["/Users/fredrik/code/public/neotest-golang/tests/go/testify2_test.go"] = {
-- package = "main",
-- receivers = { "receiverStruct", "receiverStruct2" },
-- suites = {
-- receiverStruct = "TestSuite",
-- receiverStruct2 = "TestSuite2",
-- },
-- },
-- ["/Users/fredrik/code/public/neotest-golang/tests/go/testify_test.go"] = {
-- package = "main",
-- receivers = { "ExampleTestSuite" },
-- suites = {
-- ExampleTestSuite = "TestExampleTestSuite",
-- },
-- },
-- }

if vim.tbl_isempty(lookup_map) then
lookup_map = M.generate_lookup_map()
end
Expand Down Expand Up @@ -166,11 +81,29 @@ function M.clear_lookup_map()
end

function M.generate_lookup_map()
-- local example = {
-- ["/path/to/file1_test.go"] = {
-- package = "main",
-- receivers = { receiverStruct = true, receiverStruct2 = true },
-- suites = {
-- receiverStruct = "TestSuite",
-- receiverStruct2 = "TestSuite2",
-- },
-- },
-- ["/path/to/file2_test.go"] = {
-- package = "main",
-- receivers = { receiverStruct3 = true },
-- suites = {
-- receiverStruct3 = "TestSuite3",
-- },
-- },
-- -- ... other files ...
-- }

local cwd = vim.fn.getcwd()
local go_files = M.get_go_files(cwd)
local lookup = {}
local all_receivers = {}
local all_suites = {}
local global_suites = {}

-- First pass: collect all receivers and suites
for _, filepath in ipairs(go_files) do
Expand All @@ -189,32 +122,26 @@ function M.generate_lookup_map()
-- Collect all receivers
for _, struct in ipairs(matches.struct_name or {}) do
lookup[filepath].receivers[struct.text] = true
all_receivers[struct.text] = true
end

-- Collect all test suite functions and their receivers
for _, func in ipairs(matches.function_name or {}) do
if func.text:match("^Test") then
for _, node in ipairs(matches.suite_receiver or {}) do
lookup[filepath].suites[node.text] = func.text
all_suites[node.text] = func.text
global_suites[node.text] = func.text
end
end
end
end

-- Second pass: ensure all files have all receivers and suites
for filepath, file_data in pairs(lookup) do
for receiver, _ in pairs(all_receivers) do
if not file_data.receivers[receiver] then
for receiver, suite in pairs(global_suites) do
if not file_data.receivers[receiver] and file_data.suites[receiver] then
file_data.receivers[receiver] = true
end
end
for receiver, suite in pairs(all_suites) do
if not file_data.suites[receiver] then
file_data.suites[receiver] = suite
end
end
end

return lookup
Expand Down Expand Up @@ -283,16 +210,23 @@ function M.get_node_text(node, bufnr)
end

function M.replace_receiver_with_suite(node, file_lookup)
if not file_lookup then
return node
end

local function replace_in_string(str, old, new)
return str
:gsub("::" .. old .. "::", "::" .. new .. "::")
:gsub("::" .. old .. "$", "::" .. new)
end

local function update_node(n, replacements)
local function update_node(n, replacements, suite_names)
for old, new in pairs(replacements) do
if n._data.name == old then
n._data.name = new
n._data.type = "namespace"
elseif suite_names[n._data.name] then
n._data.type = "namespace"
end
n._data.id = replace_in_string(n._data.id, old, new)
end
Expand All @@ -310,15 +244,34 @@ function M.replace_receiver_with_suite(node, file_lookup)
return new_nodes
end

local function recursive_update(n, replacements)
update_node(n, replacements)
local function recursive_update(n, replacements, suite_names)
update_node(n, replacements, suite_names)
n._nodes = update_nodes_table(n._nodes, replacements)
for _, child in ipairs(n:children()) do
recursive_update(child, replacements)
recursive_update(child, replacements, suite_names)
end
end

-- Create a global replacements table and suite names set
local global_replacements = {}
local suite_names = {}
for file_path, file_data in pairs(file_lookup) do
if file_data.suites then
for receiver, suite in pairs(file_data.suites) do
global_replacements[receiver] = suite
suite_names[suite] = true
end
else
-- no suites found for file
end
end

recursive_update(node, file_lookup.suites)
if vim.tbl_isempty(global_replacements) then
-- no replacements found
return node
end

recursive_update(node, global_replacements, suite_names)

-- After updating all nodes, ensure parent-child relationships are correct
local function fix_relationships(n)
Expand All @@ -329,6 +282,8 @@ function M.replace_receiver_with_suite(node, file_lookup)
end

fix_relationships(node)

return node
end

function M.merge_duplicate_namespaces(node)
Expand Down

0 comments on commit 7c720b4

Please sign in to comment.