From c0690a2ff4cc1486055e7f87d8981ea35f199993 Mon Sep 17 00:00:00 2001 From: Wittano Date: Tue, 19 Dec 2023 11:36:29 +0100 Subject: [PATCH] fix(file): fixed problem with non-replacing environment in path --- file/move.go | 3 +++ path/path.go | 18 +++++++++++++++ path/path_test.go | 15 +++++++++++++ watcher/watcher_linux_test.go | 42 +++++++++++++++++++++++++++++++++-- 4 files changed, 76 insertions(+), 2 deletions(-) diff --git a/file/move.go b/file/move.go index c693ee0..d2b86be 100644 --- a/file/move.go +++ b/file/move.go @@ -2,12 +2,15 @@ package file import ( "errors" + "github.com/wittano/filebot/path" "github.com/wittano/filebot/setting" "os" "path/filepath" ) 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 diff --git a/path/path.go b/path/path.go index e0a6834..d481b70 100644 --- a/path/path.go +++ b/path/path.go @@ -3,9 +3,12 @@ package path import ( "os" "path/filepath" + "strings" ) func PathsFromPattern(src string) ([]string, error) { + src = ReplaceEnvVariablesInPath(src) + reg, err := Regex(src) if err != nil { return nil, err @@ -33,7 +36,22 @@ func PathsFromPattern(src string) ([]string, error) { return paths, nil } +func ReplaceEnvVariablesInPath(src string) string { + sep := string(filepath.Separator) + parts := strings.Split(src, sep) + + for i, s := range parts { + if v, ok := os.LookupEnv(strings.ReplaceAll(s, "$", "")); ok && v != "" { + parts[i] = v + } + } + + return strings.Join(parts, sep) +} + func PathsFromPatternRecursive(path string) ([]string, error) { + path = ReplaceEnvVariablesInPath(path) + dir, pattern := filepath.Split(path) if !isFilePathIsRegex(pattern) { dir = path diff --git a/path/path_test.go b/path/path_test.go index 78fbd93..96c25e1 100644 --- a/path/path_test.go +++ b/path/path_test.go @@ -17,6 +17,21 @@ func TestPathsFromRegex(t *testing.T) { } } +func TestPathsFromRegexWithEnvVariable(t *testing.T) { + err := os.Setenv("TEST", filepath.Dir(t.TempDir())) + if err != nil { + t.Fatalf("Failed create TEST enviromnent variable. %s", err) + } + + exp := test.CreateTempFile(t) + exp = strings.ReplaceAll(exp, filepath.Dir(t.TempDir()), "$TEST") + + paths, err := PathsFromPattern(exp) + if err == nil && len(paths) != 1 && paths[0] == exp { + t.Fatalf("Failed got paths. Expected 1, acually %d", len(paths)) + } +} + func TestPathsFromRegexButRegexStartFromStar(t *testing.T) { p := test.CreateTempFile(t) exp := strings.Replace(p, "test", "*est", 1) diff --git a/watcher/watcher_linux_test.go b/watcher/watcher_linux_test.go index 44f5444..a7e84bf 100644 --- a/watcher/watcher_linux_test.go +++ b/watcher/watcher_linux_test.go @@ -7,6 +7,7 @@ import ( "github.com/wittano/filebot/setting" "os" "path/filepath" + "strings" "testing" "time" ) @@ -36,9 +37,46 @@ func TestAddFileToObservable(t *testing.T) { t.Fatal("File didn't move from started location") } - dest := conf.Dirs[0].Dest + dest := filepath.Join(conf.Dirs[0].Dest, filepath.Base(src)) if _, err := os.Stat(dest); err != nil { - t.Fatal("File didn't move to destination location") + t.Fatalf("File %s didn't move to destination location", dest) + } +} + +func TestAddFileToObservableButDestinationPathHasEnvVariable(t *testing.T) { + conf := createTestConfiguration(t) + + os.Setenv("TEST", filepath.Dir(t.TempDir())) + + conf.Dirs[0].Dest = strings.ReplaceAll(conf.Dirs[0].Dest, filepath.Dir(t.TempDir()), "$TEST") + + w := NewWatcher() + w.AddFilesToObservable(*conf) + + if len(w.WatchList()) != 1 { + t.Fatalf("Invalid number of watched files. Expected 1, actually %d", len(w.Watcher.WatchList())) + } + + src := conf.Dirs[0].Src[0] + if w.WatchList()[0] != src { + t.Fatalf( + "Invalid path was added to observation list. Expected %s, actually %s", + src, + w.WatchList()[0], + ) + } + + duration, _ := time.ParseDuration("0.5s") + time.Sleep(duration) + if _, err := os.Stat(src); !errors.Is(err, os.ErrNotExist) { + t.Fatal("File didn't move from started location") + } + + conf.Dirs[0].Dest = strings.ReplaceAll(conf.Dirs[0].Dest, "$TEST", filepath.Dir(t.TempDir())) + + dest := filepath.Join(conf.Dirs[0].Dest, filepath.Base(src)) + if _, err := os.Stat(dest); err != nil { + t.Fatalf("File %s didn't move to destination location", dest) } }