mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
publish lancet
This commit is contained in:
104
fileutil/file.go
Normal file
104
fileutil/file.go
Normal file
@@ -0,0 +1,104 @@
|
||||
// Copyright 2021 dudaodong@gmail.com. All rights reserved.
|
||||
// Use of this source code is governed by MIT license.
|
||||
|
||||
// Package fileutil implements some basic functions for file operations
|
||||
|
||||
package fileutil
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
)
|
||||
|
||||
// IsFileExists checks if a file or directory exists
|
||||
func IsExist(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
if err == nil {
|
||||
return true
|
||||
}
|
||||
if errors.Is(err, os.ErrExist) {
|
||||
return false
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// CreateFile create a file in path
|
||||
func CreateFile(path string) bool {
|
||||
file, err := os.Create(path)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
return true
|
||||
}
|
||||
|
||||
// IsFileExists checks if the path is directy or not
|
||||
func IsDir(path string) bool {
|
||||
file, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return file.IsDir()
|
||||
}
|
||||
|
||||
// RemoveFile remove the path file
|
||||
func RemoveFile(path string) error {
|
||||
return os.Remove(path)
|
||||
}
|
||||
|
||||
// CopyFile copy src file to dest file
|
||||
func CopyFile(srcFilePath string, dstFilePath string) error {
|
||||
srcFile, err := os.Open(srcFilePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer srcFile.Close()
|
||||
|
||||
distFile, err := os.Create(dstFilePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer distFile.Close()
|
||||
|
||||
var tmp = make([]byte, 1024*4)
|
||||
for {
|
||||
n, err := srcFile.Read(tmp)
|
||||
distFile.Write(tmp[:n])
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
return nil
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ListFileNames return all file names in the path
|
||||
func ListFileNames(path string) ([]string, error) {
|
||||
if !IsExist(path) {
|
||||
return []string{}, nil
|
||||
}
|
||||
|
||||
fs, err := ioutil.ReadDir(path)
|
||||
if err != nil {
|
||||
return []string{}, err
|
||||
}
|
||||
|
||||
sz := len(fs)
|
||||
if sz == 0 {
|
||||
return []string{}, nil
|
||||
}
|
||||
|
||||
res := []string{}
|
||||
for i := 0; i < sz; i++ {
|
||||
if !fs[i].IsDir() {
|
||||
res = append(res, fs[i].Name())
|
||||
}
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
99
fileutil/file_test.go
Normal file
99
fileutil/file_test.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package fileutil
|
||||
|
||||
import (
|
||||
"github.com/duke-git/lancet/utils"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsExist(t *testing.T) {
|
||||
cases := []string{"./", "./a.txt"}
|
||||
expected := []bool{true, false}
|
||||
|
||||
for i := 0; i < len(cases); i++ {
|
||||
res := IsExist(cases[i])
|
||||
if res != expected[i] {
|
||||
utils.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 {
|
||||
utils.LogFailedTestInfo(t, "CreateFile", f, f, "create file error: "+err.Error())
|
||||
t.FailNow()
|
||||
}
|
||||
if file.Name() != f {
|
||||
utils.LogFailedTestInfo(t, "CreateFile", f, f, file.Name())
|
||||
t.FailNow()
|
||||
}
|
||||
} else {
|
||||
utils.LogFailedTestInfo(t, "CreateFile", f, f, "create file error")
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
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] {
|
||||
utils.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 {
|
||||
utils.LogFailedTestInfo(t, "RemoveFile", f, f, err.Error())
|
||||
t.FailNow()
|
||||
}
|
||||
} else {
|
||||
utils.LogFailedTestInfo(t, "RemoveFile", f, f, "create file error")
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyFile(t *testing.T) {
|
||||
srcFile := "./text.txt"
|
||||
CreateFile(srcFile)
|
||||
|
||||
dstFile := "./text_copy.txt"
|
||||
|
||||
err := CopyFile(srcFile, dstFile)
|
||||
if err != nil {
|
||||
file, err := os.Open(dstFile)
|
||||
if err != nil {
|
||||
utils.LogFailedTestInfo(t, "CopyFile", srcFile, dstFile, "create file error: "+err.Error())
|
||||
t.FailNow()
|
||||
}
|
||||
if file.Name() != dstFile {
|
||||
utils.LogFailedTestInfo(t, "CopyFile", srcFile, dstFile, file.Name())
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestListFileNames(t *testing.T) {
|
||||
filesInCurrentPath, err := ListFileNames("./")
|
||||
if err != nil {
|
||||
t.FailNow()
|
||||
}
|
||||
expected := []string{"file.go", "file_test.go"}
|
||||
if !reflect.DeepEqual(filesInCurrentPath, expected) {
|
||||
utils.LogFailedTestInfo(t, "ToChar", "./", expected, filesInCurrentPath)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user