Skip to content

Commit

Permalink
Minor fixes and add test boilerplate, tests have to come later after
Browse files Browse the repository at this point in the history
mocking library.
  • Loading branch information
another-rex committed Dec 11, 2022
1 parent d5197dc commit 55afa55
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
8 changes: 8 additions & 0 deletions internal/output/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package output
import (
"fmt"
"io"
"strings"

"github.com/google/osv-scanner/pkg/models"
)
Expand All @@ -21,6 +22,13 @@ func NewReporter(stdout io.Writer, stderr io.Writer, outputAsJSON bool) *Reporte
}
}

// NewVoidReporter creates a reporter that doesn't report to anywhere
func NewVoidReporter() *Reporter {
stdout := new(strings.Builder)
stderr := new(strings.Builder)
return NewReporter(stdout, stderr, false)
}

// PrintError writes the given message to stderr, regardless of if the reporter
// is outputting as JSON or not
func (r *Reporter) PrintError(msg string) {
Expand Down
6 changes: 2 additions & 4 deletions pkg/osvscanner/osvscanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,7 @@ func filterResponse(r *output.Reporter, query osv.BatchedQuery, resp *osv.Batche
// Perform osv scanner action, with optional reporter to output information
func DoScan(actions ScannerActions, r *output.Reporter) (models.VulnerabilityResults, error) {
if r == nil {
stdout := new(strings.Builder)
stderr := new(strings.Builder)
r = output.NewReporter(stdout, stderr, false)
r = output.NewVoidReporter()
}

configManager := config.ConfigManager{
Expand Down Expand Up @@ -341,5 +339,5 @@ func DoScan(actions ScannerActions, r *output.Reporter) (models.VulnerabilityRes
return models.VulnerabilityResults{}, fmt.Errorf("Failed to hydrate OSV response: %v", err)
}

return groupResponse(r, query, hydratedResp, &configManager), nil
return groupResponseBySource(r, query, hydratedResp), nil
}
6 changes: 3 additions & 3 deletions pkg/osvscanner/vulnerability_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"github.com/google/osv-scanner/internal/grouper"
"github.com/google/osv-scanner/internal/osv"
"github.com/google/osv-scanner/internal/output"
"github.com/google/osv-scanner/pkg/config"
"github.com/google/osv-scanner/pkg/models"
)

// Convert raw OSV API response into structured vulnerability information.
func groupResponse(r *output.Reporter, query osv.BatchedQuery, resp *osv.HydratedBatchedResponse, configManager *config.ConfigManager) models.VulnerabilityResults {
// groupResponseBySource converts raw OSV API response into structured vulnerability information
// grouped by source location.
func groupResponseBySource(r *output.Reporter, query osv.BatchedQuery, resp *osv.HydratedBatchedResponse) models.VulnerabilityResults {
output := models.VulnerabilityResults{
Results: []models.PackageSource{},
}
Expand Down
32 changes: 32 additions & 0 deletions pkg/osvscanner/vulnerability_result_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package osvscanner

import (
"reflect"
"testing"

"github.com/google/osv-scanner/internal/osv"
"github.com/google/osv-scanner/internal/output"
"github.com/google/osv-scanner/pkg/config"
"github.com/google/osv-scanner/pkg/models"
)

func Test_groupResponseBySource(t *testing.T) {
type args struct {
r *output.Reporter
query osv.BatchedQuery
resp *osv.HydratedBatchedResponse
configManager *config.ConfigManager
}
tests := []struct {
name string
args args
want models.VulnerabilityResults
}{}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := groupResponseBySource(tt.args.r, tt.args.query, tt.args.resp); !reflect.DeepEqual(got, tt.want) {
t.Errorf("groupResponse() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 55afa55

Please sign in to comment.