From 280617d5689bc8b3f421b0c97e5cf68579f2350b Mon Sep 17 00:00:00 2001 From: Frederic BIDON Date: Fri, 12 Dec 2025 22:54:25 +0100 Subject: [PATCH 1/2] missing tests Signed-off-by: Frederic BIDON --- assert/assertion_format.go | 32 ++++++++++++++----- assert/assertion_forward.go | 62 +++++++++++++++++++++++++++++-------- assert/assertions.go | 46 +++++++++++++++++++++++++++ require/require.go | 60 +++++++++++++++++++++++++++++++---- require/require_forward.go | 48 ++++++++++++++++++++++++---- 5 files changed, 216 insertions(+), 32 deletions(-) diff --git a/assert/assertion_format.go b/assert/assertion_format.go index 5e11e5114..729b8f439 100644 --- a/assert/assertion_format.go +++ b/assert/assertion_format.go @@ -1,4 +1,4 @@ -// Code generated with github.com/stretchr/testify/_codegen; DO NOT EDIT. +// Code generated with github.com/go-openapi/testify/v2/_codegen; DO NOT EDIT. package assert @@ -42,7 +42,7 @@ func DirExistsf(t TestingT, path string, msg string, args ...any) bool { // listB(array, slice...) ignoring the order of the elements. If there are duplicate elements, // the number of appearances of each of them in both lists should match. // -// assert.ElementsMatchf(t, [1, 3, 2, 3], [1, 3, 3, 2], "error message %s", "formatted") +// assert.ElementsMatchf(t, [1, 3, 2, 3], [1, 3, 3, 2], "error message %s", "formatted"). func ElementsMatchf(t TestingT, listA any, listB any, msg string, args ...any) bool { if h, ok := t.(tHelper); ok { h.Helper() @@ -211,7 +211,7 @@ func Exactlyf(t TestingT, expected any, actual any, msg string, args ...any) boo return Exactly(t, expected, actual, append([]any{msg}, args...)...) } -// Failf reports a failure through +// Failf reports a failure through. func Failf(t TestingT, failureMessage string, msg string, args ...any) bool { if h, ok := t.(tHelper); ok { h.Helper() @@ -219,7 +219,7 @@ func Failf(t TestingT, failureMessage string, msg string, args ...any) bool { return Fail(t, failureMessage, append([]any{msg}, args...)...) } -// FailNowf fails test +// FailNowf fails test. func FailNowf(t TestingT, failureMessage string, msg string, args ...any) bool { if h, ok := t.(tHelper); ok { h.Helper() @@ -237,6 +237,15 @@ func Falsef(t TestingT, value bool, msg string, args ...any) bool { return False(t, value, append([]any{msg}, args...)...) } +// FileEmptyf checks whether a file exists in the given path and is empty. +// It fails if the file is not empty, if the path points to a directory or there is an error when trying to check the file. +func FileEmptyf(t TestingT, path string, msg string, args ...any) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return FileEmpty(t, path, append([]any{msg}, args...)...) +} + // FileExistsf checks whether a file exists in the given path. It also fails if // the path points to a directory or there is an error when trying to check the file. func FileExistsf(t TestingT, path string, msg string, args ...any) bool { @@ -246,6 +255,15 @@ func FileExistsf(t TestingT, path string, msg string, args ...any) bool { return FileExists(t, path, append([]any{msg}, args...)...) } +// FileNotEmptyf checks whether a file exists in the given path and is not empty. +// It fails if the file is empty, if the path points to a directory or there is an error when trying to check the file. +func FileNotEmptyf(t TestingT, path string, msg string, args ...any) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return FileNotEmpty(t, path, append([]any{msg}, args...)...) +} + // Greaterf asserts that the first element is greater than the second // // assert.Greaterf(t, 2, 1, "error message %s", "formatted") @@ -381,7 +399,7 @@ func InDeltaSlicef(t TestingT, expected any, actual any, delta float64, msg stri return InDeltaSlice(t, expected, actual, delta, append([]any{msg}, args...)...) } -// InEpsilonf asserts that expected and actual have a relative error less than epsilon +// InEpsilonf asserts that expected and actual have a relative error less than epsilon. func InEpsilonf(t TestingT, expected any, actual any, epsilon float64, msg string, args ...any) bool { if h, ok := t.(tHelper); ok { h.Helper() @@ -606,7 +624,7 @@ func NotContainsf(t TestingT, s any, contains any, msg string, args ...any) bool // // assert.NotElementsMatchf(t, [1, 1, 2, 3], [1, 2, 3], "error message %s", "formatted") -> true // -// assert.NotElementsMatchf(t, [1, 2, 3], [1, 2, 4], "error message %s", "formatted") -> true +// assert.NotElementsMatchf(t, [1, 2, 3], [1, 2, 4], "error message %s", "formatted") -> true. func NotElementsMatchf(t TestingT, listA any, listB any, msg string, args ...any) bool { if h, ok := t.(tHelper); ok { h.Helper() @@ -841,7 +859,7 @@ func Truef(t TestingT, value bool, msg string, args ...any) bool { // WithinDurationf asserts that the two times are within duration delta of each other. // -// assert.WithinDurationf(t, time.Now(), time.Now(), 10*time.Second, "error message %s", "formatted") +// assert.WithinDurationf(t, time.Now(), 10*time.Second, "error message %s", "formatted") func WithinDurationf(t TestingT, expected time.Time, actual time.Time, delta time.Duration, msg string, args ...any) bool { if h, ok := t.(tHelper); ok { h.Helper() diff --git a/assert/assertion_forward.go b/assert/assertion_forward.go index f4edf6032..2fe26d5d2 100644 --- a/assert/assertion_forward.go +++ b/assert/assertion_forward.go @@ -1,4 +1,4 @@ -// Code generated with github.com/stretchr/testify/_codegen; DO NOT EDIT. +// Code generated with github.com/go-openapi/testify/v2/_codegen; DO NOT EDIT. package assert @@ -72,7 +72,7 @@ func (a *Assertions) DirExistsf(path string, msg string, args ...any) bool { // listB(array, slice...) ignoring the order of the elements. If there are duplicate elements, // the number of appearances of each of them in both lists should match. // -// a.ElementsMatch([1, 3, 2, 3], [1, 3, 3, 2]) +// a.ElementsMatch([1, 3, 2, 3], [1, 3, 3, 2]). func (a *Assertions) ElementsMatch(listA any, listB any, msgAndArgs ...any) bool { if h, ok := a.t.(tHelper); ok { h.Helper() @@ -84,7 +84,7 @@ func (a *Assertions) ElementsMatch(listA any, listB any, msgAndArgs ...any) bool // listB(array, slice...) ignoring the order of the elements. If there are duplicate elements, // the number of appearances of each of them in both lists should match. // -// a.ElementsMatchf([1, 3, 2, 3], [1, 3, 3, 2], "error message %s", "formatted") +// a.ElementsMatchf([1, 3, 2, 3], [1, 3, 3, 2], "error message %s", "formatted"). func (a *Assertions) ElementsMatchf(listA any, listB any, msg string, args ...any) bool { if h, ok := a.t.(tHelper); ok { h.Helper() @@ -414,7 +414,7 @@ func (a *Assertions) Exactlyf(expected any, actual any, msg string, args ...any) return Exactlyf(a.t, expected, actual, msg, args...) } -// Fail reports a failure through +// Fail reports a failure through. func (a *Assertions) Fail(failureMessage string, msgAndArgs ...any) bool { if h, ok := a.t.(tHelper); ok { h.Helper() @@ -422,7 +422,7 @@ func (a *Assertions) Fail(failureMessage string, msgAndArgs ...any) bool { return Fail(a.t, failureMessage, msgAndArgs...) } -// FailNow fails test +// FailNow fails test. func (a *Assertions) FailNow(failureMessage string, msgAndArgs ...any) bool { if h, ok := a.t.(tHelper); ok { h.Helper() @@ -430,7 +430,7 @@ func (a *Assertions) FailNow(failureMessage string, msgAndArgs ...any) bool { return FailNow(a.t, failureMessage, msgAndArgs...) } -// FailNowf fails test +// FailNowf fails test. func (a *Assertions) FailNowf(failureMessage string, msg string, args ...any) bool { if h, ok := a.t.(tHelper); ok { h.Helper() @@ -438,7 +438,7 @@ func (a *Assertions) FailNowf(failureMessage string, msg string, args ...any) bo return FailNowf(a.t, failureMessage, msg, args...) } -// Failf reports a failure through +// Failf reports a failure through. func (a *Assertions) Failf(failureMessage string, msg string, args ...any) bool { if h, ok := a.t.(tHelper); ok { h.Helper() @@ -466,6 +466,24 @@ func (a *Assertions) Falsef(value bool, msg string, args ...any) bool { return Falsef(a.t, value, msg, args...) } +// FileEmpty checks whether a file exists in the given path and is empty. +// It fails if the file is not empty, if the path points to a directory or there is an error when trying to check the file. +func (a *Assertions) FileEmpty(path string, msgAndArgs ...any) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return FileEmpty(a.t, path, msgAndArgs...) +} + +// FileEmptyf checks whether a file exists in the given path and is empty. +// It fails if the file is not empty, if the path points to a directory or there is an error when trying to check the file. +func (a *Assertions) FileEmptyf(path string, msg string, args ...any) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return FileEmptyf(a.t, path, msg, args...) +} + // FileExists checks whether a file exists in the given path. It also fails if // the path points to a directory or there is an error when trying to check the file. func (a *Assertions) FileExists(path string, msgAndArgs ...any) bool { @@ -484,6 +502,24 @@ func (a *Assertions) FileExistsf(path string, msg string, args ...any) bool { return FileExistsf(a.t, path, msg, args...) } +// FileNotEmpty checks whether a file exists in the given path and is not empty. +// It fails if the file is empty, if the path points to a directory or there is an error when trying to check the file. +func (a *Assertions) FileNotEmpty(path string, msgAndArgs ...any) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return FileNotEmpty(a.t, path, msgAndArgs...) +} + +// FileNotEmptyf checks whether a file exists in the given path and is not empty. +// It fails if the file is empty, if the path points to a directory or there is an error when trying to check the file. +func (a *Assertions) FileNotEmptyf(path string, msg string, args ...any) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return FileNotEmptyf(a.t, path, msg, args...) +} + // Greater asserts that the first element is greater than the second // // a.Greater(2, 1) @@ -754,7 +790,7 @@ func (a *Assertions) InDeltaf(expected any, actual any, delta float64, msg strin return InDeltaf(a.t, expected, actual, delta, msg, args...) } -// InEpsilon asserts that expected and actual have a relative error less than epsilon +// InEpsilon asserts that expected and actual have a relative error less than epsilon. func (a *Assertions) InEpsilon(expected any, actual any, epsilon float64, msgAndArgs ...any) bool { if h, ok := a.t.(tHelper); ok { h.Helper() @@ -778,7 +814,7 @@ func (a *Assertions) InEpsilonSlicef(expected any, actual any, epsilon float64, return InEpsilonSlicef(a.t, expected, actual, epsilon, msg, args...) } -// InEpsilonf asserts that expected and actual have a relative error less than epsilon +// InEpsilonf asserts that expected and actual have a relative error less than epsilon. func (a *Assertions) InEpsilonf(expected any, actual any, epsilon float64, msg string, args ...any) bool { if h, ok := a.t.(tHelper); ok { h.Helper() @@ -1195,7 +1231,7 @@ func (a *Assertions) NotContainsf(s any, contains any, msg string, args ...any) // // a.NotElementsMatch([1, 1, 2, 3], [1, 2, 3]) -> true // -// a.NotElementsMatch([1, 2, 3], [1, 2, 4]) -> true +// a.NotElementsMatch([1, 2, 3], [1, 2, 4]) -> true. func (a *Assertions) NotElementsMatch(listA any, listB any, msgAndArgs ...any) bool { if h, ok := a.t.(tHelper); ok { h.Helper() @@ -1212,7 +1248,7 @@ func (a *Assertions) NotElementsMatch(listA any, listB any, msgAndArgs ...any) b // // a.NotElementsMatchf([1, 1, 2, 3], [1, 2, 3], "error message %s", "formatted") -> true // -// a.NotElementsMatchf([1, 2, 3], [1, 2, 4], "error message %s", "formatted") -> true +// a.NotElementsMatchf([1, 2, 3], [1, 2, 4], "error message %s", "formatted") -> true. func (a *Assertions) NotElementsMatchf(listA any, listB any, msg string, args ...any) bool { if h, ok := a.t.(tHelper); ok { h.Helper() @@ -1672,7 +1708,7 @@ func (a *Assertions) Truef(value bool, msg string, args ...any) bool { // WithinDuration asserts that the two times are within duration delta of each other. // -// a.WithinDuration(time.Now(), time.Now(), 10*time.Second) +// a.WithinDuration(time.Now(), 10*time.Second) func (a *Assertions) WithinDuration(expected time.Time, actual time.Time, delta time.Duration, msgAndArgs ...any) bool { if h, ok := a.t.(tHelper); ok { h.Helper() @@ -1682,7 +1718,7 @@ func (a *Assertions) WithinDuration(expected time.Time, actual time.Time, delta // WithinDurationf asserts that the two times are within duration delta of each other. // -// a.WithinDurationf(time.Now(), time.Now(), 10*time.Second, "error message %s", "formatted") +// a.WithinDurationf(time.Now(), 10*time.Second, "error message %s", "formatted") func (a *Assertions) WithinDurationf(expected time.Time, actual time.Time, delta time.Duration, msg string, args ...any) bool { if h, ok := a.t.(tHelper); ok { h.Helper() diff --git a/assert/assertions.go b/assert/assertions.go index 5656f5c63..ccd498519 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -1869,6 +1869,52 @@ func NoDirExists(t TestingT, path string, msgAndArgs ...any) bool { return Fail(t, fmt.Sprintf("directory %q exists", path), msgAndArgs...) } +// FileEmpty checks whether a file exists in the given path and is empty. +// It fails if the file is not empty, if the path points to a directory or there is an error when trying to check the file. +func FileEmpty(t TestingT, path string, msgAndArgs ...any) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + info, err := os.Lstat(path) + if err != nil { + if os.IsNotExist(err) { + return Fail(t, fmt.Sprintf("unable to find file %q", path), msgAndArgs...) + } + return Fail(t, fmt.Sprintf("error when running os.Lstat(%q): %s", path, err), msgAndArgs...) + } + if info.IsDir() { + return Fail(t, fmt.Sprintf("%q is a directory", path), msgAndArgs...) + } + if info.Size() > 0 { + return Fail(t, fmt.Sprintf("%q is not empty", path), msgAndArgs...) + } + + return true +} + +// FileNotEmpty checks whether a file exists in the given path and is not empty. +// It fails if the file is empty, if the path points to a directory or there is an error when trying to check the file. +func FileNotEmpty(t TestingT, path string, msgAndArgs ...any) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + info, err := os.Lstat(path) + if err != nil { + if os.IsNotExist(err) { + return Fail(t, fmt.Sprintf("unable to find file %q", path), msgAndArgs...) + } + return Fail(t, fmt.Sprintf("error when running os.Lstat(%q): %s", path, err), msgAndArgs...) + } + if info.IsDir() { + return Fail(t, fmt.Sprintf("%q is a directory", path), msgAndArgs...) + } + if info.Size() == 0 { + return Fail(t, fmt.Sprintf("%q is empty", path), msgAndArgs...) + } + + return true +} + // JSONEqBytes asserts that two JSON byte slices are equivalent. // // assert.JSONEqBytes(t, []byte(`{"hello": "world", "foo": "bar"}`), []byte(`{"foo": "bar", "hello": "world"}`)) diff --git a/require/require.go b/require/require.go index 1b2debdf9..6a54e1261 100644 --- a/require/require.go +++ b/require/require.go @@ -106,7 +106,7 @@ func ElementsMatch(t TestingT, listA any, listB any, msgAndArgs ...any) { // listB(array, slice...) ignoring the order of the elements. If there are duplicate elements, // the number of appearances of each of them in both lists should match. // -// require.ElementsMatchf(t, [1, 3, 2, 3], [1, 3, 3, 2], "error message %s", "formatted") +// require.ElementsMatchf(t, [1, 3, 2, 3], [1, 3, 3, 2], "error message %s", "formatted"). func ElementsMatchf(t TestingT, listA any, listB any, msg string, args ...any) { if h, ok := t.(tHelper); ok { h.Helper() @@ -533,7 +533,7 @@ func FailNow(t TestingT, failureMessage string, msgAndArgs ...any) { t.FailNow() } -// FailNowf fails test +// FailNowf fails test. func FailNowf(t TestingT, failureMessage string, msg string, args ...any) { if h, ok := t.(tHelper); ok { h.Helper() @@ -544,7 +544,7 @@ func FailNowf(t TestingT, failureMessage string, msg string, args ...any) { t.FailNow() } -// Failf reports a failure through +// Failf reports a failure through. func Failf(t TestingT, failureMessage string, msg string, args ...any) { if h, ok := t.(tHelper); ok { h.Helper() @@ -581,6 +581,30 @@ func Falsef(t TestingT, value bool, msg string, args ...any) { t.FailNow() } +// FileEmpty checks whether a file exists in the given path and is empty. +// It fails if the file is not empty, if the path points to a directory or there is an error when trying to check the file. +func FileEmpty(t TestingT, path string, msgAndArgs ...any) { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if assert.FileEmpty(t, path, msgAndArgs...) { + return + } + t.FailNow() +} + +// FileEmptyf checks whether a file exists in the given path and is empty. +// It fails if the file is not empty, if the path points to a directory or there is an error when trying to check the file. +func FileEmptyf(t TestingT, path string, msg string, args ...any) { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if assert.FileEmptyf(t, path, msg, args...) { + return + } + t.FailNow() +} + // FileExists checks whether a file exists in the given path. It also fails if // the path points to a directory or there is an error when trying to check the file. func FileExists(t TestingT, path string, msgAndArgs ...any) { @@ -605,6 +629,30 @@ func FileExistsf(t TestingT, path string, msg string, args ...any) { t.FailNow() } +// FileNotEmpty checks whether a file exists in the given path and is not empty. +// It fails if the file is empty, if the path points to a directory or there is an error when trying to check the file. +func FileNotEmpty(t TestingT, path string, msgAndArgs ...any) { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if assert.FileNotEmpty(t, path, msgAndArgs...) { + return + } + t.FailNow() +} + +// FileNotEmptyf checks whether a file exists in the given path and is not empty. +// It fails if the file is empty, if the path points to a directory or there is an error when trying to check the file. +func FileNotEmptyf(t TestingT, path string, msg string, args ...any) { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if assert.FileNotEmptyf(t, path, msg, args...) { + return + } + t.FailNow() +} + // Greater asserts that the first element is greater than the second // // require.Greater(t, 2, 1) @@ -980,7 +1028,7 @@ func InEpsilonSlicef(t TestingT, expected any, actual any, epsilon float64, msg t.FailNow() } -// InEpsilonf asserts that expected and actual have a relative error less than epsilon +// InEpsilonf asserts that expected and actual have a relative error less than epsilon. func InEpsilonf(t TestingT, expected any, actual any, epsilon float64, msg string, args ...any) { if h, ok := t.(tHelper); ok { h.Helper() @@ -1528,7 +1576,7 @@ func NotElementsMatch(t TestingT, listA any, listB any, msgAndArgs ...any) { // // require.NotElementsMatchf(t, [1, 1, 2, 3], [1, 2, 3], "error message %s", "formatted") -> true // -// require.NotElementsMatchf(t, [1, 2, 3], [1, 2, 4], "error message %s", "formatted") -> true +// require.NotElementsMatchf(t, [1, 2, 3], [1, 2, 4], "error message %s", "formatted") -> true. func NotElementsMatchf(t TestingT, listA any, listB any, msg string, args ...any) { if h, ok := t.(tHelper); ok { h.Helper() @@ -2124,7 +2172,7 @@ func WithinDuration(t TestingT, expected time.Time, actual time.Time, delta time // WithinDurationf asserts that the two times are within duration delta of each other. // -// require.WithinDurationf(t, time.Now(), time.Now(), 10*time.Second, "error message %s", "formatted") +// require.WithinDurationf(t, time.Now(), 10*time.Second, "error message %s", "formatted") func WithinDurationf(t TestingT, expected time.Time, actual time.Time, delta time.Duration, msg string, args ...any) { if h, ok := t.(tHelper); ok { h.Helper() diff --git a/require/require_forward.go b/require/require_forward.go index cbfe35ffd..82b921488 100644 --- a/require/require_forward.go +++ b/require/require_forward.go @@ -85,7 +85,7 @@ func (a *Assertions) ElementsMatch(listA any, listB any, msgAndArgs ...any) { // listB(array, slice...) ignoring the order of the elements. If there are duplicate elements, // the number of appearances of each of them in both lists should match. // -// a.ElementsMatchf([1, 3, 2, 3], [1, 3, 3, 2], "error message %s", "formatted") +// a.ElementsMatchf([1, 3, 2, 3], [1, 3, 3, 2], "error message %s", "formatted"). func (a *Assertions) ElementsMatchf(listA any, listB any, msg string, args ...any) { if h, ok := a.t.(tHelper); ok { h.Helper() @@ -431,7 +431,7 @@ func (a *Assertions) FailNow(failureMessage string, msgAndArgs ...any) { FailNow(a.t, failureMessage, msgAndArgs...) } -// FailNowf fails test +// FailNowf fails test. func (a *Assertions) FailNowf(failureMessage string, msg string, args ...any) { if h, ok := a.t.(tHelper); ok { h.Helper() @@ -439,7 +439,7 @@ func (a *Assertions) FailNowf(failureMessage string, msg string, args ...any) { FailNowf(a.t, failureMessage, msg, args...) } -// Failf reports a failure through +// Failf reports a failure through. func (a *Assertions) Failf(failureMessage string, msg string, args ...any) { if h, ok := a.t.(tHelper); ok { h.Helper() @@ -467,6 +467,24 @@ func (a *Assertions) Falsef(value bool, msg string, args ...any) { Falsef(a.t, value, msg, args...) } +// FileEmpty checks whether a file exists in the given path and is empty. +// It fails if the file is not empty, if the path points to a directory or there is an error when trying to check the file. +func (a *Assertions) FileEmpty(path string, msgAndArgs ...any) { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + FileEmpty(a.t, path, msgAndArgs...) +} + +// FileEmptyf checks whether a file exists in the given path and is empty. +// It fails if the file is not empty, if the path points to a directory or there is an error when trying to check the file. +func (a *Assertions) FileEmptyf(path string, msg string, args ...any) { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + FileEmptyf(a.t, path, msg, args...) +} + // FileExists checks whether a file exists in the given path. It also fails if // the path points to a directory or there is an error when trying to check the file. func (a *Assertions) FileExists(path string, msgAndArgs ...any) { @@ -485,6 +503,24 @@ func (a *Assertions) FileExistsf(path string, msg string, args ...any) { FileExistsf(a.t, path, msg, args...) } +// FileNotEmpty checks whether a file exists in the given path and is not empty. +// It fails if the file is empty, if the path points to a directory or there is an error when trying to check the file. +func (a *Assertions) FileNotEmpty(path string, msgAndArgs ...any) { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + FileNotEmpty(a.t, path, msgAndArgs...) +} + +// FileNotEmptyf checks whether a file exists in the given path and is not empty. +// It fails if the file is empty, if the path points to a directory or there is an error when trying to check the file. +func (a *Assertions) FileNotEmptyf(path string, msg string, args ...any) { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + FileNotEmptyf(a.t, path, msg, args...) +} + // Greater asserts that the first element is greater than the second // // a.Greater(2, 1) @@ -779,7 +815,7 @@ func (a *Assertions) InEpsilonSlicef(expected any, actual any, epsilon float64, InEpsilonSlicef(a.t, expected, actual, epsilon, msg, args...) } -// InEpsilonf asserts that expected and actual have a relative error less than epsilon +// InEpsilonf asserts that expected and actual have a relative error less than epsilon. func (a *Assertions) InEpsilonf(expected any, actual any, epsilon float64, msg string, args ...any) { if h, ok := a.t.(tHelper); ok { h.Helper() @@ -1213,7 +1249,7 @@ func (a *Assertions) NotElementsMatch(listA any, listB any, msgAndArgs ...any) { // // a.NotElementsMatchf([1, 1, 2, 3], [1, 2, 3], "error message %s", "formatted") -> true // -// a.NotElementsMatchf([1, 2, 3], [1, 2, 4], "error message %s", "formatted") -> true +// a.NotElementsMatchf([1, 2, 3], [1, 2, 4], "error message %s", "formatted") -> true. func (a *Assertions) NotElementsMatchf(listA any, listB any, msg string, args ...any) { if h, ok := a.t.(tHelper); ok { h.Helper() @@ -1683,7 +1719,7 @@ func (a *Assertions) WithinDuration(expected time.Time, actual time.Time, delta // WithinDurationf asserts that the two times are within duration delta of each other. // -// a.WithinDurationf(time.Now(), time.Now(), 10*time.Second, "error message %s", "formatted") +// a.WithinDurationf(time.Now(), 10*time.Second, "error message %s", "formatted") func (a *Assertions) WithinDurationf(expected time.Time, actual time.Time, delta time.Duration, msg string, args ...any) { if h, ok := a.t.(tHelper); ok { h.Helper() From a4e8df16879cae0a5a1a9274e79a5a5e48bc5c36 Mon Sep 17 00:00:00 2001 From: Frederic BIDON Date: Sun, 14 Dec 2025 19:02:44 +0100 Subject: [PATCH 2/2] feat: added FileEmpty, FileNotEmpty Signed-off-by: Frederic BIDON --- assert/assertions.go | 17 +++++++++++ assert/assertions_test.go | 58 +++++++++++++++++++++++++++++++++++++- assert/testdata/empty_file | 0 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 assert/testdata/empty_file diff --git a/assert/assertions.go b/assert/assertions.go index ccd498519..d4093317d 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -6,6 +6,7 @@ import ( "encoding/json" "errors" "fmt" + "io/fs" "math" "os" "reflect" @@ -1885,6 +1886,14 @@ func FileEmpty(t TestingT, path string, msgAndArgs ...any) bool { if info.IsDir() { return Fail(t, fmt.Sprintf("%q is a directory", path), msgAndArgs...) } + if info.Mode()&fs.ModeSymlink > 0 { + target, err := os.Readlink(path) + if err != nil { + return Fail(t, fmt.Sprintf("could not resolve symlink %q", path), msgAndArgs...) + } + return FileEmpty(t, target, msgAndArgs...) + } + if info.Size() > 0 { return Fail(t, fmt.Sprintf("%q is not empty", path), msgAndArgs...) } @@ -1908,6 +1917,14 @@ func FileNotEmpty(t TestingT, path string, msgAndArgs ...any) bool { if info.IsDir() { return Fail(t, fmt.Sprintf("%q is a directory", path), msgAndArgs...) } + if info.Mode()&fs.ModeSymlink > 0 { + target, err := os.Readlink(path) + if err != nil { + return Fail(t, fmt.Sprintf("could not resolve symlink %q", path), msgAndArgs...) + } + return FileNotEmpty(t, target, msgAndArgs...) + } + if info.Size() == 0 { return Fail(t, fmt.Sprintf("%q is empty", path), msgAndArgs...) } diff --git a/assert/assertions_test.go b/assert/assertions_test.go index 5e2151d43..2b47f105c 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -2738,7 +2738,7 @@ func getTempSymlinkPath(t *testing.T, file string) string { t.Helper() tempDir := t.TempDir() - link := filepath.Join(tempDir, file+"_symlink") + link := filepath.Join(tempDir, filepath.Base(file)+"_symlink") if err := os.Symlink(file, link); err != nil { t.Fatalf("could not create temp symlink %q pointing to %q: %v", link, file, err) } @@ -2787,6 +2787,62 @@ func TestNoDirExists(t *testing.T) { True(t, NoDirExists(mockT, link)) } +func TestFileEmpty(t *testing.T) { + t.Parallel() + + mockT := new(testing.T) + True(t, FileEmpty(mockT, filepath.Join("testdata", "empty_file"))) + + mockT = new(testing.T) + False(t, FileEmpty(mockT, "assertions.go")) + + mockT = new(testing.T) + False(t, FileEmpty(mockT, "random_file")) + + mockT = new(testing.T) + False(t, FileEmpty(mockT, "../_codegen")) + + link := getTempSymlinkPath(t, filepath.Join("testdata", "empty_file")) + mockT = new(testing.T) + True(t, FileEmpty(mockT, link)) + + link = getTempSymlinkPath(t, "assertions.go") + mockT = new(testing.T) + False(t, FileEmpty(mockT, link)) + + link = getTempSymlinkPath(t, "non_existent_file") + mockT = new(testing.T) + False(t, FileEmpty(mockT, link)) +} + +func TestFileNotEmpty(t *testing.T) { + t.Parallel() + + mockT := new(testing.T) + True(t, FileNotEmpty(mockT, "assertions.go")) + + mockT = new(testing.T) + False(t, FileNotEmpty(mockT, filepath.Join("testdata", "empty_file"))) + + mockT = new(testing.T) + False(t, FileNotEmpty(mockT, "non_existent_file")) + + mockT = new(testing.T) + False(t, FileNotEmpty(mockT, "../_codegen")) + + link := getTempSymlinkPath(t, filepath.Join("testdata", "empty_file")) + mockT = new(testing.T) + False(t, FileNotEmpty(mockT, link)) + + link = getTempSymlinkPath(t, "assertions.go") + mockT = new(testing.T) + True(t, FileNotEmpty(mockT, link)) + + link = getTempSymlinkPath(t, "non_existent_file") + mockT = new(testing.T) + False(t, NoFileExists(mockT, link)) +} + func TestJSONEq_EqualSONString(t *testing.T) { t.Parallel() diff --git a/assert/testdata/empty_file b/assert/testdata/empty_file new file mode 100644 index 000000000..e69de29bb