Skip to content

Commit

Permalink
describe how to use with go modules, remove go version from go.mod
Browse files Browse the repository at this point in the history
  • Loading branch information
joefitzgerald committed Mar 21, 2019
1 parent 17cfad8 commit 3d5cada
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 30 deletions.
125 changes: 99 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,105 @@ automatically at runtime, and writing them by hand can be quite arduous.

`counterfeiter` allows you to simply generate test doubles for a given interface.

### Install
### Supported Versions Of `go`

`counterfeiter` follows the [support policy of `go` itself](https://golang.org/doc/devel/release.html#policy):

> Each major Go release is supported until there are two newer major releases. For example, Go 1.5 was supported until the Go 1.7 release, and Go 1.6 was supported until the Go 1.8 release. We fix critical problems, including [critical security problems](https://golang.org/security), in supported releases as needed by issuing minor revisions (for example, Go 1.6.1, Go 1.6.2, and so on).
If you are having problems with `counterfieter` and are not using a supported version of go, please update to use a supported version of go before opening an issue.

### Using `counterfeiter`

We recommend you use [`go modules`](https://blog.golang.org/using-go-modules) when working with counterfeiter.

Typically, `counterfeiter` is used in `go generate` directives. It can be frustrating when you change your interface declaration and suddenly all of your generated code is suddenly out-of-date. The best practice here is to use the [`go generate` command](https://blog.golang.org/generate) to make it easier to keep your test doubles up to date.

#### Step 1 - Install `gobin`

`gobin` is used to execute a binary by specifying a package path. This allows you to run a specific version of a tool.

```shell
GO111MODULE=off go get -u github.com/myitcv/gobin
```

#### Step 2 - Create `tools.go`

You can take a dependency on tools by creating a `tools.go` file, as described in [How can I track tool dependencies for a module?](https://github.com/golang/go/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module). This ensures that everyone working with your module is using the same version of each tool you use.

```shell
$ cat tools/tools.go
```

```go
// +build tools

package tools

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

// This file imports packages that are used when running go generate, or used
// during the development process but not otherwise depended on by built code.
```

#### Step 3 - Add `go generate` Directives

You can add directives right next to your interface definitions (or not), in any `.go` file in your module.

```shell
$ cat myinterface.go
```

```go
package foo

//go:generate gobin -m -run github.com/maxbrunsfeld/counterfeiter/v6 . MySpecialInterface

type MySpecialInterface interface {
DoThings(string, uint64) (int, error)
}
```

```shell
go get -u github.com/maxbrunsfeld/counterfeiter/v6
$ go generate ./...
Writing `FakeMySpecialInterface` to `foofakes/fake_my_special_interface.go`... Done
```

#### Step 4 - Run `go generate`

You can run `go generate` in the directory with your directive, or in the root of your module (to ensure you generate for all packages in your module):

```shell
go generate ./...
```

#### Invoking `counterfeiter` from the shell

You can use the following command to invoke `counterfeiter` from within a go module:

```shell
gobin -m -run github.com/maxbrunsfeld/counterfeiter

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

#### Installing `counterfeiter` to `$GOPATH/bin`

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
$ counterfeiter

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

### Generating Test Doubles
Expand All @@ -24,12 +119,12 @@ $ cat path/to/foo/file.go
package foo

type MySpecialInterface interface {
DoThings(string, uint64) (int, error)
DoThings(string, uint64) (int, error)
}
```

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

Expand Down Expand Up @@ -67,28 +162,6 @@ 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).

### Using `go generate`

It can be frustrating when you change your interface declaration and suddenly all of your generated code is suddenly out-of-date. The best practice here is to use golang's ["go generate" command](https://blog.golang.org/generate) to make it easier to keep your test doubles up to date.

```shell
$ cat path/to/foo/file.go
```

```go
package foo

//go:generate counterfeiter . MySpecialInterface
type MySpecialInterface interface {
DoThings(string, uint64) (int, error)
}
```

```shell
$ go generate ./...
Wrote `FakeMySpecialInterface` to `path/to/foo/foofakes/fake_my_special_interface.go`
```

### Running The Tests For `counterfeiter`

If you want to run the tests for `counterfeiter` (perhaps, because you want to contribute a PR), all you have to do is run `scripts/ci.sh`.
Expand Down
2 changes: 0 additions & 2 deletions fixtures/dup_packages/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
module github.com/maxbrunsfeld/counterfeiter/v6/fixtures/dup_packages

go 1.12
2 changes: 0 additions & 2 deletions fixtures/hyphenated_package_same_name/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
module github.com/maxbrunsfeld/counterfeiter/v6/fixtures/hyphenated_package_same_name

go 1.12

0 comments on commit 3d5cada

Please sign in to comment.