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

#262: Adding "test" mode for the Fakes to get generated intot he same package #263

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
21 changes: 0 additions & 21 deletions .circleci/config.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ updates:
time: "13:00"
open-pull-requests-limit: 100
reviewers:
- "joefitzgerald"
- "ikolomiyets"
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# `counterfeiter` [![Go](https://github.com/maxbrunsfeld/counterfeiter/actions/workflows/go.yml/badge.svg)](https://github.com/maxbrunsfeld/counterfeiter/actions/workflows/go.yml) [![CircleCI](https://circleci.com/gh/maxbrunsfeld/counterfeiter.svg?style=svg)](https://circleci.com/gh/maxbrunsfeld/counterfeiter) [![Build status](https://ci.appveyor.com/api/projects/status/0j2v7pt06lp9yanm/branch/master?svg=true)](https://ci.appveyor.com/project/maxbrunsfeld/counterfeiter/branch/master)
# `counterfeiter` [![Go](https://github.com/ikolomiyets/counterfeiter/actions/workflows/go.yml/badge.svg)](https://github.com/ikolomiyets/counterfeiter/actions/workflows/go.yml) [![CircleCI](https://circleci.com/gh/ikolomiyets/counterfeiter.svg?style=svg)](https://circleci.com/gh/ikolomiyets/counterfeiter) [![Build status](https://ci.appveyor.com/api/projects/status/0j2v7pt06lp9yanm/branch/master?svg=true)](https://ci.appveyor.com/project/ikolomiyets/counterfeiter/branch/master)

When writing unit-tests for an object, it is often useful to have fake implementations
of the object's collaborators. In go, such fake implementations cannot be generated
Expand Down Expand Up @@ -34,7 +34,7 @@ $ cat tools/tools.go
package tools

import (
_ "github.com/maxbrunsfeld/counterfeiter/v6"
_ "github.com/ikolomiyets/counterfeiter/v6"
)

// This file imports packages that are used when running go generate, or used
Expand All @@ -52,7 +52,7 @@ $ cat myinterface.go
```go
package foo

//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . MySpecialInterface
//go:generate go run github.com/ikolomiyets/counterfeiter/v6 . MySpecialInterface

type MySpecialInterface interface {
DoThings(string, uint64) (int, error)
Expand All @@ -78,7 +78,7 @@ $ cat myinterface.go
package foo

// You only need **one** of these per package!
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate
//go:generate go run github.com/ikolomiyets/counterfeiter/v6 -generate

// You will add lots of directives like these in the same package...
//counterfeiter:generate . MySpecialInterface
Expand Down Expand Up @@ -112,11 +112,11 @@ $ go generate ./...
You can use the following command to invoke `counterfeiter` from within a go module:

```shell
$ go run github.com/maxbrunsfeld/counterfeiter/v6
$ go run github.com/ikolomiyets/counterfeiter/v6

USAGE
counterfeiter
[-generate] [-o <output-path>] [-p] [--fake-name <fake-name>]
[-generate] [-o <output-path>] [-p] [-t] [--fake-name <fake-name>]
[<source-path>] <interface> [-]
```

Expand All @@ -125,12 +125,12 @@ USAGE
This is unnecessary if you're using the approach described above, but does allow you to invoke `counterfeiter` in your shell _outside_ of a module:

```shell
$ GO111MODULE=off go get -u github.com/maxbrunsfeld/counterfeiter
$ GO111MODULE=off go get -u github.com/ikolomiyets/counterfeiter
$ counterfeiter

USAGE
counterfeiter
[-generate] [-o <output-path>] [-p] [--fake-name <fake-name>]
[-generate] [-o <output-path>] [-p] [-t] [--fake-name <fake-name>]
[<source-path>] <interface> [-]
```

Expand All @@ -151,7 +151,7 @@ type MySpecialInterface interface {
```

```shell
$ go run github.com/maxbrunsfeld/counterfeiter/v6 path/to/foo MySpecialInterface
$ go run github.com/ikolomiyets/counterfeiter/v6 path/to/foo MySpecialInterface
Wrote `FakeMySpecialInterface` to `path/to/foo/foofakes/fake_my_special_interface.go`
```

Expand Down Expand Up @@ -187,14 +187,14 @@ Expect(num).To(Equal(3))
Expect(err).To(Equal(errors.New("the-error")))
```

For more examples of using the `counterfeiter` API, look at [some of the provided examples](https://github.com/maxbrunsfeld/counterfeiter/blob/master/generated_fakes_test.go).
For more examples of using the `counterfeiter` API, look at [some of the provided examples](https://github.com/ikolomiyets/counterfeiter/blob/master/generated_fakes_test.go).

### Generating Test Doubles For Third Party Interfaces

For third party interfaces, you can specify the interface using the alternative syntax `<package>.<interface>`, for example:

```shell
$ go run github.com/maxbrunsfeld/counterfeiter/v6 github.com/go-redis/redis.Pipeliner
$ go run github.com/ikolomiyets/counterfeiter/v6 github.com/go-redis/redis.Pipeliner
```

### Running The Tests For `counterfeiter`
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ shallow_clone: true
clone_depth: 10
clone_folder: c:\projects\counterfeiter
image: Visual Studio 2019
stack: go 1.20
stack: go 1.21

environment:
GOPATH: c:\gopath
Expand Down
24 changes: 21 additions & 3 deletions arguments/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ func New(args []string, workingDir string, evaler Evaler, stater Stater) (*Parse
false,
"Suppress status statements",
)

testFlag := fs.Bool(
"t",
false,
"Generate fakes in the same package into a file with _test suffix",
)
helpFlag := fs.Bool(
"help",
false,
Expand All @@ -67,6 +73,10 @@ func New(args []string, workingDir string, evaler Evaler, stater Stater) (*Parse
return nil, errors.New(usage)
}

if *testFlag && (*packageFlag || *outputPathFlag != "") {
return nil, errors.New(usage)
}

packageMode := *packageFlag
result := &ParsedArguments{
PrintToStdOut: any(args, "-"),
Expand All @@ -84,7 +94,7 @@ 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.parseOutputPath(packageMode, workingDir, *outputPathFlag, *testFlag, fs.Args())
result.parseDestinationPackageName(packageMode, fs.Args())
result.parsePackagePath(packageMode, fs.Args())
return result, nil
Expand Down Expand Up @@ -136,13 +146,16 @@ func (a *ParsedArguments) parseFakeName(packageMode bool, fakeName string, args
a.FakeImplName = fakeName
}

func (a *ParsedArguments) parseOutputPath(packageMode bool, workingDir string, outputPath string, args []string) {
func (a *ParsedArguments) parseOutputPath(packageMode bool, workingDir string, outputPath string, testFlag bool, args []string) {
outputPathIsFilename := false
if strings.HasSuffix(outputPath, ".go") {
outputPathIsFilename = true
}
snakeCaseName := strings.ToLower(camelRegexp.ReplaceAllString(a.FakeImplName, "${1}_${2}"))

if testFlag {
snakeCaseName += "_test"
}
if outputPath != "" {
if !filepath.IsAbs(outputPath) {
outputPath = filepath.Join(workingDir, outputPath)
Expand All @@ -164,7 +177,12 @@ func (a *ParsedArguments) parseOutputPath(packageMode bool, workingDir string, o
if len(args) > 1 {
d = a.SourcePackageDir
}
a.OutputPath = filepath.Join(d, packageNameForPath(d), snakeCaseName+".go")

if testFlag {
a.OutputPath = filepath.Join(d, snakeCaseName+".go")
} else {
a.OutputPath = filepath.Join(d, packageNameForPath(d), snakeCaseName+".go")
}
}

func (a *ParsedArguments) parseDestinationPackageName(packageMode bool, args []string) {
Expand Down
41 changes: 40 additions & 1 deletion arguments/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !windows
// +build !windows

package arguments_test
Expand All @@ -14,7 +15,7 @@ import (

"testing"

"github.com/maxbrunsfeld/counterfeiter/v6/arguments"
"github.com/ikolomiyets/counterfeiter/v6/arguments"

. "github.com/onsi/gomega"
"github.com/sclevine/spec"
Expand Down Expand Up @@ -107,6 +108,44 @@ func testParsingArguments(t *testing.T, when spec.G, it spec.S) {
})
})

when("when a single argument is provided with the test flag", func() {
it.Before(func() {
args = []string{"counterfeiter", "-t", "someonesinterfaces.AnInterface"}
justBefore()
})

it("sets PrintToStdOut to false", func() {
Expect(parsedArgs.PrintToStdOut).To(BeFalse())
})

it("provides a name for the fake implementing the interface", func() {
Expect(parsedArgs.FakeImplName).To(Equal("FakeAnInterface"))
})

it("provides a path for the interface source", func() {
Expect(parsedArgs.PackagePath).To(Equal("someonesinterfaces"))
})

it("treats the last segment as the interface to counterfeit", func() {
Expect(parsedArgs.InterfaceName).To(Equal("AnInterface"))
})

it("snake cases the filename for the output directory", func() {
fmt.Println(parsedArgs.OutputPath)
fmt.Println(filepath.Join(
workingDir,
"workspacefakes",
"fake_an_interface.go",
))
Expect(parsedArgs.OutputPath).To(Equal(
filepath.Join(
workingDir,
"fake_an_interface_test.go",
),
))
})
})

when("when a single argument is provided with the output directory", func() {
it.Before(func() {
args = []string{"counterfeiter", "-o", "/tmp/foo", "io.Writer"}
Expand Down
3 changes: 2 additions & 1 deletion arguments/parser_windows_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build windows
// +build windows

package arguments_test
Expand All @@ -11,7 +12,7 @@ import (

"testing"

"github.com/maxbrunsfeld/counterfeiter/v6/arguments"
"github.com/ikolomiyets/counterfeiter/v6/arguments"

. "github.com/onsi/gomega"
"github.com/sclevine/spec"
Expand Down
20 changes: 17 additions & 3 deletions arguments/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package arguments
const usage = `
USAGE
counterfeiter
[-generate>] [-o <output-path>] [-p] [--fake-name <fake-name>]
[-generate>] [-o <output-path>] [-p] [-t] [--fake-name <fake-name>]
[-header <header-file>]
[<source-path>] <interface> [-]

Expand Down Expand Up @@ -37,7 +37,7 @@ OPTIONS
go generate by adding the following to a .go file:

# runs counterfeiter in generate mode
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate
//go:generate go run github.com/ikolomiyets/counterfeiter/v6 -generate

example:
Add the following to a .go file:
Expand Down Expand Up @@ -90,7 +90,7 @@ OPTIONS

example:
# having the following code in a package ...
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -header ./generic.go.txt -generate
//go:generate go run github.com/ikolomiyets/counterfeiter/v6 -header ./generic.go.txt -generate
//counterfeiter:generate -header ./specific.go.txt . MyInterface
//counterfeiter:generate . MyOtherInterface
//counterfeiter:generate . MyThirdInterface
Expand All @@ -109,4 +109,18 @@ OPTIONS
example:
# writes "CoolThing" to ./mypackagefakes/cool_thing.go
counterfeiter --fake-name CoolThing ./mypackage MyInterface

-t
Generates fake in the same package as the original interface in
the test file ending with _test.go.

This helps to avoid cycle dependency issue when interface getting
mocked is defined in the package where it is used.

This options is mutually exclusive with '-p' (package mode)
and '-o' (output directory) options.

example:
# writes "FakeMyInterface" to ./mypackage/cool_thing_test.go
counterfeiter -t ./mypackage MyInterface
`
4 changes: 2 additions & 2 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"path/filepath"
"testing"

"github.com/maxbrunsfeld/counterfeiter/v6/arguments"
"github.com/maxbrunsfeld/counterfeiter/v6/generator"
"github.com/ikolomiyets/counterfeiter/v6/arguments"
"github.com/ikolomiyets/counterfeiter/v6/generator"
)

func BenchmarkDoGenerate(b *testing.B) {
Expand Down
2 changes: 1 addition & 1 deletion command/runner_internals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func testRegexp(t *testing.T, when spec.G, it spec.S) {
matches: false,
},
{
input: "//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . Intf",
input: "//go:generate go run github.com/ikolomiyets/counterfeiter/v6 . Intf",
matches: false,
},
{
Expand Down
4 changes: 2 additions & 2 deletions command/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"path/filepath"
"testing"

"github.com/maxbrunsfeld/counterfeiter/v6/command"
"github.com/ikolomiyets/counterfeiter/v6/command"
. "github.com/onsi/gomega"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
Expand Down Expand Up @@ -61,7 +61,7 @@ func testRunner(t *testing.T, when spec.G, it spec.S) {
i, err := command.Detect(filepath.Join(".", "..", "fixtures"), []string{"counterfeiter", ".", "AliasedInterface"}, true)
Expect(err).NotTo(HaveOccurred())
Expect(i).NotTo(BeNil())
Expect(len(i)).To(Equal(19))
Expect(len(i)).To(Equal(20))
Expect(i[0].File).To(Equal("aliased_interfaces.go"))
Expect(i[0].Line).To(Equal(6))
Expect(i[0].Args).To(HaveLen(3))
Expand Down
4 changes: 2 additions & 2 deletions fixtures/aliased_interfaces.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package fixtures

import alias "github.com/maxbrunsfeld/counterfeiter/v6/fixtures/another_package"
import alias "github.com/ikolomiyets/counterfeiter/v6/fixtures/another_package"

//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate
//go:generate go run github.com/ikolomiyets/counterfeiter/v6 -generate
//counterfeiter:generate . AliasedInterface

// AliasedInterface is an interface that embeds an interface in an aliased package.
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions fixtures/aliased_package/in_aliased_package.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package the_aliased_package // import "github.com/maxbrunsfeld/counterfeiter/v6/fixtures/aliased_package"
package the_aliased_package // import "github.com/ikolomiyets/counterfeiter/v6/fixtures/aliased_package"

//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . InAliasedPackage
//go:generate go run github.com/ikolomiyets/counterfeiter/v6 . InAliasedPackage
type InAliasedPackage interface {
Stuff(int) string
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions fixtures/another_package/types.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package another_package // import "github.com/maxbrunsfeld/counterfeiter/v6/fixtures/another_package"
package another_package // import "github.com/ikolomiyets/counterfeiter/v6/fixtures/another_package"

type SomeType int

//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . AnotherInterface
//go:generate go run github.com/ikolomiyets/counterfeiter/v6 . AnotherInterface
type AnotherInterface interface {
AnotherMethod([]SomeType, map[SomeType]SomeType, *SomeType, SomeType, chan SomeType)
}
2 changes: 1 addition & 1 deletion fixtures/blank.go
Original file line number Diff line number Diff line change
@@ -1 +1 @@
package fixtures // import "github.com/maxbrunsfeld/counterfeiter/v6/fixtures"
package fixtures // import "github.com/ikolomiyets/counterfeiter/v6/fixtures"
Loading