Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

suite: fix deadlock in suite.Require()/Assert() #1535

Merged
merged 1 commit into from Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions suite/suite.go
Expand Up @@ -58,7 +58,7 @@ func (suite *Suite) Require() *require.Assertions {
suite.mu.Lock()
defer suite.mu.Unlock()
if suite.require == nil {
suite.require = require.New(suite.T())
panic("'Require' must not be called before 'Run' or 'SetT'")
}
return suite.require
}
Expand All @@ -72,7 +72,7 @@ func (suite *Suite) Assert() *assert.Assertions {
suite.mu.Lock()
defer suite.mu.Unlock()
if suite.Assertions == nil {
suite.Assertions = assert.New(suite.T())
panic("'Assert' must not be called before 'Run' or 'SetT'")
}
return suite.Assertions
}
Expand Down
24 changes: 24 additions & 0 deletions suite/suite_test.go
Expand Up @@ -692,3 +692,27 @@ func TestSubtestPanic(t *testing.T) {
assert.True(t, suite.inTearDownTest)
assert.True(t, suite.inTearDownSuite)
}

type unInitialisedSuite struct {
Suite
}

// TestUnInitialisedSuites asserts the behaviour of the suite methods when the
// suite is not initialised
func TestUnInitialisedSuites(t *testing.T) {
t.Run("should panic on Require", func(t *testing.T) {
suite := new(unInitialisedSuite)

assert.Panics(t, func() {
suite.Require().True(true)
})
})

t.Run("should panic on Assert", func(t *testing.T) {
suite := new(unInitialisedSuite)

assert.Panics(t, func() {
suite.Assert().True(true)
})
})
}