Skip to content

Commit

Permalink
fix: recursively run tests for dir (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil authored Dec 13, 2024
1 parent 148cf31 commit 3927ac9
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 23 deletions.
71 changes: 59 additions & 12 deletions lua/neotest-golang/runspec/dir.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,60 @@ local lib = require("neotest-golang.lib")

local M = {}

--- Given the pos.path, find the corresponding Go package import path.
--- Example:
--- pos.path = "~/projects/projectx/internal/core"
--- import_path = "github.com/foo/projectx/internal/core"
---
---Two strategies are going to be used:
--- 1. Perfect match.
--- 2. Sub-package match.
---@param pos neotest.Position
---@param golist_data table
local function find_go_package_import_path(pos, golist_data)
---@type string|nil
local package_import_path = nil

-- 1. Perfect match: the selected directory corresponds to a package.
for _, golist_item in ipairs(golist_data) do
if pos.path == golist_item.Dir then
if golist_item.Name == "main" then
-- found the base go package
return "./..."
else
package_import_path = golist_item.ImportPath .. "/..."
return package_import_path
end
end
end

-- 2. Sub-package match: the selected directory does not correspond
-- to a package, but might correspond to one or more sub-packages.
local subpackage_import_paths = {}
for _, golist_item in ipairs(golist_data) do
if string.find(golist_item.Dir, pos.path, 1, true) then
-- a sub-package was detected to exist under the selected dir.
table.insert(subpackage_import_paths, 1, golist_item.ImportPath)
end
end
if subpackage_import_paths then
-- let's figure out the sub-package with the shortest name.
local shortest = subpackage_import_paths[1]
local length = string.len(subpackage_import_paths[1])
for _, candidate in ipairs(subpackage_import_paths) do
if string.len(candidate) < length then
shortest = candidate
length = string.len(candidate)
end
end

package_import_path = vim.fn.fnamemodify(shortest, ":h") .. "/..."
return package_import_path
end

return nil
end

--- Build runspec for a directory.
---
--- Strategy:
Expand Down Expand Up @@ -34,20 +88,13 @@ function M.build(pos)
table.insert(errors, golist_error)
end

-- find the go package that corresponds to the go_mod_folderpath
local package_name = "./..."
for _, golist_item in ipairs(golist_data) do
if pos.path == golist_item.Dir then
if golist_item.Name == "main" then
-- do nothing, keep ./...
else
package_name = golist_item.ImportPath
break
end
end
local package_import_path = find_go_package_import_path(pos, golist_data)
if not package_import_path then
logger.error("Could not find a package for the selected dir: " .. pos.path)
end

local test_cmd, json_filepath = lib.cmd.test_command_in_package(package_name)
local test_cmd, json_filepath =
lib.cmd.test_command_in_package(package_import_path)

--- @type RunspecContext
local context = {
Expand Down
8 changes: 6 additions & 2 deletions tests/go/lookup_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ describe("Lookup", function()
package = "main",
replacements = {},
},
[folderpath .. "/subpackage/subpackage_test.go"] = {
package = "subpackage",
[folderpath .. "/subpackage/subpackage2/subpackage2_test.go"] = {
package = "subpackage2",
replacements = {},
},
[folderpath .. "/subpackage/subpackage2/subpackage3/subpackage3_test.go"] = {
package = "subpackage3",
replacements = {},
},
[folderpath .. "/testify/othersuite_test.go"] = {
Expand Down
9 changes: 9 additions & 0 deletions tests/go/subpackage/subpackage2/subpackage2_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package subpackage2

import "testing"

func TestSubpackage2(t *testing.T) {
if (1 + 2) != 3 {
t.Fail()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package subpackage3

import "testing"

func TestSubpackage3(t *testing.T) {
if (1 + 2) != 3 {
t.Fail()
}
}
9 changes: 0 additions & 9 deletions tests/go/subpackage/subpackage_test.go

This file was deleted.

0 comments on commit 3927ac9

Please sign in to comment.