|
@@ -7,10 +7,7 @@
|
|
|
package versioner
|
|
|
|
|
|
import (
|
|
|
- "context"
|
|
|
"io/ioutil"
|
|
|
- "os"
|
|
|
- "path/filepath"
|
|
|
"testing"
|
|
|
"time"
|
|
|
|
|
@@ -18,76 +15,6 @@ import (
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
|
|
)
|
|
|
|
|
|
-func TestTrashcanCleanout(t *testing.T) {
|
|
|
- // Verify that files older than the cutoff are removed, that files newer
|
|
|
- // than the cutoff are *not* removed, and that empty directories are
|
|
|
- // removed (best effort).
|
|
|
-
|
|
|
- var testcases = []struct {
|
|
|
- file string
|
|
|
- shouldRemove bool
|
|
|
- }{
|
|
|
- {"testdata/.stversions/file1", false},
|
|
|
- {"testdata/.stversions/file2", true},
|
|
|
- {"testdata/.stversions/keep1/file1", false},
|
|
|
- {"testdata/.stversions/keep1/file2", false},
|
|
|
- {"testdata/.stversions/keep2/file1", false},
|
|
|
- {"testdata/.stversions/keep2/file2", true},
|
|
|
- {"testdata/.stversions/keep3/keepsubdir/file1", false},
|
|
|
- {"testdata/.stversions/remove/file1", true},
|
|
|
- {"testdata/.stversions/remove/file2", true},
|
|
|
- {"testdata/.stversions/remove/removesubdir/file1", true},
|
|
|
- }
|
|
|
-
|
|
|
- os.RemoveAll("testdata")
|
|
|
- defer os.RemoveAll("testdata")
|
|
|
-
|
|
|
- oldTime := time.Now().Add(-8 * 24 * time.Hour)
|
|
|
- for _, tc := range testcases {
|
|
|
- os.MkdirAll(filepath.Dir(tc.file), 0777)
|
|
|
- if err := ioutil.WriteFile(tc.file, []byte("data"), 0644); err != nil {
|
|
|
- t.Fatal(err)
|
|
|
- }
|
|
|
- if tc.shouldRemove {
|
|
|
- if err := os.Chtimes(tc.file, oldTime, oldTime); err != nil {
|
|
|
- t.Fatal(err)
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- cfg := config.FolderConfiguration{
|
|
|
- FilesystemType: fs.FilesystemTypeBasic,
|
|
|
- Path: "testdata",
|
|
|
- Versioning: config.VersioningConfiguration{
|
|
|
- Params: map[string]string{
|
|
|
- "cleanoutDays": "7",
|
|
|
- },
|
|
|
- },
|
|
|
- }
|
|
|
-
|
|
|
- versioner := newTrashcan(cfg).(*trashcan)
|
|
|
- if err := versioner.Clean(context.Background()); err != nil {
|
|
|
- t.Fatal(err)
|
|
|
- }
|
|
|
-
|
|
|
- for _, tc := range testcases {
|
|
|
- _, err := os.Lstat(tc.file)
|
|
|
- if tc.shouldRemove && !os.IsNotExist(err) {
|
|
|
- t.Error(tc.file, "should have been removed")
|
|
|
- } else if !tc.shouldRemove && err != nil {
|
|
|
- t.Error(tc.file, "should not have been removed")
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if _, err := os.Lstat("testdata/.stversions/keep3"); os.IsNotExist(err) {
|
|
|
- t.Error("directory with non empty subdirs should not be removed")
|
|
|
- }
|
|
|
-
|
|
|
- if _, err := os.Lstat("testdata/.stversions/remove"); !os.IsNotExist(err) {
|
|
|
- t.Error("empty directory should have been removed")
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
func TestTrashcanArchiveRestoreSwitcharoo(t *testing.T) {
|
|
|
// This tests that trashcan versioner restoration correctly archives existing file, because trashcan versioner
|
|
|
// files are untagged, archiving existing file to replace with a restored version technically should collide in
|