Skip to content

Commit

Permalink
update package lock tests
Browse files Browse the repository at this point in the history
Signed-off-by: Benji Visser <benji@093b.org>
  • Loading branch information
noqcks committed Oct 11, 2023
1 parent 294cf5d commit 996e6fe
Show file tree
Hide file tree
Showing 4 changed files with 326 additions and 322 deletions.
78 changes: 41 additions & 37 deletions syft/pkg/cataloger/javascript/cataloger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@ func expectedPackagesAndRelationshipsLockV1(locationSet file.LocationSet, metada
},
}
rxjs := pkg.Package{
Name: "rxjs",
Version: "7.5.7",
FoundBy: "javascript-cataloger",
PURL: "pkg:npm/rxjs@7.5.7",
Locations: locationSet,
Language: pkg.JavaScript,
Type: pkg.NpmPkg,
MetadataType: pkg.NpmPackageLockJSONMetadataType,
Metadata: pkg.NpmPackageLockJSONMetadata{},
Name: "rxjs",
Version: "7.5.7",
FoundBy: "javascript-cataloger",
PURL: "pkg:npm/rxjs@7.5.7",
Locations: locationSet,
Language: pkg.JavaScript,
Type: pkg.NpmPkg,
MetadataType: pkg.NpmPackageLockJSONMetadataType,
ComponentType: pkg.ComponentTypeLibrary,
Metadata: pkg.NpmPackageLockJSONMetadata{},
}
testApp := pkg.Package{
Name: "test-app",
Expand All @@ -56,37 +57,40 @@ func expectedPackagesAndRelationshipsLockV1(locationSet file.LocationSet, metada
Metadata: pkg.NpmPackageLockJSONMetadata{},
}
tslib := pkg.Package{
Name: "tslib",
Version: "2.6.2",
FoundBy: "javascript-cataloger",
PURL: "pkg:npm/tslib@2.6.2",
Locations: locationSet,
Language: pkg.JavaScript,
Type: pkg.NpmPkg,
MetadataType: pkg.NpmPackageLockJSONMetadataType,
Metadata: pkg.NpmPackageLockJSONMetadata{},
Name: "tslib",
Version: "2.6.2",
FoundBy: "javascript-cataloger",
PURL: "pkg:npm/tslib@2.6.2",
Locations: locationSet,
Language: pkg.JavaScript,
Type: pkg.NpmPkg,
MetadataType: pkg.NpmPackageLockJSONMetadataType,
ComponentType: pkg.ComponentTypeLibrary,
Metadata: pkg.NpmPackageLockJSONMetadata{},
}
typescript := pkg.Package{
Name: "typescript",
Version: "4.7.4",
FoundBy: "javascript-cataloger",
PURL: "pkg:npm/typescript@4.7.4",
Locations: locationSet,
Language: pkg.JavaScript,
Type: pkg.NpmPkg,
MetadataType: pkg.NpmPackageLockJSONMetadataType,
Metadata: pkg.NpmPackageLockJSONMetadata{},
Name: "typescript",
Version: "4.7.4",
FoundBy: "javascript-cataloger",
PURL: "pkg:npm/typescript@4.7.4",
Locations: locationSet,
Language: pkg.JavaScript,
Type: pkg.NpmPkg,
MetadataType: pkg.NpmPackageLockJSONMetadataType,
ComponentType: pkg.ComponentTypeLibrary,
Metadata: pkg.NpmPackageLockJSONMetadata{},
}
zonejs := pkg.Package{
Name: "zone.js",
Version: "0.11.8",
FoundBy: "javascript-cataloger",
PURL: "pkg:npm/zone.js@0.11.8",
Locations: locationSet,
Language: pkg.JavaScript,
Type: pkg.NpmPkg,
MetadataType: pkg.NpmPackageLockJSONMetadataType,
Metadata: pkg.NpmPackageLockJSONMetadata{},
Name: "zone.js",
Version: "0.11.8",
FoundBy: "javascript-cataloger",
PURL: "pkg:npm/zone.js@0.11.8",
Locations: locationSet,
Language: pkg.JavaScript,
Type: pkg.NpmPkg,
MetadataType: pkg.NpmPackageLockJSONMetadataType,
ComponentType: pkg.ComponentTypeLibrary,
Metadata: pkg.NpmPackageLockJSONMetadata{},
}

l := []*pkg.Package{
Expand Down Expand Up @@ -754,7 +758,7 @@ func Test_JavaScriptCataloger_PnpmLock(t *testing.T) {
// expected []string
// }{
// {
// name: "obtain package lock files",
// name: "obtain package lock files",pcomponent_test.go
// fixture: "test-fixtures/pkg-json-and-lock/v1",
// expected: []string{
// "package-lock.json",
Expand Down
19 changes: 10 additions & 9 deletions syft/pkg/cataloger/javascript/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func packageURL(name, version string) string {
).ToString()
}

func newPackageLockV1Package(resolver file.Resolver, location file.Location, name string, u packageLockDependency) pkg.Package {
func newPackageLockV1Package(resolver file.Resolver, location file.Location, name string, componentType pkg.ComponentType, u packageLockDependency) pkg.Package {
version := u.Version

const aliasPrefixPackageLockV1 = "npm:"
Expand All @@ -143,14 +143,15 @@ func newPackageLockV1Package(resolver file.Resolver, location file.Location, nam
resolver,
location,
pkg.Package{
Name: name,
Version: version,
Locations: file.NewLocationSet(location.WithAnnotation(pkg.EvidenceAnnotationKey, pkg.PrimaryEvidenceAnnotation)),
PURL: packageURL(name, version),
Language: pkg.JavaScript,
Type: pkg.NpmPkg,
MetadataType: pkg.NpmPackageLockJSONMetadataType,
Metadata: pkg.NpmPackageLockJSONMetadata{Resolved: u.Resolved, Integrity: u.Integrity},
Name: name,
Version: version,
Locations: file.NewLocationSet(location.WithAnnotation(pkg.EvidenceAnnotationKey, pkg.PrimaryEvidenceAnnotation)),
PURL: packageURL(name, version),
Language: pkg.JavaScript,
Type: pkg.NpmPkg,
ComponentType: componentType,
MetadataType: pkg.NpmPackageLockJSONMetadataType,
Metadata: pkg.NpmPackageLockJSONMetadata{Resolved: u.Resolved, Integrity: u.Integrity},
},
)
}
Expand Down
24 changes: 22 additions & 2 deletions syft/pkg/cataloger/javascript/parse_package_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/anchore/syft/internal/log"
"github.com/anchore/syft/syft/artifact"
"github.com/anchore/syft/syft/file"
"github.com/anchore/syft/syft/pkg"

Check failure on line 17 in syft/pkg/cataloger/javascript/parse_package_lock.go

View workflow job for this annotation

GitHub Actions / Static analysis

ST1019: package "github.com/anchore/syft/syft/pkg" is being imported more than once (stylecheck)
syftPkg "github.com/anchore/syft/syft/pkg"

Check failure on line 18 in syft/pkg/cataloger/javascript/parse_package_lock.go

View workflow job for this annotation

GitHub Actions / Static analysis

ST1019(related information): other import of "github.com/anchore/syft/syft/pkg" (stylecheck)
"github.com/anchore/syft/syft/pkg/cataloger/generic"
)
Expand Down Expand Up @@ -117,6 +118,13 @@ func finalizePackageLockWithoutPackageJSON(resolver file.Resolver, pkglock *pack
}

if pkglock.LockfileVersion == 3 || pkglock.LockfileVersion == 2 {
root = newPackageLockV2Package(
resolver,
indexLocation,
pkglock.Name,
syftPkg.ComponentTypeApplication,
*pkglock.Packages[""],
)
return finalizePackageLockV2(resolver, pkglock, indexLocation, root)
}

Expand Down Expand Up @@ -163,7 +171,13 @@ func finalizePackageLockWithoutPackageJSONV1(resolver file.Resolver, pkglock *pa
// create packages
for name, lockDep := range pkglock.Dependencies {
lockDep.name = name
pkg := newPackageLockV1Package(resolver, indexLocation, name, *lockDep)
pkg := newPackageLockV1Package(
resolver,
indexLocation,
name,
pkg.ComponentTypeLibrary,
*lockDep,
)
pkgs = append(pkgs, pkg)
}
syftPkg.Sort(pkgs)
Expand All @@ -183,7 +197,13 @@ func finalizePackageLockWithPackageJSONV1(resolver file.Resolver, pkgjson *packa
// create packages
for name, lockDep := range pkglock.Dependencies {
lockDep.name = name
pkg := newPackageLockV1Package(resolver, indexLocation, name, *lockDep)
pkg := newPackageLockV1Package(
resolver,
indexLocation,
name,
pkg.ComponentTypeLibrary,
*lockDep,
)
pkgs = append(pkgs, pkg)
depnameMap[name] = pkg
}
Expand Down
Loading

0 comments on commit 996e6fe

Please sign in to comment.