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

Code before:
		"test~20160415-135830", // 1 minute 30 seconds ago
		"test~20160415-135829", // 1 minute 31 seconds ago
		"test~20160415-135700", // 3 minutes ago
		"test~20160415-135630", // 3 minutes 30 seconds ago
		"test~20160415-133000", // 30 minutes ago
		"test~20160415-132900", // 31 minutes ago
		"test~20160415-132500", // 35 minutes ago
		"test~20160415-132000", // 40 minutes ago
		"test~20160415-130000", // 60 minutes ago
		"test~20160415-124000", // 80 minutes ago
		"test~20160415-122000", // 100 minutes ago
		"test~20160415-110000", // 120 minutes ago
	}
	sort.Strings(files)

	delete := []string{
		"test~20160415-140000", // 0 seconds ago
		"test~20160415-135959", // 1 second ago
		"test~20160415-135900", // 1 minute ago
		"test~20160415-135830", // 1 minute 30 second ago
		"test~20160415-130000", // 60 minutes ago
		"test~20160415-124000", // 80 minutes ago
	}
	sort.Strings(delete)

	os.MkdirAll("testdata/.stversions", 0755)
	defer os.RemoveAll("testdata")

	v := NewStaggered("", fs.NewFilesystem(fs.FilesystemTypeBasic, "testdata"), map[string]string{"maxAge": strconv.Itoa(365 * 86400)}).(*Staggered)
	v.testCleanDone = make(chan struct{})
	defer v.Stop()
	go v.Serve()

	<-v.testCleanDone

	versionsWithMtime := make([]versionWithMtime, 0, len(files))
	for _, file := range files {
		versionTime, err := time.ParseInLocation(TimeFormat, ExtractTag(file), loc)
		if err != nil {
			t.Fatal(err)
		}
		versionsWithMtime = append(versionsWithMtime, versionWithMtime{
			name:  file,
			mtime: versionTime,
		})
	}

	rem := v.toRemove(versionsWithMtime, now)
	if diff, equal := messagediff.PrettyDiff(delete, rem); !equal {
		t.Errorf("Incorrect deleted files; got %v, expected %v\n%v", rem, delete, diff)
	}

Review comment (refactor):
This test should be rewritten. There is no need to run the versioner at all to just call toRemove, and it's now quite confusing that we extract the mtime from the time when we explicitly don't act like that on the info in the tag. (Sorry for pushing back on this; it's not your fault the test is what it is, but this makes it worse.)

Code after:
		{"test~20160415-135700", parseTime("20160415-135700")}, // 3 minutes ago
		{"test~20160415-135630", parseTime("20160415-135630")}, // 3 minutes 30 seconds ago
		{"test~20160415-133000", parseTime("20160415-133000")}, // 30 minutes ago
		{"test~20160415-132900", parseTime("20160415-132900")}, // 31 minutes ago
		{"test~20160415-132500", parseTime("20160415-132500")}, // 35 minutes ago
		{"test~20160415-132000", parseTime("20160415-132000")}, // 40 minutes ago
		{"test~20160415-130000", parseTime("20160415-130000")}, // 60 minutes ago
		{"test~20160415-124000", parseTime("20160415-124000")}, // 80 minutes ago
		{"test~20160415-122000", parseTime("20160415-122000")}, // 100 minutes ago
		{"test~20160415-110000", parseTime("20160415-110000")}, // 120 minutes ago
	}

	delete := []string{
		"test~20160415-140000", // 0 seconds ago
		"test~20160415-135959", // 1 second ago
		"test~20160415-135900", // 1 minute ago
		"test~20160415-135830", // 1 minute 30 second ago
		"test~20160415-130000", // 60 minutes ago
		"test~20160415-124000", // 80 minutes ago
	}
	sort.Strings(delete)

	v := NewStaggered("", fs.NewFilesystem(fs.FilesystemTypeFake, "testdata"), map[string]string{
		"maxAge": strconv.Itoa(365 * 86400),
	}).(*Staggered)
	rem := v.toRemove(versionsWithMtime, now)
	sort.Strings(rem)

	if diff, equal := messagediff.PrettyDiff(delete, rem); !equal {
		t.Errorf("Incorrect deleted files; got %v, expected %v\n%v", rem, delete, diff)
	}
}

func parseTime(in string) time.Time {
	t, err := time.ParseInLocation(TimeFormat, in, time.Local)
	if err != nil {
		panic(err.Error())
	}
	return t
}