package nfs_test import ( "git.noahlan.cn/noahlan/ntool/nenv" "git.noahlan.cn/noahlan/ntool/nfs" "git.noahlan.cn/noahlan/ntool/ntest/assert" "os" "testing" ) func TestMkdir(t *testing.T) { // TODO windows will error if nenv.IsWin() { t.Skip("skip mkdir test on Windows") return } err := os.Chmod("./testdata", os.ModePerm) if assert.NoErr(t, err) { assert.NoErr(t, nfs.Mkdir("./testdata/sub/sub21", os.ModePerm)) assert.NoErr(t, nfs.Mkdir("./testdata/sub/sub22", 0666)) // 066X will error assert.NoErr(t, nfs.Mkdir("./testdata/sub/sub23/sub31", 0777)) assert.NoErr(t, nfs.MkParentDir("./testdata/sub/sub24/sub32")) assert.True(t, nfs.IsDir("./testdata/sub/sub24")) assert.NoErr(t, os.RemoveAll("./testdata/sub")) } } func TestCreateFile(t *testing.T) { // TODO windows will error // if envutil.IsWin() { // return // } file, err := nfs.CreateFile("./testdata/test.txt", 0664, 0666) if assert.NoErr(t, err) { assert.Eq(t, "./testdata/test.txt", file.Name()) assert.NoErr(t, file.Close()) assert.NoErr(t, os.Remove(file.Name())) } file, err = nfs.CreateFile("./testdata/sub/test.txt", 0664, 0777) if assert.NoErr(t, err) { assert.Eq(t, "./testdata/sub/test.txt", file.Name()) assert.NoErr(t, file.Close()) assert.NoErr(t, os.RemoveAll("./testdata/sub")) } file, err = nfs.CreateFile("./testdata/sub/sub2/test.txt", 0664, 0777) if assert.NoErr(t, err) { assert.Eq(t, "./testdata/sub/sub2/test.txt", file.Name()) assert.NoErr(t, file.Close()) assert.NoErr(t, os.RemoveAll("./testdata/sub")) } fpath := "./testdata/sub/sub3/test-must-create.txt" assert.NoErr(t, nfs.RmFileIfExist(fpath)) file = nfs.MustCreateFile(fpath, 0, 0766) assert.NoErr(t, file.Close()) err = nfs.RemoveSub("./testdata/sub") assert.NoErr(t, err) } func TestQuickOpenFile(t *testing.T) { fpath := "./testdata/quick-open-file.txt" assert.NoErr(t, nfs.RmFileIfExist(fpath)) file, err := nfs.QuickOpenFile(fpath) assert.NoErr(t, err) assert.Eq(t, fpath, file.Name()) _, err = file.WriteString("hello") assert.NoErr(t, err) // close assert.NoErr(t, file.Close()) // open for read file, err = nfs.OpenReadFile(fpath) assert.NoErr(t, err) // var bts [5]byte bts := make([]byte, 5) _, err = file.Read(bts) assert.NoErr(t, err) assert.Eq(t, "hello", string(bts)) // close assert.NoErr(t, file.Close()) assert.NoErr(t, nfs.Remove(file.Name())) } func TestMustRemove(t *testing.T) { assert.Panics(t, func() { nfs.MustRemove("/path-not-exist") }) } func TestQuietRemove(t *testing.T) { assert.NotPanics(t, func() { nfs.QuietRemove("/path-not-exist") }) } func TestUnzip(t *testing.T) { assert.Err(t, nfs.Unzip("/path-not-exists", "")) }