Skip to content

Commit 8143bf7

Browse files
committed
fix: merged fix from stretchr#1825
================================== Panic situation is explained by stretchr#1699 The fix from the original author (@ccoVeille) has been adapted into our refactored assertions (with unit test). fix: avoid panic on invalid regexp ================================== Merged stretchr#1818 The original issue is described by: stretchr#1794 The original code contributed by @kdt523 has been adapted to our refactored assertions (with tests), taking into account the review comments from @ccoVeille. Thanks folks. fix: documented the fixed merged from github.com/stretchr/testify ================================== * fixes go-openapi#7 Also: since a dependency has been removed in the previous refactoring, updated the NOTICE to reflect this fact. Signed-off-by: Frederic BIDON <[email protected]>
1 parent 30ab924 commit 8143bf7

File tree

6 files changed

+101
-42
lines changed

6 files changed

+101
-42
lines changed

NOTICE

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -97,31 +97,3 @@ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
9797
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
9898
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
9999
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
100-
101-
github.com/go-openapi/testify/_codegen/internal/imports
102-
===========================
103-
104-
// SPDX-FileCopyrightText: Copyright (c) 2015 Ernesto Jiménez
105-
// SPDX-License-Identifier: MIT
106-
107-
The MIT License (MIT)
108-
109-
Copyright (c) 2015 Ernesto Jiménez
110-
111-
Permission is hereby granted, free of charge, to any person obtaining a copy
112-
of this software and associated documentation files (the "Software"), to deal
113-
in the Software without restriction, including without limitation the rights
114-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
115-
copies of the Software, and to permit persons to whom the Software is
116-
furnished to do so, subject to the following conditions:
117-
118-
The above copyright notice and this permission notice shall be included in all
119-
copies or substantial portions of the Software.
120-
121-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
122-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
123-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
124-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
125-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
126-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
127-
SOFTWARE.

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,20 @@ some adaptations into this fork:
198198
* github.com/stretchr/testify#1772 - YAML library migration to maintained fork (go.yaml.in/yaml)
199199
* github.com/stretchr/testify#1797 - Codegen package consolidation and licensing
200200
* github.com/stretchr/testify#1356 - panic(nil) handling for Go 1.21+
201+
* github.com/stretchr/testify#1825 - Fix panic when using EqualValues with uncomparable types [merged]
202+
* github.com/stretchr/testify#1818 - Fix panic on invalid regex in Regexp/NotRegexp assertions [merged]
201203

202204
### Planned merges
203205

204206
#### Critical safety fixes (high priority)
205207

206-
* github.com/stretchr/testify#1825 - Fix panic when using EqualValues with uncomparable types
207-
* github.com/stretchr/testify#1818 - Fix panic on invalid regex in Regexp/NotRegexp assertions
208+
* Follow / adapt https://github.com/stretchr/testify/pull/1824
209+
210+
Not PRs, but reported issues in the original repo:
211+
212+
* https://github.com/stretchr/testify/issues/1826
213+
* https://github.com/stretchr/testify/issues/1611
214+
* https://github.com/stretchr/testify/issues/1813
208215

209216
#### Leveraging internalized dependencies (go-spew, difflib)
210217

internal/assertions/object.go

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ func ObjectsAreEqual(expected, actual any) bool {
2525
if !ok {
2626
return false
2727
}
28+
2829
if exp == nil || act == nil {
2930
return exp == nil && act == nil
3031
}
32+
3133
return bytes.Equal(exp, act)
3234
}
3335

@@ -63,21 +65,49 @@ func ObjectsAreEqualValues(expected, actual any) bool {
6365
return false
6466
}
6567

68+
// Attempt conversion of expected to actual type.
69+
// This handles more cases than just the ConvertibleTo check above.
70+
if !expectedValue.CanConvert(actualType) {
71+
// Types are not convertible, so they cannot be equal
72+
// This prevents panics when calling [reflect.Value.Convert]
73+
return false
74+
}
75+
76+
expectedConverted := expectedValue.Convert(actualType)
77+
if !expectedConverted.CanInterface() {
78+
// Cannot interface after conversion, so cannot be equal.
79+
// This prevents panics when calling [reflect.Value.Interface].
80+
return false
81+
}
82+
6683
if !isNumericType(expectedType) || !isNumericType(actualType) {
67-
// Attempt comparison after type conversion
84+
// Attempt comparison after type conversion.
6885
return reflect.DeepEqual(
69-
expectedValue.Convert(actualType).Interface(), actual,
86+
expectedConverted.Interface(), actual,
7087
)
7188
}
7289

7390
// If BOTH values are numeric, there are chances of false positives due
7491
// to overflow or underflow. So, we need to make sure to always convert
7592
// the smaller type to a larger type before comparing.
7693
if expectedType.Size() >= actualType.Size() {
77-
return actualValue.Convert(expectedType).Interface() == expected
94+
if !actualValue.CanConvert(expectedType) {
95+
// Cannot convert actual to the expected type, so cannot be equal.
96+
// This is a hypothetical case to prevent panics when calling [reflect.Value.Convert].
97+
return false
98+
}
99+
100+
actualConverted := actualValue.Convert(expectedType)
101+
if !actualConverted.CanInterface() {
102+
// Cannot interface after conversion, so cannot be equal.
103+
// This is a hypothetical case to prevent panics when calling [reflect.Value.Convert].
104+
return false
105+
}
106+
107+
return actualConverted.Interface() == expected
78108
}
79109

80-
return expectedValue.Convert(actualType).Interface() == actual
110+
return expectedConverted.Interface() == actual
81111
}
82112

83113
// isNumericType returns true if the type is one of:

internal/assertions/object_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ func objectEqualValuesCases() iter.Seq[objectEqualCase] {
169169
{3.14, complex128(1e+100 + 1e+100i), false},
170170
{complex128(1e+10 + 1e+10i), complex64(1e+10 + 1e+10i), true},
171171
{complex64(1e+10 + 1e+10i), complex128(1e+10 + 1e+10i), true},
172+
{[]int{1, 2}, (*[3]int)(nil), false}, // panics should be caught and treated as inequality (https://github.com/stretchr/testify/issues/1699)
172173
})
173174
}
174175

internal/assertions/string.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ func Regexp(t T, rx any, str any, msgAndArgs ...any) bool {
2222
h.Helper()
2323
}
2424

25-
match := matchRegexp(rx, str)
25+
match, err := matchRegexp(rx, str)
26+
if err != nil {
27+
Fail(t, fmt.Sprintf("invalid regular expression %q: %v", rx, err), msgAndArgs...)
28+
29+
return false
30+
}
2631

2732
if !match {
2833
Fail(t, fmt.Sprintf(`Expect "%v" to match "%v"`, str, rx), msgAndArgs...)
@@ -44,7 +49,13 @@ func NotRegexp(t T, rx any, str any, msgAndArgs ...any) bool {
4449
if h, ok := t.(H); ok {
4550
h.Helper()
4651
}
47-
match := matchRegexp(rx, str)
52+
53+
match, err := matchRegexp(rx, str)
54+
if err != nil {
55+
Fail(t, fmt.Sprintf("invalid regular expression %q: %v", rx, err), msgAndArgs...)
56+
57+
return false
58+
}
4859

4960
if match {
5061
Fail(t, fmt.Sprintf("Expect \"%v\" to NOT match \"%v\"", str, rx), msgAndArgs...)
@@ -53,21 +64,28 @@ func NotRegexp(t T, rx any, str any, msgAndArgs ...any) bool {
5364
return !match
5465
}
5566

56-
// matchRegexp return true if a specified regexp matches a string.
57-
func matchRegexp(rx any, str any) bool {
67+
// matchRegexp returns whether the compiled regular expression matches the provided value.
68+
//
69+
// If rx is not a *[regexp.Regexp], rx is formatted with fmt.Sprint and compiled.
70+
// When compilation fails, an error is returned instead of panicking.
71+
func matchRegexp(rx any, str any) (bool, error) {
5872
var r *regexp.Regexp
5973
if rr, ok := rx.(*regexp.Regexp); ok {
6074
r = rr
6175
} else {
62-
r = regexp.MustCompile(fmt.Sprint(rx))
76+
var err error
77+
r, err = regexp.Compile(fmt.Sprint(rx))
78+
if err != nil {
79+
return false, err
80+
}
6381
}
6482

6583
switch v := str.(type) {
6684
case []byte:
67-
return r.Match(v)
85+
return r.Match(v), nil
6886
case string:
69-
return r.MatchString(v)
87+
return r.MatchString(v), nil
7088
default:
71-
return r.MatchString(fmt.Sprint(v))
89+
return r.MatchString(fmt.Sprint(v)), nil
7290
}
7391
}

internal/assertions/string_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,37 @@ func TestStringRegexp(t *testing.T) {
6161
}
6262
}
6363

64+
// Verifies that invalid patterns no longer cause a panic when using Regexp/NotRegexp.
65+
// Instead, the assertion should fail and return false.
66+
func TestStringRegexp_InvalidPattern(t *testing.T) {
67+
t.Parallel()
68+
69+
const (
70+
invalidPattern = "\\C"
71+
msg = "whatever"
72+
)
73+
74+
t.Run("Regexp should not panic on invalid patterns", func(t *testing.T) {
75+
result := NotPanics(t, func() {
76+
mockT := new(testing.T)
77+
False(t, Regexp(mockT, invalidPattern, msg))
78+
})
79+
if !result {
80+
t.Failed()
81+
}
82+
})
83+
84+
t.Run("NoRegexp should not panic on invalid patterns", func(t *testing.T) {
85+
result := NotPanics(t, func() {
86+
mockT := new(testing.T)
87+
False(t, NotRegexp(mockT, invalidPattern, msg))
88+
})
89+
if !result {
90+
t.Failed()
91+
}
92+
})
93+
}
94+
6495
type stringEqualCase struct {
6596
equalWant string
6697
equalGot string

0 commit comments

Comments
 (0)