Ignore anonymous imports when resolving import aliases (#150)

This commit is contained in:
Ben Atkinson 2021-07-04 04:01:46 +01:00 committed by GitHub
parent e7d0d3b298
commit b4465d5f96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 96 additions and 1 deletions

View File

@ -169,7 +169,7 @@ func parseImportsAliases(pkg *packages.Package) map[string]string {
aliases := make(map[string]string) aliases := make(map[string]string)
for _, syntax := range pkg.Syntax { for _, syntax := range pkg.Syntax {
for _, imprt := range syntax.Imports { 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 aliases[strings.Trim(imprt.Path.Value, `"`)] = imprt.Name.Name
} }
} }

View File

@ -370,6 +370,13 @@ func TestMockGolden(t *testing.T) {
interfaces: []string{"Syncer"}, interfaces: []string{"Syncer"},
goldenFile: filepath.Join("testpackages/syncimport", "syncer_moq.golden.go"), 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 { for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {

View File

@ -0,0 +1,9 @@
package anonimport
import (
"context"
)
type Example interface {
Ctx(ctx context.Context)
}

View File

@ -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
}

View File

@ -0,0 +1,5 @@
package anonimport
import (
_ "context"
)