gf/os/gfile/gfile_z_contents_test.go

330 lines
7.1 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-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"
"github.com/gogf/gf/text/gstr"
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 {
return gstr.TrimRight(os.TempDir(), "\\/")
2019-04-13 23:00:57 +08:00
}
func Test_GetContents(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
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)
2020-03-19 22:56:12 +08:00
t.Assert(gfile.GetContents(testpath()+filepaths), "my name is jroam")
t.Assert(gfile.GetContents(""), "")
2019-04-13 23:00:57 +08:00
})
}
func Test_GetBinContents(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 (
2020-05-16 14:06:35 +08:00
filepaths1 = "/testfile_t1.txt"
filepaths2 = testpath() + "/testfile_t1_no.txt"
readcontent []byte
2020-05-16 14:06:35 +08:00
str1 = "my name is jroam"
)
2019-04-16 22:11:44 +08:00
createTestFile(filepaths1, str1)
defer delTestFiles(filepaths1)
readcontent = gfile.GetBytes(testpath() + filepaths1)
2020-03-19 22:56:12 +08:00
t.Assert(readcontent, []byte(str1))
readcontent = gfile.GetBytes(filepaths2)
2020-03-19 22:56:12 +08:00
t.Assert(string(readcontent), "")
2019-04-10 17:54:28 +08:00
2020-03-19 22:56:12 +08:00
t.Assert(string(gfile.GetBytes(filepaths2)), "")
2019-04-12 18:15:31 +08:00
})
}
func Test_Truncate(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 (
2020-05-16 14:06:35 +08:00
filepaths1 = "/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)
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
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()
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
2019-04-15 22:47:09 +08:00
fileinfo, err2 := files.Stat()
2020-03-19 22:56:12 +08:00
t.Assert(err2, nil)
t.Assert(fileinfo.Size(), 10)
2019-04-15 22:47:09 +08:00
2019-04-16 22:11:44 +08:00
err = gfile.Truncate("", 10)
2020-03-19 22:56:12 +08:00
t.AssertNE(err, nil)
2019-04-10 17:54:28 +08:00
})
}
func Test_PutContents(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 (
2020-05-16 14:06:35 +08:00
filepaths = "/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!")
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
2019-04-16 22:11:44 +08:00
readcontent, err = ioutil.ReadFile(testpath() + filepaths)
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.Assert(string(readcontent), "test!")
2019-04-10 17:54:28 +08:00
2019-04-16 22:11:44 +08:00
err = gfile.PutContents("", "test!")
2020-03-19 22:56:12 +08:00
t.AssertNE(err, nil)
2019-04-10 17:54:28 +08:00
})
}
func Test_PutContentsAppend(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 (
2020-05-16 14:06:35 +08:00
filepaths = "/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")
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
2019-04-16 22:11:44 +08:00
readcontent, err = ioutil.ReadFile(testpath() + filepaths)
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.Assert(string(readcontent), "ahello")
2019-04-10 17:54:28 +08:00
2019-04-16 22:11:44 +08:00
err = gfile.PutContentsAppend("", "hello")
2020-03-19 22:56:12 +08:00
t.AssertNE(err, nil)
2019-04-10 17:54:28 +08:00
})
}
func Test_PutBinContents(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 (
2020-05-16 14:06:35 +08:00
filepaths = "/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!!"))
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
2019-04-16 22:11:44 +08:00
readcontent, err = ioutil.ReadFile(testpath() + filepaths)
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.Assert(string(readcontent), "test!!")
2019-04-10 17:54:28 +08:00
err = gfile.PutBytes("", []byte("test!!"))
2020-03-19 22:56:12 +08:00
t.AssertNE(err, nil)
2019-04-10 17:54:28 +08:00
})
}
func Test_PutBinContentsAppend(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 (
2020-05-16 14:06:35 +08:00
filepaths = "/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, "test!!")
defer delTestFiles(filepaths)
err = gfile.PutBytesAppend(testpath()+filepaths, []byte("word"))
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
2019-04-16 22:11:44 +08:00
readcontent, err = ioutil.ReadFile(testpath() + filepaths)
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.Assert(string(readcontent), "test!!word")
2019-04-10 17:54:28 +08:00
err = gfile.PutBytesAppend("", []byte("word"))
2020-03-19 22:56:12 +08:00
t.AssertNE(err, nil)
2019-04-10 17:54:28 +08:00
})
}
func Test_GetBinContentsByTwoOffsetsByPath(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
var (
2020-05-16 14:06:35 +08:00
filepaths = "/testfile_GetContents.txt"
readcontent []byte
)
2019-04-16 22:11:44 +08:00
createTestFile(filepaths, "abcdefghijk")
defer delTestFiles(filepaths)
readcontent = gfile.GetBytesByTwoOffsetsByPath(testpath()+filepaths, 2, 5)
2020-03-19 22:56:12 +08:00
t.Assert(string(readcontent), "cde")
2019-04-10 17:54:28 +08:00
readcontent = gfile.GetBytesByTwoOffsetsByPath("", 2, 5)
2020-03-19 22:56:12 +08:00
t.Assert(len(readcontent), 0)
2019-04-10 17:54:28 +08:00
})
}
func Test_GetNextCharOffsetByPath(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-10 13:39:36 +08:00
var (
2020-05-16 14:06:35 +08:00
filepaths = "/testfile_GetContents.txt"
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)
2020-03-19 22:56:12 +08:00
t.Assert(localindex, 3)
2019-04-10 17:54:28 +08:00
2019-04-16 22:11:44 +08:00
localindex = gfile.GetNextCharOffsetByPath("", 'd', 1)
2020-03-19 22:56:12 +08:00
t.Assert(localindex, -1)
2019-04-10 17:54:28 +08:00
2019-04-10 13:39:36 +08:00
})
}
func Test_GetNextCharOffset(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-10 22:16:16 +08:00
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)
2020-03-19 22:56:12 +08:00
t.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)
2020-03-19 22:56:12 +08:00
t.Assert(localindex, -1)
2019-04-10 22:16:16 +08:00
})
}
func Test_GetBinContentsByTwoOffsets(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-10 22:16:16 +08:00
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)
2020-03-19 22:56:12 +08:00
t.Assert(string(reads), "el")
2019-04-10 22:16:16 +08:00
reads = gfile.GetBytesByTwoOffsets(reader, 10, 30)
2020-03-19 22:56:12 +08:00
t.Assert(string(reads), "")
2019-04-10 22:16:16 +08:00
})
}
func Test_GetBinContentsTilChar(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-10 22:16:16 +08:00
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)
2020-03-19 22:56:12 +08:00
t.Assert(string(reads), "llow")
2019-04-10 22:16:16 +08:00
_, indexs = gfile.GetBytesTilChar(reader, 'w', 20)
2020-03-19 22:56:12 +08:00
t.Assert(indexs, -1)
2019-04-10 22:16:16 +08:00
})
}
func Test_GetBinContentsTilCharByPath(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-10 22:16:16 +08:00
var (
2019-04-12 09:47:55 +08:00
reads []byte
indexs int64
2020-05-16 14:06:35 +08:00
filepaths = "/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)
2020-03-19 22:56:12 +08:00
t.Assert(string(reads), "c")
2019-04-10 22:16:16 +08:00
reads, _ = gfile.GetBytesTilCharByPath(testpath()+filepaths, 'y', 1)
2020-03-19 22:56:12 +08:00
t.Assert(string(reads), "")
2019-04-10 22:16:16 +08:00
_, indexs = gfile.GetBytesTilCharByPath(testpath()+filepaths, 'x', 1)
2020-03-19 22:56:12 +08:00
t.Assert(indexs, -1)
2019-04-10 22:16:16 +08:00
})
}
func Test_Home(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-10 22:16:16 +08:00
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()
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.AssertNE(reads, "")
2019-04-10 22:16:16 +08:00
})
}