Skip to content

Commit

Permalink
Adds support for named fakes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Moran committed Sep 7, 2021
1 parent cf71c4d commit 3acf554
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 16 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
32 changes: 32 additions & 0 deletions acceptance/fixtures/fakes/named_interface.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 4 additions & 0 deletions acceptance/fixtures/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
29 changes: 13 additions & 16 deletions acceptance/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,31 @@ 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))

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() {
Expand Down
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 3acf554

Please sign in to comment.