Skip to content

fix(assert): handle invalid regexp gracefully instead of panicking#1854

Open
bk-simon wants to merge 2 commits intostretchr:masterfrom
bk-simon:fix/regexp-panic-invalid-expression
Open

fix(assert): handle invalid regexp gracefully instead of panicking#1854
bk-simon wants to merge 2 commits intostretchr:masterfrom
bk-simon:fix/regexp-panic-invalid-expression

Conversation

@bk-simon
Copy link

Summary

Fixes #1794

assert.Regexp and assert.NotRegexp used regexp.MustCompile internally (via matchRegexp) when the rx argument was a string or non-*regexp.Regexp value. MustCompile panics on an invalid regexp pattern, which is documented as intended only for package-level initialization of global variables.

This causes a runtime panic rather than a test failure when a user accidentally passes an invalid regexp:

func TestExample(t *testing.T) {
    require.Regexp(t, `\C`, "some string") // panics!
}

What changed

  • Changed matchRegexp signature from func matchRegexp(rx, str interface{}) bool to func matchRegexp(rx, str interface{}) (bool, error)
  • Replaced regexp.MustCompile with regexp.Compile — on error the function returns the compile error
  • Regexp and NotRegexp now call Fail with a descriptive "Invalid regexp: ..." message when the pattern fails to compile, so the test fails gracefully rather than panicking
  • Added TestRegexpInvalidExpression to guard against regression

Test plan

  • go test ./assert/ -run TestRegexp — all pass
  • go test ./... — all packages pass
  • TestRegexpInvalidExpression specifically verifies invalid patterns fail gracefully (not panic)

Replace regexp.MustCompile with regexp.Compile in matchRegexp so that
an invalid regexp pattern fails the test with a useful error message
rather than causing a runtime panic.

Fixes stretchr#1794
@ccoVeille ccoVeille requested a review from Copilot February 25, 2026 08:29
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a panic issue in assert.Regexp and assert.NotRegexp when invalid regexp patterns are provided. Previously, these functions used regexp.MustCompile, which panics on invalid patterns. Now they gracefully fail the test with a descriptive error message.

Changes:

  • Modified matchRegexp to return an error instead of panicking on invalid regexp compilation
  • Updated Regexp and NotRegexp to handle compilation errors gracefully
  • Added regression test to verify invalid patterns fail tests without panicking

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
assert/assertions.go Changed matchRegexp to return error, replaced regexp.MustCompile with regexp.Compile, added error handling in Regexp and NotRegexp
assert/assertions_test.go Added TestRegexpInvalidExpression to verify invalid patterns fail gracefully

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

…xpInvalidExpression

Address reviewer feedback:
- Switch from new(testing.T) to new(mockTestingT) so we can inspect the
  actual error message, not just that the test failed
- Assert the error string contains "Invalid regexp" for both Regexp and
  NotRegexp with an invalid pattern
- Simplify inline comment: drop historical "previously caused a panic"
  language since commit history is the right place for that context
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

assert.Regexp panics with invalid rx expression

4 participants