Skip to content

Commit

Permalink
Ignore anonymous imports when resolving import aliases (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
maneac authored Jul 4, 2021
1 parent e7d0d3b commit b4465d5
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func parseImportsAliases(pkg *packages.Package) map[string]string {
aliases := make(map[string]string)
for _, syntax := range pkg.Syntax {
for _, imprt := range syntax.Imports {
if imprt.Name != nil && imprt.Name.Name != "." {
if imprt.Name != nil && imprt.Name.Name != "." && imprt.Name.Name != "_" {
aliases[strings.Trim(imprt.Path.Value, `"`)] = imprt.Name.Name
}
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/moq/moq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,13 @@ func TestMockGolden(t *testing.T) {
interfaces: []string{"Syncer"},
goldenFile: filepath.Join("testpackages/syncimport", "syncer_moq.golden.go"),
},
{
// Tests anonymous imports are not included in the generated mock.
name: "AnonymousImport",
cfg: Config{SrcDir: "testpackages/anonimport"},
interfaces: []string{"Example"},
goldenFile: filepath.Join("testpackages/anonimport", "iface_moq.golden.go"),
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
Expand Down
9 changes: 9 additions & 0 deletions pkg/moq/testpackages/anonimport/iface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package anonimport

import (
"context"
)

type Example interface {
Ctx(ctx context.Context)
}
74 changes: 74 additions & 0 deletions pkg/moq/testpackages/anonimport/iface_moq.golden.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pkg/moq/testpackages/anonimport/second_file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package anonimport

import (
_ "context"
)

0 comments on commit b4465d5

Please sign in to comment.