testify icon indicating copy to clipboard operation
testify copied to clipboard

fix(assert): avoid panic on invalid regex in Regexp/NotRegexp (#1794)

Open kdt523 opened this issue 2 months ago • 0 comments

Summary Make assert.Regexp / assert.NotRegexp fail the assertion instead of panicking when given an invalid regex pattern.

Changes Use regexp.Compile (rather than regexp.MustCompile) when compiling user-supplied patterns in assert. Propagate compile errors so Regexp/NotRegexp report a clear failure message like: invalid regular expression "": Add assert/regexp_invalid_test.go with tests that ensure invalid patterns do not cause a panic. Files touched: assert/assertions.go assert/regexp_invalid_test.go Motivation Passing a malformed regex used to trigger a runtime panic in tests. That made debugging harder and could abort the whole test run. Reporting a failed assertion with a helpful error message is a safer, more useful behavior for test authors.

Related issues #1794

kdt523 avatar Oct 25 '25 16:10 kdt523