diff --git a/arguments/parser.go b/arguments/parser.go index 76cb7d7..ae0ea2f 100644 --- a/arguments/parser.go +++ b/arguments/parser.go @@ -46,6 +46,12 @@ func New(args []string, workingDir string, evaler Evaler, stater Stater) (*Parse "Display this help", ) + testFlag := fs.Bool( + "test", + false, + "Append \"_test\" to pacakge name", + ) + err := fs.Parse(args[1:]) if err != nil { return nil, err @@ -73,8 +79,11 @@ func New(args []string, workingDir string, evaler Evaler, stater Stater) (*Parse result.parseInterfaceName(packageMode, fs.Args()) result.parseFakeName(packageMode, *fakeNameFlag, fs.Args()) result.parseOutputPath(packageMode, workingDir, *outputPathFlag, fs.Args()) - result.parseDestinationPackageName(packageMode, fs.Args()) + result.parseDestinationPackagePath(packageMode, fs.Args()) result.parsePackagePath(packageMode, fs.Args()) + if *testFlag { + result.TestPackage = true + } return result, nil } @@ -139,8 +148,8 @@ func (a *ParsedArguments) parseOutputPath(packageMode bool, workingDir string, o } if packageMode { - a.parseDestinationPackageName(packageMode, args) - a.OutputPath = path.Join(workingDir, a.DestinationPackageName) + a.parsePackagePath(packageMode, args) + a.OutputPath = path.Join(workingDir, path.Base(a.PackagePath)+"shim") return } @@ -152,14 +161,12 @@ func (a *ParsedArguments) parseOutputPath(packageMode bool, workingDir string, o a.OutputPath = filepath.Join(d, packageNameForPath(d), snakeCaseName+".go") } -func (a *ParsedArguments) parseDestinationPackageName(packageMode bool, args []string) { +func (a *ParsedArguments) parseDestinationPackagePath(packageMode bool, args []string) { if packageMode { - a.parsePackagePath(packageMode, args) - a.DestinationPackageName = path.Base(a.PackagePath) + "shim" + a.DestinationPackagePath = a.OutputPath return } - - a.DestinationPackageName = restrictToValidPackageName(filepath.Base(filepath.Dir(a.OutputPath))) + a.DestinationPackagePath = filepath.Dir(a.OutputPath) } func (a *ParsedArguments) parsePackagePath(packageMode bool, args []string) { @@ -182,11 +189,11 @@ func (a *ParsedArguments) parsePackagePath(packageMode bool, args []string) { type ParsedArguments struct { GenerateInterfaceAndShimFromPackageDirectory bool - SourcePackageDir string // abs path to the dir containing the interface to fake - PackagePath string // package path to the package containing the interface to fake - OutputPath string // path to write the fake file to - - DestinationPackageName string // often the base-dir for OutputPath but must be a valid package name + SourcePackageDir string // abs path to the dir containing the interface to fake + PackagePath string // package path to the package containing the interface to fake + OutputPath string // path to write the fake file to + DestinationPackagePath string // path to destination package + TestPackage bool // append "_test" to package name InterfaceName string // the interface to counterfeit FakeImplName string // the name of the struct implementing the given interface @@ -241,13 +248,3 @@ func any(slice []string, needle string) bool { return false } - -func restrictToValidPackageName(input string) string { - return strings.Map(func(r rune) rune { - if (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') || r == '_' { - return r - } else { - return -1 - } - }, input) -} diff --git a/arguments/parser_test.go b/arguments/parser_test.go index 4852a95..b4e9e0b 100644 --- a/arguments/parser_test.go +++ b/arguments/parser_test.go @@ -69,7 +69,7 @@ func testParsingArguments(t *testing.T, when spec.G, it spec.S) { it("sets arguments as expected", func() { Expect(parsedArgs.SourcePackageDir).To(Equal("os")) Expect(parsedArgs.OutputPath).To(Equal(path.Join(workingDir, "osshim"))) - Expect(parsedArgs.DestinationPackageName).To(Equal("osshim")) + Expect(parsedArgs.DestinationPackagePath).To(Equal(path.Join(workingDir, "osshim"))) }) }) }) @@ -162,8 +162,13 @@ func testParsingArguments(t *testing.T, when spec.G, it spec.S) { )) }) - it("specifies the destination package name", func() { - Expect(parsedArgs.DestinationPackageName).To(Equal("my5packagefakes")) + it("specifies the destination package path", func() { + Expect(parsedArgs.DestinationPackagePath).To(Equal( + filepath.Join( + parsedArgs.SourcePackageDir, + "my5packagefakes", + ), + )) }) when("when the interface is unexported", func() { @@ -235,16 +240,18 @@ func testParsingArguments(t *testing.T, when spec.G, it spec.S) { }) }) - when("when the output dir contains characters inappropriate for a package name", func() { - it.Before(func() { - args = []string{"counterfeiter", "@my-special-package[]{}", "MySpecialInterface"} - justBefore() - }) + /* + when("when the output dir contains characters inappropriate for a package name", func() { + it.Before(func() { + args = []string{"counterfeiter", "@my-special-package[]{}", "MySpecialInterface"} + justBefore() + }) - it("should choose a valid package name", func() { - Expect(parsedArgs.DestinationPackageName).To(Equal("myspecialpackagefakes")) + it("should choose a valid package path", func() { + Expect(parsedArgs.DestinationPackagePath).To(Equal("myspecialpackagefakes")) + }) }) - }) + */ when("when three arguments are provided", func() { when("and the third one is '-'", func() { @@ -306,16 +313,18 @@ func testParsingArguments(t *testing.T, when spec.G, it spec.S) { }) }) - when("when the output dir contains underscores in package name", func() { - it.Before(func() { - args = []string{"counterfeiter", "fake_command_runner", "MySpecialInterface"} - justBefore() - }) + /* + when("when the output dir contains underscores in package name", func() { + it.Before(func() { + args = []string{"counterfeiter", "fake_command_runner", "MySpecialInterface"} + justBefore() + }) - it("should ensure underscores are in the package name", func() { - Expect(parsedArgs.DestinationPackageName).To(Equal("fake_command_runnerfakes")) + it("should ensure underscores are in the package name", func() { + Expect(parsedArgs.DestinationPackagePath).To(Equal("fake_command_runnerfakes")) + }) }) - }) + */ } func fakeFileInfo(filename string, isDir bool) os.FileInfo { diff --git a/arguments/parser_windows_test.go b/arguments/parser_windows_test.go index 4e8a7b5..5fd317a 100644 --- a/arguments/parser_windows_test.go +++ b/arguments/parser_windows_test.go @@ -24,12 +24,12 @@ func TestParsingArguments(t *testing.T) { func testParsingArguments(t *testing.T, when spec.G, it spec.S) { var ( - err error + err error parsedArgs *arguments.ParsedArguments - args []string + args []string workingDir string - evaler arguments.Evaler - stater arguments.Stater + evaler arguments.Evaler + stater arguments.Stater ) justBefore := func() { diff --git a/arguments/usage.go b/arguments/usage.go index e2926b4..f7d2ccd 100644 --- a/arguments/usage.go +++ b/arguments/usage.go @@ -3,7 +3,7 @@ package arguments const usage = ` USAGE counterfeiter - [-generate>] [-o ] [-p] [--fake-name ] + [-generate>] [-o ] [-p] [--fake-name ] [--test] [] [-] ARGUMENTS @@ -83,4 +83,8 @@ OPTIONS example: # writes "CoolThing" to ./mypackagefakes/cool_thing.go counterfeiter --fake-name CoolThing ./mypackage MyInterface + + --test + When generating into the same directory as the source interface, + append a "_test" suffix to the package name of the generated file. ` diff --git a/benchmark_test.go b/benchmark_test.go index 19aa792..eb084f5 100644 --- a/benchmark_test.go +++ b/benchmark_test.go @@ -23,7 +23,7 @@ func BenchmarkWithoutCache(b *testing.B) { SourcePackageDir: workingDir, PackagePath: workingDir, OutputPath: filepath.Join(workingDir, "fixturesfakes", "fake_something.go"), - DestinationPackageName: "fixturesfakes", + DestinationPackagePath: filepath.Join(workingDir, "fixturesfakes"), InterfaceName: "Something", FakeImplName: "FakeSomething", PrintToStdOut: false, @@ -49,7 +49,7 @@ func BenchmarkWithCache(b *testing.B) { SourcePackageDir: workingDir, PackagePath: workingDir, OutputPath: filepath.Join(workingDir, "fixturesfakes", "fake_something.go"), - DestinationPackageName: "fixturesfakes", + DestinationPackagePath: filepath.Join(workingDir, "fixturesfakes"), InterfaceName: "Something", FakeImplName: "FakeSomething", PrintToStdOut: false, diff --git a/fixtures/same/interface.go b/fixtures/same/interface.go new file mode 100644 index 0000000..29d8e3d --- /dev/null +++ b/fixtures/same/interface.go @@ -0,0 +1,9 @@ +package same // import "github.com/maxbrunsfeld/counterfeiter/v6/fixtures/same" + +//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o same_fake.go . SomeInterface +type SomeInterface interface { + DoThings(string, uint64) (int, error) + DoNothing() + DoASlice([]byte) + DoAnArray([4]byte) +} diff --git a/fixtures/same/notexported.go b/fixtures/same/notexported.go new file mode 100644 index 0000000..3af1b99 --- /dev/null +++ b/fixtures/same/notexported.go @@ -0,0 +1,9 @@ +package same // import "github.com/maxbrunsfeld/counterfeiter/v6/fixtures/same" + +//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o notexported_fake.go . someNotExportedInterface +type someNotExportedInterface interface { + DoThings(string, uint64) (int, error) + DoNothing() + DoASlice([]byte) + DoAnArray([4]byte) +} diff --git a/fixtures/same/notexported_fake.go b/fixtures/same/notexported_fake.go new file mode 100644 index 0000000..aa37198 --- /dev/null +++ b/fixtures/same/notexported_fake.go @@ -0,0 +1,225 @@ +// Code generated by counterfeiter. DO NOT EDIT. +package same + +import ( + "sync" +) + +type FakeSomeNotExportedInterface struct { + DoASliceStub func([]byte) + doASliceMutex sync.RWMutex + doASliceArgsForCall []struct { + arg1 []byte + } + DoAnArrayStub func([4]byte) + doAnArrayMutex sync.RWMutex + doAnArrayArgsForCall []struct { + arg1 [4]byte + } + DoNothingStub func() + doNothingMutex sync.RWMutex + doNothingArgsForCall []struct { + } + DoThingsStub func(string, uint64) (int, error) + doThingsMutex sync.RWMutex + doThingsArgsForCall []struct { + arg1 string + arg2 uint64 + } + doThingsReturns struct { + result1 int + result2 error + } + doThingsReturnsOnCall map[int]struct { + result1 int + result2 error + } + invocations map[string][][]interface{} + invocationsMutex sync.RWMutex +} + +func (fake *FakeSomeNotExportedInterface) DoASlice(arg1 []byte) { + var arg1Copy []byte + if arg1 != nil { + arg1Copy = make([]byte, len(arg1)) + copy(arg1Copy, arg1) + } + fake.doASliceMutex.Lock() + fake.doASliceArgsForCall = append(fake.doASliceArgsForCall, struct { + arg1 []byte + }{arg1Copy}) + fake.recordInvocation("DoASlice", []interface{}{arg1Copy}) + fake.doASliceMutex.Unlock() + if fake.DoASliceStub != nil { + fake.DoASliceStub(arg1) + } +} + +func (fake *FakeSomeNotExportedInterface) DoASliceCallCount() int { + fake.doASliceMutex.RLock() + defer fake.doASliceMutex.RUnlock() + return len(fake.doASliceArgsForCall) +} + +func (fake *FakeSomeNotExportedInterface) DoASliceCalls(stub func([]byte)) { + fake.doASliceMutex.Lock() + defer fake.doASliceMutex.Unlock() + fake.DoASliceStub = stub +} + +func (fake *FakeSomeNotExportedInterface) DoASliceArgsForCall(i int) []byte { + fake.doASliceMutex.RLock() + defer fake.doASliceMutex.RUnlock() + argsForCall := fake.doASliceArgsForCall[i] + return argsForCall.arg1 +} + +func (fake *FakeSomeNotExportedInterface) DoAnArray(arg1 [4]byte) { + fake.doAnArrayMutex.Lock() + fake.doAnArrayArgsForCall = append(fake.doAnArrayArgsForCall, struct { + arg1 [4]byte + }{arg1}) + fake.recordInvocation("DoAnArray", []interface{}{arg1}) + fake.doAnArrayMutex.Unlock() + if fake.DoAnArrayStub != nil { + fake.DoAnArrayStub(arg1) + } +} + +func (fake *FakeSomeNotExportedInterface) DoAnArrayCallCount() int { + fake.doAnArrayMutex.RLock() + defer fake.doAnArrayMutex.RUnlock() + return len(fake.doAnArrayArgsForCall) +} + +func (fake *FakeSomeNotExportedInterface) DoAnArrayCalls(stub func([4]byte)) { + fake.doAnArrayMutex.Lock() + defer fake.doAnArrayMutex.Unlock() + fake.DoAnArrayStub = stub +} + +func (fake *FakeSomeNotExportedInterface) DoAnArrayArgsForCall(i int) [4]byte { + fake.doAnArrayMutex.RLock() + defer fake.doAnArrayMutex.RUnlock() + argsForCall := fake.doAnArrayArgsForCall[i] + return argsForCall.arg1 +} + +func (fake *FakeSomeNotExportedInterface) DoNothing() { + fake.doNothingMutex.Lock() + fake.doNothingArgsForCall = append(fake.doNothingArgsForCall, struct { + }{}) + fake.recordInvocation("DoNothing", []interface{}{}) + fake.doNothingMutex.Unlock() + if fake.DoNothingStub != nil { + fake.DoNothingStub() + } +} + +func (fake *FakeSomeNotExportedInterface) DoNothingCallCount() int { + fake.doNothingMutex.RLock() + defer fake.doNothingMutex.RUnlock() + return len(fake.doNothingArgsForCall) +} + +func (fake *FakeSomeNotExportedInterface) DoNothingCalls(stub func()) { + fake.doNothingMutex.Lock() + defer fake.doNothingMutex.Unlock() + fake.DoNothingStub = stub +} + +func (fake *FakeSomeNotExportedInterface) DoThings(arg1 string, arg2 uint64) (int, error) { + fake.doThingsMutex.Lock() + ret, specificReturn := fake.doThingsReturnsOnCall[len(fake.doThingsArgsForCall)] + fake.doThingsArgsForCall = append(fake.doThingsArgsForCall, struct { + arg1 string + arg2 uint64 + }{arg1, arg2}) + fake.recordInvocation("DoThings", []interface{}{arg1, arg2}) + fake.doThingsMutex.Unlock() + if fake.DoThingsStub != nil { + return fake.DoThingsStub(arg1, arg2) + } + if specificReturn { + return ret.result1, ret.result2 + } + fakeReturns := fake.doThingsReturns + return fakeReturns.result1, fakeReturns.result2 +} + +func (fake *FakeSomeNotExportedInterface) DoThingsCallCount() int { + fake.doThingsMutex.RLock() + defer fake.doThingsMutex.RUnlock() + return len(fake.doThingsArgsForCall) +} + +func (fake *FakeSomeNotExportedInterface) DoThingsCalls(stub func(string, uint64) (int, error)) { + fake.doThingsMutex.Lock() + defer fake.doThingsMutex.Unlock() + fake.DoThingsStub = stub +} + +func (fake *FakeSomeNotExportedInterface) DoThingsArgsForCall(i int) (string, uint64) { + fake.doThingsMutex.RLock() + defer fake.doThingsMutex.RUnlock() + argsForCall := fake.doThingsArgsForCall[i] + return argsForCall.arg1, argsForCall.arg2 +} + +func (fake *FakeSomeNotExportedInterface) DoThingsReturns(result1 int, result2 error) { + fake.doThingsMutex.Lock() + defer fake.doThingsMutex.Unlock() + fake.DoThingsStub = nil + fake.doThingsReturns = struct { + result1 int + result2 error + }{result1, result2} +} + +func (fake *FakeSomeNotExportedInterface) DoThingsReturnsOnCall(i int, result1 int, result2 error) { + fake.doThingsMutex.Lock() + defer fake.doThingsMutex.Unlock() + fake.DoThingsStub = nil + if fake.doThingsReturnsOnCall == nil { + fake.doThingsReturnsOnCall = make(map[int]struct { + result1 int + result2 error + }) + } + fake.doThingsReturnsOnCall[i] = struct { + result1 int + result2 error + }{result1, result2} +} + +func (fake *FakeSomeNotExportedInterface) Invocations() map[string][][]interface{} { + fake.invocationsMutex.RLock() + defer fake.invocationsMutex.RUnlock() + fake.doASliceMutex.RLock() + defer fake.doASliceMutex.RUnlock() + fake.doAnArrayMutex.RLock() + defer fake.doAnArrayMutex.RUnlock() + fake.doNothingMutex.RLock() + defer fake.doNothingMutex.RUnlock() + fake.doThingsMutex.RLock() + defer fake.doThingsMutex.RUnlock() + copiedInvocations := map[string][][]interface{}{} + for key, value := range fake.invocations { + copiedInvocations[key] = value + } + return copiedInvocations +} + +func (fake *FakeSomeNotExportedInterface) recordInvocation(key string, args []interface{}) { + fake.invocationsMutex.Lock() + defer fake.invocationsMutex.Unlock() + if fake.invocations == nil { + fake.invocations = map[string][][]interface{}{} + } + if fake.invocations[key] == nil { + fake.invocations[key] = [][]interface{}{} + } + fake.invocations[key] = append(fake.invocations[key], args) +} + +var _ someNotExportedInterface = new(FakeSomeNotExportedInterface) diff --git a/fixtures/same/same_fake.go b/fixtures/same/same_fake.go new file mode 100644 index 0000000..b880bb9 --- /dev/null +++ b/fixtures/same/same_fake.go @@ -0,0 +1,225 @@ +// Code generated by counterfeiter. DO NOT EDIT. +package same + +import ( + "sync" +) + +type FakeSomeInterface struct { + DoASliceStub func([]byte) + doASliceMutex sync.RWMutex + doASliceArgsForCall []struct { + arg1 []byte + } + DoAnArrayStub func([4]byte) + doAnArrayMutex sync.RWMutex + doAnArrayArgsForCall []struct { + arg1 [4]byte + } + DoNothingStub func() + doNothingMutex sync.RWMutex + doNothingArgsForCall []struct { + } + DoThingsStub func(string, uint64) (int, error) + doThingsMutex sync.RWMutex + doThingsArgsForCall []struct { + arg1 string + arg2 uint64 + } + doThingsReturns struct { + result1 int + result2 error + } + doThingsReturnsOnCall map[int]struct { + result1 int + result2 error + } + invocations map[string][][]interface{} + invocationsMutex sync.RWMutex +} + +func (fake *FakeSomeInterface) DoASlice(arg1 []byte) { + var arg1Copy []byte + if arg1 != nil { + arg1Copy = make([]byte, len(arg1)) + copy(arg1Copy, arg1) + } + fake.doASliceMutex.Lock() + fake.doASliceArgsForCall = append(fake.doASliceArgsForCall, struct { + arg1 []byte + }{arg1Copy}) + fake.recordInvocation("DoASlice", []interface{}{arg1Copy}) + fake.doASliceMutex.Unlock() + if fake.DoASliceStub != nil { + fake.DoASliceStub(arg1) + } +} + +func (fake *FakeSomeInterface) DoASliceCallCount() int { + fake.doASliceMutex.RLock() + defer fake.doASliceMutex.RUnlock() + return len(fake.doASliceArgsForCall) +} + +func (fake *FakeSomeInterface) DoASliceCalls(stub func([]byte)) { + fake.doASliceMutex.Lock() + defer fake.doASliceMutex.Unlock() + fake.DoASliceStub = stub +} + +func (fake *FakeSomeInterface) DoASliceArgsForCall(i int) []byte { + fake.doASliceMutex.RLock() + defer fake.doASliceMutex.RUnlock() + argsForCall := fake.doASliceArgsForCall[i] + return argsForCall.arg1 +} + +func (fake *FakeSomeInterface) DoAnArray(arg1 [4]byte) { + fake.doAnArrayMutex.Lock() + fake.doAnArrayArgsForCall = append(fake.doAnArrayArgsForCall, struct { + arg1 [4]byte + }{arg1}) + fake.recordInvocation("DoAnArray", []interface{}{arg1}) + fake.doAnArrayMutex.Unlock() + if fake.DoAnArrayStub != nil { + fake.DoAnArrayStub(arg1) + } +} + +func (fake *FakeSomeInterface) DoAnArrayCallCount() int { + fake.doAnArrayMutex.RLock() + defer fake.doAnArrayMutex.RUnlock() + return len(fake.doAnArrayArgsForCall) +} + +func (fake *FakeSomeInterface) DoAnArrayCalls(stub func([4]byte)) { + fake.doAnArrayMutex.Lock() + defer fake.doAnArrayMutex.Unlock() + fake.DoAnArrayStub = stub +} + +func (fake *FakeSomeInterface) DoAnArrayArgsForCall(i int) [4]byte { + fake.doAnArrayMutex.RLock() + defer fake.doAnArrayMutex.RUnlock() + argsForCall := fake.doAnArrayArgsForCall[i] + return argsForCall.arg1 +} + +func (fake *FakeSomeInterface) DoNothing() { + fake.doNothingMutex.Lock() + fake.doNothingArgsForCall = append(fake.doNothingArgsForCall, struct { + }{}) + fake.recordInvocation("DoNothing", []interface{}{}) + fake.doNothingMutex.Unlock() + if fake.DoNothingStub != nil { + fake.DoNothingStub() + } +} + +func (fake *FakeSomeInterface) DoNothingCallCount() int { + fake.doNothingMutex.RLock() + defer fake.doNothingMutex.RUnlock() + return len(fake.doNothingArgsForCall) +} + +func (fake *FakeSomeInterface) DoNothingCalls(stub func()) { + fake.doNothingMutex.Lock() + defer fake.doNothingMutex.Unlock() + fake.DoNothingStub = stub +} + +func (fake *FakeSomeInterface) DoThings(arg1 string, arg2 uint64) (int, error) { + fake.doThingsMutex.Lock() + ret, specificReturn := fake.doThingsReturnsOnCall[len(fake.doThingsArgsForCall)] + fake.doThingsArgsForCall = append(fake.doThingsArgsForCall, struct { + arg1 string + arg2 uint64 + }{arg1, arg2}) + fake.recordInvocation("DoThings", []interface{}{arg1, arg2}) + fake.doThingsMutex.Unlock() + if fake.DoThingsStub != nil { + return fake.DoThingsStub(arg1, arg2) + } + if specificReturn { + return ret.result1, ret.result2 + } + fakeReturns := fake.doThingsReturns + return fakeReturns.result1, fakeReturns.result2 +} + +func (fake *FakeSomeInterface) DoThingsCallCount() int { + fake.doThingsMutex.RLock() + defer fake.doThingsMutex.RUnlock() + return len(fake.doThingsArgsForCall) +} + +func (fake *FakeSomeInterface) DoThingsCalls(stub func(string, uint64) (int, error)) { + fake.doThingsMutex.Lock() + defer fake.doThingsMutex.Unlock() + fake.DoThingsStub = stub +} + +func (fake *FakeSomeInterface) DoThingsArgsForCall(i int) (string, uint64) { + fake.doThingsMutex.RLock() + defer fake.doThingsMutex.RUnlock() + argsForCall := fake.doThingsArgsForCall[i] + return argsForCall.arg1, argsForCall.arg2 +} + +func (fake *FakeSomeInterface) DoThingsReturns(result1 int, result2 error) { + fake.doThingsMutex.Lock() + defer fake.doThingsMutex.Unlock() + fake.DoThingsStub = nil + fake.doThingsReturns = struct { + result1 int + result2 error + }{result1, result2} +} + +func (fake *FakeSomeInterface) DoThingsReturnsOnCall(i int, result1 int, result2 error) { + fake.doThingsMutex.Lock() + defer fake.doThingsMutex.Unlock() + fake.DoThingsStub = nil + if fake.doThingsReturnsOnCall == nil { + fake.doThingsReturnsOnCall = make(map[int]struct { + result1 int + result2 error + }) + } + fake.doThingsReturnsOnCall[i] = struct { + result1 int + result2 error + }{result1, result2} +} + +func (fake *FakeSomeInterface) Invocations() map[string][][]interface{} { + fake.invocationsMutex.RLock() + defer fake.invocationsMutex.RUnlock() + fake.doASliceMutex.RLock() + defer fake.doASliceMutex.RUnlock() + fake.doAnArrayMutex.RLock() + defer fake.doAnArrayMutex.RUnlock() + fake.doNothingMutex.RLock() + defer fake.doNothingMutex.RUnlock() + fake.doThingsMutex.RLock() + defer fake.doThingsMutex.RUnlock() + copiedInvocations := map[string][][]interface{}{} + for key, value := range fake.invocations { + copiedInvocations[key] = value + } + return copiedInvocations +} + +func (fake *FakeSomeInterface) recordInvocation(key string, args []interface{}) { + fake.invocationsMutex.Lock() + defer fake.invocationsMutex.Unlock() + if fake.invocations == nil { + fake.invocations = map[string][][]interface{}{} + } + if fake.invocations[key] == nil { + fake.invocations[key] = [][]interface{}{} + } + fake.invocations[key] = append(fake.invocations[key], args) +} + +var _ SomeInterface = new(FakeSomeInterface) diff --git a/fixtures/same_aliased/interface.go b/fixtures/same_aliased/interface.go new file mode 100644 index 0000000..29d8e3d --- /dev/null +++ b/fixtures/same_aliased/interface.go @@ -0,0 +1,9 @@ +package same // import "github.com/maxbrunsfeld/counterfeiter/v6/fixtures/same" + +//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o same_fake.go . SomeInterface +type SomeInterface interface { + DoThings(string, uint64) (int, error) + DoNothing() + DoASlice([]byte) + DoAnArray([4]byte) +} diff --git a/fixtures/same_aliased/same_fake.go b/fixtures/same_aliased/same_fake.go new file mode 100644 index 0000000..b880bb9 --- /dev/null +++ b/fixtures/same_aliased/same_fake.go @@ -0,0 +1,225 @@ +// Code generated by counterfeiter. DO NOT EDIT. +package same + +import ( + "sync" +) + +type FakeSomeInterface struct { + DoASliceStub func([]byte) + doASliceMutex sync.RWMutex + doASliceArgsForCall []struct { + arg1 []byte + } + DoAnArrayStub func([4]byte) + doAnArrayMutex sync.RWMutex + doAnArrayArgsForCall []struct { + arg1 [4]byte + } + DoNothingStub func() + doNothingMutex sync.RWMutex + doNothingArgsForCall []struct { + } + DoThingsStub func(string, uint64) (int, error) + doThingsMutex sync.RWMutex + doThingsArgsForCall []struct { + arg1 string + arg2 uint64 + } + doThingsReturns struct { + result1 int + result2 error + } + doThingsReturnsOnCall map[int]struct { + result1 int + result2 error + } + invocations map[string][][]interface{} + invocationsMutex sync.RWMutex +} + +func (fake *FakeSomeInterface) DoASlice(arg1 []byte) { + var arg1Copy []byte + if arg1 != nil { + arg1Copy = make([]byte, len(arg1)) + copy(arg1Copy, arg1) + } + fake.doASliceMutex.Lock() + fake.doASliceArgsForCall = append(fake.doASliceArgsForCall, struct { + arg1 []byte + }{arg1Copy}) + fake.recordInvocation("DoASlice", []interface{}{arg1Copy}) + fake.doASliceMutex.Unlock() + if fake.DoASliceStub != nil { + fake.DoASliceStub(arg1) + } +} + +func (fake *FakeSomeInterface) DoASliceCallCount() int { + fake.doASliceMutex.RLock() + defer fake.doASliceMutex.RUnlock() + return len(fake.doASliceArgsForCall) +} + +func (fake *FakeSomeInterface) DoASliceCalls(stub func([]byte)) { + fake.doASliceMutex.Lock() + defer fake.doASliceMutex.Unlock() + fake.DoASliceStub = stub +} + +func (fake *FakeSomeInterface) DoASliceArgsForCall(i int) []byte { + fake.doASliceMutex.RLock() + defer fake.doASliceMutex.RUnlock() + argsForCall := fake.doASliceArgsForCall[i] + return argsForCall.arg1 +} + +func (fake *FakeSomeInterface) DoAnArray(arg1 [4]byte) { + fake.doAnArrayMutex.Lock() + fake.doAnArrayArgsForCall = append(fake.doAnArrayArgsForCall, struct { + arg1 [4]byte + }{arg1}) + fake.recordInvocation("DoAnArray", []interface{}{arg1}) + fake.doAnArrayMutex.Unlock() + if fake.DoAnArrayStub != nil { + fake.DoAnArrayStub(arg1) + } +} + +func (fake *FakeSomeInterface) DoAnArrayCallCount() int { + fake.doAnArrayMutex.RLock() + defer fake.doAnArrayMutex.RUnlock() + return len(fake.doAnArrayArgsForCall) +} + +func (fake *FakeSomeInterface) DoAnArrayCalls(stub func([4]byte)) { + fake.doAnArrayMutex.Lock() + defer fake.doAnArrayMutex.Unlock() + fake.DoAnArrayStub = stub +} + +func (fake *FakeSomeInterface) DoAnArrayArgsForCall(i int) [4]byte { + fake.doAnArrayMutex.RLock() + defer fake.doAnArrayMutex.RUnlock() + argsForCall := fake.doAnArrayArgsForCall[i] + return argsForCall.arg1 +} + +func (fake *FakeSomeInterface) DoNothing() { + fake.doNothingMutex.Lock() + fake.doNothingArgsForCall = append(fake.doNothingArgsForCall, struct { + }{}) + fake.recordInvocation("DoNothing", []interface{}{}) + fake.doNothingMutex.Unlock() + if fake.DoNothingStub != nil { + fake.DoNothingStub() + } +} + +func (fake *FakeSomeInterface) DoNothingCallCount() int { + fake.doNothingMutex.RLock() + defer fake.doNothingMutex.RUnlock() + return len(fake.doNothingArgsForCall) +} + +func (fake *FakeSomeInterface) DoNothingCalls(stub func()) { + fake.doNothingMutex.Lock() + defer fake.doNothingMutex.Unlock() + fake.DoNothingStub = stub +} + +func (fake *FakeSomeInterface) DoThings(arg1 string, arg2 uint64) (int, error) { + fake.doThingsMutex.Lock() + ret, specificReturn := fake.doThingsReturnsOnCall[len(fake.doThingsArgsForCall)] + fake.doThingsArgsForCall = append(fake.doThingsArgsForCall, struct { + arg1 string + arg2 uint64 + }{arg1, arg2}) + fake.recordInvocation("DoThings", []interface{}{arg1, arg2}) + fake.doThingsMutex.Unlock() + if fake.DoThingsStub != nil { + return fake.DoThingsStub(arg1, arg2) + } + if specificReturn { + return ret.result1, ret.result2 + } + fakeReturns := fake.doThingsReturns + return fakeReturns.result1, fakeReturns.result2 +} + +func (fake *FakeSomeInterface) DoThingsCallCount() int { + fake.doThingsMutex.RLock() + defer fake.doThingsMutex.RUnlock() + return len(fake.doThingsArgsForCall) +} + +func (fake *FakeSomeInterface) DoThingsCalls(stub func(string, uint64) (int, error)) { + fake.doThingsMutex.Lock() + defer fake.doThingsMutex.Unlock() + fake.DoThingsStub = stub +} + +func (fake *FakeSomeInterface) DoThingsArgsForCall(i int) (string, uint64) { + fake.doThingsMutex.RLock() + defer fake.doThingsMutex.RUnlock() + argsForCall := fake.doThingsArgsForCall[i] + return argsForCall.arg1, argsForCall.arg2 +} + +func (fake *FakeSomeInterface) DoThingsReturns(result1 int, result2 error) { + fake.doThingsMutex.Lock() + defer fake.doThingsMutex.Unlock() + fake.DoThingsStub = nil + fake.doThingsReturns = struct { + result1 int + result2 error + }{result1, result2} +} + +func (fake *FakeSomeInterface) DoThingsReturnsOnCall(i int, result1 int, result2 error) { + fake.doThingsMutex.Lock() + defer fake.doThingsMutex.Unlock() + fake.DoThingsStub = nil + if fake.doThingsReturnsOnCall == nil { + fake.doThingsReturnsOnCall = make(map[int]struct { + result1 int + result2 error + }) + } + fake.doThingsReturnsOnCall[i] = struct { + result1 int + result2 error + }{result1, result2} +} + +func (fake *FakeSomeInterface) Invocations() map[string][][]interface{} { + fake.invocationsMutex.RLock() + defer fake.invocationsMutex.RUnlock() + fake.doASliceMutex.RLock() + defer fake.doASliceMutex.RUnlock() + fake.doAnArrayMutex.RLock() + defer fake.doAnArrayMutex.RUnlock() + fake.doNothingMutex.RLock() + defer fake.doNothingMutex.RUnlock() + fake.doThingsMutex.RLock() + defer fake.doThingsMutex.RUnlock() + copiedInvocations := map[string][][]interface{}{} + for key, value := range fake.invocations { + copiedInvocations[key] = value + } + return copiedInvocations +} + +func (fake *FakeSomeInterface) recordInvocation(key string, args []interface{}) { + fake.invocationsMutex.Lock() + defer fake.invocationsMutex.Unlock() + if fake.invocations == nil { + fake.invocations = map[string][][]interface{}{} + } + if fake.invocations[key] == nil { + fake.invocations[key] = [][]interface{}{} + } + fake.invocations[key] = append(fake.invocations[key], args) +} + +var _ SomeInterface = new(FakeSomeInterface) diff --git a/fixtures/samefn/fn.go b/fixtures/samefn/fn.go new file mode 100644 index 0000000..0d13944 --- /dev/null +++ b/fixtures/samefn/fn.go @@ -0,0 +1,5 @@ +package samefn // import "github.com/maxbrunsfeld/counterfeiter/v6/fixtures/samefn" + +//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o same_fake.go . SomethingFactory + +type SomethingFactory func(string, map[string]interface{}) string diff --git a/fixtures/samefn/notexported.go b/fixtures/samefn/notexported.go new file mode 100644 index 0000000..3b061dd --- /dev/null +++ b/fixtures/samefn/notexported.go @@ -0,0 +1,5 @@ +package samefn // import "github.com/maxbrunsfeld/counterfeiter/v6/fixtures/samefn" + +//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o notexported_fake.go . somethingNotExportedFactory + +type somethingNotExportedFactory func(string, map[string]interface{}) string diff --git a/fixtures/samefn/notexported_fake.go b/fixtures/samefn/notexported_fake.go new file mode 100644 index 0000000..70626cd --- /dev/null +++ b/fixtures/samefn/notexported_fake.go @@ -0,0 +1,108 @@ +// Code generated by counterfeiter. DO NOT EDIT. +package samefn + +import ( + "sync" +) + +type FakeSomethingNotExportedFactory struct { + Stub func(string, map[string]interface{}) string + mutex sync.RWMutex + argsForCall []struct { + arg1 string + arg2 map[string]interface{} + } + returns struct { + result1 string + } + returnsOnCall map[int]struct { + result1 string + } + invocations map[string][][]interface{} + invocationsMutex sync.RWMutex +} + +func (fake *FakeSomethingNotExportedFactory) Spy(arg1 string, arg2 map[string]interface{}) string { + fake.mutex.Lock() + ret, specificReturn := fake.returnsOnCall[len(fake.argsForCall)] + fake.argsForCall = append(fake.argsForCall, struct { + arg1 string + arg2 map[string]interface{} + }{arg1, arg2}) + fake.recordInvocation("somethingNotExportedFactory", []interface{}{arg1, arg2}) + fake.mutex.Unlock() + if fake.Stub != nil { + return fake.Stub(arg1, arg2) + } + if specificReturn { + return ret.result1 + } + return fake.returns.result1 +} + +func (fake *FakeSomethingNotExportedFactory) CallCount() int { + fake.mutex.RLock() + defer fake.mutex.RUnlock() + return len(fake.argsForCall) +} + +func (fake *FakeSomethingNotExportedFactory) Calls(stub func(string, map[string]interface{}) string) { + fake.mutex.Lock() + defer fake.mutex.Unlock() + fake.Stub = stub +} + +func (fake *FakeSomethingNotExportedFactory) ArgsForCall(i int) (string, map[string]interface{}) { + fake.mutex.RLock() + defer fake.mutex.RUnlock() + return fake.argsForCall[i].arg1, fake.argsForCall[i].arg2 +} + +func (fake *FakeSomethingNotExportedFactory) Returns(result1 string) { + fake.mutex.Lock() + defer fake.mutex.Unlock() + fake.Stub = nil + fake.returns = struct { + result1 string + }{result1} +} + +func (fake *FakeSomethingNotExportedFactory) ReturnsOnCall(i int, result1 string) { + fake.mutex.Lock() + defer fake.mutex.Unlock() + fake.Stub = nil + if fake.returnsOnCall == nil { + fake.returnsOnCall = make(map[int]struct { + result1 string + }) + } + fake.returnsOnCall[i] = struct { + result1 string + }{result1} +} + +func (fake *FakeSomethingNotExportedFactory) Invocations() map[string][][]interface{} { + fake.invocationsMutex.RLock() + defer fake.invocationsMutex.RUnlock() + fake.mutex.RLock() + defer fake.mutex.RUnlock() + copiedInvocations := map[string][][]interface{}{} + for key, value := range fake.invocations { + copiedInvocations[key] = value + } + return copiedInvocations +} + +func (fake *FakeSomethingNotExportedFactory) recordInvocation(key string, args []interface{}) { + fake.invocationsMutex.Lock() + defer fake.invocationsMutex.Unlock() + if fake.invocations == nil { + fake.invocations = map[string][][]interface{}{} + } + if fake.invocations[key] == nil { + fake.invocations[key] = [][]interface{}{} + } + fake.invocations[key] = append(fake.invocations[key], args) +} + +var _ somethingNotExportedFactory = new(FakeSomethingNotExportedFactory).Spy diff --git a/fixtures/samefn/same_fake.go b/fixtures/samefn/same_fake.go new file mode 100644 index 0000000..f4300f7 --- /dev/null +++ b/fixtures/samefn/same_fake.go @@ -0,0 +1,108 @@ +// Code generated by counterfeiter. DO NOT EDIT. +package samefn + +import ( + "sync" +) + +type FakeSomethingFactory struct { + Stub func(string, map[string]interface{}) string + mutex sync.RWMutex + argsForCall []struct { + arg1 string + arg2 map[string]interface{} + } + returns struct { + result1 string + } + returnsOnCall map[int]struct { + result1 string + } + invocations map[string][][]interface{} + invocationsMutex sync.RWMutex +} + +func (fake *FakeSomethingFactory) Spy(arg1 string, arg2 map[string]interface{}) string { + fake.mutex.Lock() + ret, specificReturn := fake.returnsOnCall[len(fake.argsForCall)] + fake.argsForCall = append(fake.argsForCall, struct { + arg1 string + arg2 map[string]interface{} + }{arg1, arg2}) + fake.recordInvocation("SomethingFactory", []interface{}{arg1, arg2}) + fake.mutex.Unlock() + if fake.Stub != nil { + return fake.Stub(arg1, arg2) + } + if specificReturn { + return ret.result1 + } + return fake.returns.result1 +} + +func (fake *FakeSomethingFactory) CallCount() int { + fake.mutex.RLock() + defer fake.mutex.RUnlock() + return len(fake.argsForCall) +} + +func (fake *FakeSomethingFactory) Calls(stub func(string, map[string]interface{}) string) { + fake.mutex.Lock() + defer fake.mutex.Unlock() + fake.Stub = stub +} + +func (fake *FakeSomethingFactory) ArgsForCall(i int) (string, map[string]interface{}) { + fake.mutex.RLock() + defer fake.mutex.RUnlock() + return fake.argsForCall[i].arg1, fake.argsForCall[i].arg2 +} + +func (fake *FakeSomethingFactory) Returns(result1 string) { + fake.mutex.Lock() + defer fake.mutex.Unlock() + fake.Stub = nil + fake.returns = struct { + result1 string + }{result1} +} + +func (fake *FakeSomethingFactory) ReturnsOnCall(i int, result1 string) { + fake.mutex.Lock() + defer fake.mutex.Unlock() + fake.Stub = nil + if fake.returnsOnCall == nil { + fake.returnsOnCall = make(map[int]struct { + result1 string + }) + } + fake.returnsOnCall[i] = struct { + result1 string + }{result1} +} + +func (fake *FakeSomethingFactory) Invocations() map[string][][]interface{} { + fake.invocationsMutex.RLock() + defer fake.invocationsMutex.RUnlock() + fake.mutex.RLock() + defer fake.mutex.RUnlock() + copiedInvocations := map[string][][]interface{}{} + for key, value := range fake.invocations { + copiedInvocations[key] = value + } + return copiedInvocations +} + +func (fake *FakeSomethingFactory) recordInvocation(key string, args []interface{}) { + fake.invocationsMutex.Lock() + defer fake.invocationsMutex.Unlock() + if fake.invocations == nil { + fake.invocations = map[string][][]interface{}{} + } + if fake.invocations[key] == nil { + fake.invocations[key] = [][]interface{}{} + } + fake.invocations[key] = append(fake.invocations[key], args) +} + +var _ SomethingFactory = new(FakeSomethingFactory).Spy diff --git a/generator/fake.go b/generator/fake.go index eab38e7..c9204f1 100644 --- a/generator/fake.go +++ b/generator/fake.go @@ -29,6 +29,7 @@ type Fake struct { Package *packages.Package Target *types.TypeName Mode FakeMode + DestinationPath string DestinationPackage string Name string TargetAlias string @@ -37,6 +38,8 @@ type Fake struct { Imports Imports Methods []Method Function Method + SamePackage bool + TestPackage bool } // Method is a method of the interface. @@ -48,14 +51,16 @@ type Method struct { // NewFake returns a Fake that loads the package and finds the interface or the // function. -func NewFake(fakeMode FakeMode, targetName string, packagePath string, fakeName string, destinationPackage string, workingDir string, cache Cacher) (*Fake, error) { +func NewFake(fakeMode FakeMode, targetName string, packagePath string, fakeName string, destinationPath string, testPackage bool, workingDir string, cache Cacher) (*Fake, error) { f := &Fake{ - TargetName: targetName, - TargetPackage: packagePath, - Name: fakeName, - Mode: fakeMode, - DestinationPackage: destinationPackage, - Imports: newImports(), + TargetName: targetName, + TargetPackage: packagePath, + Name: fakeName, + Mode: fakeMode, + DestinationPath: destinationPath, + SamePackage: destinationPath == packagePath, + TestPackage: testPackage, + Imports: newImports(), } f.Imports.Add("sync", "sync") @@ -113,6 +118,16 @@ func isExported(s string) bool { return unicode.IsUpper(r) } +func restrictToValidPackageName(input string) string { + return strings.Map(func(r rune) rune { + if (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') || r == '_' { + return r + } else { + return -1 + } + }, input) +} + // Generate uses the Fake to generate an implementation, optionally running // goimports on the output. func (f *Fake) Generate(runImports bool) ([]byte, error) { diff --git a/generator/function_template.go b/generator/function_template.go index 533ef72..d103774 100644 --- a/generator/function_template.go +++ b/generator/function_template.go @@ -148,7 +148,7 @@ func (fake *{{.Name}}) recordInvocation(key string, args []interface{}) { fake.invocations[key] = append(fake.invocations[key], args) } -{{if IsExported .TargetName -}} -var _ {{.TargetAlias}}.{{.TargetName}} = new({{.Name}}).Spy +{{if or (IsExported .TargetName) (eq .TargetAlias "") -}} +var _ {{with .TargetAlias}}{{.}}.{{end}}{{.TargetName}} = new({{.Name}}).Spy {{- end}} ` diff --git a/generator/generator_internals_test.go b/generator/generator_internals_test.go index ace056c..c6c7387 100644 --- a/generator/generator_internals_test.go +++ b/generator/generator_internals_test.go @@ -31,7 +31,7 @@ func testGenerator(t *testing.T, when spec.G, it spec.S) { when("the target is a nonexistent package", func() { it("errors", func() { c := &Cache{} - f, err = NewFake(InterfaceOrFunction, "NonExistent", "nonexistentpackage", "FakeNonExistent", "nonexistentpackagefakes", "", c) + f, err = NewFake(InterfaceOrFunction, "NonExistent", "nonexistentpackage", "FakeNonExistent", "nonexistentpackagefakes", false, "", c) Expect(err).To(HaveOccurred()) Expect(f).To(BeNil()) }) @@ -40,7 +40,7 @@ func testGenerator(t *testing.T, when spec.G, it spec.S) { when("the target is a package with a nonexistent interface", func() { it("errors", func() { c := &Cache{} - f, err = NewFake(InterfaceOrFunction, "NonExistent", "os", "FakeNonExistent", "osfakes", "", c) + f, err = NewFake(InterfaceOrFunction, "NonExistent", "os", "FakeNonExistent", "osfakes", false, "", c) Expect(err).To(HaveOccurred()) Expect(f).To(BeNil()) }) @@ -49,7 +49,7 @@ func testGenerator(t *testing.T, when spec.G, it spec.S) { when("the target is an interface that exists", func() { it("succeeds", func() { c := &Cache{} - f, err = NewFake(InterfaceOrFunction, "FileInfo", "os", "FakeFileInfo", "osfakes", "", c) + f, err = NewFake(InterfaceOrFunction, "FileInfo", "os", "FakeFileInfo", "osfakes", false, "", c) Expect(err).NotTo(HaveOccurred()) Expect(f).NotTo(BeNil()) Expect(f.TargetAlias).To(Equal("os")) @@ -80,7 +80,7 @@ func testGenerator(t *testing.T, when spec.G, it spec.S) { when("the target is a function that exists", func() { it("succeeds", func() { c := &Cache{} - f, err = NewFake(InterfaceOrFunction, "HandlerFunc", "net/http", "FakeHandlerFunc", "httpfakes", "", c) + f, err = NewFake(InterfaceOrFunction, "HandlerFunc", "net/http", "FakeHandlerFunc", "httpfakes", false, "", c) Expect(err).NotTo(HaveOccurred()) Expect(f).NotTo(BeNil()) diff --git a/generator/interface_template.go b/generator/interface_template.go index 75f424d..7096732 100644 --- a/generator/interface_template.go +++ b/generator/interface_template.go @@ -162,7 +162,7 @@ func (fake *{{.Name}}) recordInvocation(key string, args []interface{}) { fake.invocations[key] = append(fake.invocations[key], args) } -{{if IsExported .TargetName -}} -var _ {{.TargetAlias}}.{{.TargetName}} = new({{.Name}}) +{{if or (IsExported .TargetName) (eq .TargetAlias "") -}} +var _ {{with .TargetAlias}}{{.}}.{{end}}{{.TargetName}} = new({{.Name}}) {{- end}} ` diff --git a/generator/loader.go b/generator/loader.go index 0adc201..10206f1 100644 --- a/generator/loader.go +++ b/generator/loader.go @@ -88,16 +88,26 @@ func (f *Fake) findPackage() error { f.Target = target f.Package = pkg f.TargetPackage = imports.VendorlessPath(pkg.PkgPath) - t := f.Imports.Add(pkg.Name, f.TargetPackage) - f.TargetAlias = t.Alias - if f.Mode != Package { - f.TargetName = target.Name() + if !f.SamePackage || f.TestPackage { + t := f.Imports.Add(pkg.Name, f.TargetPackage) + f.TargetAlias = t.Alias } if f.Mode == InterfaceOrFunction { + f.TargetName = target.Name() if !f.IsInterface() && !f.IsFunction() { - return fmt.Errorf("cannot generate an fake for %s because it is not an interface or function", f.TargetName) + return fmt.Errorf("cannot generate a fake for %s because it is not an interface or function", f.TargetName) + } + if f.SamePackage { + f.DestinationPackage = pkg.Name + if f.TestPackage { + f.DestinationPackage += "_test" + } + } else { + f.DestinationPackage = restrictToValidPackageName(filepath.Base(f.DestinationPath)) } + } else { + f.DestinationPackage = filepath.Base(f.TargetPackage) + "shim" } if f.IsInterface() { diff --git a/integration/roundtrip_test.go b/integration/roundtrip_test.go index b18afdd..68e9e1d 100644 --- a/integration/roundtrip_test.go +++ b/integration/roundtrip_test.go @@ -120,14 +120,15 @@ func runTests(useGopath bool, t *testing.T, when spec.G, it spec.S) { it("succeeds", func() { initModuleFunc() cache := &generator.FakeCache{} - f, err := generator.NewFake(generator.InterfaceOrFunction, "WriteCloser", "io", "FakeWriteCloser", "custom", baseDir, cache) + destinationPath := filepath.Join(baseDir, "fixturesfakes", "fake_write_closer.go") + f, err := generator.NewFake(generator.InterfaceOrFunction, "WriteCloser", "io", "FakeWriteCloser", filepath.Dir(destinationPath), false, baseDir, cache) Expect(err).NotTo(HaveOccurred()) b, err := f.Generate(true) // Flip to false to see output if goimports fails Expect(err).NotTo(HaveOccurred()) if writeToTestData { WriteOutput(b, filepath.Join("testdata", "output", "write_closer", "actual.go")) } - WriteOutput(b, filepath.Join(baseDir, "fixturesfakes", "fake_write_closer.go")) + WriteOutput(b, destinationPath) RunBuild(baseDir) b2, err := ioutil.ReadFile(filepath.Join("testdata", "expected_fake_writecloser.txt")) Expect(err).NotTo(HaveOccurred()) @@ -139,14 +140,15 @@ func runTests(useGopath bool, t *testing.T, when spec.G, it spec.S) { it("succeeds", func() { initModuleFunc() cache := &generator.FakeCache{} - f, err := generator.NewFake(generator.Package, "", "os", "Os", "custom", baseDir, cache) + destinationPath := filepath.Join(baseDir, "fixturesfakes", "fake_os.go") + f, err := generator.NewFake(generator.Package, "", "os", "Os", filepath.Dir(destinationPath), false, baseDir, cache) Expect(err).NotTo(HaveOccurred()) b, err := f.Generate(true) // Flip to false to see output if goimports fails Expect(err).NotTo(HaveOccurred()) if writeToTestData { WriteOutput(b, filepath.Join("testdata", "output", "package_mode", "actual.go")) } - WriteOutput(b, filepath.Join(baseDir, "fixturesfakes", "fake_os.go")) + WriteOutput(b, destinationPath) RunBuild(baseDir) }) }) @@ -175,14 +177,15 @@ func runTests(useGopath bool, t *testing.T, when spec.G, it spec.S) { WriteOutput([]byte(fmt.Sprintf("module github.com/maxbrunsfeld/counterfeiter/v6/fixtures%s\n", suffix)), filepath.Join(baseDir, "go.mod")) } cache := &generator.FakeCache{} - f, err := generator.NewFake(generator.InterfaceOrFunction, interfaceName, fmt.Sprintf("github.com/maxbrunsfeld/counterfeiter/v6/fixtures%s", suffix), "Fake"+interfaceName, "fixturesfakes", baseDir, cache) + destinationPath := filepath.Join(baseDir, "fixturesfakes", "fake_"+filename) + f, err := generator.NewFake(generator.InterfaceOrFunction, interfaceName, fmt.Sprintf("github.com/maxbrunsfeld/counterfeiter/v6/fixtures%s", suffix), "Fake"+interfaceName, filepath.Dir(destinationPath), false, baseDir, cache) Expect(err).NotTo(HaveOccurred()) b, err := f.Generate(true) // Flip to false to see output if goimports fails Expect(err).NotTo(HaveOccurred()) if writeToTestData { WriteOutput(b, filepath.Join("testdata", "output", strings.Replace(filename, ".go", "", -1), "actual.go")) } - WriteOutput(b, filepath.Join(baseDir, "fixturesfakes", "fake_"+filename)) + WriteOutput(b, destinationPath) RunBuild(baseDir) }) }) @@ -205,6 +208,10 @@ func runTests(useGopath bool, t *testing.T, when spec.G, it spec.S) { t("Something", "something.go", "") t("SomethingFactory", "typed_function.go", "") t("SyncSomething", "interface.go", "sync") + t("SomeInterface", "interface.go", "same") + t("someNotExportedInterface", "notexported.go", "same") + t("SomethingFactory", "fn.go", "samefn") + t("somethingNotExportedFactory", "notexported.go", "samefn") when("working with duplicate packages", func() { t := func(interfaceName string, offset string, fakePackageName string) { @@ -223,14 +230,15 @@ func runTests(useGopath bool, t *testing.T, when spec.G, it spec.S) { pkgPath = pkgPath + "/" + offset } cache := &generator.FakeCache{} - f, err := generator.NewFake(generator.InterfaceOrFunction, interfaceName, pkgPath, "Fake"+interfaceName, fakePackageName, baseDir, cache) + destinationPath := filepath.Join(baseDir, offset, fakePackageName, "fake_"+strings.ToLower(interfaceName)+".go") + f, err := generator.NewFake(generator.InterfaceOrFunction, interfaceName, pkgPath, "Fake"+interfaceName, filepath.Dir(destinationPath), false, baseDir, cache) Expect(err).NotTo(HaveOccurred()) b, err := f.Generate(false) // Flip to false to see output if goimports fails Expect(err).NotTo(HaveOccurred()) if writeToTestData { WriteOutput(b, filepath.Join("testdata", "output", "dup_"+strings.ToLower(interfaceName), "actual.go")) } - WriteOutput(b, filepath.Join(baseDir, offset, fakePackageName, "fake_"+strings.ToLower(interfaceName)+".go")) + WriteOutput(b, destinationPath) RunBuild(filepath.Join(baseDir, offset, fakePackageName)) }) }) diff --git a/integration/testdata/expected_fake_writecloser.txt b/integration/testdata/expected_fake_writecloser.txt index 36e337f..e8bc741 100644 --- a/integration/testdata/expected_fake_writecloser.txt +++ b/integration/testdata/expected_fake_writecloser.txt @@ -1,5 +1,5 @@ // Code generated by counterfeiter. DO NOT EDIT. -package custom +package fixturesfakes import ( "io" diff --git a/main.go b/main.go index 565e5c7..f102eb5 100644 --- a/main.go +++ b/main.go @@ -113,7 +113,7 @@ func doGenerate(workingDir string, args *arguments.ParsedArguments, cache genera if args.GenerateInterfaceAndShimFromPackageDirectory { mode = generator.Package } - f, err := generator.NewFake(mode, args.InterfaceName, args.PackagePath, args.FakeImplName, args.DestinationPackageName, workingDir, cache) + f, err := generator.NewFake(mode, args.InterfaceName, args.PackagePath, args.FakeImplName, args.DestinationPackagePath, args.TestPackage, workingDir, cache) if err != nil { return nil, err }