Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit 5f713a8

Browse files
authored
Merge branch 'main' into saitoy/overridable-mock
2 parents 39bfb30 + e86b1bf commit 5f713a8

File tree

82 files changed

+275
-271
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+275
-271
lines changed

.github/workflows/test.yaml

+12-13
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
11
name: Run tests
22
on:
33
push:
4-
branches: [main]
4+
branches: ['*']
55
pull_request:
6-
branches: [main]
6+
branches: ['*']
7+
8+
permissions:
9+
contents: read
10+
711
env:
812
GO111MODULE: on
13+
914
jobs:
1015
test:
1116
strategy:
1217
matrix:
13-
go-version: [1.15.x, 1.18.x]
18+
go-version: [1.19.x, 1.20.x]
1419
os: [ubuntu-latest]
1520
runs-on: ${{ matrix.os }}
1621
steps:
1722

1823
- name: Install Go
19-
uses: actions/setup-go@v2
24+
uses: actions/setup-go@v4
2025
with:
2126
go-version: ${{ matrix.go-version }}
2227

2328
- name: Checkout code
24-
uses: actions/checkout@v2
29+
uses: actions/checkout@v3
2530

2631
- name: Vet and build
2732
run: |
@@ -30,23 +35,17 @@ jobs:
3035
3136
- name: Install mockgen
3237
run: |
33-
go install github.com/golang/mock/mockgen
38+
go install go.uber.org/mock/mockgen
3439
3540
- name: Run test script
3641
run: |
3742
./ci/test.sh
3843
./ci/check_panic_handling.sh
3944
40-
- name: Run Go tests all
41-
if: ${{ startsWith(matrix.go-version, '1.18') }}
45+
- name: Run Tests
4246
run: |
4347
for i in $(find $PWD -name go.mod); do
4448
pushd $(dirname $i)
4549
go test ./...
4650
popd
4751
done
48-
49-
- name: Run Go tests some
50-
if: ${{ startsWith(matrix.go-version, '1.18') == false }}
51-
run: |
52-
go test ./...

CHANGELOG.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
5+
6+
## 0.1.0 (29 Jun 2023)
7+
8+
This is a minor version that mirrors the original golang/mock
9+
project that this project originates from.
10+
11+
Any users on golang/mock project should be able to migrate to
12+
this project as-is, and expect exact same set of features (apart
13+
from supported Go versions. See [README](README.md#supported-go-versions)
14+
for more details.

CONTRIBUTING.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ First off, thank you for taking an interest to contribute to this project!
44

55
## Opening issues
66

7-
When opening a [new issue](https://github.com/golang/mock/issues/new/choose)
7+
When opening a [new issue](https://github.com/uber/mock/issues/new/choose)
88
please:
99

1010
1. Make sure there are not other open/closed issues asking/reporting/requesting

MAINTAINERS

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# This is the current list of maintainers to Uber's fork of gomock repository.
2+
3+
Sung Yoon Whang <[email protected]>
4+
Ryan Hang <[email protected]>
5+
Zhongpeng Lin <[email protected]>

README.md

+14-44
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,21 @@ gomock is a mocking framework for the [Go programming language][golang]. It
66
integrates well with Go's built-in `testing` package, but can be used in other
77
contexts too.
88

9-
## Installation
9+
This project originates from Google's `golang/mock` repo. Unfortunately Google
10+
no longer maintains this project, and given the heavy usage of gomock project
11+
within Uber, we've decided to fork and maintain this going forward at Uber.
1012

11-
Once you have [installed Go][golang-install], install the `mockgen` tool.
13+
Contributions are welcome in the form of GitHub issue or PR!
1214

13-
**Note**: If you have not done so already be sure to add `$GOPATH/bin` to your
14-
`PATH`.
15+
## Status
1516

16-
To get the latest released version use:
17+
This project is still WIP. We will be tagging a release shortly, in early July.
1718

18-
### Go version < 1.16
19+
## Supported Go Versions
1920

20-
```bash
21-
GO111MODULE=on go get github.com/golang/mock/[email protected]
22-
```
23-
24-
### Go 1.16+
25-
26-
```bash
27-
go install github.com/golang/mock/[email protected]
28-
```
29-
30-
If you use `mockgen` in your CI pipeline, it may be more appropriate to fixate
31-
on a specific mockgen version. You should try to keep the library in sync with
32-
the version of mockgen used to generate your mocks.
21+
go.uber.org/mock supports all Go versions supported by the official
22+
[Go Release Policy](https://go.dev/doc/devel/release#policy). That is,
23+
the two most recent releases of Go.
3324

3425
## Running mockgen
3526

@@ -250,28 +241,7 @@ If the received value is `3`, then it will be printed as `03`.
250241
251242
[golang]: http://golang.org/
252243
[golang-install]: http://golang.org/doc/install.html#releases
253-
[gomock-reference]: https://pkg.go.dev/github.com/golang/mock/gomock
254-
[ci-badge]: https://github.com/golang/mock/actions/workflows/test.yaml/badge.svg
255-
[ci-runs]: https://github.com/golang/mock/actions
256-
[reference-badge]: https://pkg.go.dev/badge/github.com/golang/mock.svg
257-
[reference]: https://pkg.go.dev/github.com/golang/mock
258-
259-
## Debugging Errors
260-
261-
### reflect vendoring error
262-
263-
```text
264-
cannot find package "."
265-
... github.com/golang/mock/mockgen/model
266-
```
267-
268-
If you come across this error while using reflect mode and vendoring
269-
dependencies there are three workarounds you can choose from:
270-
271-
1. Use source mode.
272-
2. Include an empty import `import _ "github.com/golang/mock/mockgen/model"`.
273-
3. Add `--build_flags=--mod=mod` to your mockgen command.
274-
275-
This error is due to changes in default behavior of the `go` command in more
276-
recent versions. More details can be found in
277-
[#494](https://github.com/golang/mock/issues/494).
244+
[ci-badge]: https://github.com/uber/mock/actions/workflows/test.yaml/badge.svg
245+
[ci-runs]: https://github.com/uber/mock/actions
246+
[reference-badge]: https://pkg.go.dev/badge/github.com/uber/mock.svg
247+
[reference]: https://pkg.go.dev/github.com/uber/mock

go.mod

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
module github.com/golang/mock
1+
module go.uber.org/mock
2+
3+
go 1.19
24

35
require (
46
golang.org/x/mod v0.5.1
57
golang.org/x/tools v0.1.8
68
)
79

8-
go 1.15
10+
require (
11+
github.com/yuin/goldmark v1.4.1 // indirect
12+
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 // indirect
13+
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
14+
)

go.sum

-19
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,10 @@
11
github.com/yuin/goldmark v1.4.1 h1:/vn0k+RBvwlxEmP5E7SZMqNxPhfMVFEJiykr15/0XKM=
22
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
3-
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
4-
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
53
golang.org/x/mod v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38=
64
golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
7-
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
8-
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
9-
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
10-
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
11-
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
12-
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
13-
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
14-
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
15-
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
165
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 h1:id054HUawV2/6IGm2IV8KZQjqtwAOo2CYlOToYqa0d0=
176
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
18-
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
19-
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
20-
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
21-
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
22-
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
23-
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
247
golang.org/x/tools v0.1.8 h1:P1HhGGuLW4aAclzjtmJdf0mJOjVUZUzOTqkAkWL+l6w=
258
golang.org/x/tools v0.1.8/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU=
26-
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
27-
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
289
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
2910
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

gomock/callset.go

+37-3
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ import (
1818
"bytes"
1919
"errors"
2020
"fmt"
21+
"sync"
2122
)
2223

2324
// callSet represents a set of expected calls, indexed by receiver and method
2425
// name.
2526
type callSet struct {
2627
// Calls that are still expected.
27-
expected map[callSetKey][]*Call
28+
expected map[callSetKey][]*Call
29+
expectedMu *sync.Mutex
2830
// Calls that have been exhausted.
2931
exhausted map[callSetKey][]*Call
3032
// when set to true,
@@ -39,14 +41,16 @@ type callSetKey struct {
3941

4042
func newCallSet() *callSet {
4143
return &callSet{
42-
expected: make(map[callSetKey][]*Call),
43-
exhausted: make(map[callSetKey][]*Call),
44+
expected: make(map[callSetKey][]*Call),
45+
expectedMu: &sync.Mutex{},
46+
exhausted: make(map[callSetKey][]*Call),
4447
}
4548
}
4649

4750
func newOverridableCallSet() *callSet {
4851
return &callSet{
4952
expected: make(map[callSetKey][]*Call),
53+
expectedMu: &sync.Mutex{},
5054
exhausted: make(map[callSetKey][]*Call),
5155
allowOverride: true,
5256
}
@@ -55,6 +59,10 @@ func newOverridableCallSet() *callSet {
5559
// Add adds a new expected call.
5660
func (cs callSet) Add(call *Call) {
5761
key := callSetKey{call.receiver, call.method}
62+
63+
cs.expectedMu.Lock()
64+
defer cs.expectedMu.Unlock()
65+
5866
m := cs.expected
5967
if call.exhausted() {
6068
m = cs.exhausted
@@ -70,6 +78,10 @@ func (cs callSet) Add(call *Call) {
7078
// Remove removes an expected call.
7179
func (cs callSet) Remove(call *Call) {
7280
key := callSetKey{call.receiver, call.method}
81+
82+
cs.expectedMu.Lock()
83+
defer cs.expectedMu.Unlock()
84+
7385
calls := cs.expected[key]
7486
for i, c := range calls {
7587
if c == call {
@@ -85,6 +97,9 @@ func (cs callSet) Remove(call *Call) {
8597
func (cs callSet) FindMatch(receiver interface{}, method string, args []interface{}) (*Call, error) {
8698
key := callSetKey{receiver, method}
8799

100+
cs.expectedMu.Lock()
101+
defer cs.expectedMu.Unlock()
102+
88103
// Search through the expected calls.
89104
expected := cs.expected[key]
90105
var callsErrors bytes.Buffer
@@ -119,6 +134,9 @@ func (cs callSet) FindMatch(receiver interface{}, method string, args []interfac
119134

120135
// Failures returns the calls that are not satisfied.
121136
func (cs callSet) Failures() []*Call {
137+
cs.expectedMu.Lock()
138+
defer cs.expectedMu.Unlock()
139+
122140
failures := make([]*Call, 0, len(cs.expected))
123141
for _, calls := range cs.expected {
124142
for _, call := range calls {
@@ -129,3 +147,19 @@ func (cs callSet) Failures() []*Call {
129147
}
130148
return failures
131149
}
150+
151+
// Satisfied returns true in case all expected calls in this callSet are satisfied.
152+
func (cs callSet) Satisfied() bool {
153+
cs.expectedMu.Lock()
154+
defer cs.expectedMu.Unlock()
155+
156+
for _, calls := range cs.expected {
157+
for _, call := range calls {
158+
if !call.satisfied() {
159+
return false
160+
}
161+
}
162+
}
163+
164+
return true
165+
}

gomock/callset_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func TestCallSetRemove(t *testing.T) {
9595

9696
func TestCallSetFindMatch(t *testing.T) {
9797
t.Run("call is exhausted", func(t *testing.T) {
98-
cs := callSet{}
98+
cs := newCallSet()
9999
var receiver interface{} = "TestReceiver"
100100
method := "TestMethod"
101101
args := []interface{}{}

gomock/controller.go

+23-17
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,24 @@ type cleanuper interface {
5151
// goroutines. Each test should create a new Controller and invoke Finish via
5252
// defer.
5353
//
54-
// func TestFoo(t *testing.T) {
55-
// ctrl := gomock.NewController(t)
56-
// defer ctrl.Finish()
57-
// // ..
58-
// }
54+
// func TestFoo(t *testing.T) {
55+
// ctrl := gomock.NewController(t)
56+
// defer ctrl.Finish()
57+
// // ..
58+
// }
5959
//
60-
// func TestBar(t *testing.T) {
61-
// t.Run("Sub-Test-1", st) {
62-
// ctrl := gomock.NewController(st)
63-
// defer ctrl.Finish()
64-
// // ..
65-
// })
66-
// t.Run("Sub-Test-2", st) {
67-
// ctrl := gomock.NewController(st)
68-
// defer ctrl.Finish()
69-
// // ..
70-
// })
71-
// })
60+
// func TestBar(t *testing.T) {
61+
// t.Run("Sub-Test-1", st) {
62+
// ctrl := gomock.NewController(st)
63+
// defer ctrl.Finish()
64+
// // ..
65+
// })
66+
// t.Run("Sub-Test-2", st) {
67+
// ctrl := gomock.NewController(st)
68+
// defer ctrl.Finish()
69+
// // ..
70+
// })
71+
// })
7272
type Controller struct {
7373
// T should only be called within a generated mock. It is not intended to
7474
// be used in user code and may be changed in future versions. T is the
@@ -251,6 +251,12 @@ func (ctrl *Controller) Finish() {
251251
ctrl.finish(false, err)
252252
}
253253

254+
// Satisfied returns whether all expected calls bound to this Controller have been satisfied.
255+
// Calling Finish is then guaranteed to not fail due to missing calls.
256+
func (ctrl *Controller) Satisfied() bool {
257+
return ctrl.expectedCalls.Satisfied()
258+
}
259+
254260
func (ctrl *Controller) finish(cleanup bool, panicErr interface{}) {
255261
ctrl.T.Helper()
256262

gomock/controller_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121

2222
"strings"
2323

24-
"github.com/golang/mock/gomock"
24+
"go.uber.org/mock/gomock"
2525
)
2626

2727
type ErrorReporter struct {

0 commit comments

Comments
 (0)