Skip to content

Commit

Permalink
fix: asssume .go file in root (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil authored Dec 29, 2024
1 parent c7ac9ec commit 3799619
Show file tree
Hide file tree
Showing 18 changed files with 231 additions and 38 deletions.
27 changes: 18 additions & 9 deletions lua/neotest-golang/runspec/dir.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,29 @@ 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.
-- 1. Perfect match: main package.
for _, golist_item in ipairs(golist_data) do
if
(
golist_item.Module.GoMod
== pos.path .. lib.find.os_path_sep .. "go.mod"
and golist_item.Name == "main"
) or (pos.path == golist_item.Dir and golist_item.Name == "main")
then
package_import_path = golist_item.ImportPath .. "/..."
return "./..."
end
end

-- 2. 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
package_import_path = golist_item.ImportPath .. "/..."
return package_import_path
end
end

-- 2. Sub-package match: the selected directory does not correspond
-- 3. 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
Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions tests/go/internal/positions/add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package positions

// Function used for tests.
func Add(a, b int) int {
return a + b
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ end
describe("Discovery of test positions", function()
it("Discover OK", function()
-- Arrange
local test_filepath = vim.loop.cwd() .. "/tests/go/positions_test.go"
local test_filepath = vim.loop.cwd()
.. "/tests/go/internal/positions/positions_test.go"
local expected = {
{
id = test_filepath,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package positions

import (
"os"
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe("With testify_enabled=false", function()
it("Discover test functions", function()
-- Arrange
local test_filepath = vim.loop.cwd()
.. "/tests/go/testify/positions_test.go"
.. "/tests/go/internal/testify/positions_test.go"
local expected = {
{
id = test_filepath,
Expand Down Expand Up @@ -80,7 +80,7 @@ describe("With testify_enabled=true", function()
it("Discover namespaces, test methods and test function", function()
-- Arrange
local test_filepath = vim.loop.cwd()
.. "/tests/go/testify/positions_test.go"
.. "/tests/go/internal/testify/positions_test.go"
options.set({ testify_enabled = true }) -- enable testify
local filepaths = lib.find.go_test_filepaths(test_filepath)
testify.lookup.initialize_lookup(filepaths) -- generate lookup
Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions tests/go/internal/testname/add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package testname

// Function used for tests.
func Add(a, b int) int {
return a + b
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ local _ = require("plenary")

describe("Neotest position to Go test name", function()
-- Arrange
local test_filepath = vim.loop.cwd() .. "/tests/go/testname_test.go"
local test_filepath = vim.loop.cwd()
.. "/tests/go/internal/testname/testname_test.go"

---@type neotest.Tree
local tree =
Expand Down Expand Up @@ -86,7 +87,8 @@ end)

describe("Full Go test name conversion", function()
-- Arrange
local test_filepath = vim.loop.cwd() .. "/tests/go/testname_test.go"
local test_filepath = vim.loop.cwd()
.. "/tests/go/internal/testname/testname_test.go"

---@type neotest.Tree
local tree =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package testname

import "testing"

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package x_test
import (
"testing"

"github.com/fredrikaverpil/neotest-golang/x"
"github.com/fredrikaverpil/neotest-golang/internal/x"
)

func TestBlackBox(t *testing.T) {
Expand Down
File renamed without changes.
28 changes: 14 additions & 14 deletions tests/go/lookup_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,46 @@ describe("Lookup", function()
it("Generates tree replacement instructions", function()
-- Arrange
options.set({ testify_enabled = true }) -- enable testify
local folderpath = vim.loop.cwd() .. "/tests/go"
local filepaths = lib.find.go_test_filepaths(vim.loop.cwd())
local folderpath = vim.uv.cwd() .. "/tests/go"
local filepaths = lib.find.go_test_filepaths(vim.uv.cwd())
local expected_lookup = {
[folderpath .. "/positions_test.go"] = {
package = "main",
[folderpath .. "/internal/positions/positions_test.go"] = {
package = "positions",
replacements = {},
},
[folderpath .. "/subpackage/subpackage2/subpackage2_test.go"] = {
[folderpath .. "/internal/subpackage/subpackage2/subpackage2_test.go"] = {
package = "subpackage2",
replacements = {},
},
[folderpath .. "/subpackage/subpackage2/subpackage3/subpackage3_test.go"] = {
[folderpath .. "/internal/subpackage/subpackage2/subpackage3/subpackage3_test.go"] = {
package = "subpackage3",
replacements = {},
},
[folderpath .. "/testify/othersuite_test.go"] = {
[folderpath .. "/internal/testify/othersuite_test.go"] = {
package = "testify",
replacements = {
OtherTestSuite = "TestOtherTestSuite",
},
},
[folderpath .. "/testify/positions_test.go"] = {
[folderpath .. "/internal/testify/positions_test.go"] = {
package = "testify",
replacements = {
ExampleTestSuite = "TestExampleTestSuite",
ExampleTestSuite2 = "TestExampleTestSuite2",
},
},
[folderpath .. "/testname_test.go"] = {
package = "main",
replacements = {},
},
[folderpath .. "/x/xtest_blackbox_test.go"] = {
[folderpath .. "/internal/x/xtest_blackbox_test.go"] = {
package = "x_test",
replacements = {},
},
[folderpath .. "/x/xtest_whitebox_test.go"] = {
[folderpath .. "/internal/x/xtest_whitebox_test.go"] = {
package = "x",
replacements = {},
},
[folderpath .. "/internal/testname/testname_test.go"] = {
package = "testname",
replacements = {},
},
}

-- Act
Expand Down
183 changes: 176 additions & 7 deletions tests/unit/golist_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local _ = require("plenary")

local lib = require("neotest-golang.lib")

describe("go list output", function()
describe("go list output from root", function()
it("contains expected keys/values", function()
local tests_filepath = vim.loop.cwd() .. "/tests/go"
local output = lib.cmd.golist_data(tests_filepath)
Expand Down Expand Up @@ -55,24 +55,193 @@ describe("go list output", function()
"unicode/utf8",
"unsafe",
},
Dir = tests_filepath,
Dir = tests_filepath .. "/cmd/main",
GoFiles = { "main.go" },
ImportPath = "github.com/fredrikaverpil/neotest-golang",
ImportPath = "github.com/fredrikaverpil/neotest-golang/cmd/main",
Imports = { "fmt" },
Match = { "./..." },
Module = {
Dir = tests_filepath,
GoMod = tests_filepath .. "/go.mod",
GoVersion = "1.23.1",
Main = true,
Path = "github.com/fredrikaverpil/neotest-golang",
},
Name = "main",
Root = tests_filepath,
Stale = true,
StaleReason = "build ID mismatch",
Target = "/Users/fredrik/go/bin/neotest-golang",
TestGoFiles = { "positions_test.go", "testname_test.go" },
}

-- ignored keys, as they might differ between OS/CI/platforms/too often
expected.Module.GoVersion = nil
first_entry.Module.GoVersion = nil
expected.Deps = nil
first_entry.Deps = nil
expected.StaleReason = nil
first_entry.StaleReason = nil
expected.Target = nil
first_entry.Target = nil

assert.are_same(vim.inspect(expected), vim.inspect(first_entry))
assert.are_same(expected, first_entry)
end)
end)

describe("go list output from internal", function()
it("contains expected keys/values", function()
local tests_filepath = vim.loop.cwd() .. "/tests/go"
local internal_filepath = vim.loop.cwd() .. "/tests/go/internal"
local output = lib.cmd.golist_data(internal_filepath)
local first_entry = output[1]
local expected = {

Deps = {
"cmp",
"errors",
"fmt",
"internal/abi",
"internal/bytealg",
"internal/chacha8rand",
"internal/coverage/rtcov",
"internal/cpu",
"internal/fmtsort",
"internal/goarch",
"internal/godebugs",
"internal/goexperiment",
"internal/goos",
"internal/itoa",
"internal/oserror",
"internal/poll",
"internal/race",
"internal/reflectlite",
"internal/safefilepath",
"internal/syscall/execenv",
"internal/syscall/unix",
"internal/testlog",
"internal/unsafeheader",
"io",
"io/fs",
"math",
"math/bits",
"os",
"path",
"reflect",
"runtime",
"runtime/internal/atomic",
"runtime/internal/math",
"runtime/internal/sys",
"slices",
"sort",
"strconv",
"sync",
"sync/atomic",
"syscall",
"time",
"unicode",
"unicode/utf8",
"unsafe",
},
Dir = internal_filepath .. "/positions",
GoFiles = { "add.go" },
ImportPath = "github.com/fredrikaverpil/neotest-golang/internal/positions",
Match = { "./..." },
Module = {
Dir = tests_filepath,
GoMod = tests_filepath .. "/go.mod",
GoVersion = "1.23.1",
Main = true,
Path = "github.com/fredrikaverpil/neotest-golang",
},
Name = "positions",
Root = tests_filepath,
Stale = true,
TestGoFiles = { "positions_test.go" },
TestImports = { "os", "testing" },
}

-- ignored keys, as they might differ between OS/CI/platforms/too often
expected.Module.GoVersion = nil
first_entry.Module.GoVersion = nil
expected.Deps = nil
first_entry.Deps = nil
expected.StaleReason = nil
first_entry.StaleReason = nil
expected.Target = nil
first_entry.Target = nil

assert.are_same(vim.inspect(expected), vim.inspect(first_entry))
assert.are_same(expected, first_entry)
end)
end)

describe("go list output from internal/positions", function()
it("contains expected keys/values", function()
local tests_filepath = vim.loop.cwd() .. "/tests/go"
local positions_filepath = vim.loop.cwd() .. "/tests/go/internal/positions"
local output = lib.cmd.golist_data(positions_filepath)
local first_entry = output[1]
local expected = {

Deps = {
"cmp",
"errors",
"fmt",
"internal/abi",
"internal/bytealg",
"internal/chacha8rand",
"internal/coverage/rtcov",
"internal/cpu",
"internal/fmtsort",
"internal/goarch",
"internal/godebugs",
"internal/goexperiment",
"internal/goos",
"internal/itoa",
"internal/oserror",
"internal/poll",
"internal/race",
"internal/reflectlite",
"internal/safefilepath",
"internal/syscall/execenv",
"internal/syscall/unix",
"internal/testlog",
"internal/unsafeheader",
"io",
"io/fs",
"math",
"math/bits",
"os",
"path",
"reflect",
"runtime",
"runtime/internal/atomic",
"runtime/internal/math",
"runtime/internal/sys",
"slices",
"sort",
"strconv",
"sync",
"sync/atomic",
"syscall",
"time",
"unicode",
"unicode/utf8",
"unsafe",
},
Dir = positions_filepath,
GoFiles = { "add.go" },
ImportPath = "github.com/fredrikaverpil/neotest-golang/internal/positions",
Match = { "./..." },
Module = {
Dir = tests_filepath,
GoMod = tests_filepath .. "/go.mod",
GoVersion = "1.23.1",
Main = true,
Path = "github.com/fredrikaverpil/neotest-golang",
},
Name = "positions",
Root = tests_filepath,
Stale = true,
TestGoFiles = { "positions_test.go" },
TestImports = { "os", "testing" },
}

Expand Down

0 comments on commit 3799619

Please sign in to comment.