github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/cmd/syft/internal/test/integration/sbom_cataloger_test.go (about) 1 package integration 2 3 import ( 4 "testing" 5 6 "github.com/anchore/syft/syft/pkg" 7 "github.com/anchore/syft/syft/sbom" 8 "github.com/anchore/syft/syft/source" 9 ) 10 11 func TestSbomCataloger(t *testing.T) { 12 assertCount := func(t *testing.T, sbom sbom.SBOM, expectedGoModCatalogerPkgs int, expectedSbomCatalogerPkgs int) { 13 actualSbomPkgs := 0 14 actualGoModPkgs := 0 15 16 for p := range sbom.Artifacts.Packages.Enumerate(pkg.GoModulePkg) { 17 if p.FoundBy == "go-module-file-cataloger" { 18 actualGoModPkgs += 1 19 } else if p.FoundBy == "sbom-cataloger" { 20 actualSbomPkgs += 1 21 } 22 } 23 24 if actualGoModPkgs != expectedGoModCatalogerPkgs { 25 t.Errorf("unexpected number of packages from go mod cataloger: %d != %d", expectedGoModCatalogerPkgs, actualGoModPkgs) 26 } 27 if actualSbomPkgs != expectedSbomCatalogerPkgs { 28 t.Errorf("unexpected number of packages from sbom cataloger: %d != %d", expectedSbomCatalogerPkgs, actualSbomPkgs) 29 } 30 } 31 32 t.Run("default catalogers", func(t *testing.T) { 33 sbom, _ := catalogFixtureImage(t, "image-sbom-cataloger", source.SquashedScope, "+go-module-file-cataloger") 34 35 expectedSbomCatalogerPkgs := 0 36 expectedGoModCatalogerPkgs := 2 37 assertCount(t, sbom, expectedGoModCatalogerPkgs, expectedSbomCatalogerPkgs) 38 }) 39 40 // The image contains a go.mod file with 2 dependencies and an spdx json sbom. 41 // The go.mod file contains 2 dependencies, and the sbom includes a go dependency 42 // that overlaps with the go.mod 43 t.Run("with sbom cataloger", func(t *testing.T) { 44 sbom, _ := catalogFixtureImage(t, "image-sbom-cataloger", source.SquashedScope, "+go-module-file-cataloger", "+sbom-cataloger") 45 46 expectedSbomCatalogerPkgs := 1 47 expectedGoModCatalogerPkgs := 2 48 assertCount(t, sbom, expectedGoModCatalogerPkgs, expectedSbomCatalogerPkgs) 49 }) 50 }