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
32 changes: 25 additions & 7 deletions assert/assertion_format.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 49 additions & 13 deletions assert/assertion_forward.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"math"
"os"
"reflect"
Expand Down Expand Up @@ -1869,6 +1870,68 @@ 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.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...)
}

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.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...)
}

return true
}

// JSONEqBytes asserts that two JSON byte slices are equivalent.
//
// assert.JSONEqBytes(t, []byte(`{"hello": "world", "foo": "bar"}`), []byte(`{"foo": "bar", "hello": "world"}`))
Expand Down
Loading
Loading