From b4465d5f96d36ef673f70477a222f3b17e430f29 Mon Sep 17 00:00:00 2001 From: Ben Atkinson <42865129+maneac@users.noreply.github.com> Date: Sun, 4 Jul 2021 04:01:46 +0100 Subject: [PATCH] Ignore anonymous imports when resolving import aliases (#150) --- internal/registry/registry.go | 2 +- pkg/moq/moq_test.go | 7 ++ pkg/moq/testpackages/anonimport/iface.go | 9 +++ .../anonimport/iface_moq.golden.go | 74 +++++++++++++++++++ .../testpackages/anonimport/second_file.go | 5 ++ 5 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 pkg/moq/testpackages/anonimport/iface.go create mode 100644 pkg/moq/testpackages/anonimport/iface_moq.golden.go create mode 100644 pkg/moq/testpackages/anonimport/second_file.go 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" +)