From 87af8343f0496cf89f236fb8bc46ed356413af48 Mon Sep 17 00:00:00 2001 From: Wittano Bonarotti Date: Sun, 25 Feb 2024 12:01:28 +0100 Subject: [PATCH] fix(file): create dest directory even it doesn't exist --- file/move.go | 3 +-- file/move_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 file/move_test.go diff --git a/file/move.go b/file/move.go index d2b86be..2b30c6a 100644 --- a/file/move.go +++ b/file/move.go @@ -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 { diff --git a/file/move_test.go b/file/move_test.go new file mode 100644 index 0000000..4a62494 --- /dev/null +++ b/file/move_test.go @@ -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) + } +}