Skip to content

Commit

Permalink
fix(file): create dest directory even it doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
Wittano committed Feb 25, 2024
1 parent 58cfc82 commit 87af834
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
3 changes: 1 addition & 2 deletions file/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ func MoveToDestination(dest string, paths ...string) {
dest = path.ReplaceEnvVariablesInPath(dest)

if _, err := os.Stat(dest); errors.Is(err, os.ErrNotExist) {
setting.Logger().Errorf("Destination directory %s doesn't exist", err, dest)
return
os.MkdirAll(dest, 0644)
}

for _, src := range paths {
Expand Down
38 changes: 38 additions & 0 deletions file/move_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package file

import (
"errors"
"os"
"path/filepath"
"testing"
)

func TestMoveFileToDestination(t *testing.T) {
dir := t.TempDir()
src, err := os.CreateTemp(dir, "tests")
if err != nil {
t.Fatal(err)
}

resPath := filepath.Join(dir, "test2")
MoveToDestination(resPath, src.Name())

if _, err = os.Stat(resPath); errors.Is(err, os.ErrNotExist) {
t.Fatalf("Failed move file from %s to %s", src.Name(), resPath)
}
}

func TestMoveFileToDestinationButDestDirNotExist(t *testing.T) {
dir := t.TempDir()
src, err := os.CreateTemp(dir, "tests")
if err != nil {
t.Fatal(err)
}

resPath := filepath.Join(dir, "path", "to", "my", "testFile")
MoveToDestination(resPath, src.Name())

if _, err = os.Stat(resPath); errors.Is(err, os.ErrNotExist) {
t.Fatalf("Failed move file from %s to %s", src.Name(), resPath)
}
}

0 comments on commit 87af834

Please sign in to comment.