Skip to content

Commit

Permalink
fixing panic in calls to assertion with nil m.mutex
Browse files Browse the repository at this point in the history
This reverts a change that was made in stretchr#1182
The PR makes m.mutex a pointer which now needs to be checked but it's not checked for nil everywhere.

This should also help with these issues:
- stretchr#1208
- stretchr#1210
  • Loading branch information
Edward Raigosa authored Jun 23, 2022
1 parent c206b2e commit aca7672
Showing 1 changed file with 2 additions and 9 deletions.
11 changes: 2 additions & 9 deletions mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ type Mock struct {
// this data completely allowing you to do whatever you like with it.
testData objx.Map

mutex *sync.Mutex
mutex sync.Mutex
}

// String provides a %v format string for Mock.
Expand All @@ -282,10 +282,6 @@ func (m *Mock) TestData() objx.Map {

// Test sets the test struct variable of the mock object
func (m *Mock) Test(t TestingT) {
if m.mutex == nil {
m.mutex = &sync.Mutex{}
}

m.mutex.Lock()
defer m.mutex.Unlock()
m.test = t
Expand Down Expand Up @@ -317,7 +313,7 @@ func (m *Mock) On(methodName string, arguments ...interface{}) *Call {
}

// Since we start mocks with the .On() function, m.mutex should be reset
m.mutex = &sync.Mutex{}
m.mutex = sync.Mutex{}

m.mutex.Lock()
defer m.mutex.Unlock()
Expand Down Expand Up @@ -545,9 +541,6 @@ func (m *Mock) AssertExpectations(t TestingT) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
if m.mutex == nil {
m.mutex = &sync.Mutex{}
}

m.mutex.Lock()
defer m.mutex.Unlock()
Expand Down

0 comments on commit aca7672

Please sign in to comment.