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:
Christopher Angelo Phillips 2025-05-06 11:45:47 -04:00 committed by GitHub
parent 7b25ea5eda
commit 6eff158ad3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 28 additions and 7 deletions

View 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.

View File

@ -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
}

View File

@ -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 {