From d9125497d74d7133321f88bd9c03ca4dcac11754 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Mon, 16 Jun 2025 12:06:06 +0200 Subject: [PATCH 1/3] assert/tests: enable parallel testing for Test{,No}{File,Dir}Exists In package assert, fix TestFileExists, TestNoFileExists, TestDirExists, TestNoDirExists to be able to run in parallel: - use t.TempDir() as the storage location for temporary created symlinks. This also allows the cleanup of that storage to be automatically handled by "go test". testing.T.TempDir is available since go1.15. - enable parallel testing on each test --- assert/assertions_test.go | 76 ++++++++------------------------------- 1 file changed, 15 insertions(+), 61 deletions(-) diff --git a/assert/assertions_test.go b/assert/assertions_test.go index fc0f215b5..7e626f512 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -2579,7 +2579,7 @@ func TestNotZero(t *testing.T) { } func TestFileExists(t *testing.T) { - // FIXME t.Parallel() + t.Parallel() mockT := new(testing.T) True(t, FileExists(mockT, "assertions.go")) @@ -2590,32 +2590,23 @@ func TestFileExists(t *testing.T) { mockT = new(testing.T) False(t, FileExists(mockT, "../_codegen")) - var tempFiles []string - - link, err := getTempSymlinkPath("assertions.go") + link, err := getTempSymlinkPath(t, "assertions.go") if err != nil { t.Fatal("could not create temp symlink, err:", err) } - tempFiles = append(tempFiles, link) mockT = new(testing.T) True(t, FileExists(mockT, link)) - link, err = getTempSymlinkPath("non_existent_file") + link, err = getTempSymlinkPath(t, "non_existent_file") if err != nil { t.Fatal("could not create temp symlink, err:", err) } - tempFiles = append(tempFiles, link) mockT = new(testing.T) True(t, FileExists(mockT, link)) - - errs := cleanUpTempFiles(tempFiles) - if len(errs) > 0 { - t.Fatal("could not clean up temporary files") - } } func TestNoFileExists(t *testing.T) { - // FIXME t.Parallel() + t.Parallel() mockT := new(testing.T) False(t, NoFileExists(mockT, "assertions.go")) @@ -2626,49 +2617,30 @@ func TestNoFileExists(t *testing.T) { mockT = new(testing.T) True(t, NoFileExists(mockT, "../_codegen")) - var tempFiles []string - - link, err := getTempSymlinkPath("assertions.go") + link, err := getTempSymlinkPath(t, "assertions.go") if err != nil { t.Fatal("could not create temp symlink, err:", err) } - tempFiles = append(tempFiles, link) mockT = new(testing.T) False(t, NoFileExists(mockT, link)) - link, err = getTempSymlinkPath("non_existent_file") + link, err = getTempSymlinkPath(t, "non_existent_file") if err != nil { t.Fatal("could not create temp symlink, err:", err) } - tempFiles = append(tempFiles, link) mockT = new(testing.T) False(t, NoFileExists(mockT, link)) - - errs := cleanUpTempFiles(tempFiles) - if len(errs) > 0 { - t.Fatal("could not clean up temporary files") - } } -func getTempSymlinkPath(file string) (string, error) { - link := file + "_symlink" +func getTempSymlinkPath(t *testing.T, file string) (string, error) { + tempDir := t.TempDir() + link := filepath.Join(tempDir, file+"_symlink") err := os.Symlink(file, link) return link, err } -func cleanUpTempFiles(paths []string) []error { - var res []error - for _, path := range paths { - err := os.Remove(path) - if err != nil { - res = append(res, err) - } - } - return res -} - func TestDirExists(t *testing.T) { - // FIXME t.Parallel() + t.Parallel() mockT := new(testing.T) False(t, DirExists(mockT, "assertions.go")) @@ -2679,32 +2651,23 @@ func TestDirExists(t *testing.T) { mockT = new(testing.T) True(t, DirExists(mockT, "../_codegen")) - var tempFiles []string - - link, err := getTempSymlinkPath("assertions.go") + link, err := getTempSymlinkPath(t, "assertions.go") if err != nil { t.Fatal("could not create temp symlink, err:", err) } - tempFiles = append(tempFiles, link) mockT = new(testing.T) False(t, DirExists(mockT, link)) - link, err = getTempSymlinkPath("non_existent_dir") + link, err = getTempSymlinkPath(t, "non_existent_dir") if err != nil { t.Fatal("could not create temp symlink, err:", err) } - tempFiles = append(tempFiles, link) mockT = new(testing.T) False(t, DirExists(mockT, link)) - - errs := cleanUpTempFiles(tempFiles) - if len(errs) > 0 { - t.Fatal("could not clean up temporary files") - } } func TestNoDirExists(t *testing.T) { - // FIXME t.Parallel() + t.Parallel() mockT := new(testing.T) True(t, NoDirExists(mockT, "assertions.go")) @@ -2715,28 +2678,19 @@ func TestNoDirExists(t *testing.T) { mockT = new(testing.T) False(t, NoDirExists(mockT, "../_codegen")) - var tempFiles []string - - link, err := getTempSymlinkPath("assertions.go") + link, err := getTempSymlinkPath(t, "assertions.go") if err != nil { t.Fatal("could not create temp symlink, err:", err) } - tempFiles = append(tempFiles, link) mockT = new(testing.T) True(t, NoDirExists(mockT, link)) - link, err = getTempSymlinkPath("non_existent_dir") + link, err = getTempSymlinkPath(t, "non_existent_dir") if err != nil { t.Fatal("could not create temp symlink, err:", err) } - tempFiles = append(tempFiles, link) mockT = new(testing.T) True(t, NoDirExists(mockT, link)) - - errs := cleanUpTempFiles(tempFiles) - if len(errs) > 0 { - t.Fatal("could not clean up temporary files") - } } func TestJSONEq_EqualSONString(t *testing.T) { From 50277a850b12d5f24761afbe0eb5b56e5a035782 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Mon, 16 Jun 2025 12:22:09 +0200 Subject: [PATCH 2/3] assert/tests: simplify Test{No,}{File,Dir}Exists In tests of the assert package, move more logic from each test into the helper getTempSymlinkPath. --- assert/assertions_test.go | 50 ++++++++++++--------------------------- 1 file changed, 15 insertions(+), 35 deletions(-) diff --git a/assert/assertions_test.go b/assert/assertions_test.go index 7e626f512..6b5366e83 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -2590,17 +2590,11 @@ func TestFileExists(t *testing.T) { mockT = new(testing.T) False(t, FileExists(mockT, "../_codegen")) - link, err := getTempSymlinkPath(t, "assertions.go") - if err != nil { - t.Fatal("could not create temp symlink, err:", err) - } + link := getTempSymlinkPath(t, "assertions.go") mockT = new(testing.T) True(t, FileExists(mockT, link)) - link, err = getTempSymlinkPath(t, "non_existent_file") - if err != nil { - t.Fatal("could not create temp symlink, err:", err) - } + link = getTempSymlinkPath(t, "non_existent_file") mockT = new(testing.T) True(t, FileExists(mockT, link)) } @@ -2617,26 +2611,24 @@ func TestNoFileExists(t *testing.T) { mockT = new(testing.T) True(t, NoFileExists(mockT, "../_codegen")) - link, err := getTempSymlinkPath(t, "assertions.go") - if err != nil { - t.Fatal("could not create temp symlink, err:", err) - } + link := getTempSymlinkPath(t, "assertions.go") mockT = new(testing.T) False(t, NoFileExists(mockT, link)) - link, err = getTempSymlinkPath(t, "non_existent_file") - if err != nil { - t.Fatal("could not create temp symlink, err:", err) - } + link = getTempSymlinkPath(t, "non_existent_file") mockT = new(testing.T) False(t, NoFileExists(mockT, link)) } -func getTempSymlinkPath(t *testing.T, file string) (string, error) { +func getTempSymlinkPath(t *testing.T, file string) string { + t.Helper() + tempDir := t.TempDir() link := filepath.Join(tempDir, file+"_symlink") - err := os.Symlink(file, link) - return link, err + if err := os.Symlink(file, link); err != nil { + t.Fatal("could not create temp symlink, err:", err) + } + return link } func TestDirExists(t *testing.T) { @@ -2651,17 +2643,11 @@ func TestDirExists(t *testing.T) { mockT = new(testing.T) True(t, DirExists(mockT, "../_codegen")) - link, err := getTempSymlinkPath(t, "assertions.go") - if err != nil { - t.Fatal("could not create temp symlink, err:", err) - } + link := getTempSymlinkPath(t, "assertions.go") mockT = new(testing.T) False(t, DirExists(mockT, link)) - link, err = getTempSymlinkPath(t, "non_existent_dir") - if err != nil { - t.Fatal("could not create temp symlink, err:", err) - } + link = getTempSymlinkPath(t, "non_existent_dir") mockT = new(testing.T) False(t, DirExists(mockT, link)) } @@ -2678,17 +2664,11 @@ func TestNoDirExists(t *testing.T) { mockT = new(testing.T) False(t, NoDirExists(mockT, "../_codegen")) - link, err := getTempSymlinkPath(t, "assertions.go") - if err != nil { - t.Fatal("could not create temp symlink, err:", err) - } + link := getTempSymlinkPath(t, "assertions.go") mockT = new(testing.T) True(t, NoDirExists(mockT, link)) - link, err = getTempSymlinkPath(t, "non_existent_dir") - if err != nil { - t.Fatal("could not create temp symlink, err:", err) - } + link = getTempSymlinkPath(t, "non_existent_dir") mockT = new(testing.T) True(t, NoDirExists(mockT, link)) } From 44c0281fe0e044a759afd2921efb60eacea594b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Mon, 16 Jun 2025 12:25:47 +0200 Subject: [PATCH 3/3] assert/tests: improve failure reporting in Test{No,}{File,Dir}Exists Improve error reporting in getTempSymlinkPath by displaying the file paths. --- assert/assertions_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assert/assertions_test.go b/assert/assertions_test.go index 6b5366e83..298b685d6 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -2626,7 +2626,7 @@ func getTempSymlinkPath(t *testing.T, file string) string { tempDir := t.TempDir() link := filepath.Join(tempDir, file+"_symlink") if err := os.Symlink(file, link); err != nil { - t.Fatal("could not create temp symlink, err:", err) + t.Fatalf("could not create temp symlink %q pointing to %q: %v", link, file, err) } return link }