Skip to content

Commit

Permalink
Replace manual mock generation with go generate
Browse files Browse the repository at this point in the history
This uses the builtin Go method for generating mocks; read more in:
https://blog.codecentric.de/en/2017/08/gomock-tutorial/#using-gomock-with-gogenerate

As a result, we can clean up quite a bit of code and manual workarounds,
and the generated code is identical (minus the copyright header); that
said, this feature was added on 25 Dec 2018 in
golang/mock#234 which was after the most-recent
release of v1.2.0 on 8 Dec 2018, so it's not available to us just yet.
  • Loading branch information
mbrukman committed Mar 12, 2019
1 parent 291af73 commit b89abb0
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 325 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ before_script:
# Install dependencies for testing.
- go get github.com/golang/mock/gomock@v1.2.0
- go get github.com/golang/mock/mockgen@v1.2.0
# Generate the mocks for testing.
- go generate ./...

script:
- make test
7 changes: 1 addition & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,8 @@ gofmt_test:
$(VERB) echo "Running 'go fmt' test ..."
$(VERB) ./gofmt_test.sh

ghutil_test:
$(VERB) echo
$(VERB) echo "Running tests in 'ghutil' recursively ..."
$(VERB) $(MAKE) VERBOSE=$(VERBOSE) -s -C ghutil test

go_mod_tidy_test:
$(VERB) echo "Running 'go mod tidy' test ..."
$(VERB) ./go_mod_tidy_test.sh

test: go_test gofmt_test ghutil_test go_mod_tidy_test
test: go_test gofmt_test go_mod_tidy_test
19 changes: 8 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ $ go get github.com/golang/mock/gomock@v1.2.0
$ go get github.com/golang/mock/mockgen@v1.2.0
```

Generate the mocks:

```bash
$ go generate ./...
```

This specific version of both `gomock` and `mockgen` tools is what's used in
this repo, and tests will fail if your version of these tools generates
different code, including comments.
Expand All @@ -59,18 +65,9 @@ To update the versions of these tools used in this repo:
1. update the version numbers in this file (above) and in
[`.travis.yml`](.travis.yml) to match
1. run the commands above to get those specific versions of the tools
1. update the generated code in this repo via:

```bash
$ cd ghutil
$ rm -f mock_ghutil.go
$ make mock
```

1. [run the tests](#testing) from the top-level of the tree
1. commit your changes to this file (`README.md`), `.travis.yml`, and
`ghutil/mock_ghutil.go` and make sure the build passes on Travis CI before
merging the change
1. commit your changes to this file (`README.md`) and `.travis.yml`, making
sure that the build passes on Travis CI before merging the change

## Testing

Expand Down
1 change: 1 addition & 0 deletions ghutil/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mock_ghutil.go
33 changes: 0 additions & 33 deletions ghutil/Makefile

This file was deleted.

4 changes: 4 additions & 0 deletions ghutil/ghutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// TODO(mbrukman): in the future, consider using the recently-added
// `-copyright_filename` flag: https://github.com/golang/mock/pull/234

//go:generate mockgen -source ghutil.go -destination mock_ghutil.go -package ghutil
package ghutil

import (
Expand Down
261 changes: 0 additions & 261 deletions ghutil/mock_ghutil.go

This file was deleted.

14 changes: 0 additions & 14 deletions ghutil/mock_header.txt

This file was deleted.

0 comments on commit b89abb0

Please sign in to comment.