Skip to content

Commit bac1f27

Browse files
committed
add test for osv-scan parser not erroring when receiving an empty file
1 parent 88728fc commit bac1f27

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

components/scanners/osv-scanner/internal/transformer/testdata/empty.json

Whitespace-only changes.

components/scanners/osv-scanner/internal/transformer/transformer.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,12 @@ func (b *OSVScannerTransformer) Transform(ctx context.Context) ([]*ocsf.Vulnerab
121121
if err != nil {
122122
return nil, errors.Errorf("could not read file %s", b.rawOutFile)
123123
}
124+
124125
if len(fileContents) == 0 {
125126
logger.Info("Scanner SARIF file is empty, exiting")
126127
return []*ocsf.VulnerabilityFinding{}, nil
127128
}
129+
128130
var report sarifschemav210.SchemaJson
129131
if err := report.UnmarshalJSON(fileContents); err != nil {
130132
return nil, errors.Errorf("%w: %w", ErrMalformedSARIFfile, err)

components/scanners/osv-scanner/internal/transformer/transformer_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ func TestTransformer_Transform(t *testing.T) {
4141
transformMethodTest(t, ocsfTransformer.Transform, nil, 11)
4242
})
4343
t.Run("it should exit cleanly when there are no results", func(t *testing.T) {
44-
path, err := os.Getwd()
45-
require.NoError(t, err)
46-
os.Setenv("RAW_OUT_FILE", "./testdata/empty.json")
44+
45+
path := filepath.Join(t.TempDir(), "empty.sarif.json")
46+
require.NoError(t, os.WriteFile(path, []byte("{}"), 0644))
47+
os.Setenv("RAW_OUT_FILE", path)
48+
4749
ocsfTransformer, err := New(
4850
OSVScannerTransformerWithClock(clock),
4951
OSVScannerTransformerWithProjectRoot(filepath.Join(path, ".")),

0 commit comments

Comments
 (0)