diff --git a/cron/trash.go b/cron/trash.go index 257696d..161a51f 100644 --- a/cron/trash.go +++ b/cron/trash.go @@ -10,6 +10,7 @@ import ( "time" ) +// TODO Improve Trash path for other block devices e.g. for NTFS devices var TrashPath = filepath.Join(os.Getenv("HOME"), ".locale", "share", "Trash", "files") func moveToTrashTask() { @@ -23,9 +24,9 @@ func moveToTrashTask() { } func moveFileToTrash(dir setting.Directory) { - for _, paths := range dir.Src { - if isAfterDateOfRemovingFile(paths, dir.After) { - go file.MoveToDestination(TrashPath, paths) + for _, p := range dir.Src { + if isAfterDateOfRemovingFile(p, dir.After) { + go file.MoveToDestination(TrashPath, p) } } } @@ -37,5 +38,10 @@ func isAfterDateOfRemovingFile(path string, after uint) bool { return false } - return stat.ModTime().Add(time.Hour * 24 * time.Duration(after)).After(time.Now()) + afterTime := time.Hour * 24 * time.Duration(after) + if afterTime == 0 { + return true + } + + return stat.ModTime().Add(afterTime).After(time.Now()) } diff --git a/cron/trash_test.go b/cron/trash_test.go new file mode 100644 index 0000000..5f18398 --- /dev/null +++ b/cron/trash_test.go @@ -0,0 +1,61 @@ +package cron + +import ( + "github.com/wittano/filebot/internal/test" + "github.com/wittano/filebot/setting" + "os" + "path/filepath" + "testing" + "time" +) + +func TestIsAfterDateOfRemovingFile(t *testing.T) { + f := test.CreateTempFile(t) + + res := isAfterDateOfRemovingFile(f, 1) + + if res == false { + t.Fatalf("File %s is older then should be", f) + } +} + +func TestIsAfterDateOfRemovingFileButFileDoesNotExist(t *testing.T) { + res := isAfterDateOfRemovingFile("/path/to/non/existing/file", 1) + + if res { + t.Fatalf("Non-exisiting file was found") + } +} + +func TestIsAfterDateOfRemovingFileButAfterTimeIsEqualZero(t *testing.T) { + f := test.CreateTempFile(t) + + res := isAfterDateOfRemovingFile(f, 0) + + if res == false { + t.Fatalf("File %s shouldn't move. Function isAfterDateOfRemovingFile returned true", f) + } +} + +func TestMoveFileToTrash(t *testing.T) { + f := test.CreateTempFile(t) + TrashPath = t.TempDir() + dir := setting.Directory{ + Src: []string{f}, + After: 0, + } + + moveFileToTrash(dir) + + time.Sleep(10 * time.Millisecond) + + if _, err := os.Stat(f); err == nil { + t.Fatalf("File %s didn't move from original source", f) + } + + newFilePath := filepath.Join(TrashPath, filepath.Base(f)) + + if _, err := os.Stat(newFilePath); err != nil { + t.Fatalf("File %s didn't move to destination directory. %s", f, err) + } +} diff --git a/internal/test/config.go b/internal/test/config.go new file mode 100644 index 0000000..a395e29 --- /dev/null +++ b/internal/test/config.go @@ -0,0 +1,46 @@ +package test + +import ( + "github.com/wittano/filebot/setting" + "os" + "testing" +) + +func CreateTestConfiguration(t *testing.T) *setting.Config { + tempDir := t.TempDir() + secondTempDir := t.TempDir() + tempFile, err := os.CreateTemp(tempDir, "test.mp4") + if err != nil { + t.Fatalf("Failed creating temp file: %s", err) + } + defer tempFile.Close() + + return &setting.Config{Dirs: []setting.Directory{ + { + Src: []string{tempFile.Name()}, + Dest: secondTempDir, + Recursive: false, + }, + }} +} + +func CreateTestConfigurationWithRecursive(t *testing.T) *setting.Config { + tempDir := t.TempDir() + secondTempDir := tempDir + "/test" + + os.Mkdir(secondTempDir, 0777) + + tempFile, err := os.CreateTemp(secondTempDir, "test.mp4") + if err != nil { + t.Fatalf("Failed creating temp file: %s", err) + } + defer tempFile.Close() + + return &setting.Config{Dirs: []setting.Directory{ + { + Src: []string{tempFile.Name()}, + Dest: tempDir, + Recursive: true, + }, + }} +} diff --git a/internal/test/file.go b/internal/test/file.go new file mode 100644 index 0000000..89b1dbf --- /dev/null +++ b/internal/test/file.go @@ -0,0 +1,31 @@ +package test + +import ( + "os" + "testing" +) + +func CreateTempFile(t *testing.T) string { + file, err := os.CreateTemp(t.TempDir(), "test") + if err != nil { + t.Fatalf("Failed created temp file. %s", err) + } + defer file.Close() + + return file.Name() +} + +func CreateNestedTempDirWithFiles(t *testing.T, filename string) (string, string) { + dir := t.TempDir() + nestedDir := dir + "test" + + os.Mkdir(nestedDir, 0777) + + file, err := os.CreateTemp(nestedDir, filename) + if err != nil { + t.Fatalf("Failed created temp file. %s", err) + } + defer file.Close() + + return nestedDir, file.Name() +} diff --git a/path/path_test.go b/path/path_test.go index 4ab21ff..71e231b 100644 --- a/path/path_test.go +++ b/path/path_test.go @@ -1,6 +1,7 @@ package path import ( + "github.com/wittano/filebot/internal/test" "os" "path/filepath" "strings" @@ -8,7 +9,7 @@ import ( ) func TestGetPathsFromRegex(t *testing.T) { - exp := createTempDirWithFile(t) + exp := test.CreateTempFile(t) paths, err := GetPathsFromPattern(exp) if err == nil && len(paths) != 1 && paths[0] == exp { @@ -17,7 +18,7 @@ func TestGetPathsFromRegex(t *testing.T) { } func TestGetPathsFromRegexButRegexStartFromStar(t *testing.T) { - p := createTempDirWithFile(t) + p := test.CreateTempFile(t) exp := strings.Replace(p, "test", "*est", 1) paths, err := GetPathsFromPattern(exp) @@ -27,7 +28,7 @@ func TestGetPathsFromRegexButRegexStartFromStar(t *testing.T) { } func TestGetPathsFromRegexButFunctionReturnNil(t *testing.T) { - p := createTempDirWithFile(t) + p := test.CreateTempFile(t) exp := strings.Replace(p, "test", "tset", 1) paths, err := GetPathsFromPattern(exp) @@ -37,7 +38,7 @@ func TestGetPathsFromRegexButFunctionReturnNil(t *testing.T) { } func TestGetPathsFromRegexRecursive(t *testing.T) { - _, expFilename := createNestedTempDirWithFiles(t, "test.mp4") + _, expFilename := test.CreateNestedTempDirWithFiles(t, "test.mp4") dir := filepath.Dir(expFilename) paths, err := GetPathFromPatternRecursive(dir + "*.mp4*") @@ -51,7 +52,7 @@ func TestGetPathsFromRegexRecursive(t *testing.T) { } func TestGetPathsFromRegexRecursiveButFunctionReturnNil(t *testing.T) { - expDir, _ := createNestedTempDirWithFiles(t, "test") + expDir, _ := test.CreateNestedTempDirWithFiles(t, "test") dir := strings.Replace(expDir, "test", "tset", 1) paths, err := GetPathFromPatternRecursive(dir) @@ -83,28 +84,3 @@ func BenchmarkGetPathsFromRegex(b *testing.B) { b.ReportAllocs() } - -func createTempDirWithFile(t *testing.T) string { - file, err := os.CreateTemp(t.TempDir(), "test") - if err != nil { - t.Fatalf("Failed created temp file. %s", err) - } - defer file.Close() - - return file.Name() -} - -func createNestedTempDirWithFiles(t *testing.T, filename string) (string, string) { - dir := t.TempDir() - nestedDir := dir + "test" - - os.Mkdir(nestedDir, 0777) - - file, err := os.CreateTemp(nestedDir, filename) - if err != nil { - t.Fatalf("Failed created temp file. %s", err) - } - defer file.Close() - - return nestedDir, file.Name() -} diff --git a/setting/config_file.go b/setting/config_file.go index 95efe08..f81f3b5 100644 --- a/setting/config_file.go +++ b/setting/config_file.go @@ -31,6 +31,7 @@ func load(path string) (*Config, error) { return nil, err } + // TODO Add validator config = new(Config) config.Dirs = maps.Values(unmarshal) diff --git a/watcher/watcher_linux_test.go b/watcher/watcher_linux_test.go index 364019a..7245953 100644 --- a/watcher/watcher_linux_test.go +++ b/watcher/watcher_linux_test.go @@ -4,6 +4,7 @@ package watcher import ( "errors" + "github.com/wittano/filebot/internal/test" "github.com/wittano/filebot/setting" "os" "testing" @@ -11,7 +12,7 @@ import ( ) func TestAddFileToObservable(t *testing.T) { - conf := createTestConfiguration(t) + conf := test.CreateTestConfiguration(t) w := NewWatcher() w.AddFilesToObservable(conf) @@ -42,7 +43,7 @@ func TestAddFileToObservable(t *testing.T) { } func TestAddFileToObservableRecursive(t *testing.T) { - conf := createTestConfigurationWithRecursive(t) + conf := test.CreateTestConfigurationWithRecursive(t) w := NewWatcher() w.AddFilesToObservable(conf) @@ -97,42 +98,3 @@ func BenchmarkAddFilesToObservable(b *testing.B) { b.ReportAllocs() } - -func createTestConfiguration(t *testing.T) *setting.Config { - tempDir := t.TempDir() - secondTempDir := t.TempDir() - tempFile, err := os.CreateTemp(tempDir, "test.mp4") - if err != nil { - t.Fatalf("Failed creating temp file: %s", err) - } - defer tempFile.Close() - - return &setting.Config{Dirs: []setting.Directory{ - { - Src: []string{tempFile.Name()}, - Dest: secondTempDir, - Recursive: false, - }, - }} -} - -func createTestConfigurationWithRecursive(t *testing.T) *setting.Config { - tempDir := t.TempDir() - secondTempDir := tempDir + "/test" - - os.Mkdir(secondTempDir, 0777) - - tempFile, err := os.CreateTemp(secondTempDir, "test.mp4") - if err != nil { - t.Fatalf("Failed creating temp file: %s", err) - } - defer tempFile.Close() - - return &setting.Config{Dirs: []setting.Directory{ - { - Src: []string{tempFile.Name()}, - Dest: tempDir, - Recursive: true, - }, - }} -}