TempDir creates a temporary directory for testing within the test run directory. It automatically cleans up the directory when the test completes. This replaces the use of os.MkdirTemp or t.TempDir() to ensure all test artifacts are isolated in a known location outside any git repository.
(t *testing.T, pattern string)
| 51 | // This replaces the use of os.MkdirTemp or t.TempDir() to ensure all test |
| 52 | // artifacts are isolated in a known location outside any git repository. |
| 53 | func TempDir(t *testing.T, pattern string) string { |
| 54 | t.Helper() |
| 55 | |
| 56 | baseDir := GetTestRunDir() |
| 57 | |
| 58 | // Create a unique subdirectory within the test run directory |
| 59 | tempDir, err := os.MkdirTemp(baseDir, pattern) |
| 60 | if err != nil { |
| 61 | t.Fatalf("failed to create temp directory: %v", err) |
| 62 | } |
| 63 | |
| 64 | // Register cleanup to remove the directory after test completes |
| 65 | t.Cleanup(func() { |
| 66 | if err := os.RemoveAll(tempDir); err != nil { |
| 67 | t.Logf("Warning: failed to clean up temporary directory %s: %v", tempDir, err) |
| 68 | } |
| 69 | }) |
| 70 | |
| 71 | return tempDir |
| 72 | } |
| 73 | |
| 74 | // CaptureStderr runs fn and returns everything written to os.Stderr during its execution. |
| 75 | // It restores os.Stderr automatically via t.Cleanup. |