2019-04-09 23:11:04 +08:00
|
|
|
package gfile
|
|
|
|
|
2019-04-10 22:16:16 +08:00
|
|
|
import (
|
|
|
|
"github.com/gogf/gf/g/test/gtest"
|
2019-04-09 23:11:04 +08:00
|
|
|
"io/ioutil"
|
2019-04-12 18:15:31 +08:00
|
|
|
"os"
|
2019-04-13 18:27:01 +08:00
|
|
|
"path/filepath"
|
2019-04-10 22:16:16 +08:00
|
|
|
"strings"
|
2019-04-09 23:11:04 +08:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2019-04-12 18:15:31 +08:00
|
|
|
//创建测试文件
|
2019-04-13 18:27:01 +08:00
|
|
|
func CreateTestFile(filename, content string) error {
|
|
|
|
TempDir := os.TempDir()
|
|
|
|
err := ioutil.WriteFile(TempDir+filename, []byte(content), 0666)
|
2019-04-12 18:15:31 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
//测试完删除文件或目录
|
2019-04-13 18:27:01 +08:00
|
|
|
func DelTestFiles(filenames string) {
|
2019-04-12 18:15:31 +08:00
|
|
|
os.RemoveAll(filenames)
|
|
|
|
}
|
|
|
|
|
|
|
|
//创建目录
|
2019-04-13 18:27:01 +08:00
|
|
|
func CreateDir(paths string) {
|
|
|
|
TempDir := os.TempDir()
|
|
|
|
os.Mkdir(TempDir+paths, 0666)
|
2019-04-12 18:15:31 +08:00
|
|
|
}
|
|
|
|
|
2019-04-13 18:27:01 +08:00
|
|
|
//统一格式化文件目录为"/"
|
|
|
|
func Formatpaths(paths []string) []string {
|
|
|
|
for k, v := range paths {
|
|
|
|
paths[k] = filepath.ToSlash(v)
|
|
|
|
paths[k] = strings.Replace(paths[k], "./", "/", 1)
|
|
|
|
}
|
2019-04-12 18:15:31 +08:00
|
|
|
|
2019-04-13 18:27:01 +08:00
|
|
|
return paths
|
|
|
|
}
|
|
|
|
|
|
|
|
//统一格式化文件目录为"/"
|
|
|
|
func Formatpath(paths string) string {
|
|
|
|
paths = filepath.ToSlash(paths)
|
|
|
|
paths = strings.Replace(paths, "./", "/", 1)
|
|
|
|
return paths
|
|
|
|
}
|
2019-04-12 18:15:31 +08:00
|
|
|
|
2019-04-09 23:11:04 +08:00
|
|
|
func TestGetContents(t *testing.T) {
|
2019-04-12 09:47:55 +08:00
|
|
|
gtest.Case(t, func() {
|
2019-04-12 18:15:31 +08:00
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
var (
|
2019-04-12 18:15:31 +08:00
|
|
|
filepaths string = "/testfile_t1.txt"
|
2019-04-09 23:11:04 +08:00
|
|
|
)
|
2019-04-13 18:27:01 +08:00
|
|
|
CreateTestFile(filepaths, "my name is jroam")
|
2019-04-09 23:11:04 +08:00
|
|
|
|
2019-04-13 13:14:30 +08:00
|
|
|
gtest.Assert(GetContents(os.TempDir()+filepaths), "my name is jroam")
|
2019-04-12 09:47:55 +08:00
|
|
|
gtest.Assert(GetContents(""), "")
|
2019-04-12 18:15:31 +08:00
|
|
|
defer DelTestFiles(filepaths)
|
2019-04-09 23:11:04 +08:00
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetBinContents(t *testing.T) {
|
2019-04-12 09:47:55 +08:00
|
|
|
gtest.Case(t, func() {
|
|
|
|
var (
|
2019-04-13 18:27:01 +08:00
|
|
|
filepaths1 string = "/testfile_t1.txt" //存在文件
|
|
|
|
filepaths2 string = os.TempDir() + "/testfile_t1_no.txt" //不存大文件
|
2019-04-09 23:11:04 +08:00
|
|
|
readcontent []byte
|
2019-04-13 18:27:01 +08:00
|
|
|
str1 string = "my name is jroam"
|
2019-04-09 23:11:04 +08:00
|
|
|
)
|
2019-04-13 18:27:01 +08:00
|
|
|
CreateTestFile(filepaths1, str1)
|
|
|
|
defer DelTestFiles(filepaths1)
|
|
|
|
readcontent = GetBinContents(os.TempDir() + filepaths1)
|
2019-04-12 18:15:31 +08:00
|
|
|
gtest.Assert(readcontent, []byte(str1))
|
2019-04-09 23:11:04 +08:00
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
readcontent = GetBinContents(filepaths2)
|
2019-04-13 18:27:01 +08:00
|
|
|
gtest.Assert(string(readcontent), "")
|
2019-04-10 17:54:28 +08:00
|
|
|
|
2019-04-09 23:25:20 +08:00
|
|
|
//if readcontent!=nil{
|
|
|
|
// t.Error("文件应不存在")
|
|
|
|
//}
|
2019-04-13 18:27:01 +08:00
|
|
|
gtest.Assert(string(GetBinContents(filepaths2)), "")
|
2019-04-12 18:15:31 +08:00
|
|
|
|
2019-04-09 23:11:04 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-10 13:39:36 +08:00
|
|
|
//截断文件为指定的大小
|
2019-04-09 23:11:04 +08:00
|
|
|
func TestTruncate(t *testing.T) {
|
2019-04-12 09:47:55 +08:00
|
|
|
gtest.Case(t, func() {
|
|
|
|
var (
|
2019-04-12 18:15:31 +08:00
|
|
|
filepaths1 string = "/testfile_GetContents.txt" //存在文件
|
2019-04-12 09:47:55 +08:00
|
|
|
err error
|
2019-04-10 13:39:36 +08:00
|
|
|
)
|
2019-04-13 18:27:01 +08:00
|
|
|
CreateTestFile(filepaths1, "abcdefghijkmln")
|
|
|
|
defer DelTestFiles(filepaths1)
|
2019-04-13 13:14:30 +08:00
|
|
|
err = Truncate(os.TempDir()+filepaths1, 200)
|
2019-04-12 09:47:55 +08:00
|
|
|
gtest.Assert(err, nil)
|
2019-04-09 23:11:04 +08:00
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
err = Truncate("", 200)
|
|
|
|
gtest.AssertNE(err, nil)
|
2019-04-10 17:54:28 +08:00
|
|
|
|
2019-04-09 23:11:04 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPutContents(t *testing.T) {
|
2019-04-12 09:47:55 +08:00
|
|
|
gtest.Case(t, func() {
|
|
|
|
var (
|
2019-04-12 18:15:31 +08:00
|
|
|
filepaths string = "/testfile_PutContents.txt"
|
2019-04-12 09:47:55 +08:00
|
|
|
err error
|
2019-04-09 23:11:04 +08:00
|
|
|
readcontent []byte
|
|
|
|
)
|
2019-04-13 18:27:01 +08:00
|
|
|
CreateTestFile(filepaths, "a")
|
|
|
|
defer DelTestFiles(filepaths)
|
2019-04-09 23:11:04 +08:00
|
|
|
|
2019-04-13 13:14:30 +08:00
|
|
|
err = PutContents(os.TempDir()+filepaths, "test!")
|
2019-04-12 09:47:55 +08:00
|
|
|
gtest.Assert(err, nil)
|
2019-04-09 23:11:04 +08:00
|
|
|
|
|
|
|
//==================判断是否真正写入
|
2019-04-13 18:27:01 +08:00
|
|
|
readcontent, err = ioutil.ReadFile(os.TempDir() + filepaths)
|
2019-04-12 09:47:55 +08:00
|
|
|
gtest.Assert(err, nil)
|
2019-04-13 13:14:30 +08:00
|
|
|
gtest.Assert(string(readcontent), "test!")
|
2019-04-10 17:54:28 +08:00
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
err = PutContents("", "test!")
|
|
|
|
gtest.AssertNE(err, nil)
|
2019-04-10 17:54:28 +08:00
|
|
|
|
2019-04-09 23:11:04 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPutContentsAppend(t *testing.T) {
|
2019-04-12 09:47:55 +08:00
|
|
|
gtest.Case(t, func() {
|
|
|
|
var (
|
2019-04-12 18:15:31 +08:00
|
|
|
filepaths string = "/testfile_PutContents.txt"
|
2019-04-12 09:47:55 +08:00
|
|
|
err error
|
2019-04-09 23:11:04 +08:00
|
|
|
readcontent []byte
|
|
|
|
)
|
|
|
|
|
2019-04-13 18:27:01 +08:00
|
|
|
CreateTestFile(filepaths, "a")
|
2019-04-12 18:15:31 +08:00
|
|
|
defer DelTestFiles(filepaths)
|
2019-04-13 13:14:30 +08:00
|
|
|
err = PutContentsAppend(os.TempDir()+filepaths, "hello")
|
2019-04-12 09:47:55 +08:00
|
|
|
gtest.Assert(err, nil)
|
2019-04-09 23:11:04 +08:00
|
|
|
|
|
|
|
//==================判断是否真正写入
|
2019-04-13 18:27:01 +08:00
|
|
|
readcontent, err = ioutil.ReadFile(os.TempDir() + filepaths)
|
2019-04-12 09:47:55 +08:00
|
|
|
gtest.Assert(err, nil)
|
2019-04-12 18:15:31 +08:00
|
|
|
gtest.Assert(string(readcontent), "ahello")
|
2019-04-10 17:54:28 +08:00
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
err = PutContentsAppend("", "hello")
|
|
|
|
gtest.AssertNE(err, nil)
|
2019-04-10 17:54:28 +08:00
|
|
|
|
2019-04-09 23:11:04 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
func TestPutBinContents(t *testing.T) {
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
var (
|
2019-04-12 18:15:31 +08:00
|
|
|
filepaths string = "/testfile_PutContents.txt"
|
2019-04-12 09:47:55 +08:00
|
|
|
err error
|
2019-04-09 23:11:04 +08:00
|
|
|
readcontent []byte
|
|
|
|
)
|
2019-04-13 18:27:01 +08:00
|
|
|
CreateTestFile(filepaths, "a")
|
2019-04-12 18:15:31 +08:00
|
|
|
defer DelTestFiles(filepaths)
|
2019-04-09 23:11:04 +08:00
|
|
|
|
2019-04-13 13:14:30 +08:00
|
|
|
err = PutBinContents(os.TempDir()+filepaths, []byte("test!!"))
|
2019-04-12 09:47:55 +08:00
|
|
|
gtest.Assert(err, nil)
|
2019-04-09 23:11:04 +08:00
|
|
|
|
|
|
|
//==================判断是否真正写入
|
2019-04-13 18:27:01 +08:00
|
|
|
readcontent, err = ioutil.ReadFile(os.TempDir() + filepaths)
|
2019-04-12 09:47:55 +08:00
|
|
|
gtest.Assert(err, nil)
|
2019-04-13 13:14:30 +08:00
|
|
|
gtest.Assert(string(readcontent), "test!!")
|
2019-04-10 17:54:28 +08:00
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
err = PutBinContents("", []byte("test!!"))
|
|
|
|
gtest.AssertNE(err, nil)
|
2019-04-10 17:54:28 +08:00
|
|
|
|
2019-04-09 23:11:04 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPutBinContentsAppend(t *testing.T) {
|
2019-04-12 09:47:55 +08:00
|
|
|
gtest.Case(t, func() {
|
|
|
|
var (
|
2019-04-12 18:15:31 +08:00
|
|
|
filepaths string = "/testfile_PutContents.txt" //原文件内容: yy
|
2019-04-12 09:47:55 +08:00
|
|
|
err error
|
2019-04-09 23:11:04 +08:00
|
|
|
readcontent []byte
|
|
|
|
)
|
2019-04-13 18:27:01 +08:00
|
|
|
CreateTestFile(filepaths, "test!!")
|
2019-04-12 18:15:31 +08:00
|
|
|
defer DelTestFiles(filepaths)
|
2019-04-13 13:14:30 +08:00
|
|
|
err = PutBinContentsAppend(os.TempDir()+filepaths, []byte("word"))
|
2019-04-12 09:47:55 +08:00
|
|
|
gtest.Assert(err, nil)
|
2019-04-09 23:11:04 +08:00
|
|
|
|
|
|
|
//==================判断是否真正写入
|
2019-04-13 18:27:01 +08:00
|
|
|
readcontent, err = ioutil.ReadFile(os.TempDir() + filepaths)
|
2019-04-12 09:47:55 +08:00
|
|
|
gtest.Assert(err, nil)
|
|
|
|
gtest.Assert(string(readcontent), "test!!word")
|
2019-04-10 17:54:28 +08:00
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
err = PutBinContentsAppend("", []byte("word"))
|
|
|
|
gtest.AssertNE(err, nil)
|
2019-04-10 17:54:28 +08:00
|
|
|
|
2019-04-09 23:11:04 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetBinContentsByTwoOffsetsByPath(t *testing.T) {
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
var (
|
2019-04-12 18:15:31 +08:00
|
|
|
filepaths string = "/testfile_GetContents.txt" //文件内容: abcdefghijk
|
2019-04-09 23:11:04 +08:00
|
|
|
readcontent []byte
|
|
|
|
)
|
|
|
|
|
2019-04-13 18:27:01 +08:00
|
|
|
CreateTestFile(filepaths, "abcdefghijk")
|
2019-04-12 18:15:31 +08:00
|
|
|
defer DelTestFiles(filepaths)
|
2019-04-13 13:14:30 +08:00
|
|
|
readcontent = GetBinContentsByTwoOffsetsByPath(os.TempDir()+filepaths, 2, 5)
|
2019-04-09 23:11:04 +08:00
|
|
|
|
|
|
|
gtest.Assert(string(readcontent), "cde")
|
2019-04-10 17:54:28 +08:00
|
|
|
|
|
|
|
readcontent = GetBinContentsByTwoOffsetsByPath("", 2, 5)
|
2019-04-12 09:47:55 +08:00
|
|
|
gtest.Assert(len(readcontent), 0)
|
2019-04-10 17:54:28 +08:00
|
|
|
|
2019-04-09 23:11:04 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-04-10 13:39:36 +08:00
|
|
|
func TestGetNextCharOffsetByPath(t *testing.T) {
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
var (
|
2019-04-12 18:15:31 +08:00
|
|
|
filepaths string = "/testfile_GetContents.txt" //文件内容: abcdefghijk
|
2019-04-10 13:39:36 +08:00
|
|
|
localindex int64
|
|
|
|
)
|
2019-04-13 18:27:01 +08:00
|
|
|
CreateTestFile(filepaths, "abcdefghijk")
|
2019-04-12 18:15:31 +08:00
|
|
|
defer DelTestFiles(filepaths)
|
2019-04-13 13:14:30 +08:00
|
|
|
localindex = GetNextCharOffsetByPath(os.TempDir()+filepaths, 'd', 1)
|
2019-04-10 13:39:36 +08:00
|
|
|
gtest.Assert(localindex, 3)
|
2019-04-10 17:54:28 +08:00
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
localindex = GetNextCharOffsetByPath("", 'd', 1)
|
2019-04-10 17:54:28 +08:00
|
|
|
gtest.Assert(localindex, -1)
|
|
|
|
|
2019-04-10 13:39:36 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-10 22:16:16 +08:00
|
|
|
func TestGetNextCharOffset(t *testing.T) {
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
var (
|
|
|
|
localindex int64
|
|
|
|
)
|
2019-04-12 09:47:55 +08:00
|
|
|
reader := strings.NewReader("helloword")
|
2019-04-10 22:16:16 +08:00
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
localindex = GetNextCharOffset(reader, 'w', 1)
|
|
|
|
gtest.Assert(localindex, 5)
|
2019-04-10 22:16:16 +08:00
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
localindex = GetNextCharOffset(reader, 'j', 1)
|
|
|
|
gtest.Assert(localindex, -1)
|
2019-04-10 22:16:16 +08:00
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetBinContentsByTwoOffsets(t *testing.T) {
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
var (
|
|
|
|
reads []byte
|
|
|
|
)
|
2019-04-12 09:47:55 +08:00
|
|
|
reader := strings.NewReader("helloword")
|
2019-04-10 22:16:16 +08:00
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
reads = GetBinContentsByTwoOffsets(reader, 1, 3)
|
|
|
|
gtest.Assert(string(reads), "el")
|
2019-04-10 22:16:16 +08:00
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
reads = GetBinContentsByTwoOffsets(reader, 10, 30)
|
|
|
|
gtest.Assert(string(reads), "")
|
2019-04-10 22:16:16 +08:00
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetBinContentsTilChar(t *testing.T) {
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
var (
|
2019-04-12 09:47:55 +08:00
|
|
|
reads []byte
|
2019-04-10 22:16:16 +08:00
|
|
|
indexs int64
|
|
|
|
)
|
2019-04-12 09:47:55 +08:00
|
|
|
reader := strings.NewReader("helloword")
|
2019-04-10 22:16:16 +08:00
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
reads, _ = GetBinContentsTilChar(reader, 'w', 2)
|
|
|
|
gtest.Assert(string(reads), "llow")
|
2019-04-10 22:16:16 +08:00
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
_, indexs = GetBinContentsTilChar(reader, 'w', 20)
|
|
|
|
gtest.Assert(indexs, -1)
|
2019-04-10 22:16:16 +08:00
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetBinContentsTilCharByPath(t *testing.T) {
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
var (
|
2019-04-12 09:47:55 +08:00
|
|
|
reads []byte
|
|
|
|
indexs int64
|
2019-04-12 18:15:31 +08:00
|
|
|
filepaths string = "/testfile_GetContents.txt"
|
2019-04-10 22:16:16 +08:00
|
|
|
)
|
|
|
|
|
2019-04-13 18:27:01 +08:00
|
|
|
CreateTestFile(filepaths, "abcdefghijklmn")
|
2019-04-12 18:15:31 +08:00
|
|
|
defer DelTestFiles(filepaths)
|
|
|
|
|
2019-04-13 13:14:30 +08:00
|
|
|
reads, _ = GetBinContentsTilCharByPath(os.TempDir()+filepaths, 'c', 2)
|
2019-04-12 09:47:55 +08:00
|
|
|
gtest.Assert(string(reads), "c")
|
2019-04-10 22:16:16 +08:00
|
|
|
|
2019-04-13 13:14:30 +08:00
|
|
|
reads, _ = GetBinContentsTilCharByPath(os.TempDir()+filepaths, 'y', 1)
|
2019-04-12 09:47:55 +08:00
|
|
|
gtest.Assert(string(reads), "")
|
2019-04-10 22:16:16 +08:00
|
|
|
|
2019-04-13 13:14:30 +08:00
|
|
|
_, indexs = GetBinContentsTilCharByPath(os.TempDir()+filepaths, 'x', 1)
|
2019-04-12 09:47:55 +08:00
|
|
|
gtest.Assert(indexs, -1)
|
2019-04-10 22:16:16 +08:00
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHome(t *testing.T) {
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
var (
|
|
|
|
reads string
|
2019-04-12 09:47:55 +08:00
|
|
|
err error
|
2019-04-10 22:16:16 +08:00
|
|
|
)
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
reads, err = Home()
|
|
|
|
gtest.Assert(err, nil)
|
|
|
|
gtest.AssertNE(reads, "")
|
2019-04-10 22:16:16 +08:00
|
|
|
|
|
|
|
})
|
|
|
|
}
|