mirror of
https://github.com/anchore/syft.git
synced 2026-11-01 06:56:40 +01:00
detect license ID from full text when incidentally provided as a value (#3876)
--------- Signed-off-by: Christopher Phillips <32073428+spiffcs@users.noreply.github.com> Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> Co-authored-by: Alex Goodman <wagoodman@users.noreply.github.com>
This commit is contained in:
co-authored by
Alex Goodman
parent
b4d717fb30
commit
f77d503892
@@ -117,6 +117,114 @@ func TestFilterNonCompliantPackages(t *testing.T) {
|
||||
assert.Equal(t, p2, droppedPkgs[0])
|
||||
}
|
||||
|
||||
func TestApplyLicenseContentRules(t *testing.T) {
|
||||
licenseWithSPDX := pkg.License{
|
||||
SPDXExpression: "MIT",
|
||||
Contents: "MIT license content",
|
||||
}
|
||||
licenseWithoutSPDX := pkg.License{
|
||||
Value: "License-Not-A-SPDX-Expression",
|
||||
Contents: "Non-SPDX license content",
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
inputLicenses []pkg.License
|
||||
cfg cataloging.LicenseConfig
|
||||
expectedLicenses []pkg.License
|
||||
}{
|
||||
{
|
||||
name: "LicenseContentIncludeUnknown",
|
||||
inputLicenses: []pkg.License{
|
||||
licenseWithSPDX,
|
||||
licenseWithoutSPDX,
|
||||
},
|
||||
cfg: cataloging.LicenseConfig{
|
||||
IncludeContent: cataloging.LicenseContentIncludeUnknown,
|
||||
},
|
||||
expectedLicenses: []pkg.License{
|
||||
{
|
||||
SPDXExpression: "MIT",
|
||||
Contents: "", // content cleared for SPDX license
|
||||
},
|
||||
{
|
||||
Value: "License-Not-A-SPDX-Expression",
|
||||
Contents: "Non-SPDX license content", // content preserved for non-SPDX
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "LicenseContentExcludeAll",
|
||||
inputLicenses: []pkg.License{
|
||||
licenseWithSPDX,
|
||||
licenseWithoutSPDX,
|
||||
},
|
||||
cfg: cataloging.LicenseConfig{
|
||||
IncludeContent: cataloging.LicenseContentExcludeAll,
|
||||
},
|
||||
expectedLicenses: []pkg.License{
|
||||
{
|
||||
SPDXExpression: "MIT",
|
||||
Contents: "", // content cleared
|
||||
},
|
||||
{
|
||||
Value: "License-Not-A-SPDX-Expression",
|
||||
Contents: "", // content cleared
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "IncludeLicenseContentDefault",
|
||||
inputLicenses: []pkg.License{
|
||||
licenseWithSPDX,
|
||||
licenseWithoutSPDX,
|
||||
},
|
||||
cfg: cataloging.LicenseConfig{
|
||||
IncludeContent: cataloging.LicenseContentIncludeAll,
|
||||
},
|
||||
expectedLicenses: []pkg.License{
|
||||
{
|
||||
SPDXExpression: "MIT",
|
||||
Contents: "MIT license content", // content preserved
|
||||
},
|
||||
{
|
||||
Value: "License-Not-A-SPDX-Expression",
|
||||
Contents: "Non-SPDX license content", // content preserved
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Empty licenses",
|
||||
inputLicenses: []pkg.License{},
|
||||
cfg: cataloging.LicenseConfig{
|
||||
IncludeContent: cataloging.LicenseContentIncludeAll,
|
||||
},
|
||||
expectedLicenses: []pkg.License{},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
inputPkg := &pkg.Package{
|
||||
Licenses: pkg.NewLicenseSet(tt.inputLicenses...),
|
||||
}
|
||||
|
||||
inputPkg.SetID()
|
||||
originalID := inputPkg.ID()
|
||||
|
||||
applyLicenseContentRules(inputPkg, tt.cfg)
|
||||
|
||||
assert.Equal(t, originalID, inputPkg.ID(), "package ID changed unexpectedly")
|
||||
|
||||
actualLicenses := inputPkg.Licenses.ToSlice()
|
||||
expectedLicenses := pkg.NewLicenseSet(tt.expectedLicenses...).ToSlice()
|
||||
|
||||
assert.Equal(t, expectedLicenses, actualLicenses, "license contents do not match expected values")
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyComplianceRules_DropAndStub(t *testing.T) {
|
||||
p := pkg.Package{Name: "", Version: ""}
|
||||
p.SetID()
|
||||
|
||||
Reference in New Issue
Block a user