From fdc1effd6e17e56e71dd040f3d74a14703c8e362 Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Sun, 10 Oct 2021 19:50:50 -0700 Subject: [PATCH] add test and linting fixes Signed-off-by: Alex Goodman --- internal/anchore/import_package_sbom.go | 2 +- internal/anchore/import_package_sbom_test.go | 28 ++++++++----------- internal/formats/common/testutils/utils.go | 2 +- .../spdx22json/model/syft_package_data.go | 1 + internal/formats/syftjson/model/package.go | 1 + internal/formats/syftjson/to_format_model.go | 23 +++++---------- syft/lib.go | 2 +- syft/pkg/cataloger/deb/cataloger.go | 2 +- 8 files changed, 24 insertions(+), 37 deletions(-) diff --git a/internal/anchore/import_package_sbom.go b/internal/anchore/import_package_sbom.go index 858eef77d..870fc77e1 100644 --- a/internal/anchore/import_package_sbom.go +++ b/internal/anchore/import_package_sbom.go @@ -25,7 +25,7 @@ type packageSBOMImportAPI interface { ImportImagePackages(context.Context, string, external.ImagePackageManifest) (external.ImageImportContentResponse, *http.Response, error) } -func packageSbomModel(s source.Metadata, catalog *pkg.Catalog, d *distro.Distro, scope source.Scope) (*external.ImagePackageManifest, error) { +func packageSbomModel(s source.Metadata, catalog *pkg.Catalog, d *distro.Distro, _ source.Scope) (*external.ImagePackageManifest, error) { var buf bytes.Buffer pres := formats.ByOption(format.JSONOption).Presenter(catalog, &s, d) err := pres.Present(&buf) diff --git a/internal/anchore/import_package_sbom_test.go b/internal/anchore/import_package_sbom_test.go index 80e5055c2..63a0b2c4d 100644 --- a/internal/anchore/import_package_sbom_test.go +++ b/internal/anchore/import_package_sbom_test.go @@ -1,7 +1,6 @@ package anchore import ( - "bytes" "context" "encoding/json" "fmt" @@ -9,20 +8,16 @@ import ( "strings" "testing" - model2 "github.com/anchore/syft/internal/formats/syftjson/model" - - "github.com/anchore/syft/internal/presenter/packages" - - "github.com/wagoodman/go-progress" - - "github.com/anchore/syft/syft/distro" - - "github.com/docker/docker/pkg/ioutils" - "github.com/anchore/client-go/pkg/external" + syftjsonModel "github.com/anchore/syft/internal/formats/syftjson/model" + "github.com/anchore/syft/syft" + "github.com/anchore/syft/syft/distro" + "github.com/anchore/syft/syft/format" "github.com/anchore/syft/syft/pkg" "github.com/anchore/syft/syft/source" + "github.com/docker/docker/pkg/ioutils" "github.com/go-test/deep" + "github.com/wagoodman/go-progress" ) func must(c pkg.CPE, e error) pkg.CPE { @@ -89,20 +84,19 @@ func TestPackageSbomToModel(t *testing.T) { t.Fatalf("unable to marshal model: %+v", err) } - var buf bytes.Buffer - pres := packages.NewJSONPresenter(c, m, &d, source.AllLayersScope) - if err := pres.Present(&buf); err != nil { + by, err := syft.Encode(c, &m, &d, format.JSONOption) + if err != nil { t.Fatalf("unable to get expected json: %+v", err) } // unmarshal expected result - var expectedDoc model2.Document - if err := json.Unmarshal(buf.Bytes(), &expectedDoc); err != nil { + var expectedDoc syftjsonModel.Document + if err := json.Unmarshal(by, &expectedDoc); err != nil { t.Fatalf("unable to parse json doc: %+v", err) } // unmarshal actual result - var actualDoc model2.Document + var actualDoc syftjsonModel.Document if err := json.Unmarshal(modelJSON, &actualDoc); err != nil { t.Fatalf("unable to parse json doc: %+v", err) } diff --git a/internal/formats/common/testutils/utils.go b/internal/formats/common/testutils/utils.go index db5f2d78c..dc5a8bad3 100644 --- a/internal/formats/common/testutils/utils.go +++ b/internal/formats/common/testutils/utils.go @@ -168,7 +168,7 @@ func DirectoryInput(t testing.TB) (*pkg.Catalog, source.Metadata, *distro.Distro Version: "1.0.1", Files: []pkg.PythonFileRecord{ { - Path: "/some/path/pkg1/depedencies/foo", + Path: "/some/path/pkg1/dependencies/foo", }, }, }, diff --git a/internal/formats/spdx22json/model/syft_package_data.go b/internal/formats/spdx22json/model/syft_package_data.go index 1b33db575..92d99509c 100644 --- a/internal/formats/spdx22json/model/syft_package_data.go +++ b/internal/formats/spdx22json/model/syft_package_data.go @@ -38,6 +38,7 @@ func (p *syftPackageMetadataUnpacker) String() string { } // UnmarshalJSON is a custom unmarshaller for handling basic values and values with ambiguous types. +// nolint:funlen func (p *SyftPackageData) UnmarshalJSON(b []byte) error { var basic SyftPackageBasicData if err := json.Unmarshal(b, &basic); err != nil { diff --git a/internal/formats/syftjson/model/package.go b/internal/formats/syftjson/model/package.go index 9ee468062..4078d3101 100644 --- a/internal/formats/syftjson/model/package.go +++ b/internal/formats/syftjson/model/package.go @@ -46,6 +46,7 @@ func (p *packageMetadataUnpacker) String() string { } // UnmarshalJSON is a custom unmarshaller for handling basic values and values with ambiguous types. +// nolint:funlen func (p *Package) UnmarshalJSON(b []byte) error { var basic PackageBasicData if err := json.Unmarshal(b, &basic); err != nil { diff --git a/internal/formats/syftjson/to_format_model.go b/internal/formats/syftjson/to_format_model.go index eacce565e..260cedead 100644 --- a/internal/formats/syftjson/to_format_model.go +++ b/internal/formats/syftjson/to_format_model.go @@ -24,13 +24,8 @@ func ToFormatModel(catalog *pkg.Catalog, srcMetadata *source.Metadata, d *distro log.Warnf("unable to create syft-json source object: %+v", err) } - artifacts, err := toPackageModels(catalog) - if err != nil { - return model.Document{} - } - return model.Document{ - Artifacts: artifacts, + Artifacts: toPackageModels(catalog), ArtifactRelationships: toRelationshipModel(pkg.NewRelationships(catalog)), Source: src, Distro: toDistroModel(d), @@ -46,23 +41,19 @@ func ToFormatModel(catalog *pkg.Catalog, srcMetadata *source.Metadata, d *distro } } -func toPackageModels(catalog *pkg.Catalog) ([]model.Package, error) { +func toPackageModels(catalog *pkg.Catalog) []model.Package { artifacts := make([]model.Package, 0) if catalog == nil { - return artifacts, nil + return artifacts } for _, p := range catalog.Sorted() { - art, err := toPackageModel(p) - if err != nil { - return nil, err - } - artifacts = append(artifacts, art) + artifacts = append(artifacts, toPackageModel(p)) } - return artifacts, nil + return artifacts } // toPackageModel crates a new Package from the given pkg.Package. -func toPackageModel(p *pkg.Package) (model.Package, error) { +func toPackageModel(p *pkg.Package) model.Package { var cpes = make([]string, len(p.CPEs)) for i, c := range p.CPEs { cpes[i] = c.BindToFmtString() @@ -96,7 +87,7 @@ func toPackageModel(p *pkg.Package) (model.Package, error) { MetadataType: p.MetadataType, Metadata: p.Metadata, }, - }, nil + } } func toRelationshipModel(relationships []pkg.Relationship) []model.Relationship { diff --git a/syft/lib.go b/syft/lib.go index 3a074e6d8..872b73acf 100644 --- a/syft/lib.go +++ b/syft/lib.go @@ -75,7 +75,7 @@ func CatalogPackages(src *source.Source, scope source.Scope) (*pkg.Catalog, *dis func Encode(catalog *pkg.Catalog, metadata *source.Metadata, dist *distro.Distro, option format.Option) ([]byte, error) { f := formats.ByOption(option) if f == nil { - return nil, nil + return nil, fmt.Errorf("unsupported format: %+v", option) } buff := bytes.Buffer{} diff --git a/syft/pkg/cataloger/deb/cataloger.go b/syft/pkg/cataloger/deb/cataloger.go index 1cd000707..f86666e94 100644 --- a/syft/pkg/cataloger/deb/cataloger.go +++ b/syft/pkg/cataloger/deb/cataloger.go @@ -36,7 +36,7 @@ func (c *Cataloger) Name() string { } // Catalog is given an object to resolve file references and content, this function returns any discovered Packages after analyzing dpkg support files. -// nolint:funlen + func (c *Cataloger) Catalog(resolver source.FileResolver) ([]pkg.Package, error) { dbFileMatches, err := resolver.FilesByGlob(pkg.DpkgDbGlob) if err != nil {