gf/os/gfile/gfile_z_contents_test.go

336 lines
7.6 KiB
Go
Raw Normal View History

2019-04-16 22:11:44 +08:00
package gfile_test
2019-04-10 22:16:16 +08:00
import (
"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"
"testing"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/os/gfile"
"github.com/gogf/gf/test/gtest"
)
2019-04-16 22:11:44 +08:00
// 创建测试文件
func createTestFile(filename, content string) error {
TempDir := testpath()
2019-04-13 18:27:01 +08:00
err := ioutil.WriteFile(TempDir+filename, []byte(content), 0666)
2019-04-12 18:15:31 +08:00
return err
}
2019-04-16 22:11:44 +08:00
// 测试完删除文件或目录
func delTestFiles(filenames string) {
os.RemoveAll(testpath() + filenames)
2019-04-12 18:15:31 +08:00
}
2019-04-16 22:11:44 +08:00
// 创建目录
func createDir(paths string) {
TempDir := testpath()
2019-04-14 22:41:11 +08:00
os.Mkdir(TempDir+paths, 0777)
2019-04-12 18:15:31 +08:00
}
2019-04-16 22:11:44 +08:00
// 统一格式化文件目录为"/"
func formatpaths(paths []string) []string {
2019-04-13 18:27:01 +08:00
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
}
2019-04-16 22:11:44 +08:00
// 统一格式化文件目录为"/"
func formatpath(paths string) string {
2019-04-13 18:27:01 +08:00
paths = filepath.ToSlash(paths)
paths = strings.Replace(paths, "./", "/", 1)
return paths
}
2019-04-12 18:15:31 +08:00
2019-04-16 22:11:44 +08:00
// 指定返回要测试的目录
func testpath() string {
2019-04-16 23:07:53 +08:00
return os.TempDir()
2019-04-13 23:00:57 +08:00
}
func Test_GetContents(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-16 22:11:44 +08:00
createTestFile(filepaths, "my name is jroam")
defer delTestFiles(filepaths)
2019-04-16 22:11:44 +08:00
gtest.Assert(gfile.GetContents(testpath()+filepaths), "my name is jroam")
gtest.Assert(gfile.GetContents(""), "")
2019-04-13 23:00:57 +08:00
})
}
func Test_GetBinContents(t *testing.T) {
2019-04-12 09:47:55 +08:00
gtest.Case(t, func() {
var (
2019-04-16 22:11:44 +08:00
filepaths1 string = "/testfile_t1.txt" // 文件存在时
filepaths2 string = testpath() + "/testfile_t1_no.txt" // 文件不存在时
readcontent []byte
2019-04-13 18:27:01 +08:00
str1 string = "my name is jroam"
)
2019-04-16 22:11:44 +08:00
createTestFile(filepaths1, str1)
defer delTestFiles(filepaths1)
readcontent = gfile.GetBytes(testpath() + filepaths1)
2019-04-12 18:15:31 +08:00
gtest.Assert(readcontent, []byte(str1))
readcontent = gfile.GetBytes(filepaths2)
2019-04-13 18:27:01 +08:00
gtest.Assert(string(readcontent), "")
2019-04-10 17:54:28 +08:00
gtest.Assert(string(gfile.GetBytes(filepaths2)), "")
2019-04-12 18:15:31 +08:00
})
}
2019-04-16 22:11:44 +08:00
// 截断文件为指定的大小
func Test_Truncate(t *testing.T) {
2019-04-12 09:47:55 +08:00
gtest.Case(t, func() {
var (
2019-04-15 22:47:09 +08:00
filepaths1 string = "/testfile_GetContentsyyui.txt" //文件存在时
2019-04-12 09:47:55 +08:00
err error
2019-04-15 22:47:09 +08:00
files *os.File
2019-04-10 13:39:36 +08:00
)
2019-04-16 22:11:44 +08:00
createTestFile(filepaths1, "abcdefghijkmln")
defer delTestFiles(filepaths1)
err = gfile.Truncate(testpath()+filepaths1, 10)
2019-04-12 09:47:55 +08:00
gtest.Assert(err, nil)
2019-04-15 22:47:09 +08:00
//=========================检查修改文后的大小,是否与期望一致
2019-04-16 22:11:44 +08:00
files, err = os.Open(testpath() + filepaths1)
2019-04-15 22:47:09 +08:00
defer files.Close()
gtest.Assert(err, nil)
fileinfo, err2 := files.Stat()
gtest.Assert(err2, nil)
gtest.Assert(fileinfo.Size(), 10)
//====测试当为空时,是否报错
2019-04-16 22:11:44 +08:00
err = gfile.Truncate("", 10)
2019-04-12 09:47:55 +08:00
gtest.AssertNE(err, nil)
2019-04-10 17:54:28 +08:00
})
}
func Test_PutContents(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
readcontent []byte
)
2019-04-16 22:11:44 +08:00
createTestFile(filepaths, "a")
defer delTestFiles(filepaths)
2019-04-16 22:11:44 +08:00
err = gfile.PutContents(testpath()+filepaths, "test!")
2019-04-12 09:47:55 +08:00
gtest.Assert(err, nil)
//==================判断是否真正写入
2019-04-16 22:11:44 +08:00
readcontent, err = ioutil.ReadFile(testpath() + 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-16 22:11:44 +08:00
err = gfile.PutContents("", "test!")
2019-04-12 09:47:55 +08:00
gtest.AssertNE(err, nil)
2019-04-10 17:54:28 +08:00
})
}
func Test_PutContentsAppend(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
readcontent []byte
)
2019-04-16 22:11:44 +08:00
createTestFile(filepaths, "a")
defer delTestFiles(filepaths)
err = gfile.PutContentsAppend(testpath()+filepaths, "hello")
2019-04-12 09:47:55 +08:00
gtest.Assert(err, nil)
//==================判断是否真正写入
2019-04-16 22:11:44 +08:00
readcontent, err = ioutil.ReadFile(testpath() + 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-16 22:11:44 +08:00
err = gfile.PutContentsAppend("", "hello")
2019-04-12 09:47:55 +08:00
gtest.AssertNE(err, nil)
2019-04-10 17:54:28 +08:00
})
}
func Test_PutBinContents(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
readcontent []byte
)
2019-04-16 22:11:44 +08:00
createTestFile(filepaths, "a")
defer delTestFiles(filepaths)
err = gfile.PutBytes(testpath()+filepaths, []byte("test!!"))
2019-04-12 09:47:55 +08:00
gtest.Assert(err, nil)
2019-04-16 22:11:44 +08:00
// 判断是否真正写入
readcontent, err = ioutil.ReadFile(testpath() + 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
err = gfile.PutBytes("", []byte("test!!"))
2019-04-12 09:47:55 +08:00
gtest.AssertNE(err, nil)
2019-04-10 17:54:28 +08:00
})
}
func Test_PutBinContentsAppend(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
readcontent []byte
)
2019-04-16 22:11:44 +08:00
createTestFile(filepaths, "test!!")
defer delTestFiles(filepaths)
err = gfile.PutBytesAppend(testpath()+filepaths, []byte("word"))
2019-04-12 09:47:55 +08:00
gtest.Assert(err, nil)
2019-04-16 22:11:44 +08:00
// 判断是否真正写入
readcontent, err = ioutil.ReadFile(testpath() + 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
err = gfile.PutBytesAppend("", []byte("word"))
2019-04-12 09:47:55 +08:00
gtest.AssertNE(err, nil)
2019-04-10 17:54:28 +08:00
})
}
func Test_GetBinContentsByTwoOffsetsByPath(t *testing.T) {
gtest.Case(t, func() {
var (
2019-04-16 22:11:44 +08:00
filepaths string = "/testfile_GetContents.txt" // 文件内容: abcdefghijk
readcontent []byte
)
2019-04-16 22:11:44 +08:00
createTestFile(filepaths, "abcdefghijk")
defer delTestFiles(filepaths)
readcontent = gfile.GetBytesByTwoOffsetsByPath(testpath()+filepaths, 2, 5)
gtest.Assert(string(readcontent), "cde")
2019-04-10 17:54:28 +08:00
readcontent = gfile.GetBytesByTwoOffsetsByPath("", 2, 5)
2019-04-12 09:47:55 +08:00
gtest.Assert(len(readcontent), 0)
2019-04-10 17:54:28 +08:00
})
}
func Test_GetNextCharOffsetByPath(t *testing.T) {
2019-04-10 13:39:36 +08:00
gtest.Case(t, func() {
var (
2019-04-16 22:11:44 +08:00
filepaths string = "/testfile_GetContents.txt" // 文件内容: abcdefghijk
2019-04-10 13:39:36 +08:00
localindex int64
)
2019-04-16 22:11:44 +08:00
createTestFile(filepaths, "abcdefghijk")
defer delTestFiles(filepaths)
localindex = gfile.GetNextCharOffsetByPath(testpath()+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-16 22:11:44 +08:00
localindex = gfile.GetNextCharOffsetByPath("", 'd', 1)
2019-04-10 17:54:28 +08:00
gtest.Assert(localindex, -1)
2019-04-10 13:39:36 +08:00
})
}
func Test_GetNextCharOffset(t *testing.T) {
2019-04-10 22:16:16 +08:00
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-16 22:11:44 +08:00
localindex = gfile.GetNextCharOffset(reader, 'w', 1)
2019-04-12 09:47:55 +08:00
gtest.Assert(localindex, 5)
2019-04-10 22:16:16 +08:00
2019-04-16 22:11:44 +08:00
localindex = gfile.GetNextCharOffset(reader, 'j', 1)
2019-04-12 09:47:55 +08:00
gtest.Assert(localindex, -1)
2019-04-10 22:16:16 +08:00
})
}
func Test_GetBinContentsByTwoOffsets(t *testing.T) {
2019-04-10 22:16:16 +08:00
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
reads = gfile.GetBytesByTwoOffsets(reader, 1, 3)
2019-04-12 09:47:55 +08:00
gtest.Assert(string(reads), "el")
2019-04-10 22:16:16 +08:00
reads = gfile.GetBytesByTwoOffsets(reader, 10, 30)
2019-04-12 09:47:55 +08:00
gtest.Assert(string(reads), "")
2019-04-10 22:16:16 +08:00
})
}
func Test_GetBinContentsTilChar(t *testing.T) {
2019-04-10 22:16:16 +08:00
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
reads, _ = gfile.GetBytesTilChar(reader, 'w', 2)
2019-04-12 09:47:55 +08:00
gtest.Assert(string(reads), "llow")
2019-04-10 22:16:16 +08:00
_, indexs = gfile.GetBytesTilChar(reader, 'w', 20)
2019-04-12 09:47:55 +08:00
gtest.Assert(indexs, -1)
2019-04-10 22:16:16 +08:00
})
}
func Test_GetBinContentsTilCharByPath(t *testing.T) {
2019-04-10 22:16:16 +08:00
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-16 22:11:44 +08:00
createTestFile(filepaths, "abcdefghijklmn")
defer delTestFiles(filepaths)
2019-04-12 18:15:31 +08:00
reads, _ = gfile.GetBytesTilCharByPath(testpath()+filepaths, 'c', 2)
2019-04-12 09:47:55 +08:00
gtest.Assert(string(reads), "c")
2019-04-10 22:16:16 +08:00
reads, _ = gfile.GetBytesTilCharByPath(testpath()+filepaths, 'y', 1)
2019-04-12 09:47:55 +08:00
gtest.Assert(string(reads), "")
2019-04-10 22:16:16 +08:00
_, indexs = gfile.GetBytesTilCharByPath(testpath()+filepaths, 'x', 1)
2019-04-12 09:47:55 +08:00
gtest.Assert(indexs, -1)
2019-04-10 22:16:16 +08:00
})
}
func Test_Home(t *testing.T) {
2019-04-10 22:16:16 +08:00
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-16 22:11:44 +08:00
reads, err = gfile.Home()
2019-04-12 09:47:55 +08:00
gtest.Assert(err, nil)
gtest.AssertNE(reads, "")
2019-04-10 22:16:16 +08:00
})
}