Skip to content

Commit

Permalink
feat(test): add tests for trash cron task
Browse files Browse the repository at this point in the history
  • Loading branch information
Wittano committed Nov 20, 2023
1 parent fef4077 commit 06f7304
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 75 deletions.
14 changes: 10 additions & 4 deletions cron/trash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)
}
}
}
Expand All @@ -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())
}
61 changes: 61 additions & 0 deletions cron/trash_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
46 changes: 46 additions & 0 deletions internal/test/config.go
Original file line number Diff line number Diff line change
@@ -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,
},
}}
}
31 changes: 31 additions & 0 deletions internal/test/file.go
Original file line number Diff line number Diff line change
@@ -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()
}
36 changes: 6 additions & 30 deletions path/path_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package path

import (
"github.com/wittano/filebot/internal/test"
"os"
"path/filepath"
"strings"
"testing"
)

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 {
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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*")
Expand All @@ -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)
Expand Down Expand Up @@ -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()
}
1 change: 1 addition & 0 deletions setting/config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func load(path string) (*Config, error) {
return nil, err
}

// TODO Add validator
config = new(Config)
config.Dirs = maps.Values(unmarshal)

Expand Down
44 changes: 3 additions & 41 deletions watcher/watcher_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ package watcher

import (
"errors"
"github.com/wittano/filebot/internal/test"
"github.com/wittano/filebot/setting"
"os"
"testing"
"time"
)

func TestAddFileToObservable(t *testing.T) {
conf := createTestConfiguration(t)
conf := test.CreateTestConfiguration(t)

w := NewWatcher()
w.AddFilesToObservable(conf)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
},
}}
}

0 comments on commit 06f7304

Please sign in to comment.