Skip to content

Commit

Permalink
gfile
Browse files Browse the repository at this point in the history
  • Loading branch information
snail007 committed Dec 21, 2023
1 parent 3df25b1 commit 6d414e7
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
26 changes: 26 additions & 0 deletions util/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,29 @@ func Write(file string, data []byte, append bool) (err error) {
func WriteString(file string, data string, append bool) (err error) {
return Write(file, []byte(data), append)
}

// Copy source path file to destination file path, when mkdir is true and destination path not exists,
// the destination dir will be created
func Copy(srcFile, dstFile string, mkdir bool) error {
sourceFile, err := os.Open(srcFile)
if err != nil {
return err
}
defer sourceFile.Close()
dstPath := filepath.Dir(dstFile)
if !IsDir(dstPath) {
if mkdir {
err = os.MkdirAll(dstPath, 0755)
}
}
if err != nil {
return err
}
destinationFile, err := os.Create(dstFile)
if err != nil {
return err
}
defer destinationFile.Close()
_, err = io.Copy(destinationFile, sourceFile)
return err
}
60 changes: 60 additions & 0 deletions util/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/magiconair/properties/assert"
gvalue "github.com/snail007/gmc/util/value"
assert2 "github.com/stretchr/testify/assert"
"io/ioutil"
"os"
"testing"
"time"
Expand Down Expand Up @@ -118,3 +119,62 @@ func TestReadAll(t *testing.T) {
s := ReadAll(file)
assert.Equal(s, "abc")
}

func TestCopyFile(t *testing.T) {
tempDir, err := ioutil.TempDir("", "copy_test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)

sourceFilePath := tempDir + "/source.txt"
destinationFilePath := tempDir + "/destination.txt"

err = ioutil.WriteFile(sourceFilePath, []byte("Hello, world!"), 0644)
if err != nil {
t.Fatal(err)
}

err = Copy(sourceFilePath, destinationFilePath, true)
if err != nil {
t.Fatalf("Error copying file: %v", err)
}

destinationContent, err := ioutil.ReadFile(destinationFilePath)
if err != nil {
t.Fatalf("Error reading destination file: %v", err)
}

expectedContent := []byte("Hello, world!")
if string(destinationContent) != string(expectedContent) {
t.Fatalf("Copied file content does not match. Expected %q, got %q", expectedContent, destinationContent)
}

nonExistentDirPath := tempDir + "/nonexistent"
nonExistentDestinationPath := nonExistentDirPath + "/destination.txt"

err = Copy(sourceFilePath, nonExistentDestinationPath, false)
if err == nil {
t.Fatal("Expected error when destination directory does not exist, but got none")
} else {
t.Logf("Expected error: %v", err)
}

unwritableDirPath := "/unwritable"
unwritableDestinationPath := unwritableDirPath + "/destination.txt"

err = Copy(sourceFilePath, unwritableDestinationPath, true)
if err == nil {
t.Fatal("Expected error when destination directory is unwritable, but got none")
} else {
t.Logf("Expected error: %v", err)
}

nonExistentSourcePath := tempDir + "/nonexistent/source.txt"
err = Copy(nonExistentSourcePath, destinationFilePath, true)
if err == nil {
t.Fatal("Expected error when source file does not exist, but got none")
} else {
t.Logf("Expected error: %v", err)
}
}
2 changes: 1 addition & 1 deletion util/value/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ func Contains(sliceInterface interface{}, value interface{}) bool {
func IndexOf(sliceInterface interface{}, value interface{}) int {
sliceValue := reflect.ValueOf(sliceInterface)

if sliceValue.Kind() != reflect.Slice {
if sliceValue.Kind() != reflect.Slice && sliceValue.Kind() != reflect.Array {
return -1
}

Expand Down

0 comments on commit 6d414e7

Please sign in to comment.