Skip to content

Commit

Permalink
fix find package
Browse files Browse the repository at this point in the history
  • Loading branch information
crhntr committed Dec 2, 2024
1 parent d415774 commit a8b1991
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions internal/check/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ func TestTree(t *testing.T) {
if loadErr != nil {
t.Fatal(loadErr)
}
checkTestPackage := findPackage(t, packageList, "github.com/crhntr/muxt/internal/check")
checkTestPackage := find(t, packageList, func(p *packages.Package) bool {
return p.Name == "check_test"
})
for _, tt := range []struct {
Name string
Template string
Expand Down Expand Up @@ -432,8 +434,12 @@ func TestExampleTemplate(t *testing.T) {
if loadErr != nil {
t.Fatal(loadErr)
}
pkg := findPackage(t, packageList, "github.com/crhntr/muxt/example")
netHTTP := findPackage(t, packageList, "net/http")
pkg := find(t, packageList, func(p *packages.Package) bool {
return p.PkgPath == "github.com/crhntr/muxt/example"
})
netHTTP := find(t, packageList, func(p *packages.Package) bool {
return p.PkgPath == "net/http"
})
backend := pkg.Types.Scope().Lookup("Backend")
require.NotNil(t, backend)

Expand All @@ -457,15 +463,14 @@ func TestExampleTemplate(t *testing.T) {
}
}

func findPackage(t *testing.T, list []*packages.Package, path string) *packages.Package {
func find[T any](t *testing.T, list []T, match func(p T) bool) T {
t.Helper()
if i := slices.IndexFunc(list, func(p *packages.Package) bool {
return p.PkgPath == path
}); i >= 0 {
if i := slices.IndexFunc(list, match); i >= 0 {
return list[i]
} else {
t.Fatalf("failed to load %q package", path)
return nil
var zero T
t.Fatalf("failed to find")
return zero
}
}

Expand Down

0 comments on commit a8b1991

Please sign in to comment.