1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-09 07:02:29 +08:00
Files
lancet/fileutil/file_test.go
2022-01-06 15:15:59 +08:00

184 lines
4.0 KiB
Go

package fileutil
import (
"os"
"reflect"
"testing"
"github.com/duke-git/lancet/internal"
)
func TestIsExist(t *testing.T) {
cases := []string{"./", "./file.go", "./a.txt"}
expected := []bool{true, true, false}
for i := 0; i < len(cases); i++ {
res := IsExist(cases[i])
if res != expected[i] {
internal.LogFailedTestInfo(t, "IsExist", cases[i], expected[i], res)
t.FailNow()
}
}
}
func TestCreateFile(t *testing.T) {
f := "./text.txt"
if CreateFile(f) {
file, err := os.Open(f)
if err != nil {
internal.LogFailedTestInfo(t, "CreateFile", f, f, "create file error: "+err.Error())
t.FailNow()
}
if file.Name() != f {
internal.LogFailedTestInfo(t, "CreateFile", f, f, file.Name())
t.FailNow()
}
} else {
internal.LogFailedTestInfo(t, "CreateFile", f, f, "create file error")
t.FailNow()
}
os.Remove(f)
}
func TestIsDir(t *testing.T) {
cases := []string{"./", "./a.txt"}
expected := []bool{true, false}
for i := 0; i < len(cases); i++ {
res := IsDir(cases[i])
if res != expected[i] {
internal.LogFailedTestInfo(t, "IsDir", cases[i], expected[i], res)
t.FailNow()
}
}
}
func TestRemoveFile(t *testing.T) {
f := "./text.txt"
if CreateFile(f) {
err := RemoveFile(f)
if err != nil {
internal.LogFailedTestInfo(t, "RemoveFile", f, f, err.Error())
t.FailNow()
}
} else {
internal.LogFailedTestInfo(t, "RemoveFile", f, f, "create file error")
t.FailNow()
}
}
func TestCopyFile(t *testing.T) {
srcFile := "./text.txt"
CreateFile(srcFile)
destFile := "./text_copy.txt"
err := CopyFile(srcFile, destFile)
if err != nil {
file, err := os.Open(destFile)
if err != nil {
internal.LogFailedTestInfo(t, "CopyFile", srcFile, destFile, "create file error: "+err.Error())
t.FailNow()
}
if file.Name() != destFile {
internal.LogFailedTestInfo(t, "CopyFile", srcFile, destFile, file.Name())
t.FailNow()
}
}
os.Remove(srcFile)
os.Remove(destFile)
}
func TestListFileNames(t *testing.T) {
filesInCurrentPath, err := ListFileNames("../datetime/")
if err != nil {
t.FailNow()
}
expected := []string{"datetime.go", "datetime_test.go"}
if !reflect.DeepEqual(filesInCurrentPath, expected) {
internal.LogFailedTestInfo(t, "ToChar", "./", expected, filesInCurrentPath)
t.FailNow()
}
}
func TestReadFileToString(t *testing.T) {
path := "./text.txt"
CreateFile(path)
f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777)
f.WriteString("hello world")
res, _ := ReadFileToString(path)
if res != "hello world" {
internal.LogFailedTestInfo(t, "ReadFileToString", path, "hello world", res)
t.FailNow()
}
os.Remove(path)
}
func TestClearFile(t *testing.T) {
path := "./text.txt"
CreateFile(path)
f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777)
f.WriteString("hello world")
err := ClearFile(path)
if err != nil {
t.Error("Clear file error: ", err)
}
fileContent, _ := ReadFileToString(path)
if fileContent != "" {
internal.LogFailedTestInfo(t, "ClearFile", path, "", fileContent)
t.FailNow()
}
os.Remove(path)
}
func TestReadFileByLine(t *testing.T) {
path := "./text.txt"
CreateFile(path)
f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777)
f.WriteString("hello\nworld")
expected := []string{"hello", "world"}
res, _ := ReadFileByLine(path)
if !reflect.DeepEqual(res, expected) {
internal.LogFailedTestInfo(t, "ReadFileByLine", path, expected, res)
t.FailNow()
}
os.Remove(path)
}
func TestZipAndUnZip(t *testing.T) {
srcFile := "./text.txt"
CreateFile(srcFile)
file, _ := os.OpenFile(srcFile, os.O_WRONLY|os.O_TRUNC, 0777)
file.WriteString("hello\nworld")
zipFile := "./text.zip"
err := Zip(srcFile, zipFile)
if err != nil {
internal.LogFailedTestInfo(t, "Zip", srcFile, zipFile, err)
t.FailNow()
}
unZipPath := "./unzip"
err = UnZip(zipFile, unZipPath)
if err != nil {
internal.LogFailedTestInfo(t, "UnZip", srcFile, unZipPath, err)
t.FailNow()
}
unZipFile := "./unzip/text.txt"
if !IsExist(unZipFile) {
internal.LogFailedTestInfo(t, "UnZip", zipFile, zipFile, err)
t.FailNow()
}
os.Remove(srcFile)
os.Remove(zipFile)
os.RemoveAll(unZipPath)
}