Skip to content

Commit 966a7c2

Browse files
committed
Replace manual mock generation with go generate
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.
1 parent 291af73 commit 966a7c2

File tree

8 files changed

+21
-328
lines changed

8 files changed

+21
-328
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ before_script:
2929
# Install dependencies for testing.
3030
- go get github.com/golang/mock/gomock@v1.2.0
3131
- go get github.com/golang/mock/mockgen@v1.2.0
32+
# Generate the mocks for testing.
33+
- go generate ./...
3234

3335
script:
3436
- make test

Makefile

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,8 @@ gofmt_test:
2525
$(VERB) echo "Running 'go fmt' test ..."
2626
$(VERB) ./gofmt_test.sh
2727

28-
ghutil_test:
29-
$(VERB) echo
30-
$(VERB) echo "Running tests in 'ghutil' recursively ..."
31-
$(VERB) $(MAKE) VERBOSE=$(VERBOSE) -s -C ghutil test
32-
3328
go_mod_tidy_test:
3429
$(VERB) echo "Running 'go mod tidy' test ..."
3530
$(VERB) ./go_mod_tidy_test.sh
3631

37-
test: go_test gofmt_test ghutil_test go_mod_tidy_test
32+
test: go_test gofmt_test go_mod_tidy_test

README.md

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,27 +50,26 @@ $ go get github.com/golang/mock/gomock@v1.2.0
5050
$ go get github.com/golang/mock/mockgen@v1.2.0
5151
```
5252

53+
Generate the mocks:
54+
55+
```bash
56+
$ go generate ./...
57+
```
58+
5359
This specific version of both `gomock` and `mockgen` tools is what's used in
5460
this repo, and tests will fail if your version of these tools generates
5561
different code, including comments.
5662

5763
To update the versions of these tools used in this repo:
5864

59-
1. update the version numbers in this file (above) and in
60-
[`.travis.yml`](.travis.yml) to match
61-
1. run the commands above to get those specific versions of the tools
62-
1. update the generated code in this repo via:
63-
64-
```bash
65-
$ cd ghutil
66-
$ rm -f mock_ghutil.go
67-
$ make mock
68-
```
69-
65+
1. update the version numbers in this file (above) as well as in
66+
[`.travis.yml`](.travis.yml) and [`go.mod`](go.mod) to match
67+
1. run `go mod tidy` to update the `go.sum` file
68+
1. run the updated `go get` commands above to get newer versions of the tools
69+
1. run the `go generate` command above to regenerate the mocks
7070
1. [run the tests](#testing) from the top-level of the tree
71-
1. commit your changes to this file (`README.md`), `.travis.yml`, and
72-
`ghutil/mock_ghutil.go` and make sure the build passes on Travis CI before
73-
merging the change
71+
1. commit your changes to this file (`README.md`) and `.travis.yml`, making
72+
sure that the build passes on Travis CI before merging the change
7473

7574
## Testing
7675

ghutil/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mock_ghutil.go

ghutil/Makefile

Lines changed: 0 additions & 33 deletions
This file was deleted.

ghutil/ghutil.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
// TODO(mbrukman): in the future, consider using the recently-added
16+
// `-copyright_filename` flag: https://github.com/golang/mock/pull/234
17+
18+
//go:generate mockgen -source ghutil.go -destination mock_ghutil.go -package ghutil
1519
package ghutil
1620

1721
import (

ghutil/mock_ghutil.go

Lines changed: 0 additions & 261 deletions
This file was deleted.

ghutil/mock_header.txt

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy