Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 0 additions & 28 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -97,31 +97,3 @@ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

github.com/go-openapi/testify/_codegen/internal/imports
===========================

// SPDX-FileCopyrightText: Copyright (c) 2015 Ernesto Jiménez
// SPDX-License-Identifier: MIT

The MIT License (MIT)

Copyright (c) 2015 Ernesto Jiménez

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,20 @@ some adaptations into this fork:
* github.com/stretchr/testify#1772 - YAML library migration to maintained fork (go.yaml.in/yaml)
* github.com/stretchr/testify#1797 - Codegen package consolidation and licensing
* github.com/stretchr/testify#1356 - panic(nil) handling for Go 1.21+
* github.com/stretchr/testify#1825 - Fix panic when using EqualValues with uncomparable types [merged]
* github.com/stretchr/testify#1818 - Fix panic on invalid regex in Regexp/NotRegexp assertions [merged]

### Planned merges

#### Critical safety fixes (high priority)

* github.com/stretchr/testify#1825 - Fix panic when using EqualValues with uncomparable types
* github.com/stretchr/testify#1818 - Fix panic on invalid regex in Regexp/NotRegexp assertions
* Follow / adapt https://github.com/stretchr/testify/pull/1824

Not PRs, but reported issues in the original repo:

* https://github.com/stretchr/testify/issues/1826
* https://github.com/stretchr/testify/issues/1611
* https://github.com/stretchr/testify/issues/1813

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

Expand Down
38 changes: 34 additions & 4 deletions internal/assertions/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ func ObjectsAreEqual(expected, actual any) bool {
if !ok {
return false
}

if exp == nil || act == nil {
return exp == nil && act == nil
}

return bytes.Equal(exp, act)
}

Expand Down Expand Up @@ -63,21 +65,49 @@ func ObjectsAreEqualValues(expected, actual any) bool {
return false
}

// Attempt conversion of expected to actual type.
// This handles more cases than just the ConvertibleTo check above.
if !expectedValue.CanConvert(actualType) {
// Types are not convertible, so they cannot be equal
// This prevents panics when calling [reflect.Value.Convert]
return false
}

expectedConverted := expectedValue.Convert(actualType)
if !expectedConverted.CanInterface() {
// Cannot interface after conversion, so cannot be equal.
// This prevents panics when calling [reflect.Value.Interface].
return false
}

if !isNumericType(expectedType) || !isNumericType(actualType) {
// Attempt comparison after type conversion
// Attempt comparison after type conversion.
return reflect.DeepEqual(
expectedValue.Convert(actualType).Interface(), actual,
expectedConverted.Interface(), actual,
)
}

// If BOTH values are numeric, there are chances of false positives due
// to overflow or underflow. So, we need to make sure to always convert
// the smaller type to a larger type before comparing.
if expectedType.Size() >= actualType.Size() {
return actualValue.Convert(expectedType).Interface() == expected
if !actualValue.CanConvert(expectedType) {
// Cannot convert actual to the expected type, so cannot be equal.
// This is a hypothetical case to prevent panics when calling [reflect.Value.Convert].
return false
}

actualConverted := actualValue.Convert(expectedType)
if !actualConverted.CanInterface() {
// Cannot interface after conversion, so cannot be equal.
// This is a hypothetical case to prevent panics when calling [reflect.Value.Convert].
return false
}

return actualConverted.Interface() == expected
}

return expectedValue.Convert(actualType).Interface() == actual
return expectedConverted.Interface() == actual
}

// isNumericType returns true if the type is one of:
Expand Down
1 change: 1 addition & 0 deletions internal/assertions/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func objectEqualValuesCases() iter.Seq[objectEqualCase] {
{3.14, complex128(1e+100 + 1e+100i), false},
{complex128(1e+10 + 1e+10i), complex64(1e+10 + 1e+10i), true},
{complex64(1e+10 + 1e+10i), complex128(1e+10 + 1e+10i), true},
{[]int{1, 2}, (*[3]int)(nil), false}, // panics should be caught and treated as inequality (https://github.com/stretchr/testify/issues/1699)
})
}

Expand Down
34 changes: 26 additions & 8 deletions internal/assertions/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ func Regexp(t T, rx any, str any, msgAndArgs ...any) bool {
h.Helper()
}

match := matchRegexp(rx, str)
match, err := matchRegexp(rx, str)
if err != nil {
Fail(t, fmt.Sprintf("invalid regular expression %q: %v", rx, err), msgAndArgs...)

return false
}

if !match {
Fail(t, fmt.Sprintf(`Expect "%v" to match "%v"`, str, rx), msgAndArgs...)
Expand All @@ -44,7 +49,13 @@ func NotRegexp(t T, rx any, str any, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
match := matchRegexp(rx, str)

match, err := matchRegexp(rx, str)
if err != nil {
Fail(t, fmt.Sprintf("invalid regular expression %q: %v", rx, err), msgAndArgs...)

return false
}

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

// matchRegexp return true if a specified regexp matches a string.
func matchRegexp(rx any, str any) bool {
// matchRegexp returns whether the compiled regular expression matches the provided value.
//
// If rx is not a *[regexp.Regexp], rx is formatted with fmt.Sprint and compiled.
// When compilation fails, an error is returned instead of panicking.
func matchRegexp(rx any, str any) (bool, error) {
var r *regexp.Regexp
if rr, ok := rx.(*regexp.Regexp); ok {
r = rr
} else {
r = regexp.MustCompile(fmt.Sprint(rx))
var err error
r, err = regexp.Compile(fmt.Sprint(rx))
if err != nil {
return false, err
}
}

switch v := str.(type) {
case []byte:
return r.Match(v)
return r.Match(v), nil
case string:
return r.MatchString(v)
return r.MatchString(v), nil
default:
return r.MatchString(fmt.Sprint(v))
return r.MatchString(fmt.Sprint(v)), nil
}
}
31 changes: 31 additions & 0 deletions internal/assertions/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,37 @@ func TestStringRegexp(t *testing.T) {
}
}

// Verifies that invalid patterns no longer cause a panic when using Regexp/NotRegexp.
// Instead, the assertion should fail and return false.
func TestStringRegexp_InvalidPattern(t *testing.T) {
t.Parallel()

const (
invalidPattern = "\\C"
msg = "whatever"
)

t.Run("Regexp should not panic on invalid patterns", func(t *testing.T) {
result := NotPanics(t, func() {
mockT := new(testing.T)
False(t, Regexp(mockT, invalidPattern, msg))
})
if !result {
t.Failed()
}
})

t.Run("NoRegexp should not panic on invalid patterns", func(t *testing.T) {
result := NotPanics(t, func() {
mockT := new(testing.T)
False(t, NotRegexp(mockT, invalidPattern, msg))
})
if !result {
t.Failed()
}
})
}

type stringEqualCase struct {
equalWant string
equalGot string
Expand Down
Loading