Skip to content

Commit c16e048

Browse files
authored
Merge pull request #635 from jfly/fix-flaky-tests
chore: fix flaky tests
2 parents c24e1f1 + 718fbbb commit c16e048

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

cmd/root_test.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,7 +1382,6 @@ func TestGit(t *testing.T) {
13821382
as.NoError(gitCmd.Run(), "failed to add everything to the index")
13831383

13841384
treefmt(t,
1385-
withConfig(configPath, cfg),
13861385
withNoError(t),
13871386
withStats(t, map[stats.Type]int{
13881387
stats.Traversed: 33,
@@ -1401,7 +1400,6 @@ func TestGit(t *testing.T) {
14011400
})
14021401

14031402
treefmt(t,
1404-
withConfig(configPath, cfg),
14051403
withNoError(t),
14061404
withStats(t, map[stats.Type]int{
14071405
stats.Traversed: 33,
@@ -1417,7 +1415,6 @@ func TestGit(t *testing.T) {
14171415
// we should traverse and match against fewer files, but no formatting should occur as no formatting signatures
14181416
// are impacted
14191417
treefmt(t,
1420-
withConfig(configPath, cfg),
14211418
withNoError(t),
14221419
withStats(t, map[stats.Type]int{
14231420
stats.Traversed: 30,
@@ -1436,7 +1433,6 @@ func TestGit(t *testing.T) {
14361433
// traverse 82 files.
14371434
treefmt(t,
14381435
withArgs("--walk", "filesystem"),
1439-
withConfig(configPath, cfg),
14401436
withNoError(t),
14411437
withStats(t, map[stats.Type]int{
14421438
stats.Traversed: 82,
@@ -1605,7 +1601,6 @@ func TestJujutsu(t *testing.T) {
16051601
})
16061602

16071603
treefmt(t,
1608-
withConfig(configPath, cfg),
16091604
withNoError(t),
16101605
withStats(t, map[stats.Type]int{
16111606
stats.Traversed: 33,
@@ -1625,7 +1620,6 @@ func TestJujutsu(t *testing.T) {
16251620
// we should traverse and match against fewer files, but no formatting should occur as no formatting signatures
16261621
// are impacted
16271622
treefmt(t,
1628-
withConfig(configPath, cfg),
16291623
withNoError(t),
16301624
withStats(t, map[stats.Type]int{
16311625
stats.Traversed: 30,
@@ -1644,7 +1638,6 @@ func TestJujutsu(t *testing.T) {
16441638
// traverse 130 files.
16451639
treefmt(t,
16461640
withArgs("--walk", "filesystem"),
1647-
withConfig(configPath, cfg),
16481641
withNoError(t),
16491642
withStats(t, map[stats.Type]int{
16501643
stats.Traversed: 133,

test/test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ func SetenvXdgConfigDir(t *testing.T) {
2828
func WriteConfig(t *testing.T, path string, cfg *config.Config) {
2929
t.Helper()
3030

31+
oldInfo, err := os.Lstat(path)
32+
33+
switch {
34+
case os.IsNotExist(err):
35+
// It's fine if there was no old config file, the new file is guaranteed to appear new =)
36+
oldInfo = nil
37+
case err != nil:
38+
t.Fatalf("failed to stat old config file: %v", path)
39+
}
40+
3141
f, err := os.Create(path)
3242
if err != nil {
3343
t.Fatalf("failed to create a new config file: %v", err)
@@ -37,6 +47,30 @@ func WriteConfig(t *testing.T, path string, cfg *config.Config) {
3747
if err = encoder.Encode(cfg); err != nil {
3848
t.Fatalf("failed to write to config file: %v", err)
3949
}
50+
51+
// Ensure the modtime of the config file always increases
52+
// (even if this requires setting it to the future!)
53+
// If we don't do this, we end up with flaky tests that behave differently
54+
// depending on if they run quickly enough for the modtime to stay constant throughout the test.
55+
newInfo, err := os.Lstat(path)
56+
if err != nil {
57+
t.Fatalf("failed to create a new config file: %v", err)
58+
}
59+
// Note: we're comparing `Unix()` (which is only 1 second granularity) for consistency with
60+
// `walk.go::formatSignature`.
61+
if oldInfo != nil && oldInfo.ModTime().Unix() == newInfo.ModTime().Unix() {
62+
// Ideally we wouldn't change the atime at all, but it's hard to fetch
63+
// the original atime in a cross-platform manner.
64+
newAtime := time.Now()
65+
66+
// Increase the mtime so it's different.
67+
newMtime := oldInfo.ModTime().Add(time.Second)
68+
69+
err = Lutimes(t, path, newAtime, newMtime)
70+
if err != nil {
71+
t.Fatalf("failed to change file times: %v", err)
72+
}
73+
}
4074
}
4175

4276
func TempExamples(t *testing.T) string {

0 commit comments

Comments
 (0)