Skip to content

Commit

Permalink
✅ test: fsutil - update some util func and add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 12, 2023
1 parent 197c22d commit 2d7deb4
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 8 deletions.
1 change: 1 addition & 0 deletions fsutil/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func TestFsUtil_common(t *testing.T) {
assert.Eq(t, "test.jpg", fsutil.Name("path/to/test.jpg"))
assert.Eq(t, "", fsutil.Name(""))

assert.NotEmpty(t, fsutil.DirPath("path/to/test.jpg"))
if runtime.GOOS == "windows" {
assert.Eq(t, "path\\to", fsutil.Dir("path/to/test.jpg"))
} else {
Expand Down
29 changes: 29 additions & 0 deletions fsutil/define_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package fsutil_test

import (
"fmt"
"testing"

"github.com/gookit/goutil/fsutil"
"github.com/gookit/goutil/testutil/assert"
"github.com/gookit/goutil/testutil/fakeobj"
)

func TestNewEntry(t *testing.T) {
fPath := "/path/to/some.txt"
ent := fakeobj.NewDirEntry(fPath)

fe := fsutil.NewEntry(fPath, ent)
assert.False(t, fe.IsDir())
assert.Eq(t, fPath, fe.Path())
assert.Eq(t, fsutil.Name(fPath), fe.Name())
assert.Equal(t, "file: /path/to/some.txt", fmt.Sprint(fe))

fi, err := fe.Info()
assert.NoError(t, err)
// dump.P(fi)
assert.Equal(t, "some.txt", fi.Name())

nfi := fsutil.NewFileInfo(fPath, fi)
assert.Equal(t, fPath, nfi.Path())
}
3 changes: 3 additions & 0 deletions fsutil/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
"github.com/gookit/goutil/internal/comfunc"
)

// DirPath get dir path from filepath, without last name.
func DirPath(fpath string) string { return filepath.Dir(fpath) }

// Dir get dir path from filepath, without last name.
func Dir(fpath string) string { return filepath.Dir(fpath) }

Expand Down
5 changes: 5 additions & 0 deletions fsutil/operate.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,11 @@ func MustRemove(fPath string) {
// NOTICE: will ignore error
func QuietRemove(fPath string) { _ = os.Remove(fPath) }

// SafeRemoveAll removes path and any children it contains. will ignore error
func SafeRemoveAll(path string) {
_ = os.RemoveAll(path)
}

// RmIfExist removes the named file or (empty) directory on exists.
func RmIfExist(fPath string) error { return DeleteIfExist(fPath) }

Expand Down
17 changes: 10 additions & 7 deletions fsutil/operate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,21 @@ func TestMkdir(t *testing.T) {
return
}

err := os.Chmod("./testdata", os.ModePerm)
err := os.Chmod("testdata", os.ModePerm)

if assert.NoErr(t, err) {
assert.NoErr(t, fsutil.Mkdir("./testdata/sub/sub21", os.ModePerm))
assert.NoErr(t, fsutil.Mkdir("./testdata/sub/sub22", 0666))
assert.NoErr(t, fsutil.Mkdir("testdata/sub/sub21", os.ModePerm))
assert.NoErr(t, fsutil.Mkdir("testdata/sub/sub22", 0666))
// 066X will error
assert.NoErr(t, fsutil.Mkdir("./testdata/sub/sub23/sub31", 0777))
assert.NoErr(t, fsutil.Mkdir("testdata/sub/sub23/sub31", 0774))

assert.NoErr(t, fsutil.MkParentDir("./testdata/sub/sub24/sub32"))
assert.True(t, fsutil.IsDir("./testdata/sub/sub24"))
assert.NoErr(t, fsutil.MkParentDir("testdata/sub/sub24/sub32"))
assert.True(t, fsutil.IsDir("testdata/sub/sub24"))
fsutil.SafeRemoveAll("testdata/sub")

assert.NoErr(t, os.RemoveAll("./testdata/sub"))
assert.NoErr(t, fsutil.MkDirs(0774, "testdata/sub1/sub21", "testdata/sub1/sub22"))
assert.NoErr(t, fsutil.MkSubDirs(0774, "testdata/sub2", "sub21", "sub22"))
assert.NoErr(t, fsutil.RemoveSub("testdata", fsutil.ExcludeDotFile, fsutil.ExcludeSuffix(".jpg")))
}
}

Expand Down
2 changes: 1 addition & 1 deletion fsutil/opread.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func ReadStringOrErr(in any) (string, error) {
}

// ReadAll read contents from path or io.Reader, will panic on in type error
func ReadAll(in any) []byte { return GetContents(in) }
func ReadAll(in any) []byte { return MustRead(in) }

// GetContents read contents from path or io.Reader, will panic on in type error
func GetContents(in any) []byte { return MustRead(in) }
Expand Down

0 comments on commit 2d7deb4

Please sign in to comment.