Skip to content

Commit b5da3fc

Browse files
committed
feat: added FileEmpty, FileNotEmpty
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
1 parent 16518d5 commit b5da3fc

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

assert/assertions.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"errors"
88
"fmt"
9+
"io/fs"
910
"math"
1011
"os"
1112
"reflect"
@@ -1885,6 +1886,14 @@ func FileEmpty(t TestingT, path string, msgAndArgs ...any) bool {
18851886
if info.IsDir() {
18861887
return Fail(t, fmt.Sprintf("%q is a directory", path), msgAndArgs...)
18871888
}
1889+
if info.Mode()&fs.ModeSymlink > 0 {
1890+
target, err := os.Readlink(path)
1891+
if err != nil {
1892+
return Fail(t, fmt.Sprintf("could not resolve symlink %q", path), msgAndArgs...)
1893+
}
1894+
return FileEmpty(t, target, msgAndArgs...)
1895+
}
1896+
18881897
if info.Size() > 0 {
18891898
return Fail(t, fmt.Sprintf("%q is not empty", path), msgAndArgs...)
18901899
}
@@ -1908,6 +1917,14 @@ func FileNotEmpty(t TestingT, path string, msgAndArgs ...any) bool {
19081917
if info.IsDir() {
19091918
return Fail(t, fmt.Sprintf("%q is a directory", path), msgAndArgs...)
19101919
}
1920+
if info.Mode()&fs.ModeSymlink > 0 {
1921+
target, err := os.Readlink(path)
1922+
if err != nil {
1923+
return Fail(t, fmt.Sprintf("could not resolve symlink %q", path), msgAndArgs...)
1924+
}
1925+
return FileNotEmpty(t, target, msgAndArgs...)
1926+
}
1927+
19111928
if info.Size() == 0 {
19121929
return Fail(t, fmt.Sprintf("%q is empty", path), msgAndArgs...)
19131930
}

assert/assertions_test.go

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2738,7 +2738,7 @@ func getTempSymlinkPath(t *testing.T, file string) string {
27382738
t.Helper()
27392739

27402740
tempDir := t.TempDir()
2741-
link := filepath.Join(tempDir, file+"_symlink")
2741+
link := filepath.Join(tempDir, filepath.Base(file)+"_symlink")
27422742
if err := os.Symlink(file, link); err != nil {
27432743
t.Fatalf("could not create temp symlink %q pointing to %q: %v", link, file, err)
27442744
}
@@ -2787,6 +2787,62 @@ func TestNoDirExists(t *testing.T) {
27872787
True(t, NoDirExists(mockT, link))
27882788
}
27892789

2790+
func TestFileEmpty(t *testing.T) {
2791+
t.Parallel()
2792+
2793+
mockT := new(testing.T)
2794+
True(t, FileEmpty(mockT, filepath.Join("testdata", "empty_file")))
2795+
2796+
mockT = new(testing.T)
2797+
False(t, FileEmpty(mockT, "assertions.go"))
2798+
2799+
mockT = new(testing.T)
2800+
False(t, FileEmpty(mockT, "random_file"))
2801+
2802+
mockT = new(testing.T)
2803+
False(t, FileEmpty(mockT, "../_codegen"))
2804+
2805+
link := getTempSymlinkPath(t, filepath.Join("testdata", "empty_file"))
2806+
mockT = new(testing.T)
2807+
True(t, FileEmpty(mockT, link))
2808+
2809+
link = getTempSymlinkPath(t, "assertions.go")
2810+
mockT = new(testing.T)
2811+
False(t, FileEmpty(mockT, link))
2812+
2813+
link = getTempSymlinkPath(t, "non_existent_file")
2814+
mockT = new(testing.T)
2815+
False(t, FileEmpty(mockT, link))
2816+
}
2817+
2818+
func TestFileNotEmpty(t *testing.T) {
2819+
t.Parallel()
2820+
2821+
mockT := new(testing.T)
2822+
True(t, FileNotEmpty(mockT, "assertions.go"))
2823+
2824+
mockT = new(testing.T)
2825+
False(t, FileNotEmpty(mockT, filepath.Join("testdata", "empty_file")))
2826+
2827+
mockT = new(testing.T)
2828+
False(t, FileNotEmpty(mockT, "non_existent_file"))
2829+
2830+
mockT = new(testing.T)
2831+
False(t, FileNotEmpty(mockT, "../_codegen"))
2832+
2833+
link := getTempSymlinkPath(t, filepath.Join("testdata", "empty_file"))
2834+
mockT = new(testing.T)
2835+
False(t, FileNotEmpty(mockT, link))
2836+
2837+
link = getTempSymlinkPath(t, "assertions.go")
2838+
mockT = new(testing.T)
2839+
True(t, FileNotEmpty(mockT, link))
2840+
2841+
link = getTempSymlinkPath(t, "non_existent_file")
2842+
mockT = new(testing.T)
2843+
False(t, NoFileExists(mockT, link))
2844+
}
2845+
27902846
func TestJSONEq_EqualSONString(t *testing.T) {
27912847
t.Parallel()
27922848

assert/testdata/empty_file

Whitespace-only changes.

0 commit comments

Comments
 (0)