diff --git a/internal/registry/registry.go b/internal/registry/registry.go index e017183..714cdd9 100644 --- a/internal/registry/registry.go +++ b/internal/registry/registry.go @@ -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 } } diff --git a/pkg/moq/moq_test.go b/pkg/moq/moq_test.go index 3878ce3..e3b1a1b 100644 --- a/pkg/moq/moq_test.go +++ b/pkg/moq/moq_test.go @@ -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) { diff --git a/pkg/moq/testpackages/anonimport/iface.go b/pkg/moq/testpackages/anonimport/iface.go new file mode 100644 index 0000000..a56856a --- /dev/null +++ b/pkg/moq/testpackages/anonimport/iface.go @@ -0,0 +1,9 @@ +package anonimport + +import ( + "context" +) + +type Example interface { + Ctx(ctx context.Context) +} diff --git a/pkg/moq/testpackages/anonimport/iface_moq.golden.go b/pkg/moq/testpackages/anonimport/iface_moq.golden.go new file mode 100644 index 0000000..48a08c1 --- /dev/null +++ b/pkg/moq/testpackages/anonimport/iface_moq.golden.go @@ -0,0 +1,74 @@ +// Code generated by moq; DO NOT EDIT. +// github.com/matryer/moq + +package anonimport + +import ( + "context" + "sync" +) + +// Ensure, that ExampleMock does implement Example. +// If this is not the case, regenerate this file with moq. +var _ Example = &ExampleMock{} + +// ExampleMock is a mock implementation of Example. +// +// func TestSomethingThatUsesExample(t *testing.T) { +// +// // make and configure a mocked Example +// mockedExample := &ExampleMock{ +// CtxFunc: func(ctx context.Context) { +// panic("mock out the Ctx method") +// }, +// } +// +// // use mockedExample in code that requires Example +// // and then make assertions. +// +// } +type ExampleMock struct { + // CtxFunc mocks the Ctx method. + CtxFunc func(ctx context.Context) + + // calls tracks calls to the methods. + calls struct { + // Ctx holds details about calls to the Ctx method. + Ctx []struct { + // Ctx is the ctx argument value. + Ctx context.Context + } + } + lockCtx sync.RWMutex +} + +// Ctx calls CtxFunc. +func (mock *ExampleMock) Ctx(ctx context.Context) { + if mock.CtxFunc == nil { + panic("ExampleMock.CtxFunc: method is nil but Example.Ctx was just called") + } + callInfo := struct { + Ctx context.Context + }{ + Ctx: ctx, + } + mock.lockCtx.Lock() + mock.calls.Ctx = append(mock.calls.Ctx, callInfo) + mock.lockCtx.Unlock() + mock.CtxFunc(ctx) +} + +// CtxCalls gets all the calls that were made to Ctx. +// Check the length with: +// len(mockedExample.CtxCalls()) +func (mock *ExampleMock) CtxCalls() []struct { + Ctx context.Context +} { + var calls []struct { + Ctx context.Context + } + mock.lockCtx.RLock() + calls = mock.calls.Ctx + mock.lockCtx.RUnlock() + return calls +} diff --git a/pkg/moq/testpackages/anonimport/second_file.go b/pkg/moq/testpackages/anonimport/second_file.go new file mode 100644 index 0000000..c4cadf2 --- /dev/null +++ b/pkg/moq/testpackages/anonimport/second_file.go @@ -0,0 +1,5 @@ +package anonimport + +import ( + _ "context" +)