Skip to content

Commit 614e266

Browse files
authored
test: use a fixed length name for temp directories (#2544)
Most of the time `os.MkdirTemp` creates directories with the same number of characters, but its not required to and will occasionally has one less character than what is common, which can result in the table output being slightly different. This is compounded by us replacing the temp directory with a fixed string `<tempdir>` marker, as it ends up looking like the table marks are just randomly changing for no reason 🤦
1 parent a796e33 commit 614e266

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

internal/testutility/utility.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package testutility
33
import (
44
"flag"
55
"fmt"
6+
"math/rand"
67
"os"
8+
"path/filepath"
79
"runtime"
810
"strings"
911
"testing"
@@ -105,14 +107,23 @@ func ValueIfOnWindows(win, or string) string {
105107
return or
106108
}
107109

110+
func fixedLengthTempDir(parent string) (string, error) {
111+
n := rand.Int63n(1_000_000_000_000) //nolint:gosec // 10^12
112+
suffix := fmt.Sprintf("%0*d", 12, n)
113+
114+
name := "osv-scanner-test-" + suffix
115+
path := filepath.Join(parent, name)
116+
117+
return path, os.Mkdir(path, 0o700)
118+
}
119+
108120
// CreateTestDir makes a temporary directory for use in testing that involves
109121
// writing and reading files from disk, which is automatically cleaned up
110122
// when testing finishes
111123
func CreateTestDir(t *testing.T) string {
112124
t.Helper()
113125

114-
//nolint:usetesting // we need to customize the directory name to replace in snapshots
115-
p, err := os.MkdirTemp("", "osv-scanner-test-*")
126+
p, err := fixedLengthTempDir(os.TempDir())
116127
if err != nil {
117128
t.Fatalf("could not create test directory: %v", err)
118129
}

0 commit comments

Comments
 (0)