-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for spdx expression (#304)
Signed-off-by: harisudarsan1 <sudarshanhari561@gmail.com> error handling and add examples for license filtering Handled the excluded errors in filter evaluator Examples are updated for the SPDX license filtering Signed-off-by: harisudarsan1 <sudarshanhari561@gmail.com> test: Add test case for spdx license evaluator chore: Update go.mod chore: Update vulnerable dependencies
- Loading branch information
1 parent
3fab469
commit 6b050fe
Showing
5 changed files
with
179 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package filter | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/safedep/vet/gen/filtersuite" | ||
"github.com/safedep/vet/gen/insightapi" | ||
"github.com/safedep/vet/pkg/models" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestEvaluatorLicenseExpression(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
packageLicenses []string | ||
filterString string | ||
expected bool | ||
skip bool | ||
skipReason string | ||
}{ | ||
{ | ||
name: "License match by exists (current behavior)", | ||
packageLicenses: []string{"MIT", "Apache-2.0"}, | ||
filterString: "licenses.exists(p, p == 'MIT')", | ||
expected: true, | ||
}, | ||
{ | ||
name: "Package has license expression does not match exists", | ||
packageLicenses: []string{"MIT OR Apache-2.0"}, | ||
filterString: "licenses.exists(p, p == 'MIT')", | ||
expected: false, | ||
}, | ||
{ | ||
name: "Package has license expression matches expression", | ||
packageLicenses: []string{"MIT OR Apache-2.0"}, | ||
filterString: "licenses.contains_license('MIT')", | ||
expected: true, | ||
}, | ||
{ | ||
name: "Package has license expression matches expression", | ||
packageLicenses: []string{"MIT OR Apache-2.0"}, | ||
filterString: "licenses.contains_license('Apache-2.0 OR MIT')", | ||
expected: true, | ||
}, | ||
{ | ||
name: "Package has license expression does not match expression", | ||
packageLicenses: []string{"MIT OR Apache-2.0"}, | ||
filterString: "licenses.contains_license('Apache-2.0 AND MIT')", | ||
expected: false, | ||
skip: true, | ||
skipReason: "AND expressions in filters are not supported yet", | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.name, func(t *testing.T) { | ||
if c.skip { | ||
t.Skip(c.skipReason) | ||
} | ||
|
||
f, err := NewEvaluator("test", false) | ||
assert.NoError(t, err) | ||
|
||
err = f.AddFilter(&filtersuite.Filter{ | ||
Name: "test", | ||
Value: c.filterString, | ||
}) | ||
assert.NoError(t, err) | ||
|
||
licenses := []insightapi.License{} | ||
for _, l := range c.packageLicenses { | ||
licenses = append(licenses, insightapi.License(l)) | ||
} | ||
|
||
pkg := &models.Package{ | ||
Insights: &insightapi.PackageVersionInsight{ | ||
Licenses: &licenses, | ||
}, | ||
} | ||
|
||
result, err := f.EvalPackage(pkg) | ||
assert.NoError(t, err) | ||
assert.Equal(t, c.expected, result.Matched()) | ||
}) | ||
} | ||
} |