gf/os/gfile/gfile_z_unit_test.go

683 lines
13 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
2019-04-16 22:11:44 +08:00
package gfile_test
2019-04-07 22:46:14 +08:00
import (
"os"
2019-04-08 23:13:28 +08:00
"path/filepath"
2019-04-10 16:22:29 +08:00
"strings"
2019-04-07 22:46:14 +08:00
"testing"
2019-07-08 11:37:02 +08:00
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/os/gfile"
2021-11-15 20:26:31 +08:00
"github.com/gogf/gf/v2/os/gtime"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/test/gtest"
2021-11-15 20:26:31 +08:00
"github.com/gogf/gf/v2/util/gconv"
2019-04-07 22:46:14 +08:00
)
func Test_IsDir(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-13 18:27:01 +08:00
paths := "/testfile"
2019-04-16 22:11:44 +08:00
createDir(paths)
defer delTestFiles(paths)
2019-04-12 18:15:31 +08:00
2020-03-19 22:56:12 +08:00
t.Assert(gfile.IsDir(testpath()+paths), true)
t.Assert(gfile.IsDir("./testfile2"), false)
t.Assert(gfile.IsDir("./testfile/tt.txt"), false)
t.Assert(gfile.IsDir(""), false)
2019-04-07 22:46:14 +08:00
})
}
2019-09-05 19:22:11 +08:00
func Test_IsEmpty(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
path := "/testdir_" + gconv.String(gtime.TimestampNano())
2019-09-05 19:22:11 +08:00
createDir(path)
defer delTestFiles(path)
2020-03-19 22:56:12 +08:00
t.Assert(gfile.IsEmpty(testpath()+path), true)
t.Assert(gfile.IsEmpty(testpath()+path+gfile.Separator+"test.txt"), true)
2019-09-05 19:22:11 +08:00
})
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
path := "/testfile_" + gconv.String(gtime.TimestampNano())
2019-09-05 19:22:11 +08:00
createTestFile(path, "")
defer delTestFiles(path)
2020-03-19 22:56:12 +08:00
t.Assert(gfile.IsEmpty(testpath()+path), true)
2019-09-05 19:22:11 +08:00
})
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
path := "/testfile_" + gconv.String(gtime.TimestampNano())
2019-09-05 19:22:11 +08:00
createTestFile(path, "1")
defer delTestFiles(path)
2020-03-19 22:56:12 +08:00
t.Assert(gfile.IsEmpty(testpath()+path), false)
2019-09-05 19:22:11 +08:00
})
}
func Test_Create(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-07 22:46:14 +08:00
var (
2019-04-13 18:27:01 +08:00
err error
2019-04-07 22:46:14 +08:00
filepaths []string
2019-04-13 18:27:01 +08:00
fileobj *os.File
2019-04-07 22:46:14 +08:00
)
2019-04-13 23:00:57 +08:00
filepaths = append(filepaths, "/testfile_cc1.txt")
filepaths = append(filepaths, "/testfile_cc2.txt")
2019-04-12 09:47:55 +08:00
for _, v := range filepaths {
2019-04-16 22:11:44 +08:00
fileobj, err = gfile.Create(testpath() + v)
defer delTestFiles(v)
2019-04-12 18:15:31 +08:00
fileobj.Close()
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2019-04-12 09:47:55 +08:00
}
2019-04-07 22:46:14 +08:00
})
}
func Test_Open(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-12 09:47:55 +08:00
var (
2019-04-13 18:27:01 +08:00
err error
files []string
flags []bool
2019-04-12 18:15:31 +08:00
fileobj *os.File
2019-04-07 22:46:14 +08:00
)
2019-04-13 18:27:01 +08:00
file1 := "/testfile_nc1.txt"
2019-04-16 22:11:44 +08:00
createTestFile(file1, "")
defer delTestFiles(file1)
2019-04-07 22:46:14 +08:00
2019-04-12 18:15:31 +08:00
files = append(files, file1)
2019-04-12 09:47:55 +08:00
flags = append(flags, true)
2019-04-07 22:46:14 +08:00
2019-04-12 18:15:31 +08:00
files = append(files, "./testfile/file1/c1.txt")
flags = append(flags, false)
2019-04-07 22:46:14 +08:00
2019-04-12 18:15:31 +08:00
for k, v := range files {
2019-04-16 22:11:44 +08:00
fileobj, err = gfile.Open(testpath() + v)
2019-04-12 18:15:31 +08:00
fileobj.Close()
2019-04-12 09:47:55 +08:00
if flags[k] {
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2019-04-12 09:47:55 +08:00
} else {
2020-03-19 22:56:12 +08:00
t.AssertNE(err, nil)
2019-04-07 22:46:14 +08:00
}
}
})
}
func Test_OpenFile(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-12 09:47:55 +08:00
var (
2019-04-13 18:27:01 +08:00
err error
files []string
flags []bool
2019-04-12 18:15:31 +08:00
fileobj *os.File
2019-04-07 22:46:14 +08:00
)
2019-04-12 09:47:55 +08:00
files = append(files, "./testfile/file1/nc1.txt")
flags = append(flags, false)
2019-04-07 22:46:14 +08:00
2019-04-13 18:27:01 +08:00
f1 := "/testfile_tt.txt"
2019-04-16 22:11:44 +08:00
createTestFile(f1, "")
defer delTestFiles(f1)
2019-04-12 18:15:31 +08:00
files = append(files, f1)
2019-04-12 09:47:55 +08:00
flags = append(flags, true)
2019-04-07 22:46:14 +08:00
2019-04-12 09:47:55 +08:00
for k, v := range files {
2019-04-16 22:11:44 +08:00
fileobj, err = gfile.OpenFile(testpath()+v, os.O_RDWR, 0666)
2019-04-12 18:15:31 +08:00
fileobj.Close()
2019-04-12 09:47:55 +08:00
if flags[k] {
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2019-04-12 09:47:55 +08:00
} else {
2020-03-19 22:56:12 +08:00
t.AssertNE(err, nil)
2019-04-07 22:46:14 +08:00
}
}
})
}
func Test_OpenWithFlag(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-12 09:47:55 +08:00
var (
2019-04-13 18:27:01 +08:00
err error
files []string
flags []bool
2019-04-12 18:15:31 +08:00
fileobj *os.File
2019-04-07 22:46:14 +08:00
)
2019-04-14 22:41:11 +08:00
2019-04-13 18:27:01 +08:00
file1 := "/testfile_t1.txt"
2019-04-16 22:11:44 +08:00
createTestFile(file1, "")
defer delTestFiles(file1)
2019-04-12 18:15:31 +08:00
files = append(files, file1)
2019-04-12 09:47:55 +08:00
flags = append(flags, true)
2019-04-10 17:54:28 +08:00
2019-04-14 22:41:11 +08:00
files = append(files, "/testfiless/dirfiles/t1_no.txt")
2019-04-12 09:47:55 +08:00
flags = append(flags, false)
2019-04-07 22:46:14 +08:00
2019-04-12 09:47:55 +08:00
for k, v := range files {
2019-04-16 22:11:44 +08:00
fileobj, err = gfile.OpenWithFlag(testpath()+v, os.O_RDWR)
2019-04-12 18:15:31 +08:00
fileobj.Close()
2019-04-12 09:47:55 +08:00
if flags[k] {
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2019-04-12 09:47:55 +08:00
} else {
2020-03-19 22:56:12 +08:00
t.AssertNE(err, nil)
2019-04-07 22:46:14 +08:00
}
}
})
}
func Test_OpenWithFlagPerm(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-12 09:47:55 +08:00
var (
2019-04-13 18:27:01 +08:00
err error
files []string
flags []bool
2019-04-12 18:15:31 +08:00
fileobj *os.File
2019-04-07 22:46:14 +08:00
)
2019-04-13 18:27:01 +08:00
file1 := "/testfile_nc1.txt"
2019-04-16 22:11:44 +08:00
createTestFile(file1, "")
defer delTestFiles(file1)
2019-04-12 18:15:31 +08:00
files = append(files, file1)
flags = append(flags, true)
2019-04-07 22:46:14 +08:00
2019-04-14 22:41:11 +08:00
files = append(files, "/testfileyy/tt.txt")
2019-04-12 18:15:31 +08:00
flags = append(flags, false)
2019-04-07 22:46:14 +08:00
2019-04-12 09:47:55 +08:00
for k, v := range files {
2021-11-05 14:30:50 +08:00
fileobj, err = gfile.OpenWithFlagPerm(testpath()+v, os.O_RDWR, 0666)
2019-04-12 18:15:31 +08:00
fileobj.Close()
2019-04-12 09:47:55 +08:00
if flags[k] {
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2019-04-12 09:47:55 +08:00
} else {
2020-03-19 22:56:12 +08:00
t.AssertNE(err, nil)
2019-04-07 22:46:14 +08:00
}
}
})
}
func Test_Exists(t *testing.T) {
2019-04-07 22:46:14 +08:00
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-12 09:47:55 +08:00
var (
flag bool
2019-04-07 22:46:14 +08:00
files []string
flags []bool
)
2019-04-13 18:27:01 +08:00
file1 := "/testfile_GetContents.txt"
2019-04-16 22:11:44 +08:00
createTestFile(file1, "")
defer delTestFiles(file1)
2019-04-12 18:15:31 +08:00
files = append(files, file1)
2019-04-12 09:47:55 +08:00
flags = append(flags, true)
2019-04-07 22:46:14 +08:00
2019-04-12 09:47:55 +08:00
files = append(files, "./testfile/havefile1/tt_no.txt")
flags = append(flags, false)
2019-04-10 17:54:28 +08:00
2019-04-12 09:47:55 +08:00
for k, v := range files {
2019-04-16 22:11:44 +08:00
flag = gfile.Exists(testpath() + v)
2019-04-12 09:47:55 +08:00
if flags[k] {
2020-03-19 22:56:12 +08:00
t.Assert(flag, true)
2019-04-12 09:47:55 +08:00
} else {
2020-03-19 22:56:12 +08:00
t.Assert(flag, false)
2019-04-07 22:46:14 +08:00
}
}
})
}
func Test_Pwd(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-12 09:47:55 +08:00
paths, err := os.Getwd()
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-19 22:56:12 +08:00
t.Assert(gfile.Pwd(), paths)
2019-04-07 22:46:14 +08:00
})
}
func Test_IsFile(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-12 09:47:55 +08:00
var (
flag bool
2019-04-07 22:46:14 +08:00
files []string
flags []bool
)
2019-04-13 18:27:01 +08:00
file1 := "/testfile_tt.txt"
2019-04-16 22:11:44 +08:00
createTestFile(file1, "")
defer delTestFiles(file1)
2019-04-12 18:15:31 +08:00
files = append(files, file1)
2019-04-12 09:47:55 +08:00
flags = append(flags, true)
2019-04-07 22:46:14 +08:00
2019-04-13 18:27:01 +08:00
dir1 := "/testfiless"
2019-04-16 22:11:44 +08:00
createDir(dir1)
defer delTestFiles(dir1)
2019-04-12 18:15:31 +08:00
files = append(files, dir1)
flags = append(flags, false)
files = append(files, "./testfiledd/tt1.txt")
2019-04-12 09:47:55 +08:00
flags = append(flags, false)
2019-04-07 22:46:14 +08:00
2019-04-12 09:47:55 +08:00
for k, v := range files {
2019-04-16 22:11:44 +08:00
flag = gfile.IsFile(testpath() + v)
2019-04-12 09:47:55 +08:00
if flags[k] {
2020-03-19 22:56:12 +08:00
t.Assert(flag, true)
2019-04-12 09:47:55 +08:00
} else {
2020-03-19 22:56:12 +08:00
t.Assert(flag, false)
2019-04-07 22:46:14 +08:00
}
}
})
}
func Test_Info(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-12 09:47:55 +08:00
var (
err error
2019-04-12 18:15:31 +08:00
paths string = "/testfile_t1.txt"
2019-04-12 09:47:55 +08:00
files os.FileInfo
2019-04-07 22:46:14 +08:00
files2 os.FileInfo
)
2019-04-16 22:11:44 +08:00
createTestFile(paths, "")
defer delTestFiles(paths)
2021-11-02 09:56:53 +08:00
files, err = gfile.Stat(testpath() + paths)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2019-04-07 22:46:14 +08:00
2019-04-16 22:11:44 +08:00
files2, err = os.Stat(testpath() + paths)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2019-04-07 22:46:14 +08:00
2020-03-19 22:56:12 +08:00
t.Assert(files, files2)
2019-04-07 22:46:14 +08:00
})
}
func Test_Move(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-15 23:14:48 +08:00
var (
paths string = "/ovetest"
filepaths string = "/testfile_ttn1.txt"
topath string = "/testfile_ttn2.txt"
)
2019-04-16 22:11:44 +08:00
createDir("/ovetest")
createTestFile(paths+filepaths, "a")
2019-04-15 23:14:48 +08:00
2019-04-16 22:11:44 +08:00
defer delTestFiles(paths)
2019-04-15 23:14:48 +08:00
2019-04-16 22:11:44 +08:00
yfile := testpath() + paths + filepaths
tofile := testpath() + paths + topath
2019-04-15 23:14:48 +08:00
2020-03-19 22:56:12 +08:00
t.Assert(gfile.Move(yfile, tofile), nil)
2019-04-15 23:14:48 +08:00
2019-04-16 22:11:44 +08:00
// 检查移动后的文件是否真实存在
2019-04-15 23:14:48 +08:00
_, err := os.Stat(tofile)
2020-03-19 22:56:12 +08:00
t.Assert(os.IsNotExist(err), false)
2019-04-15 23:14:48 +08:00
})
}
func Test_Rename(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-15 23:14:48 +08:00
var (
paths string = "/testfiles"
ypath string = "/testfilettm1.txt"
topath string = "/testfilettm2.txt"
)
2019-04-16 22:11:44 +08:00
createDir(paths)
createTestFile(paths+ypath, "a")
defer delTestFiles(paths)
2019-04-15 23:14:48 +08:00
2019-04-16 22:11:44 +08:00
ypath = testpath() + paths + ypath
topath = testpath() + paths + topath
2019-04-15 23:14:48 +08:00
2020-03-19 22:56:12 +08:00
t.Assert(gfile.Rename(ypath, topath), nil)
t.Assert(gfile.IsFile(topath), true)
2019-04-15 23:14:48 +08:00
2020-03-19 22:56:12 +08:00
t.AssertNE(gfile.Rename("", ""), nil)
2019-04-15 23:14:48 +08:00
})
}
2019-04-07 22:46:14 +08:00
func Test_DirNames(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-12 09:47:55 +08:00
var (
2019-04-12 18:15:31 +08:00
paths string = "/testdirs"
2019-04-12 09:47:55 +08:00
err error
2019-04-08 23:13:28 +08:00
readlist []string
)
2019-04-12 09:47:55 +08:00
havelist := []string{
2019-04-08 23:13:28 +08:00
"t1.txt",
"t2.txt",
}
2019-04-12 18:15:31 +08:00
2019-04-16 22:11:44 +08:00
// 创建测试文件
createDir(paths)
2019-04-13 18:27:01 +08:00
for _, v := range havelist {
2019-04-16 22:11:44 +08:00
createTestFile(paths+"/"+v, "")
2019-04-12 18:15:31 +08:00
}
2019-04-16 22:11:44 +08:00
defer delTestFiles(paths)
2019-04-12 18:15:31 +08:00
2019-04-16 22:11:44 +08:00
readlist, err = gfile.DirNames(testpath() + paths)
2019-04-10 17:54:28 +08:00
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-19 22:56:12 +08:00
t.AssertIN(readlist, havelist)
2019-04-08 23:13:28 +08:00
2019-04-16 22:11:44 +08:00
_, err = gfile.DirNames("")
2020-03-19 22:56:12 +08:00
t.AssertNE(err, nil)
2019-04-08 23:13:28 +08:00
})
}
func Test_Glob(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-12 09:47:55 +08:00
var (
2019-04-12 18:15:31 +08:00
paths string = "/testfiles/*.txt"
2019-04-13 18:27:01 +08:00
dirpath string = "/testfiles"
2019-04-12 09:47:55 +08:00
err error
2019-04-08 23:13:28 +08:00
resultlist []string
)
2019-04-12 09:47:55 +08:00
havelist1 := []string{
2019-04-08 23:13:28 +08:00
"t1.txt",
"t2.txt",
}
2019-04-12 09:47:55 +08:00
havelist2 := []string{
2019-04-16 22:11:44 +08:00
testpath() + "/testfiles/t1.txt",
testpath() + "/testfiles/t2.txt",
2019-04-12 18:15:31 +08:00
}
2021-11-15 20:26:31 +08:00
// ===============================构建测试文件
2019-04-16 22:11:44 +08:00
createDir(dirpath)
2019-04-13 18:27:01 +08:00
for _, v := range havelist1 {
2019-04-16 22:11:44 +08:00
createTestFile(dirpath+"/"+v, "")
2019-04-08 23:13:28 +08:00
}
2019-04-16 22:11:44 +08:00
defer delTestFiles(dirpath)
2019-04-12 18:15:31 +08:00
2019-04-16 22:11:44 +08:00
resultlist, err = gfile.Glob(testpath()+paths, true)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-19 22:56:12 +08:00
t.Assert(resultlist, havelist1)
2019-04-08 23:13:28 +08:00
2019-04-16 22:11:44 +08:00
resultlist, err = gfile.Glob(testpath()+paths, false)
2019-04-08 23:13:28 +08:00
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-19 22:56:12 +08:00
t.Assert(formatpaths(resultlist), formatpaths(havelist2))
2019-04-08 23:13:28 +08:00
2019-04-16 22:11:44 +08:00
_, err = gfile.Glob("", true)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2019-04-10 17:54:28 +08:00
2019-04-08 23:13:28 +08:00
})
}
func Test_Remove(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-12 09:47:55 +08:00
var (
2019-04-12 18:15:31 +08:00
paths string = "/testfile_t1.txt"
2019-04-08 23:13:28 +08:00
)
2019-04-16 22:11:44 +08:00
createTestFile(paths, "")
2020-03-19 22:56:12 +08:00
t.Assert(gfile.Remove(testpath()+paths), nil)
2019-04-12 18:15:31 +08:00
2020-03-19 22:56:12 +08:00
t.Assert(gfile.Remove(""), nil)
2019-04-08 23:13:28 +08:00
2019-04-16 22:11:44 +08:00
defer delTestFiles(paths)
2019-04-08 23:13:28 +08:00
})
}
2019-04-07 22:46:14 +08:00
func Test_IsReadable(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-12 09:47:55 +08:00
var (
2019-04-12 18:15:31 +08:00
paths1 string = "/testfile_GetContents.txt"
paths2 string = "./testfile_GetContents_no.txt"
2019-04-10 13:45:42 +08:00
)
2019-04-12 18:15:31 +08:00
2019-04-16 22:11:44 +08:00
createTestFile(paths1, "")
defer delTestFiles(paths1)
2019-04-12 18:15:31 +08:00
2020-03-19 22:56:12 +08:00
t.Assert(gfile.IsReadable(testpath()+paths1), true)
t.Assert(gfile.IsReadable(paths2), false)
2019-04-10 13:45:42 +08:00
})
}
func Test_IsWritable(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-12 09:47:55 +08:00
var (
2019-04-12 18:15:31 +08:00
paths1 string = "/testfile_GetContents.txt"
paths2 string = "./testfile_GetContents_no.txt"
2019-04-10 16:22:29 +08:00
)
2019-04-12 18:15:31 +08:00
2019-04-16 22:11:44 +08:00
createTestFile(paths1, "")
defer delTestFiles(paths1)
2020-03-19 22:56:12 +08:00
t.Assert(gfile.IsWritable(testpath()+paths1), true)
t.Assert(gfile.IsWritable(paths2), false)
2019-04-10 16:22:29 +08:00
})
}
func Test_Chmod(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-12 09:47:55 +08:00
var (
2019-04-12 18:15:31 +08:00
paths1 string = "/testfile_GetContents.txt"
paths2 string = "./testfile_GetContents_no.txt"
2019-04-10 16:22:29 +08:00
)
2019-04-16 22:11:44 +08:00
createTestFile(paths1, "")
defer delTestFiles(paths1)
2019-04-10 16:22:29 +08:00
2020-03-19 22:56:12 +08:00
t.Assert(gfile.Chmod(testpath()+paths1, 0777), nil)
t.AssertNE(gfile.Chmod(paths2, 0777), nil)
2019-04-10 16:22:29 +08:00
})
}
2019-04-16 22:11:44 +08:00
// 获取绝对目录地址
func Test_RealPath(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-12 09:47:55 +08:00
var (
2019-04-12 18:15:31 +08:00
paths1 string = "/testfile_files"
2019-04-10 16:22:29 +08:00
readlPath string
tempstr string
)
2019-04-12 18:15:31 +08:00
2019-04-16 22:11:44 +08:00
createDir(paths1)
defer delTestFiles(paths1)
2019-04-12 18:15:31 +08:00
2019-04-16 22:11:44 +08:00
readlPath = gfile.RealPath("./")
2019-04-10 16:22:29 +08:00
2019-04-12 09:47:55 +08:00
tempstr, _ = filepath.Abs("./")
2019-04-10 16:22:29 +08:00
2020-03-19 22:56:12 +08:00
t.Assert(readlPath, tempstr)
2019-04-10 17:54:28 +08:00
2020-03-19 22:56:12 +08:00
t.Assert(gfile.RealPath("./nodirs"), "")
2019-04-10 16:22:29 +08:00
})
}
2019-04-16 22:11:44 +08:00
// 获取当前执行文件的目录
func Test_SelfPath(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-12 09:47:55 +08:00
var (
paths1 string
2019-04-10 16:22:29 +08:00
readlPath string
2019-04-12 09:47:55 +08:00
tempstr string
2019-04-10 16:22:29 +08:00
)
2019-04-16 22:11:44 +08:00
readlPath = gfile.SelfPath()
2019-04-12 09:47:55 +08:00
readlPath = filepath.ToSlash(readlPath)
2019-04-10 16:22:29 +08:00
2019-04-12 09:47:55 +08:00
tempstr, _ = filepath.Abs(os.Args[0])
paths1 = filepath.ToSlash(tempstr)
paths1 = strings.Replace(paths1, "./", "/", 1)
2019-04-10 16:22:29 +08:00
2020-03-19 22:56:12 +08:00
t.Assert(readlPath, paths1)
2019-04-10 16:22:29 +08:00
})
}
func Test_SelfDir(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-12 09:47:55 +08:00
var (
paths1 string
2019-04-10 16:22:29 +08:00
readlPath string
2019-04-12 09:47:55 +08:00
tempstr string
2019-04-10 16:22:29 +08:00
)
2019-04-16 22:11:44 +08:00
readlPath = gfile.SelfDir()
2019-04-10 16:22:29 +08:00
2019-04-12 09:47:55 +08:00
tempstr, _ = filepath.Abs(os.Args[0])
paths1 = filepath.Dir(tempstr)
2019-04-10 16:22:29 +08:00
2020-03-19 22:56:12 +08:00
t.Assert(readlPath, paths1)
2019-04-10 16:22:29 +08:00
})
}
func Test_Basename(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-12 09:47:55 +08:00
var (
2019-04-12 18:15:31 +08:00
paths1 string = "/testfilerr_GetContents.txt"
2019-04-10 16:22:29 +08:00
readlPath string
)
2019-04-12 18:15:31 +08:00
2019-04-16 22:11:44 +08:00
createTestFile(paths1, "")
defer delTestFiles(paths1)
2019-04-12 18:15:31 +08:00
2019-04-16 22:11:44 +08:00
readlPath = gfile.Basename(testpath() + paths1)
2020-03-19 22:56:12 +08:00
t.Assert(readlPath, "testfilerr_GetContents.txt")
2019-04-10 16:22:29 +08:00
})
}
func Test_Dir(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-12 09:47:55 +08:00
var (
2019-04-12 18:15:31 +08:00
paths1 string = "/testfiless"
2019-04-10 16:22:29 +08:00
readlPath string
)
2019-04-16 22:11:44 +08:00
createDir(paths1)
defer delTestFiles(paths1)
2019-04-12 18:15:31 +08:00
2019-04-16 22:11:44 +08:00
readlPath = gfile.Dir(testpath() + paths1)
2019-04-10 16:22:29 +08:00
2020-03-19 22:56:12 +08:00
t.Assert(readlPath, testpath())
2019-04-10 16:22:29 +08:00
2022-02-28 22:57:53 +08:00
t.Assert(len(gfile.Dir(".")) > 0, true)
2019-04-10 16:22:29 +08:00
})
}
func Test_Ext(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-12 09:47:55 +08:00
var (
2019-04-13 18:27:01 +08:00
paths1 string = "/testfile_GetContents.txt"
dirpath1 = "/testdirs"
2019-04-10 16:22:29 +08:00
)
2019-04-16 22:11:44 +08:00
createTestFile(paths1, "")
defer delTestFiles(paths1)
2019-04-12 18:15:31 +08:00
2019-04-16 22:11:44 +08:00
createDir(dirpath1)
defer delTestFiles(dirpath1)
2019-04-10 16:22:29 +08:00
2020-03-19 22:56:12 +08:00
t.Assert(gfile.Ext(testpath()+paths1), ".txt")
t.Assert(gfile.Ext(testpath()+dirpath1), "")
2019-09-23 16:21:19 +08:00
})
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
t.Assert(gfile.Ext("/var/www/test.js"), ".js")
t.Assert(gfile.Ext("/var/www/test.min.js"), ".js")
t.Assert(gfile.Ext("/var/www/test.js?1"), ".js")
t.Assert(gfile.Ext("/var/www/test.min.js?v1"), ".js")
2019-09-23 16:21:19 +08:00
})
}
2019-04-10 16:22:29 +08:00
2019-09-23 16:21:19 +08:00
func Test_ExtName(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
t.Assert(gfile.ExtName("/var/www/test.js"), "js")
t.Assert(gfile.ExtName("/var/www/test.min.js"), "js")
t.Assert(gfile.ExtName("/var/www/test.js?v=1"), "js")
t.Assert(gfile.ExtName("/var/www/test.min.js?v=1"), "js")
2019-04-10 16:22:29 +08:00
})
}
func Test_TempDir(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2020-05-16 14:06:35 +08:00
if gfile.Separator != "/" || !gfile.Exists("/tmp") {
t.Assert(gfile.Temp(), os.TempDir())
2020-05-16 14:06:35 +08:00
} else {
t.Assert(gfile.Temp(), "/tmp")
2020-05-16 14:06:35 +08:00
}
2019-04-10 16:22:29 +08:00
})
}
func Test_Mkdir(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-12 09:47:55 +08:00
var (
2019-04-12 18:15:31 +08:00
tpath string = "/testfile/createdir"
2019-04-12 09:47:55 +08:00
err error
2019-04-10 22:16:16 +08:00
)
2019-04-16 22:11:44 +08:00
defer delTestFiles("/testfile")
2019-04-12 18:15:31 +08:00
2019-04-16 22:11:44 +08:00
err = gfile.Mkdir(testpath() + tpath)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2019-04-10 22:16:16 +08:00
2019-04-16 22:11:44 +08:00
err = gfile.Mkdir("")
2020-03-19 22:56:12 +08:00
t.AssertNE(err, nil)
2019-04-10 22:16:16 +08:00
2019-04-16 22:11:44 +08:00
err = gfile.Mkdir(testpath() + tpath + "2/t1")
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2019-04-10 22:16:16 +08:00
})
}
func Test_Stat(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-12 09:47:55 +08:00
var (
tpath1 = "/testfile_t1.txt"
tpath2 = "./testfile_t1_no.txt"
2019-04-15 22:52:19 +08:00
err error
fileiofo os.FileInfo
2019-04-10 22:16:16 +08:00
)
2019-04-16 22:11:44 +08:00
createTestFile(tpath1, "a")
defer delTestFiles(tpath1)
2019-04-12 18:15:31 +08:00
2019-04-16 22:11:44 +08:00
fileiofo, err = gfile.Stat(testpath() + tpath1)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2019-04-10 22:16:16 +08:00
2020-03-19 22:56:12 +08:00
t.Assert(fileiofo.Size(), 1)
2019-04-15 22:52:19 +08:00
2019-04-16 22:11:44 +08:00
_, err = gfile.Stat(tpath2)
2020-03-19 22:56:12 +08:00
t.AssertNE(err, nil)
2019-04-10 22:16:16 +08:00
})
}
func Test_MainPkgPath(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-05-17 21:37:48 +08:00
reads := gfile.MainPkgPath()
2020-03-19 22:56:12 +08:00
t.Assert(reads, "")
2019-04-10 22:16:16 +08:00
})
}
2022-02-28 22:57:53 +08:00
func Test_SelfName(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.Assert(len(gfile.SelfName()) > 0, true)
})
}
func Test_MTimestamp(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.Assert(gfile.MTimestamp(gfile.Temp()) > 0, true)
})
}