Skip to content

Commit

Permalink
up: update some fs util pkg, add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jun 10, 2022
1 parent 363cfe4 commit 5a8002e
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 30 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@
*.out
*.cov
.DS_Store

testdata/
13 changes: 0 additions & 13 deletions fsutil/fsutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package fsutil_test

import (
"bytes"
"strings"
"testing"

"github.com/gookit/goutil/fsutil"
Expand Down Expand Up @@ -30,18 +29,6 @@ func TestMimeType(t *testing.T) {
assert.True(t, fsutil.IsImageFile("testdata/test.jpg"))
}

func TestDiscardReader(t *testing.T) {
sr := strings.NewReader("hello")
fsutil.DiscardReader(sr)

assert.Empty(t, fsutil.MustReadReader(sr))
assert.Empty(t, fsutil.GetContents(sr))
}

// func TestDir(t *testing.T) {
//
// }

func TestTempDir(t *testing.T) {
dir, err := fsutil.TempDir("testdata", "temp.*")
assert.NoError(t, err)
Expand Down
5 changes: 1 addition & 4 deletions fsutil/operate.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ func MustReadReader(r io.Reader) []byte {
if err != nil {
panic(err)
}

return bs
}

Expand Down Expand Up @@ -91,13 +90,12 @@ func OpenFile(filepath string, flag int, perm os.FileMode) (*os.File, error) {
if err != nil {
return nil, err
}

return file, nil
}

/* TODO MustOpenFile() */

// QuickOpenFile like os.OpenFile for write, if not exists, will create it.
// QuickOpenFile like os.OpenFile, open for write, if not exists, will create it.
func QuickOpenFile(filepath string) (*os.File, error) {
return OpenFile(filepath, DefaultFileFlags, DefaultFilePerm)
}
Expand Down Expand Up @@ -129,7 +127,6 @@ func MustCreateFile(filePath string, filePerm, dirPerm os.FileMode) *os.File {
if err != nil {
panic(err)
}

return file
}

Expand Down
73 changes: 62 additions & 11 deletions fsutil/operate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package fsutil_test

import (
"os"
"strings"
"testing"

"github.com/gookit/goutil/cliutil"
"github.com/gookit/goutil/envutil"
"github.com/gookit/goutil/fsutil"
"github.com/stretchr/testify/assert"
Expand All @@ -20,9 +22,15 @@ func TestMkdir(t *testing.T) {
if assert.NoError(t, err) {
assert.NoError(t, fsutil.Mkdir("./testdata/sub/sub21", os.ModePerm))
assert.NoError(t, fsutil.Mkdir("./testdata/sub/sub22", 0666))
assert.NoError(t, fsutil.Mkdir("./testdata/sub/sub23/sub31", 0777)) // 066X will error
// 066X will error
assert.NoError(t, fsutil.Mkdir("./testdata/sub/sub23/sub31", 0777))

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

assert.NoError(t, os.RemoveAll("./testdata/sub"))
} else {
cliutil.Redln("chmod dir ./testdata fail")
}
}

Expand Down Expand Up @@ -52,16 +60,38 @@ func TestCreateFile(t *testing.T) {
assert.NoError(t, file.Close())
assert.NoError(t, os.RemoveAll("./testdata/sub"))
}

fpath := "./testdata/sub/sub3/test-must-create.txt"
assert.NoError(t, fsutil.RmFileIfExist(fpath))
file = fsutil.MustCreateFile(fpath, 0, 0766)
assert.NoError(t, file.Close())
}

func TestQuickOpenFile(t *testing.T) {
fname := "./testdata/quick-open-file.txt"
file, err := fsutil.QuickOpenFile(fname)
if assert.NoError(t, err) {
assert.Equal(t, fname, file.Name())
assert.NoError(t, file.Close())
assert.NoError(t, os.Remove(file.Name()))
}
fpath := "./testdata/quick-open-file.txt"
assert.NoError(t, fsutil.RmFileIfExist(fpath))

file, err := fsutil.QuickOpenFile(fpath)
assert.NoError(t, err)
assert.Equal(t, fpath, file.Name())

_, err = file.WriteString("hello")
assert.NoError(t, err)

// close
assert.NoError(t, file.Close())

// open for read
file, err = fsutil.OpenReadFile(fpath)
// var bts [5]byte
bts := make([]byte, 5)
_, err = file.Read(bts)
assert.NoError(t, err)
assert.Equal(t, "hello", string(bts))

// close
assert.NoError(t, file.Close())
assert.NoError(t, fsutil.Remove(file.Name()))
}

func TestMustRemove(t *testing.T) {
Expand All @@ -84,6 +114,29 @@ func TestQuietRemove(t *testing.T) {
})
}

func TestDiscardReader(t *testing.T) {
sr := strings.NewReader("hello")
fsutil.DiscardReader(sr)

assert.Empty(t, fsutil.MustReadReader(sr))
assert.Empty(t, fsutil.GetContents(sr))
}

func TestGetContents(t *testing.T) {
fpath := "./testdata/get-contents.txt"
assert.NoError(t, fsutil.RmFileIfExist(fpath))

_, err := fsutil.PutContents(fpath, "hello")
assert.NoError(t, err)

assert.Nil(t, fsutil.ReadExistFile("/path-not-exist"))
assert.Equal(t, []byte("hello"), fsutil.ReadExistFile(fpath))

assert.Panics(t, func() {
fsutil.GetContents(45)
})
}

func TestMustCopyFile(t *testing.T) {
srcPath := "./testdata/cp-file-src.txt"
dstPath := "./testdata/cp-file-dst.txt"
Expand All @@ -92,9 +145,7 @@ func TestMustCopyFile(t *testing.T) {
assert.NoError(t, fsutil.RmFileIfExist(dstPath))

_, err := fsutil.PutContents(srcPath, "hello")
if err != nil {
assert.NoError(t, err)
}
assert.NoError(t, err)

fsutil.MustCopyFile(srcPath, dstPath)
assert.Equal(t, []byte("hello"), fsutil.GetContents(dstPath))
Expand Down
1 change: 0 additions & 1 deletion fsutil/testdata/cp-file-dst.txt

This file was deleted.

1 change: 0 additions & 1 deletion fsutil/testdata/cp-file-src.txt

This file was deleted.

0 comments on commit 5a8002e

Please sign in to comment.