diff --git a/README.md b/README.md index 149358a..150e6fc 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,9 @@ Flags: --file, -f, GOFILE string the name of the file to parse --help, -h bool prints the usage --interface, -i string the name of the interface to fake + --name, -n string the name to give the generated type --output, -o string the name of the file to write + --package, -p string the name of the package that contains the interface --version, -v bool prints the version ``` diff --git a/acceptance/fixtures/fakes/named_interface.go b/acceptance/fixtures/fakes/named_interface.go new file mode 100644 index 0000000..f6cb2a3 --- /dev/null +++ b/acceptance/fixtures/fakes/named_interface.go @@ -0,0 +1,32 @@ +package fakes + +import ( + "bytes" + "io" + "sync" +) + +type SomeNamedInterface struct { + SomeMethodCall struct { + sync.Mutex + CallCount int + Receives struct { + SomeParam *bytes.Buffer + } + Returns struct { + SomeResult io.Reader + } + Stub func(*bytes.Buffer) io.Reader + } +} + +func (f *SomeNamedInterface) SomeMethod(param1 *bytes.Buffer) io.Reader { + f.SomeMethodCall.Lock() + defer f.SomeMethodCall.Unlock() + f.SomeMethodCall.CallCount++ + f.SomeMethodCall.Receives.SomeParam = param1 + if f.SomeMethodCall.Stub != nil { + return f.SomeMethodCall.Stub(param1) + } + return f.SomeMethodCall.Returns.SomeResult +} diff --git a/acceptance/fixtures/interfaces.go b/acceptance/fixtures/interfaces.go index df37afd..9766433 100644 --- a/acceptance/fixtures/interfaces.go +++ b/acceptance/fixtures/interfaces.go @@ -31,3 +31,7 @@ type DuplicateArgumentInterface interface { type FunctionInterface interface { FuncMethod(func(string) error) func(int) bool } + +type NamedInterface interface { + SomeMethod(someParam *bytes.Buffer) (someResult io.Reader) +} diff --git a/acceptance/generate_test.go b/acceptance/generate_test.go index 45d9f84..0864747 100644 --- a/acceptance/generate_test.go +++ b/acceptance/generate_test.go @@ -36,13 +36,9 @@ var _ = Describe("faux", func() { }) DescribeTable("fake generation", - func(filePath, packagePath, interfaceName, fixtureFileName string) { - command := exec.Command(executable, - "--file", filePath, - "--package", packagePath, - "--output", outputFile, - "--interface", interfaceName) - + func(fixture string, flags ...string) { + flags = append(flags, "--output", outputFile) + command := exec.Command(executable, flags...) session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter) Expect(err).NotTo(HaveOccurred()) Eventually(session, "10s").Should(gexec.Exit(0)) @@ -50,20 +46,21 @@ var _ = Describe("faux", func() { outputContent, err := ioutil.ReadFile(outputFile) Expect(err).NotTo(HaveOccurred()) - expectedContent, err := ioutil.ReadFile(filepath.Join("fixtures", "fakes", fixtureFileName)) + expectedContent, err := ioutil.ReadFile(filepath.Join("fixtures", "fakes", fixture)) Expect(err).NotTo(HaveOccurred()) Expect(string(outputContent)).To(ContainSubstring(string(expectedContent))) }, - Entry("simple", "./fixtures/interfaces.go", "", "SimpleInterface", "simple_interface.go"), - Entry("channels", "./fixtures/interfaces.go", "", "ChanInterface", "chan_interface.go"), - Entry("duplicate arguments", "./fixtures/interfaces.go", "", "DuplicateArgumentInterface", "duplicate_argument_interface.go"), - Entry("gomod", "./fixtures/interfaces.go", "", "ModuleInterface", "module_interface.go"), - Entry("gopath", "", "github.com/pivotal-cf/jhanda", "Command", "jhanda_command.go"), - Entry("stdlib", "", "io", "Reader", "io_reader.go"), - Entry("variadic", "./fixtures/interfaces.go", "", "VariadicInterface", "variadic_interface.go"), - Entry("functions", "./fixtures/interfaces.go", "", "FunctionInterface", "function_interface.go"), + Entry("simple", "simple_interface.go", "--file", "./fixtures/interfaces.go", "--interface", "SimpleInterface"), + Entry("channels", "chan_interface.go", "--file", "./fixtures/interfaces.go", "--interface", "ChanInterface"), + Entry("duplicate arguments", "duplicate_argument_interface.go", "--file", "./fixtures/interfaces.go", "--interface", "DuplicateArgumentInterface"), + Entry("gomod", "module_interface.go", "--file", "./fixtures/interfaces.go", "--interface", "ModuleInterface"), + Entry("gopath", "jhanda_command.go", "--package", "github.com/pivotal-cf/jhanda", "--interface", "Command"), + Entry("stdlib", "io_reader.go", "--package", "io", "--interface", "Reader"), + Entry("variadic", "variadic_interface.go", "--file", "./fixtures/interfaces.go", "--interface", "VariadicInterface"), + Entry("functions", "function_interface.go", "--file", "./fixtures/interfaces.go", "--interface", "FunctionInterface"), + Entry("name", "named_interface.go", "--file", "./fixtures/interfaces.go", "--interface", "NamedInterface", "--name", "SomeNamedInterface"), ) Context("when the source file is provided via an environment variable", func() { diff --git a/main.go b/main.go index 1b5f86c..8c232f7 100644 --- a/main.go +++ b/main.go @@ -26,6 +26,7 @@ func main() { File string `long:"file" short:"f" env:"GOFILE" description:"the name of the file to parse"` Output string `long:"output" short:"o" description:"the name of the file to write"` Interface string `long:"interface" short:"i" description:"the name of the interface to fake"` + Name string `long:"name" short:"n" description:"the name to give the generated type"` } stdout := log.New(os.Stdout, "", 0) @@ -73,6 +74,10 @@ Flags: stderr.Fatal(err) } + if options.Name != "" { + iface.Name = options.Name + } + err = os.MkdirAll(filepath.Dir(options.Output), 0755) if err != nil { stderr.Fatalf("could not create directory: %s", err)