Skip to content

Commit 9a2515c

Browse files
committed
Added grouping tests
1 parent cc40dc5 commit 9a2515c

3 files changed

Lines changed: 204 additions & 2 deletions

File tree

vulnfeeds/conversion/grouping.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,12 @@ func cleanEvents(events []*osvschema.Event) []*osvschema.Event {
259259
// - commits: A slice of affected commits to be converted into events and added to ranges.
260260
// - successfulRepos: A slice of repository URLs that were successfully processed.
261261
// - metrics: A pointer to ConversionMetrics to track the outcome and notes.
262-
func MergeRangesAndCreateAffected(resolvedRanges []*osvschema.Range, commits []models.AffectedCommit, successfulRepos []string, metrics *models.ConversionMetrics) *osvschema.Affected {
262+
func MergeRangesAndCreateAffected(
263+
resolvedRanges []*osvschema.Range,
264+
commits []models.AffectedCommit,
265+
successfulRepos []string,
266+
metrics *models.ConversionMetrics,
267+
) *osvschema.Affected {
263268
var newResolvedRanges []*osvschema.Range
264269
// Combine the ranges appropriately
265270
if len(resolvedRanges) > 0 {

vulnfeeds/conversion/grouping_test.go

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55

66
"github.com/google/go-cmp/cmp"
7+
"github.com/google/osv/vulnfeeds/models"
78
"github.com/ossf/osv-schema/bindings/go/osvschema"
89
"google.golang.org/protobuf/testing/protocmp"
910
"google.golang.org/protobuf/types/known/structpb"
@@ -435,3 +436,200 @@ func TestGroupAffectedRanges(t *testing.T) {
435436
})
436437
}
437438
}
439+
440+
func TestMergeRangesAndCreateAffected(t *testing.T) {
441+
tests := []struct {
442+
name string
443+
resolvedRanges []*osvschema.Range
444+
commits []models.AffectedCommit
445+
successfulRepos []string
446+
want *osvschema.Affected
447+
}{
448+
{
449+
name: "Merge existing ranges with commits for the same repo",
450+
resolvedRanges: []*osvschema.Range{
451+
{
452+
Type: osvschema.Range_GIT,
453+
Repo: "repo1",
454+
Events: []*osvschema.Event{
455+
{Introduced: "0"},
456+
{Fixed: "1.0"},
457+
},
458+
},
459+
},
460+
commits: []models.AffectedCommit{
461+
{
462+
Repo: "repo1",
463+
Introduced: "1.1",
464+
},
465+
{
466+
Repo: "repo1",
467+
Fixed: "1.2",
468+
},
469+
},
470+
successfulRepos: []string{"repo1"},
471+
want: &osvschema.Affected{
472+
Ranges: []*osvschema.Range{
473+
{
474+
Type: osvschema.Range_GIT,
475+
Repo: "repo1",
476+
Events: []*osvschema.Event{
477+
{Introduced: "1.1"},
478+
{Introduced: "0"},
479+
{Fixed: "1.0"},
480+
{Fixed: "1.2"},
481+
},
482+
},
483+
},
484+
},
485+
},
486+
{
487+
name: "No resolved ranges, only commits",
488+
resolvedRanges: nil,
489+
commits: []models.AffectedCommit{
490+
{
491+
Repo: "repo2",
492+
Introduced: "0",
493+
Fixed: "1.0",
494+
},
495+
},
496+
successfulRepos: []string{"repo2"},
497+
want: &osvschema.Affected{
498+
Ranges: []*osvschema.Range{
499+
{
500+
Events: []*osvschema.Event{
501+
{Introduced: "0"},
502+
{Fixed: "1.0"},
503+
},
504+
},
505+
},
506+
},
507+
},
508+
{
509+
name: "Duplicate events are deduplicated",
510+
resolvedRanges: []*osvschema.Range{
511+
{
512+
Type: osvschema.Range_GIT,
513+
Repo: "repo3",
514+
Events: []*osvschema.Event{
515+
{Introduced: "0"},
516+
{Fixed: "1.0"},
517+
},
518+
},
519+
},
520+
commits: []models.AffectedCommit{
521+
// duplicate fixed
522+
{
523+
Repo: "repo3",
524+
Fixed: "1.0",
525+
},
526+
// duplicate introduced
527+
{
528+
Repo: "repo3",
529+
Introduced: "0",
530+
},
531+
// new last affected
532+
{
533+
Repo: "repo3",
534+
LastAffected: "0.5",
535+
},
536+
},
537+
successfulRepos: []string{"repo3"},
538+
want: &osvschema.Affected{
539+
Ranges: []*osvschema.Range{
540+
{
541+
Type: osvschema.Range_GIT,
542+
Repo: "repo3",
543+
Events: []*osvschema.Event{
544+
{Introduced: "0"},
545+
{Fixed: "1.0"},
546+
{LastAffected: "0.5"},
547+
},
548+
},
549+
},
550+
},
551+
},
552+
{
553+
name: "Commits for repos not in successfulRepos are ignored when resolvedRanges exist",
554+
resolvedRanges: []*osvschema.Range{
555+
{
556+
Type: osvschema.Range_GIT,
557+
Repo: "repo4",
558+
Events: []*osvschema.Event{
559+
{Introduced: "0"},
560+
},
561+
},
562+
},
563+
commits: []models.AffectedCommit{
564+
{
565+
Repo: "repo_ignored",
566+
Introduced: "1.1",
567+
},
568+
},
569+
successfulRepos: []string{"repo4"}, // repo_ignored is absent
570+
want: &osvschema.Affected{
571+
Ranges: []*osvschema.Range{
572+
{
573+
Type: osvschema.Range_GIT,
574+
Repo: "repo4",
575+
Events: []*osvschema.Event{
576+
{Introduced: "0"},
577+
},
578+
},
579+
},
580+
},
581+
},
582+
{
583+
name: "Multiple resolved ranges for same repo are merged and commits appended",
584+
resolvedRanges: []*osvschema.Range{
585+
{
586+
Type: osvschema.Range_GIT,
587+
Repo: "repo5",
588+
Events: []*osvschema.Event{
589+
{Introduced: "0"},
590+
{Fixed: "1.0"},
591+
},
592+
},
593+
{
594+
Type: osvschema.Range_GIT,
595+
Repo: "repo5",
596+
Events: []*osvschema.Event{
597+
{Introduced: "2.0"},
598+
{Fixed: "3.0"},
599+
},
600+
},
601+
},
602+
commits: []models.AffectedCommit{
603+
{
604+
Repo: "repo5",
605+
Fixed: "4.0",
606+
},
607+
},
608+
successfulRepos: []string{"repo5"},
609+
want: &osvschema.Affected{
610+
Ranges: []*osvschema.Range{
611+
{
612+
Type: osvschema.Range_GIT,
613+
Repo: "repo5",
614+
Events: []*osvschema.Event{
615+
{Introduced: "0"},
616+
{Fixed: "1.0"},
617+
{Introduced: "2.0"},
618+
{Fixed: "3.0"},
619+
{Fixed: "4.0"},
620+
},
621+
},
622+
},
623+
},
624+
},
625+
}
626+
627+
for _, tt := range tests {
628+
t.Run(tt.name, func(t *testing.T) {
629+
got := MergeRangesAndCreateAffected(tt.resolvedRanges, tt.commits, tt.successfulRepos, &models.ConversionMetrics{})
630+
if diff := cmp.Diff(tt.want, got, protocmp.Transform()); diff != "" {
631+
t.Errorf("MergeRangesAndCreateAffected() mismatch (-want +got):\n%s", diff)
632+
}
633+
})
634+
}
635+
}

vulnfeeds/conversion/versions.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ func Repo(u string) (string, error) {
194194
return fmt.Sprintf("%s://%s/%s", parsedURL.Scheme, parsedURL.Hostname(), pathParts[2]), nil
195195
}
196196
if parsedURL.Hostname() == "sourceware.org" {
197-
// Call out to m function for GitWeb URLs
198197
return repoGitWeb(parsedURL)
199198
}
200199
if parsedURL.Hostname() == "git.postgresql.org" {

0 commit comments

Comments
 (0)