Skip to content
This repository has been archived by the owner on Dec 29, 2024. It is now read-only.

Commit

Permalink
fix(file): fixed problem with non-replacing environment in path
Browse files Browse the repository at this point in the history
  • Loading branch information
Wittano committed Dec 19, 2023
1 parent 8ad1f29 commit c0690a2
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
3 changes: 3 additions & 0 deletions file/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions path/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions path/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
42 changes: 40 additions & 2 deletions watcher/watcher_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/wittano/filebot/setting"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -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)
}
}

Expand Down

0 comments on commit c0690a2

Please sign in to comment.