Skip to content

Commit

Permalink
feat: fix Move and MoveTo not creating new folders
Browse files Browse the repository at this point in the history
feat: fix `MoveTo` taking wrong base path.
feat: add more test cases
  • Loading branch information
ShindouMihou committed Sep 11, 2023
1 parent e378269 commit d223c96
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
11 changes: 9 additions & 2 deletions siopao/api_move.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
// move the file to another folder. If you want to simply rename the file's name, use Rename instead, otherwise,
// if you want to keep the name, but move the folder, use MoveTo instead.
func (file *File) Move(dest string) error {
if err := mkparent(dest); err != nil {
return err
}
return os.Rename(file.path, dest)
}

Expand All @@ -28,6 +31,10 @@ func (file *File) Rename(name string) error {
// If you want to move the file into an entirely new folder, use Move instead.
// You can also use Rename if you want to rename the file's name.
func (file *File) MoveTo(dir string) error {
base := filepath.Base(dir)
return os.Rename(file.path, filepath.Join(dir, base))
base := filepath.Base(file.path)
dest := filepath.Join(dir, base)
if err := mkparent(dest); err != nil {
return err
}
return os.Rename(file.path, dest)
}
8 changes: 6 additions & 2 deletions siopao/core_open.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ func (file *File) clear() error {

// MkdirParent creates the parent folders of the path.
func (file *File) mkparent() error {
if strings.Contains(file.path, "\\") || strings.Contains(file.path, "/") {
if err := os.MkdirAll(filepath.Dir(file.path), os.ModePerm); err != nil {
return mkparent(file.path)
}

func mkparent(path string) error {
if strings.Contains(path, "\\") || strings.Contains(path, "/") {
if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil {
return err
}
}
Expand Down
43 changes: 43 additions & 0 deletions siopao/siopao_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,49 @@ func TestFile_Checksum(t *testing.T) {
}
}

func TestFile_Copy(t *testing.T) {
file := Open(".tests/write-01.txt")
err := file.Copy(".tests/copy-01.txt")
if err != nil {
t.Fatal("failed to copy to test text file: ", err)
}
}

func TestFile_CopyWithHash(t *testing.T) {
file := Open(".tests/write-01.txt")
checksum, err := file.CopyWithHash(Md5Checksum, ".tests/copy-02.txt")
if err != nil {
t.Fatal("failed to copy to test text file: ", err)
}
t.Log("checksum of copy: ", checksum)
}

func TestFile_Rename(t *testing.T) {
file := Open(".tests/write-01.txt")
err := file.Copy(".tests/copy-03.txt")
if err != nil {
t.Fatal("failed to copy to test text file: ", err)
}
file = Open(".tests/copy-03.txt")
if err := file.Rename("rename-01.txt"); err != nil {
t.Fatal("failed to rename test text file: ", err)
}
}

func TestFile_MoveTo(t *testing.T) {
file := Open(".tests/rename-01.txt")
if err := file.MoveTo(".tests/renamed"); err != nil {
t.Fatal("failed to move to new directory: ", err)
}
}

func TestFile_Move(t *testing.T) {
file := Open(".tests/renamed/rename-01.txt")
if err := file.Move(".tests/renamed/rename-02.txt"); err != nil {
t.Fatal("failed to force move to new directory: ", err)
}
}

type Hello struct {
World string `json:"world"`
}
Expand Down

0 comments on commit d223c96

Please sign in to comment.