diff --git a/cmd/osv-scanner/main_test.go b/cmd/osv-scanner/main_test.go index 2c701a546c3..3f3a7d362aa 100644 --- a/cmd/osv-scanner/main_test.go +++ b/cmd/osv-scanner/main_test.go @@ -62,7 +62,25 @@ func normalizeRootDirectory(t *testing.T, str string) string { return strings.ReplaceAll(str, cwd, "") } -// normalizeRootDirectory attempts to replace references to the temp directory +// normalizeUserCacheDirectory attempts to replace references to the current working +// directory with "", in order to reduce the noise of the cmp diff +func normalizeUserCacheDirectory(t *testing.T, str string) string { + t.Helper() + + cacheDir, err := os.UserCacheDir() + if err != nil { + t.Errorf("could not get user cache (%v) - results and diff might be inaccurate!", err) + } + + cacheDir = normalizeFilePaths(t, cacheDir) + + // file uris with Windows end up with three slashes, so we normalize that too + str = strings.ReplaceAll(str, "file:///"+cacheDir, "file://") + + return strings.ReplaceAll(str, cacheDir, "") +} + +// normalizeTempDirectory attempts to replace references to the temp directory // with "", to ensure tests pass across different OSs func normalizeTempDirectory(t *testing.T, str string) string { t.Helper() @@ -95,6 +113,7 @@ func normalizeStdStream(t *testing.T, std *bytes.Buffer) string { normalizeFilePaths, normalizeRootDirectory, normalizeTempDirectory, + normalizeUserCacheDirectory, normalizeErrors, } { str = normalizer(t, str) @@ -503,18 +522,19 @@ func TestRun_LocalDatabases(t *testing.T) { exit: 0, }, } + for _, tt := range tests { tt := tt t.Run(tt.name, func(t *testing.T) { t.Parallel() - testDir, cleanupTestDir := createTestDir(t) - defer cleanupTestDir() - - old := tt.args - - tt.args = []string{"", "--experimental-local-db-path", testDir} - tt.args = append(tt.args, old[1:]...) + if testutility.IsAcceptanceTest() { + testDir, cleanupTestDir := createTestDir(t) + defer cleanupTestDir() + old := tt.args + tt.args = []string{"", "--experimental-local-db-path", testDir} + tt.args = append(tt.args, old[1:]...) + } // run each test twice since they should provide the same output, // and the second run should be fast as the db is already available diff --git a/internal/local/check.go b/internal/local/check.go index 915ca240a1f..988c10d988e 100644 --- a/internal/local/check.go +++ b/internal/local/check.go @@ -73,10 +73,10 @@ func setupLocalDBDirectory(localDBPath string) (string, error) { } } - err = os.MkdirAll(path.Join(localDBPath, "osv-scanner"), 0750) - + altPath := path.Join(localDBPath, "osv-scanner") + err = os.MkdirAll(altPath, 0750) if err == nil { - return path.Join(localDBPath, "osv-scanner"), nil + return altPath, nil } // if we're implicitly picking a path, try the temp directory before giving up diff --git a/internal/sourceanalysis/rust_test.go b/internal/sourceanalysis/rust_test.go index caf0f10e3c5..a9fd8dabdd5 100644 --- a/internal/sourceanalysis/rust_test.go +++ b/internal/sourceanalysis/rust_test.go @@ -65,7 +65,7 @@ func Test_functionsFromDWARF(t *testing.T) { } func Test_rustBuildSource(t *testing.T) { - testutility.AcceptanceTests(t, "Requires rust toolchain to be installed") + testutility.SkipIfNotAcceptanceTesting(t, "Requires rust toolchain to be installed") t.Parallel() workingDir, err := os.Getwd() diff --git a/internal/testutility/utility.go b/internal/testutility/utility.go index ae544775674..e7b3395afa4 100644 --- a/internal/testutility/utility.go +++ b/internal/testutility/utility.go @@ -34,11 +34,17 @@ func Skip(t *testing.T, args ...any) { snaps.Skip(t, args...) } +// Access to environment variable that toggles acceptance testing execution paths +// Acceptance testing is "On" only when var set to "true" +func IsAcceptanceTest() bool { + return os.Getenv("TEST_ACCEPTANCE") == "true" +} + // AcceptanceTests marks this test function as a extended that require additional dependencies // automatically skipped unless running in a CI environment -func AcceptanceTests(t *testing.T, reason string) { +func SkipIfNotAcceptanceTesting(t *testing.T, reason string) { t.Helper() - if os.Getenv("TEST_ACCEPTANCE") != "true" { + if !IsAcceptanceTest() { Skip(t, "Skipping extended test: ", reason) } }