Skip to content

Commit

Permalink
Added a switch for using cached local db in test to improve speed (#826)
Browse files Browse the repository at this point in the history
regarding : #741

As discussed with google team, moving to using cached local db when not
running acceptance tests.

---------

Co-authored-by: Gareth Jones <[email protected]>
  • Loading branch information
billielynch and G-Rath authored Feb 28, 2024
1 parent aa771d2 commit c78b38b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
36 changes: 28 additions & 8 deletions cmd/osv-scanner/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,25 @@ func normalizeRootDirectory(t *testing.T, str string) string {
return strings.ReplaceAll(str, cwd, "<rootdir>")
}

// normalizeRootDirectory attempts to replace references to the temp directory
// normalizeUserCacheDirectory attempts to replace references to the current working
// directory with "<tempdir>", 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://<tempdir>")

return strings.ReplaceAll(str, cacheDir, "<tempdir>")
}

// normalizeTempDirectory attempts to replace references to the temp directory
// with "<tempdir>", to ensure tests pass across different OSs
func normalizeTempDirectory(t *testing.T, str string) string {
t.Helper()
Expand Down Expand Up @@ -95,6 +113,7 @@ func normalizeStdStream(t *testing.T, std *bytes.Buffer) string {
normalizeFilePaths,
normalizeRootDirectory,
normalizeTempDirectory,
normalizeUserCacheDirectory,
normalizeErrors,
} {
str = normalizer(t, str)
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions internal/local/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/sourceanalysis/rust_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
10 changes: 8 additions & 2 deletions internal/testutility/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down

0 comments on commit c78b38b

Please sign in to comment.