mirror of
https://github.com/anchore/syft.git
synced 2025-11-17 08:23:15 +01:00
chore: update license sort to be stable with contents field (#3860)
--------- Signed-off-by: Christopher Phillips <32073428+spiffcs@users.noreply.github.com>
This commit is contained in:
parent
7b25ea5eda
commit
6eff158ad3
5
internal/licenses/test-fixtures/Knuth-CTAN
Normal file
5
internal/licenses/test-fixtures/Knuth-CTAN
Normal file
@ -0,0 +1,5 @@
|
||||
Knuth CTAN License
|
||||
Full name
|
||||
Knuth CTAN License
|
||||
|
||||
This software is copyrighted. Unlimited copying and redistribution of this package and/or its individual files are permitted as long as there are no modifications. Modifications, and redistribution of modifications, are also permitted, but only if the resulting package and/or files are renamed.
|
||||
@ -49,13 +49,16 @@ func (l Licenses) Less(i, j int) bool {
|
||||
if l[i].Value == l[j].Value {
|
||||
if l[i].SPDXExpression == l[j].SPDXExpression {
|
||||
if l[i].Type == l[j].Type {
|
||||
// While URLs and location are not exclusive fields
|
||||
// returning true here reduces the number of swaps
|
||||
// while keeping a consistent sort order of
|
||||
// the order that they appear in the list initially
|
||||
// If users in the future have preference to sorting based
|
||||
// on the slice representation of either field we can update this code
|
||||
return true
|
||||
if l[i].Contents == l[j].Contents {
|
||||
// While URLs and location are not exclusive fields
|
||||
// returning true here reduces the number of swaps
|
||||
// while keeping a consistent sort order of
|
||||
// the order that they appear in the list initially
|
||||
// If users in the future have preference to sorting based
|
||||
// on the slice representation of either field we can update this code
|
||||
return true
|
||||
}
|
||||
return l[i].Contents < l[j].Contents
|
||||
}
|
||||
return l[i].Type < l[j].Type
|
||||
}
|
||||
|
||||
@ -84,6 +84,19 @@ func Test_Sort(t *testing.T) {
|
||||
NewLicenseFromLocations("MIT", file.NewLocation("place!")),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "multiple licenses with only contents",
|
||||
licenses: []License{
|
||||
NewLicense(readFileAsString("../../internal/licenses/test-fixtures/nvidia-software-and-cuda-supplement")),
|
||||
NewLicense(readFileAsString("../../internal/licenses/test-fixtures/Knuth-CTAN")),
|
||||
NewLicense(readFileAsString("../../internal/licenses/test-fixtures/apache-license-2.0")),
|
||||
},
|
||||
expected: Licenses{
|
||||
NewLicense(readFileAsString("../../internal/licenses/test-fixtures/apache-license-2.0")),
|
||||
NewLicense(readFileAsString("../../internal/licenses/test-fixtures/nvidia-software-and-cuda-supplement")),
|
||||
NewLicense(readFileAsString("../../internal/licenses/test-fixtures/Knuth-CTAN")),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user