Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions internal/testutility/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package testutility
import (
"flag"
"fmt"
"math/rand"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
Expand Down Expand Up @@ -105,14 +107,23 @@ func ValueIfOnWindows(win, or string) string {
return or
}

func fixedLengthTempDir(parent string) (string, error) {
n := rand.Int63n(1_000_000_000_000) //nolint:gosec // 10^12
suffix := fmt.Sprintf("%0*d", 12, n)

name := "osv-scanner-test-" + suffix
path := filepath.Join(parent, name)

return path, os.Mkdir(path, 0o700)
}

// CreateTestDir makes a temporary directory for use in testing that involves
// writing and reading files from disk, which is automatically cleaned up
// when testing finishes
func CreateTestDir(t *testing.T) string {
t.Helper()

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