@@ -613,16 +613,16 @@ func TestGetDirectComponents(t *testing.T) {
613613 name : "one direct component with target" ,
614614 target : filepath .Join ("root" , "dir" , "file" ),
615615 impactPaths : [][]services.ImpactPathNode {{services.ImpactPathNode {ComponentId : "gav://jfrog:pack1:1.2.3" }, services.ImpactPathNode {ComponentId : "gav://jfrog:pack2:1.2.3" }}},
616- expectedDirectComponentRows : []formats.ComponentRow {{Id : "gav://jfrog:pack2:1.2.3" , Name : "jfrog:pack2" , Version : "1.2.3" , Location : & formats.Location {File : filepath .Join ("root" , "dir" , "file" )}}},
616+ expectedDirectComponentRows : []formats.ComponentRow {{Id : "gav://jfrog:pack2:1.2.3" , Name : "jfrog:pack2" , Version : "1.2.3" , PreferredLocation : & formats.Location {File : filepath .Join ("root" , "dir" , "file" )}}},
617617 expectedConvImpactPaths : [][]formats.ComponentRow {{{Id : "gav://jfrog:pack1:1.2.3" , Name : "jfrog:pack1" , Version : "1.2.3" }, {Id : "gav://jfrog:pack2:1.2.3" , Name : "jfrog:pack2" , Version : "1.2.3" }}},
618618 },
619619 {
620620 name : "multiple direct components" ,
621621 target : filepath .Join ("root" , "dir" , "file" ),
622622 impactPaths : [][]services.ImpactPathNode {{services.ImpactPathNode {ComponentId : "gav://jfrog:pack1:1.2.3" }, services.ImpactPathNode {ComponentId : "gav://jfrog:pack21:1.2.3" }, services.ImpactPathNode {ComponentId : "gav://jfrog:pack3:1.2.3" }}, {services.ImpactPathNode {ComponentId : "gav://jfrog:pack1:1.2.3" }, services.ImpactPathNode {ComponentId : "gav://jfrog:pack22:1.2.3" }, services.ImpactPathNode {ComponentId : "gav://jfrog:pack3:1.2.3" }}},
623623 expectedDirectComponentRows : []formats.ComponentRow {
624- {Id : "gav://jfrog:pack21:1.2.3" , Name : "jfrog:pack21" , Version : "1.2.3" , Location : & formats.Location {File : filepath .Join ("root" , "dir" , "file" )}},
625- {Id : "gav://jfrog:pack22:1.2.3" , Name : "jfrog:pack22" , Version : "1.2.3" , Location : & formats.Location {File : filepath .Join ("root" , "dir" , "file" )}},
624+ {Id : "gav://jfrog:pack21:1.2.3" , Name : "jfrog:pack21" , Version : "1.2.3" , PreferredLocation : & formats.Location {File : filepath .Join ("root" , "dir" , "file" )}},
625+ {Id : "gav://jfrog:pack22:1.2.3" , Name : "jfrog:pack22" , Version : "1.2.3" , PreferredLocation : & formats.Location {File : filepath .Join ("root" , "dir" , "file" )}},
626626 },
627627 expectedConvImpactPaths : [][]formats.ComponentRow {{{Id : "gav://jfrog:pack1:1.2.3" , Name : "jfrog:pack1" , Version : "1.2.3" }, {Id : "gav://jfrog:pack21:1.2.3" , Name : "jfrog:pack21" , Version : "1.2.3" }, {Id : "gav://jfrog:pack3:1.2.3" , Name : "jfrog:pack3" , Version : "1.2.3" }}, {{Id : "gav://jfrog:pack1:1.2.3" , Name : "jfrog:pack1" , Version : "1.2.3" }, {Id : "gav://jfrog:pack22:1.2.3" , Name : "jfrog:pack22" , Version : "1.2.3" }, {Id : "gav://jfrog:pack3:1.2.3" , Name : "jfrog:pack3" , Version : "1.2.3" }}},
628628 },
@@ -800,7 +800,7 @@ func TestExtractComponentDirectComponentsInBOM(t *testing.T) {
800800 },
801801 impactPaths : [][]formats.ComponentRow {{{Id : "root" , Name : "Root Component" , Version : "1.0.0" }, {Id : "direct1" , Name : "Direct 1" , Version : "2.0.0" }}},
802802 expectedDirects : []formats.ComponentRow {
803- {Id : "direct1" , Name : "Direct 1" , Version : "2.0.0" , Location : & formats.Location {File : "package.json" }},
803+ {Id : "direct1" , Name : "Direct 1" , Version : "2.0.0" , Evidences : [] formats.Location {{ File : "package.json" } }},
804804 },
805805 },
806806 {
@@ -2869,3 +2869,109 @@ func TestExtractCdxDependenciesCves(t *testing.T) {
28692869 })
28702870 }
28712871}
2872+
2873+ func TestGetBestLocation (t * testing.T ) {
2874+ tests := []struct {
2875+ name string
2876+ component formats.ComponentRow
2877+ expected string
2878+ }{
2879+ {
2880+ name : "Component with no location" ,
2881+ },
2882+ {
2883+ name : "Component with preferred location" ,
2884+ component : formats.ComponentRow {PreferredLocation : & formats.Location {File : "package.json" }},
2885+ expected : "package.json" ,
2886+ },
2887+ {
2888+ name : "Component with evidences (npm)" ,
2889+ component : formats.ComponentRow {Evidences : []formats.Location {{File : "package.json" }, {File : "package-lock.json" }}},
2890+ expected : "package.json" ,
2891+ },
2892+ {
2893+ name : "Component with evidences (pip)" ,
2894+ component : formats.ComponentRow {Evidences : []formats.Location {{File : "requirements.txt" }, {File : "requirements.lock" }}},
2895+ expected : "requirements.txt" ,
2896+ },
2897+ {
2898+ name : "Component with preferred location and evidences" ,
2899+ component : formats.ComponentRow {PreferredLocation : & formats.Location {File : "package.json" }, Evidences : []formats.Location {{File : "package-lock.json" }}},
2900+ expected : "package.json" ,
2901+ },
2902+ }
2903+
2904+ for _ , test := range tests {
2905+ t .Run (test .name , func (t * testing.T ) {
2906+ assert .Equal (t , test .expected , GetBestLocation (test .component ))
2907+ })
2908+ }
2909+ }
2910+
2911+ func TestCdxEvidencesToPreferredLocation (t * testing.T ) {
2912+ tests := []struct {
2913+ name string
2914+ component cyclonedx.Component
2915+ expected * formats.Location
2916+ }{
2917+ {
2918+ name : "Component with no location" ,
2919+ component : cyclonedx.Component {Evidence : & cyclonedx.Evidence {Occurrences : & []cyclonedx.EvidenceOccurrence {{Location : "package.json" }}}},
2920+ expected : & formats.Location {File : "package.json" },
2921+ },
2922+ {
2923+ name : "Component with preferred location" ,
2924+ component : cyclonedx.Component {Evidence : & cyclonedx.Evidence {Occurrences : & []cyclonedx.EvidenceOccurrence {{Location : "package.json" }}}},
2925+ expected : & formats.Location {File : "package.json" },
2926+ },
2927+ {
2928+ name : "Component with evidences (npm)" ,
2929+ component : cyclonedx.Component {Evidence : & cyclonedx.Evidence {Occurrences : & []cyclonedx.EvidenceOccurrence {{Location : "package.json" }, {Location : "package-lock.json" }}}},
2930+ expected : & formats.Location {File : "package.json" },
2931+ },
2932+ {
2933+ name : "Component with evidences (pip)" ,
2934+ component : cyclonedx.Component {Evidence : & cyclonedx.Evidence {Occurrences : & []cyclonedx.EvidenceOccurrence {{Location : "requirements.txt" }, {Location : "requirements.lock" }}}},
2935+ expected : & formats.Location {File : "requirements.txt" },
2936+ },
2937+ }
2938+
2939+ for _ , test := range tests {
2940+ t .Run (test .name , func (t * testing.T ) {
2941+ assert .Equal (t , test .expected , CdxEvidencesToPreferredLocation (test .component ))
2942+ })
2943+ }
2944+ }
2945+
2946+ func TestCdxEvidencesToLocations (t * testing.T ) {
2947+ tests := []struct {
2948+ name string
2949+ component cyclonedx.Component
2950+ expected []formats.Location
2951+ }{
2952+ {
2953+ name : "Component with no location" ,
2954+ },
2955+ {
2956+ name : "Component with preferred location" ,
2957+ component : cyclonedx.Component {Evidence : & cyclonedx.Evidence {Occurrences : & []cyclonedx.EvidenceOccurrence {{Location : "package.json" }}}},
2958+ expected : []formats.Location {{File : "package.json" }},
2959+ },
2960+ {
2961+ name : "Component with evidences (npm)" ,
2962+ component : cyclonedx.Component {Evidence : & cyclonedx.Evidence {Occurrences : & []cyclonedx.EvidenceOccurrence {{Location : "package.json" }, {Location : "package-lock.json" }}}},
2963+ expected : []formats.Location {{File : "package.json" }, {File : "package-lock.json" }},
2964+ },
2965+ {
2966+ name : "Component with evidences (pip)" ,
2967+ component : cyclonedx.Component {Evidence : & cyclonedx.Evidence {Occurrences : & []cyclonedx.EvidenceOccurrence {{Location : "requirements.txt" }, {Location : "requirements.lock" }}}},
2968+ expected : []formats.Location {{File : "requirements.txt" }, {File : "requirements.lock" }},
2969+ },
2970+ }
2971+
2972+ for _ , test := range tests {
2973+ t .Run (test .name , func (t * testing.T ) {
2974+ assert .Equal (t , test .expected , CdxEvidencesToLocations (test .component ))
2975+ })
2976+ }
2977+ }
0 commit comments