-
Notifications
You must be signed in to change notification settings - Fork 587
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
port php cataloger to new generic cataloger pattern (#1315)
Signed-off-by: Alex Goodman <[email protected]> Signed-off-by: Alex Goodman <[email protected]>
- Loading branch information
Showing
8 changed files
with
263 additions
and
212 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package php | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/anchore/packageurl-go" | ||
"github.com/anchore/syft/syft/pkg" | ||
"github.com/anchore/syft/syft/source" | ||
) | ||
|
||
func newComposerLockPackage(m pkg.PhpComposerJSONMetadata, location ...source.Location) pkg.Package { | ||
p := pkg.Package{ | ||
Name: m.Name, | ||
Version: m.Version, | ||
Locations: source.NewLocationSet(location...), | ||
PURL: packageURL(m), | ||
Language: pkg.PHP, | ||
Type: pkg.PhpComposerPkg, | ||
MetadataType: pkg.PhpComposerJSONMetadataType, | ||
Metadata: m, | ||
} | ||
|
||
p.SetID() | ||
return p | ||
} | ||
|
||
func packageURL(m pkg.PhpComposerJSONMetadata) string { | ||
var name, vendor string | ||
fields := strings.Split(m.Name, "/") | ||
switch len(fields) { | ||
case 0: | ||
return "" | ||
case 1: | ||
name = m.Name | ||
case 2: | ||
vendor = fields[0] | ||
name = fields[1] | ||
default: | ||
vendor = fields[0] | ||
name = strings.Join(fields[1:], "-") | ||
} | ||
|
||
pURL := packageurl.NewPackageURL( | ||
packageurl.TypeComposer, | ||
vendor, | ||
name, | ||
m.Version, | ||
nil, | ||
"") | ||
return pURL.ToString() | ||
} |
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,53 @@ | ||
package php | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/sergi/go-diff/diffmatchpatch" | ||
|
||
"github.com/anchore/syft/syft/pkg" | ||
) | ||
|
||
func Test_packageURL(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
metadata pkg.PhpComposerJSONMetadata | ||
expected string | ||
}{ | ||
{ | ||
name: "with extractable vendor", | ||
metadata: pkg.PhpComposerJSONMetadata{ | ||
Name: "ven/name", | ||
Version: "1.0.1", | ||
}, | ||
expected: "pkg:composer/ven/[email protected]", | ||
}, | ||
{ | ||
name: "name with slashes (invalid)", | ||
metadata: pkg.PhpComposerJSONMetadata{ | ||
Name: "ven/name/component", | ||
Version: "1.0.1", | ||
}, | ||
expected: "pkg:composer/ven/[email protected]", | ||
}, | ||
{ | ||
name: "unknown vendor", | ||
metadata: pkg.PhpComposerJSONMetadata{ | ||
Name: "name", | ||
Version: "1.0.1", | ||
}, | ||
expected: "pkg:composer/[email protected]", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
actual := packageURL(test.metadata) | ||
if actual != test.expected { | ||
dmp := diffmatchpatch.New() | ||
diffs := dmp.DiffMain(test.expected, actual, true) | ||
t.Errorf("diff: %s", dmp.DiffPrettyText(diffs)) | ||
} | ||
}) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,19 +1,24 @@ | ||
package php | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/go-test/deep" | ||
|
||
"github.com/anchore/syft/syft/artifact" | ||
"github.com/anchore/syft/syft/pkg" | ||
"github.com/anchore/syft/syft/pkg/cataloger/internal/pkgtest" | ||
"github.com/anchore/syft/syft/source" | ||
) | ||
|
||
func TestParseComposerFileLock(t *testing.T) { | ||
expected := []*pkg.Package{ | ||
var expectedRelationships []artifact.Relationship | ||
fixture := "test-fixtures/composer.lock" | ||
locations := source.NewLocationSet(source.NewLocation(fixture)) | ||
expectedPkgs := []pkg.Package{ | ||
{ | ||
Name: "adoy/fastcgi-client", | ||
Version: "1.0.2", | ||
PURL: "pkg:composer/adoy/[email protected]", | ||
Locations: locations, | ||
Language: pkg.PHP, | ||
Type: pkg.PhpComposerPkg, | ||
MetadataType: pkg.PhpComposerJSONMetadataType, | ||
|
@@ -52,6 +57,8 @@ func TestParseComposerFileLock(t *testing.T) { | |
{ | ||
Name: "alcaeus/mongo-php-adapter", | ||
Version: "1.1.11", | ||
Locations: locations, | ||
PURL: "pkg:composer/alcaeus/[email protected]", | ||
Language: pkg.PHP, | ||
Type: pkg.PhpComposerPkg, | ||
MetadataType: pkg.PhpComposerJSONMetadataType, | ||
|
@@ -106,18 +113,5 @@ func TestParseComposerFileLock(t *testing.T) { | |
}, | ||
}, | ||
} | ||
fixture, err := os.Open("test-fixtures/composer.lock") | ||
if err != nil { | ||
t.Fatalf("failed to open fixture: %+v", err) | ||
} | ||
|
||
// TODO: no relationships are under test yet | ||
actual, _, err := parseComposerLock(fixture.Name(), fixture) | ||
if err != nil { | ||
t.Fatalf("failed to parse requirements: %+v", err) | ||
} | ||
|
||
for _, d := range deep.Equal(expected, actual) { | ||
t.Errorf("diff: %+v", d) | ||
} | ||
pkgtest.TestFileParser(t, fixture, parseComposerLock, expectedPkgs, expectedRelationships) | ||
} |
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
Oops, something went wrong.