clean package names in python parsers (#1417)

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
This commit is contained in:
Alex Goodman
2022-12-21 18:31:49 +00:00
committed by GitHub
parent c8b8b1ca11
commit 5dd726fc86
5 changed files with 791 additions and 42 deletions
+92 -41
View File
@@ -3,6 +3,8 @@ package python
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/anchore/syft/syft/artifact"
"github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/pkg/cataloger/internal/pkgtest"
@@ -10,52 +12,101 @@ import (
)
func TestParseSetup(t *testing.T) {
fixture := "test-fixtures/setup/setup.py"
locations := source.NewLocationSet(source.NewLocation(fixture))
expectedPkgs := []pkg.Package{
tests := []struct {
fixture string
expected []pkg.Package
}{
{
Name: "pathlib3",
Version: "2.2.0",
PURL: "pkg:pypi/pathlib3@2.2.0",
Locations: locations,
Language: pkg.Python,
Type: pkg.PythonPkg,
fixture: "test-fixtures/setup/setup.py",
expected: []pkg.Package{
{
Name: "pathlib3",
Version: "2.2.0",
PURL: "pkg:pypi/pathlib3@2.2.0",
Language: pkg.Python,
Type: pkg.PythonPkg,
},
{
Name: "mypy",
Version: "v0.770",
PURL: "pkg:pypi/mypy@v0.770",
Language: pkg.Python,
Type: pkg.PythonPkg,
},
{
Name: "mypy1",
Version: "v0.770",
PURL: "pkg:pypi/mypy1@v0.770",
Language: pkg.Python,
Type: pkg.PythonPkg,
},
{
Name: "mypy2",
Version: "v0.770",
PURL: "pkg:pypi/mypy2@v0.770",
Language: pkg.Python,
Type: pkg.PythonPkg,
},
{
Name: "mypy3",
Version: "v0.770",
PURL: "pkg:pypi/mypy3@v0.770",
Language: pkg.Python,
Type: pkg.PythonPkg,
},
},
},
{
Name: "mypy",
Version: "v0.770",
PURL: "pkg:pypi/mypy@v0.770",
Locations: locations,
Language: pkg.Python,
Type: pkg.PythonPkg,
},
{
Name: "mypy1",
Version: "v0.770",
PURL: "pkg:pypi/mypy1@v0.770",
Locations: locations,
Language: pkg.Python,
Type: pkg.PythonPkg,
},
{
Name: "mypy2",
Version: "v0.770",
PURL: "pkg:pypi/mypy2@v0.770",
Locations: locations,
Language: pkg.Python,
Type: pkg.PythonPkg,
},
{
Name: "mypy3",
Version: "v0.770",
PURL: "pkg:pypi/mypy3@v0.770",
Locations: locations,
Language: pkg.Python,
Type: pkg.PythonPkg,
// regression... ensure we clean packages names and don't find "%s" as the name
fixture: "test-fixtures/setup/dynamic-setup.py",
expected: nil,
},
}
var expectedRelationships []artifact.Relationship
for _, tt := range tests {
t.Run(tt.fixture, func(t *testing.T) {
locations := source.NewLocationSet(source.NewLocation(tt.fixture))
for i := range tt.expected {
tt.expected[i].Locations = locations
}
var expectedRelationships []artifact.Relationship
pkgtest.TestFileParser(t, tt.fixture, parseSetup, tt.expected, expectedRelationships)
})
}
pkgtest.TestFileParser(t, fixture, parseSetup, expectedPkgs, expectedRelationships)
}
func Test_hasTemplateDirective(t *testing.T) {
tests := []struct {
input string
want bool
}{
{
input: "foo",
want: false,
},
{
input: "foo %s",
want: true,
},
{
input: "%s",
want: true,
},
{
input: "{f_string}",
want: true,
},
{
input: "{}", // .format() directive
want: true,
},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
assert.Equal(t, tt.want, hasTemplateDirective(tt.input))
})
}
}