mirror of
https://github.com/anchore/syft.git
synced 2026-11-05 00:46:38 +01:00
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:
@@ -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))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user