Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor imports #109

Merged
merged 5 commits into from
Jan 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions generator/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Fake struct {
TargetAlias string
TargetName string
TargetPackage string
Imports []Import
Imports Imports
Methods []Method
Function Method
}
Expand All @@ -55,10 +55,10 @@ func NewFake(fakeMode FakeMode, targetName string, packagePath string, fakeName
Name: fakeName,
Mode: fakeMode,
DestinationPackage: destinationPackage,
Imports: []Import{},
Imports: newImports(),
}

f.AddImport("sync", "sync")
f.Imports.Add("sync", "sync")
err := f.loadPackages(workingDir)
if err != nil {
return nil, err
Expand Down
3 changes: 1 addition & 2 deletions generator/function_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ func (f *Fake) loadMethodForFunction() error {
return errors.New("target does not have an underlying function signature")
}
f.addTypesForMethod(sig)
importsMap := f.importsMap()
f.Function = methodForSignature(sig, f.TargetName, importsMap)
f.Function = methodForSignature(sig, f.TargetName, f.Imports)
return nil
}
4 changes: 2 additions & 2 deletions generator/function_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const functionTemplate string = `// Code generated by counterfeiter. DO NOT EDIT
package {{.DestinationPackage}}

import (
{{- range .Imports}}
{{.Alias}} "{{.Path}}"
{{- range $index, $import := .Imports.ByAlias}}
{{$import.Alias}} "{{$import.PkgPath}}"
{{- end}}
)

Expand Down
188 changes: 62 additions & 126 deletions generator/generator_internals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,18 @@ func testGenerator(t *testing.T, when spec.G, it spec.S) {
Expect(f.Name).To(Equal("FakeFileInfo"))
Expect(f.Mode).To(Equal(InterfaceOrFunction))
Expect(f.DestinationPackage).To(Equal("osfakes"))
Expect(f.Imports).To(HaveLen(3))
Expect(f.Imports).To(ConsistOf(
Import{Alias: "os", Path: "os"},
Import{Alias: "sync", Path: "sync"},
Import{Alias: "time", Path: "time"},
))
Expect(f.Imports).To(BeEquivalentTo(Imports{
ByAlias: map[string]Import{
"os": {Alias: "os", PkgPath: "os"},
"sync": {Alias: "sync", PkgPath: "sync"},
"time": {Alias: "time", PkgPath: "time"},
},
ByPkgPath: map[string]Import{
"os": {Alias: "os", PkgPath: "os"},
"sync": {Alias: "sync", PkgPath: "sync"},
"time": {Alias: "time", PkgPath: "time"},
},
}))
Expect(f.Function).To(BeZero())
Expect(f.Packages).NotTo(BeNil())
Expect(f.Package).NotTo(BeNil())
Expand All @@ -79,11 +85,16 @@ func testGenerator(t *testing.T, when spec.G, it spec.S) {
Expect(f.Name).To(Equal("FakeHandlerFunc"))
Expect(f.Mode).To(Equal(InterfaceOrFunction))
Expect(f.DestinationPackage).To(Equal("httpfakes"))
Expect(f.Imports).To(HaveLen(2))
Expect(f.Imports).To(ConsistOf(
Import{Alias: "http", Path: "net/http"},
Import{Alias: "sync", Path: "sync"},
))
Expect(f.Imports).To(BeEquivalentTo(Imports{
ByAlias: map[string]Import{
"http": {Alias: "http", PkgPath: "net/http"},
"sync": {Alias: "sync", PkgPath: "sync"},
},
ByPkgPath: map[string]Import{
"net/http": {Alias: "http", PkgPath: "net/http"},
"sync": {Alias: "sync", PkgPath: "sync"},
},
}))
Expect(f.Function).NotTo(BeZero())
Expect(f.Packages).NotTo(BeNil())
Expect(f.Package).NotTo(BeNil())
Expand All @@ -97,20 +108,29 @@ func testGenerator(t *testing.T, when spec.G, it spec.S) {

when("manually constructing a fake", func() {
it.Before(func() {
f = &Fake{}
f = &Fake{Imports: newImports()}
})

when("there are imports", func() {
when("duplicate import package names are added", func() {
it.Before(func() {
f.AddImport("sync", "sync")
f.AddImport("sync", "github.com/maxbrunsfeld/counterfeiter/fixtures/sync")
f.AddImport("sync", "github.com/maxbrunsfeld/counterfeiter/fixtures/othersync")
f.Imports.Add("sync", "sync")
f.Imports.Add("sync", "github.com/maxbrunsfeld/counterfeiter/fixtures/sync")
f.Imports.Add("sync", "github.com/maxbrunsfeld/counterfeiter/fixtures/othersync")
})

it("always leaves the built-in sync in position 0", func() {
f.sortImports()
Expect(f.Imports[0].Alias).To(Equal("sync"))
Expect(f.Imports[0].Path).To(Equal("sync"))
it("all packages have unique aliases", func() {
Expect(f.Imports).To(BeEquivalentTo(Imports{
ByAlias: map[string]Import{
"sync": {Alias: "sync", PkgPath: "sync"},
"synca": {Alias: "synca", PkgPath: "github.com/maxbrunsfeld/counterfeiter/fixtures/sync"},
"syncb": {Alias: "syncb", PkgPath: "github.com/maxbrunsfeld/counterfeiter/fixtures/othersync"},
},
ByPkgPath: map[string]Import{
"sync": {Alias: "sync", PkgPath: "sync"},
"github.com/maxbrunsfeld/counterfeiter/fixtures/sync": {Alias: "synca", PkgPath: "github.com/maxbrunsfeld/counterfeiter/fixtures/sync"},
"github.com/maxbrunsfeld/counterfeiter/fixtures/othersync": {Alias: "syncb", PkgPath: "github.com/maxbrunsfeld/counterfeiter/fixtures/othersync"},
},
}))
})
})

Expand Down Expand Up @@ -259,141 +279,57 @@ func testGenerator(t *testing.T, when spec.G, it spec.S) {
f.loadMethods()
Expect(len(f.Methods)).To(BeNumerically(">=", 51)) // yes, this is crazy because go 1.11 added a function
Expect(len(f.Methods)).To(BeNumerically("<=", 53))
Expect(len(f.Imports)).To(Equal(2))
Expect(len(f.Imports.ByAlias)).To(Equal(2))
})
})
})

when("working with imports", func() {
when("there are no imports", func() {
it("returns an empty alias map", func() {
m := f.aliasMap()
Expect(m).To(BeEmpty())
Expect(f.Imports.ByAlias).To(BeEmpty())
})

it("turns a vendor path into the correct import", func() {
i := f.AddImport("apackage", "github.com/maxbrunsfeld/counterfeiter/fixtures/vendored/vendor/apackage")
i := f.Imports.Add("apackage", "github.com/maxbrunsfeld/counterfeiter/fixtures/vendored/vendor/apackage")
Expect(i.Alias).To(Equal("apackage"))
Expect(i.Path).To(Equal("apackage"))
Expect(i.PkgPath).To(Equal("apackage"))

i = f.AddImport("anotherpackage", "vendor/anotherpackage")
i = f.Imports.Add("anotherpackage", "vendor/anotherpackage")
Expect(i.Alias).To(Equal("anotherpackage"))
Expect(i.Path).To(Equal("anotherpackage"))
Expect(i.PkgPath).To(Equal("anotherpackage"))
})
})

when("there is a single import", func() {
it.Before(func() {
f.AddImport("os", "os")
f.Imports.Add("os", "os")
})

it("is present in the map", func() {
expected := Import{Alias: "os", Path: "os"}
m := f.aliasMap()
Expect(m).To(HaveLen(1))
Expect(m).To(HaveKeyWithValue("os", []Import{expected}))
})

it("returns the existing imports if there is a path match", func() {
i := f.AddImport("aliasedos", "os")
Expect(i.Alias).To(Equal("os"))
Expect(i.Path).To(Equal("os"))
Expect(f.Imports).To(HaveLen(1))
Expect(f.Imports[0].Alias).To(Equal("os"))
Expect(f.Imports[0].Path).To(Equal("os"))
})
})

when("there are imports", func() {
it.Before(func() {
f.Imports = []Import{
Import{
Alias: "dup_packages",
Path: "github.com/maxbrunsfeld/counterfeiter/fixtures/dup_packages",
},
Import{
Alias: "foo",
Path: "github.com/maxbrunsfeld/counterfeiter/fixtures/dup_packages/a/foo",
Expect(f.Imports).To(BeEquivalentTo(Imports{
ByAlias: map[string]Import{
"os": {Alias: "os", PkgPath: "os"},
},
Import{
Alias: "foo",
Path: "github.com/maxbrunsfeld/counterfeiter/fixtures/dup_packages/b/foo",
ByPkgPath: map[string]Import{
"os": {Alias: "os", PkgPath: "os"},
},
Import{
Alias: "sync",
Path: "sync",
},
}
}))
})

it("collects duplicates", func() {
m := f.aliasMap()
Expect(m).To(HaveLen(3))
Expect(m).To(HaveKey("dup_packages"))
Expect(m).To(HaveKey("sync"))
Expect(m).To(HaveKey("foo"))
Expect(m["foo"]).To(ConsistOf(
Import{
Alias: "foo",
Path: "github.com/maxbrunsfeld/counterfeiter/fixtures/dup_packages/a/foo",
it("returns the existing imports if there is a path match", func() {
i := f.Imports.Add("aliasedos", "os")
Expect(i.Alias).To(Equal("os"))
Expect(i.PkgPath).To(Equal("os"))
Expect(f.Imports).To(BeEquivalentTo(Imports{
ByAlias: map[string]Import{
"os": {Alias: "os", PkgPath: "os"},
},
Import{
Alias: "foo",
Path: "github.com/maxbrunsfeld/counterfeiter/fixtures/dup_packages/b/foo",
ByPkgPath: map[string]Import{
"os": {Alias: "os", PkgPath: "os"},
},
))
})

it("disambiguates aliases", func() {
m := f.aliasMap()
Expect(m).To(HaveLen(3))
f.disambiguateAliases()
m = f.aliasMap()
Expect(m).To(HaveLen(4))
Expect(m["fooa"]).To(ConsistOf(Import{
Alias: "fooa",
Path: "github.com/maxbrunsfeld/counterfeiter/fixtures/dup_packages/b/foo",
}))
})

when("there is a package named sync", func() {
it.Before(func() {
f.Imports = []Import{
Import{
Alias: "sync",
Path: "github.com/maxbrunsfeld/counterfeiter/fixtures/othersync",
},
Import{
Alias: "sync",
Path: "sync",
},
Import{
Alias: "sync",
Path: "github.com/maxbrunsfeld/counterfeiter/fixtures/sync",
},
}
})

it("preserves the stdlib sync alias", func() {
m := f.aliasMap()
Expect(m).To(HaveLen(1))
f.disambiguateAliases()
m = f.aliasMap()
Expect(m).To(HaveLen(3))
Expect(m["sync"]).To(ConsistOf(Import{
Alias: "sync",
Path: "sync",
}))
Expect(m["syncb"]).To(ConsistOf(Import{
Alias: "syncb",
Path: "github.com/maxbrunsfeld/counterfeiter/fixtures/sync",
}))
Expect(m["synca"]).To(ConsistOf(Import{
Alias: "synca",
Path: "github.com/maxbrunsfeld/counterfeiter/fixtures/othersync",
}))
})
})
})
})
})
Expand Down
Loading