-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(test): add tests for trash cron task
- Loading branch information
Showing
7 changed files
with
158 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters