From f7b05517b9cbf45d2c2098807b9317f5814f1916 Mon Sep 17 00:00:00 2001 From: Fredrik Averpil Date: Thu, 12 Dec 2024 21:53:18 +0100 Subject: [PATCH] fix: recursively run tests for dir without corresponding package --- lua/neotest-golang/runspec/dir.lua | 71 +++++++++++++++---- .../subpackage2/subpackage2_test.go | 9 +++ .../subpackage3/subpackage3_test.go | 9 +++ tests/go/subpackage/subpackage_test.go | 9 --- 4 files changed, 77 insertions(+), 21 deletions(-) create mode 100644 tests/go/subpackage/subpackage2/subpackage2_test.go create mode 100644 tests/go/subpackage/subpackage2/subpackage3/subpackage3_test.go delete mode 100644 tests/go/subpackage/subpackage_test.go diff --git a/lua/neotest-golang/runspec/dir.lua b/lua/neotest-golang/runspec/dir.lua index 31f4e99..1f54725 100644 --- a/lua/neotest-golang/runspec/dir.lua +++ b/lua/neotest-golang/runspec/dir.lua @@ -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: @@ -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 = { diff --git a/tests/go/subpackage/subpackage2/subpackage2_test.go b/tests/go/subpackage/subpackage2/subpackage2_test.go new file mode 100644 index 0000000..a9f0a28 --- /dev/null +++ b/tests/go/subpackage/subpackage2/subpackage2_test.go @@ -0,0 +1,9 @@ +package subpackage2 + +import "testing" + +func TestSubpackage2(t *testing.T) { + if (1 + 2) != 3 { + t.Fail() + } +} diff --git a/tests/go/subpackage/subpackage2/subpackage3/subpackage3_test.go b/tests/go/subpackage/subpackage2/subpackage3/subpackage3_test.go new file mode 100644 index 0000000..830574e --- /dev/null +++ b/tests/go/subpackage/subpackage2/subpackage3/subpackage3_test.go @@ -0,0 +1,9 @@ +package subpackage3 + +import "testing" + +func TestSubpackage3(t *testing.T) { + if (1 + 2) != 3 { + t.Fail() + } +} diff --git a/tests/go/subpackage/subpackage_test.go b/tests/go/subpackage/subpackage_test.go deleted file mode 100644 index 1bd3cc6..0000000 --- a/tests/go/subpackage/subpackage_test.go +++ /dev/null @@ -1,9 +0,0 @@ -package subpackage - -import "testing" - -func TestSubpackage(t *testing.T) { - if (1 + 2) != 3 { - t.Fail() - } -}