From 230a677c74996854f3f793fabcada45cc03d81f7 Mon Sep 17 00:00:00 2001 From: emilylange Date: Mon, 17 Jun 2024 17:43:08 +0200 Subject: [PATCH] test(dump): don't depend on directory listing order cmd/dump.go uses os.Readdir to list the directory. This is fine on its own, but TestAddRecursiveExclude in cmd/dump_test.go depends on the order of the directory listing, which is where the issue lays. Directory listings using os.Readdir (lstat) don't actually guarantee an order. They can differ due to a number of factors. Most notably the OS, file system and settings. As such, the test should not check the /order of the files/ added to the archive, but instead simply check whether the archive /contains/ them. So this is precisely what this commit does. Note that only TestAddRecursiveExclude/File_inside_directory/No_exclude has been observed to fail due to this, but all TestAddRecursiveExclude subtests have been updated for consistency. --- cmd/dump_test.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/cmd/dump_test.go b/cmd/dump_test.go index 7b83c70f0..d33619bab 100644 --- a/cmd/dump_test.go +++ b/cmd/dump_test.go @@ -50,7 +50,7 @@ func TestAddRecursiveExclude(t *testing.T) { err = addRecursiveExclude(archiver, "", dir, nil, false) assert.NoError(t, err) assert.Len(t, archiver.addedFiles, 1) - assert.EqualValues(t, "example", archiver.addedFiles[0]) + assert.Contains(t, archiver.addedFiles, "example") }) t.Run("With exclude", func(t *testing.T) { @@ -77,11 +77,11 @@ func TestAddRecursiveExclude(t *testing.T) { err = addRecursiveExclude(archiver, "", dir, nil, false) assert.NoError(t, err) assert.Len(t, archiver.addedFiles, 5) - assert.EqualValues(t, "deep", archiver.addedFiles[0]) - assert.EqualValues(t, "deep/nested", archiver.addedFiles[1]) - assert.EqualValues(t, "deep/nested/folder", archiver.addedFiles[2]) - assert.EqualValues(t, "deep/nested/folder/example", archiver.addedFiles[3]) - assert.EqualValues(t, "deep/nested/folder/another-file", archiver.addedFiles[4]) + assert.Contains(t, archiver.addedFiles, "deep") + assert.Contains(t, archiver.addedFiles, "deep/nested") + assert.Contains(t, archiver.addedFiles, "deep/nested/folder") + assert.Contains(t, archiver.addedFiles, "deep/nested/folder/example") + assert.Contains(t, archiver.addedFiles, "deep/nested/folder/another-file") }) t.Run("Exclude first directory", func(t *testing.T) { @@ -98,8 +98,8 @@ func TestAddRecursiveExclude(t *testing.T) { err = addRecursiveExclude(archiver, "", dir, []string{dir + "/deep/nested/folder"}, false) assert.NoError(t, err) assert.Len(t, archiver.addedFiles, 2) - assert.EqualValues(t, "deep", archiver.addedFiles[0]) - assert.EqualValues(t, "deep/nested", archiver.addedFiles[1]) + assert.Contains(t, archiver.addedFiles, "deep") + assert.Contains(t, archiver.addedFiles, "deep/nested") }) t.Run("Exclude file", func(t *testing.T) { @@ -108,10 +108,10 @@ func TestAddRecursiveExclude(t *testing.T) { err = addRecursiveExclude(archiver, "", dir, []string{dir + "/deep/nested/folder/example"}, false) assert.NoError(t, err) assert.Len(t, archiver.addedFiles, 4) - assert.EqualValues(t, "deep", archiver.addedFiles[0]) - assert.EqualValues(t, "deep/nested", archiver.addedFiles[1]) - assert.EqualValues(t, "deep/nested/folder", archiver.addedFiles[2]) - assert.EqualValues(t, "deep/nested/folder/another-file", archiver.addedFiles[3]) + assert.Contains(t, archiver.addedFiles, "deep") + assert.Contains(t, archiver.addedFiles, "deep/nested") + assert.Contains(t, archiver.addedFiles, "deep/nested/folder") + assert.Contains(t, archiver.addedFiles, "deep/nested/folder/another-file") }) }) }