Ignore anonymous imports when resolving import aliases (#150)
This commit is contained in:
parent
e7d0d3b298
commit
b4465d5f96
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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) {
|
||||
|
9
pkg/moq/testpackages/anonimport/iface.go
Normal file
9
pkg/moq/testpackages/anonimport/iface.go
Normal file
@ -0,0 +1,9 @@
|
||||
package anonimport
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type Example interface {
|
||||
Ctx(ctx context.Context)
|
||||
}
|
74
pkg/moq/testpackages/anonimport/iface_moq.golden.go
Normal file
74
pkg/moq/testpackages/anonimport/iface_moq.golden.go
Normal 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
|
||||
}
|
5
pkg/moq/testpackages/anonimport/second_file.go
Normal file
5
pkg/moq/testpackages/anonimport/second_file.go
Normal file
@ -0,0 +1,5 @@
|
||||
package anonimport
|
||||
|
||||
import (
|
||||
_ "context"
|
||||
)
|
Loading…
Reference in New Issue
Block a user