de-duped imports
This commit is contained in:
parent
ae57d77f27
commit
3c6751fed4
@ -22,7 +22,7 @@ type Mocker struct {
|
|||||||
pkgs map[string]*ast.Package
|
pkgs map[string]*ast.Package
|
||||||
pkgName string
|
pkgName string
|
||||||
|
|
||||||
imports []string
|
imports map[string]bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// New makes a new Mocker for the specified package directory.
|
// New makes a new Mocker for the specified package directory.
|
||||||
@ -57,6 +57,7 @@ func New(src, packageName string) (*Mocker, error) {
|
|||||||
fset: fset,
|
fset: fset,
|
||||||
pkgs: pkgs,
|
pkgs: pkgs,
|
||||||
pkgName: packageName,
|
pkgName: packageName,
|
||||||
|
imports: make(map[string]bool),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,7 +107,9 @@ func (m *Mocker) Mock(w io.Writer, name ...string) error {
|
|||||||
doc.Objects = append(doc.Objects, obj)
|
doc.Objects = append(doc.Objects, obj)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
doc.Imports = append(doc.Imports, m.imports...)
|
for pkgToImport := range m.imports {
|
||||||
|
doc.Imports = append(doc.Imports, pkgToImport)
|
||||||
|
}
|
||||||
err := m.tmpl.Execute(w, doc)
|
err := m.tmpl.Execute(w, doc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -118,7 +121,7 @@ func (m *Mocker) packageQualifier(pkg *types.Package) string {
|
|||||||
if m.pkgName == pkg.Name() {
|
if m.pkgName == pkg.Name() {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
m.imports = append(m.imports, pkg.Path())
|
m.imports[pkg.Path()] = true
|
||||||
return pkg.Name()
|
return pkg.Name()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -160,5 +160,8 @@ func TestImports(t *testing.T) {
|
|||||||
if !strings.Contains(s, str) {
|
if !strings.Contains(s, str) {
|
||||||
t.Errorf("expected by missing: \"%s\"", str)
|
t.Errorf("expected by missing: \"%s\"", str)
|
||||||
}
|
}
|
||||||
|
if len(strings.Split(s, str)) > 2 {
|
||||||
|
t.Errorf("more than one: \"%s\"", str)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
1
package/moq/testdata/imports/two/two.go
vendored
1
package/moq/testdata/imports/two/two.go
vendored
@ -7,4 +7,5 @@ import (
|
|||||||
// DoSomething does something.
|
// DoSomething does something.
|
||||||
type DoSomething interface {
|
type DoSomething interface {
|
||||||
Do(thing one.Thing) error
|
Do(thing one.Thing) error
|
||||||
|
Another(thing one.Thing) error
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user