Repository: syncthing/syncthing
File: lib/versioner/versioner_test.go (Go)

Code before:
	"time"

	"github.com/syncthing/syncthing/lib/config"
	"github.com/syncthing/syncthing/lib/fs"
)

func TestVersionerCleanOut(t *testing.T) {
	simpleCfg := config.FolderConfiguration{
		FilesystemType: fs.FilesystemTypeBasic,
		Path:           "testdata",
		Versioning: config.VersioningConfiguration{
			Params: map[string]string{
				"cleanoutDays": "7",
			},
		},
	}

	trashCfg := config.FolderConfiguration{
		FilesystemType: fs.FilesystemTypeBasic,
		Path:           "testdata",
		Versioning: config.VersioningConfiguration{
			Params: map[string]string{
				"cleanoutDays": "7",
			},
		},
	}
	testCasesVersioner := []Versioner{
		newSimple(simpleCfg),
		newTrashcan(trashCfg).(*trashcan),
	}

	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},
	}

	for index, versioner := range testCasesVersioner {
		t.Run(fmt.Sprintf("versioner trashcan clean up %d in %d", index, len(testCasesVersioner)), func(t *testing.T) {
			os.RemoveAll("testdata")
			defer os.RemoveAll("testdata")

Review comment (suggestion):
Either I'm blind or this is an exact copy of `simpleCfg` and thus there' shouldn't be any need to have two separate ones.

Code after:
	"time"

	"github.com/syncthing/syncthing/lib/config"
	"github.com/syncthing/syncthing/lib/fs"
)

func TestVersionerCleanOut(t *testing.T) {
	cfg := config.FolderConfiguration{
		FilesystemType: fs.FilesystemTypeBasic,
		Path:           "testdata",
		Versioning: config.VersioningConfiguration{
			Params: map[string]string{
				"cleanoutDays": "7",
			},
		},
	}

	testCasesVersioner := map[string]Versioner{
		"simple":   newSimple(cfg),
		"trashcan": newTrashcan(cfg),
	}

	var testcases = map[string]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,
	}

	for versionerType, versioner := range testCasesVersioner {
		t.Run(fmt.Sprintf("%v versioner trashcan clean up", versionerType), func(t *testing.T) {
			os.RemoveAll("testdata")
			defer os.RemoveAll("testdata")

			oldTime := time.Now().Add(-8 * 24 * time.Hour)
			for file, shouldRemove := range testcases {
				os.MkdirAll(filepath.Dir(file), 0777)
				if err := ioutil.WriteFile(file, []byte("data"), 0644); err != nil {
					t.Fatal(err)
				}
				if shouldRemove {
					if err := os.Chtimes(file, oldTime, oldTime); err != nil {
						t.Fatal(err)
					}
				}