Skip to content

Commit 3c33e07

Browse files
Added Go 1.18.1 as a build/supported version (#1182)
* Added Go 1.18.1 as a build/supported version Removed Go 1.15.13 as a build version as it's no longer supported * Fix mutex passed by value for the Mock struct * Add mutex initialisation for Mock Co-authored-by: Boyan Soubachov <[email protected]>
1 parent e2b56b3 commit 3c33e07

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

.github/workflows/main.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jobs:
66
runs-on: ubuntu-latest
77
strategy:
88
matrix:
9-
go_version: ["1.17.8", "1.16.15", "1.15.15"]
9+
go_version: ["1.18.1", "1.17.6", "1.16.5"]
1010
steps:
1111
- uses: actions/checkout@v2
1212
- name: Setup Go

mock/mock.go

+17-19
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ type Mock struct {
218218
// this data completely allowing you to do whatever you like with it.
219219
testData objx.Map
220220

221-
mutex sync.Mutex
221+
mutex *sync.Mutex
222222
}
223223

224224
// String provides a %v format string for Mock.
@@ -232,7 +232,6 @@ func (m *Mock) String() string {
232232
// TestData holds any data that might be useful for testing. Testify ignores
233233
// this data completely allowing you to do whatever you like with it.
234234
func (m *Mock) TestData() objx.Map {
235-
236235
if m.testData == nil {
237236
m.testData = make(objx.Map)
238237
}
@@ -246,6 +245,10 @@ func (m *Mock) TestData() objx.Map {
246245

247246
// Test sets the test struct variable of the mock object
248247
func (m *Mock) Test(t TestingT) {
248+
if m.mutex == nil {
249+
m.mutex = &sync.Mutex{}
250+
}
251+
249252
m.mutex.Lock()
250253
defer m.mutex.Unlock()
251254
m.test = t
@@ -276,6 +279,9 @@ func (m *Mock) On(methodName string, arguments ...interface{}) *Call {
276279
}
277280
}
278281

282+
// Since we start mocks with the .On() function, m.mutex should be reset
283+
m.mutex = &sync.Mutex{}
284+
279285
m.mutex.Lock()
280286
defer m.mutex.Unlock()
281287
c := newCall(m, methodName, assert.CallerInfo(), arguments...)
@@ -354,7 +360,6 @@ func (m *Mock) findClosestCall(method string, arguments ...interface{}) (*Call,
354360
}
355361

356362
func callString(method string, arguments Arguments, includeArgumentValues bool) string {
357-
358363
var argValsString string
359364
if includeArgumentValues {
360365
var argVals []string
@@ -378,10 +383,10 @@ func (m *Mock) Called(arguments ...interface{}) Arguments {
378383
panic("Couldn't get the caller information")
379384
}
380385
functionPath := runtime.FuncForPC(pc).Name()
381-
//Next four lines are required to use GCCGO function naming conventions.
382-
//For Ex: github_com_docker_libkv_store_mock.WatchTree.pN39_github_com_docker_libkv_store_mock.Mock
383-
//uses interface information unlike golang github.com/docker/libkv/store/mock.(*Mock).WatchTree
384-
//With GCCGO we need to remove interface information starting from pN<dd>.
386+
// Next four lines are required to use GCCGO function naming conventions.
387+
// For Ex: github_com_docker_libkv_store_mock.WatchTree.pN39_github_com_docker_libkv_store_mock.Mock
388+
// uses interface information unlike golang github.com/docker/libkv/store/mock.(*Mock).WatchTree
389+
// With GCCGO we need to remove interface information starting from pN<dd>.
385390
re := regexp.MustCompile("\\.pN\\d+_")
386391
if re.MatchString(functionPath) {
387392
functionPath = re.Split(functionPath, -1)[0]
@@ -397,7 +402,7 @@ func (m *Mock) Called(arguments ...interface{}) Arguments {
397402
// If Call.WaitFor is set, blocks until the channel is closed or receives a message.
398403
func (m *Mock) MethodCalled(methodName string, arguments ...interface{}) Arguments {
399404
m.mutex.Lock()
400-
//TODO: could combine expected and closes in single loop
405+
// TODO: could combine expected and closes in single loop
401406
found, call := m.findExpectedCall(methodName, arguments...)
402407

403408
if found < 0 {
@@ -781,12 +786,12 @@ func (args Arguments) Is(objects ...interface{}) bool {
781786
//
782787
// Returns the diff string and number of differences found.
783788
func (args Arguments) Diff(objects []interface{}) (string, int) {
784-
//TODO: could return string as error and nil for No difference
789+
// TODO: could return string as error and nil for No difference
785790

786-
var output = "\n"
791+
output := "\n"
787792
var differences int
788793

789-
var maxArgCount = len(args)
794+
maxArgCount := len(args)
790795
if len(objects) > maxArgCount {
791796
maxArgCount = len(objects)
792797
}
@@ -819,22 +824,19 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
819824
output = fmt.Sprintf("%s\t%d: FAIL: %s not matched by %s\n", output, i, actualFmt, matcher)
820825
}
821826
} else if reflect.TypeOf(expected) == reflect.TypeOf((*AnythingOfTypeArgument)(nil)).Elem() {
822-
823827
// type checking
824828
if reflect.TypeOf(actual).Name() != string(expected.(AnythingOfTypeArgument)) && reflect.TypeOf(actual).String() != string(expected.(AnythingOfTypeArgument)) {
825829
// not match
826830
differences++
827831
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, expected, reflect.TypeOf(actual).Name(), actualFmt)
828832
}
829-
830833
} else if reflect.TypeOf(expected) == reflect.TypeOf((*IsTypeArgument)(nil)) {
831834
t := expected.(*IsTypeArgument).t
832835
if reflect.TypeOf(t) != reflect.TypeOf(actual) {
833836
differences++
834837
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, reflect.TypeOf(t).Name(), reflect.TypeOf(actual).Name(), actualFmt)
835838
}
836839
} else {
837-
838840
// normal checking
839841

840842
if assert.ObjectsAreEqual(expected, Anything) || assert.ObjectsAreEqual(actual, Anything) || assert.ObjectsAreEqual(actual, expected) {
@@ -854,7 +856,6 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
854856
}
855857

856858
return output, differences
857-
858859
}
859860

860861
// Assert compares the arguments with the specified objects and fails if
@@ -876,7 +877,6 @@ func (args Arguments) Assert(t TestingT, objects ...interface{}) bool {
876877
t.Errorf("%sArguments do not match.", assert.CallerInfo())
877878

878879
return false
879-
880880
}
881881

882882
// String gets the argument at the specified index. Panics if there is no argument, or
@@ -885,7 +885,6 @@ func (args Arguments) Assert(t TestingT, objects ...interface{}) bool {
885885
// If no index is provided, String() returns a complete string representation
886886
// of the arguments.
887887
func (args Arguments) String(indexOrNil ...int) string {
888-
889888
if len(indexOrNil) == 0 {
890889
// normal String() method - return a string representation of the args
891890
var argsStr []string
@@ -895,7 +894,7 @@ func (args Arguments) String(indexOrNil ...int) string {
895894
return strings.Join(argsStr, ",")
896895
} else if len(indexOrNil) == 1 {
897896
// Index has been specified - get the argument at that index
898-
var index = indexOrNil[0]
897+
index := indexOrNil[0]
899898
var s string
900899
var ok bool
901900
if s, ok = args.Get(index).(string); !ok {
@@ -905,7 +904,6 @@ func (args Arguments) String(indexOrNil ...int) string {
905904
}
906905

907906
panic(fmt.Sprintf("assert: arguments: Wrong number of arguments passed to String. Must be 0 or 1, not %d", len(indexOrNil)))
908-
909907
}
910908

911909
// Int gets the argument at the specified index. Panics if there is no argument, or

0 commit comments

Comments
 (0)