112 lines
3 KiB
Go
112 lines
3 KiB
Go
|
package archiver
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"os"
|
||
|
"path/filepath"
|
||
|
"testing"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func TestFileArchiver(t *testing.T) {
|
||
|
tmpPath, err := os.MkdirTemp("", ".filearchiver_test")
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
defer os.RemoveAll(tmpPath)
|
||
|
|
||
|
conf := Config{
|
||
|
Folder: tmpPath,
|
||
|
ArchiveFolder: "",
|
||
|
}
|
||
|
f := NewFileArchiver(&conf)
|
||
|
|
||
|
okFile := filepath.Join(tmpPath, "ok.txt")
|
||
|
_ = os.WriteFile(okFile, []byte("test"), 0644)
|
||
|
f(okFile, nil)
|
||
|
|
||
|
_, err = os.Stat(okFile)
|
||
|
if !os.IsNotExist(err) {
|
||
|
t.Errorf("file %s still exists", okFile)
|
||
|
}
|
||
|
archivedPath := filepath.Join(tmpPath, ArchiveFolder, time.Now().Format(_YEARMONTHONLY), "ok.txt")
|
||
|
_, err = os.Stat(archivedPath)
|
||
|
if os.IsNotExist(err) {
|
||
|
t.Errorf("file %s does not Exist", archivedPath)
|
||
|
}
|
||
|
|
||
|
failFile := filepath.Join(tmpPath, "fail.txt")
|
||
|
_ = os.WriteFile(failFile, []byte("test"), 0644)
|
||
|
f(failFile, fmt.Errorf("some failure"))
|
||
|
|
||
|
_, err = os.Stat(failFile)
|
||
|
if !os.IsNotExist(err) {
|
||
|
t.Errorf("file %s still exists", failFile)
|
||
|
}
|
||
|
_, err = os.Stat(filepath.Join(tmpPath, ArchiveFolder, time.Now().Format(_YEARMONTHONLY), "fail.txt"))
|
||
|
if !os.IsNotExist(err) {
|
||
|
t.Errorf("file %s is in Archive", failFile)
|
||
|
}
|
||
|
erroredPath := filepath.Join(tmpPath, ErrorFolder, "fail.txt")
|
||
|
_, err = os.Stat(erroredPath)
|
||
|
if os.IsNotExist(err) {
|
||
|
t.Errorf("file %s does not Exist", erroredPath)
|
||
|
}
|
||
|
_, err = os.Stat(filepath.Join(tmpPath, ErrorFolder, "fail.txt.err"))
|
||
|
if os.IsNotExist(err) {
|
||
|
t.Errorf("file fail.txt.err is not in Error")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestGlobbedArchiver(t *testing.T) {
|
||
|
tmpPath, err := os.MkdirTemp("", ".filearchiver_test")
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
defer os.RemoveAll(tmpPath)
|
||
|
|
||
|
conf := Config{
|
||
|
Folder: filepath.Join(tmpPath, "*"),
|
||
|
ArchiveFolder: "",
|
||
|
}
|
||
|
f := NewFileArchiver(&conf)
|
||
|
|
||
|
_ = os.MkdirAll(filepath.Join(tmpPath, "foo"), os.ModePerm)
|
||
|
okFile := filepath.Join(tmpPath, "foo", "ok.txt")
|
||
|
_ = os.WriteFile(okFile, []byte("test"), 0644)
|
||
|
f(okFile, nil)
|
||
|
|
||
|
_, err = os.Stat(okFile)
|
||
|
if !os.IsNotExist(err) {
|
||
|
t.Errorf("file %s still exists", okFile)
|
||
|
}
|
||
|
archivedPath := filepath.Join(tmpPath, ArchiveFolder, "foo", time.Now().Format(_YEARMONTHONLY), "ok.txt")
|
||
|
_, err = os.Stat(archivedPath)
|
||
|
if os.IsNotExist(err) {
|
||
|
t.Errorf("file %s does not Exist", archivedPath)
|
||
|
}
|
||
|
|
||
|
_ = os.MkdirAll(filepath.Join(tmpPath, "bar"), os.ModePerm)
|
||
|
failFile := filepath.Join(tmpPath, "bar", "fail.txt")
|
||
|
_ = os.WriteFile(failFile, []byte("test"), 0644)
|
||
|
f(failFile, fmt.Errorf("some failure"))
|
||
|
|
||
|
_, err = os.Stat(failFile)
|
||
|
if !os.IsNotExist(err) {
|
||
|
t.Errorf("file %s still exists", failFile)
|
||
|
}
|
||
|
_, err = os.Stat(filepath.Join(tmpPath, ArchiveFolder, "bar", time.Now().Format(_YEARMONTHONLY), "fail.txt"))
|
||
|
if !os.IsNotExist(err) {
|
||
|
t.Errorf("file %s is in Archive", failFile)
|
||
|
}
|
||
|
erroredPath := filepath.Join(tmpPath, ErrorFolder, "bar", "fail.txt")
|
||
|
_, err = os.Stat(erroredPath)
|
||
|
if os.IsNotExist(err) {
|
||
|
t.Errorf("file %s does not Exist", erroredPath)
|
||
|
}
|
||
|
_, err = os.Stat(filepath.Join(tmpPath, ErrorFolder, "bar", "fail.txt.err"))
|
||
|
if os.IsNotExist(err) {
|
||
|
t.Errorf("file fail.txt.err is not in Error")
|
||
|
}
|
||
|
}
|