github.com/kastenhq/syft@v0.0.0-20230821225854-0710af25cdbe/syft/pkg/cataloger/package_exclusions_test.go (about)

     1  package cataloger
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/kastenhq/syft/syft/artifact"
     7  	"github.com/kastenhq/syft/syft/pkg"
     8  	"github.com/kastenhq/syft/syft/pkg/cataloger/apkdb"
     9  	"github.com/kastenhq/syft/syft/pkg/cataloger/binary"
    10  )
    11  
    12  func TestExclude(t *testing.T) {
    13  	packageA := pkg.Package{Name: "package-a", Type: pkg.ApkPkg, FoundBy: apkdb.CatalogerName}
    14  	packageB := pkg.Package{Name: "package-a", Type: pkg.PythonPkg, FoundBy: "language-cataloger"}
    15  	packageC := pkg.Package{Name: "package-a", Type: pkg.BinaryPkg, FoundBy: binary.CatalogerName}
    16  	packageD := pkg.Package{Name: "package-d", Type: pkg.BinaryPkg, FoundBy: binary.CatalogerName}
    17  	for _, p := range []*pkg.Package{&packageA, &packageB, &packageC, &packageD} {
    18  		p := p
    19  		p.SetID()
    20  	}
    21  
    22  	tests := []struct {
    23  		name          string
    24  		relationship  artifact.Relationship
    25  		packages      *pkg.Collection
    26  		shouldExclude bool
    27  	}{
    28  		{
    29  			name: "no exclusions from os -> python",
    30  			relationship: artifact.Relationship{
    31  				Type: artifact.OwnershipByFileOverlapRelationship,
    32  				From: packageA,
    33  				To:   packageB,
    34  			},
    35  			packages:      pkg.NewCollection(packageA, packageB),
    36  			shouldExclude: false,
    37  		},
    38  		{
    39  			name: "exclusions from os -> binary",
    40  			relationship: artifact.Relationship{
    41  				Type: artifact.OwnershipByFileOverlapRelationship,
    42  				From: packageA,
    43  				To:   packageC,
    44  			},
    45  			packages:      pkg.NewCollection(packageA, packageC),
    46  			shouldExclude: true,
    47  		},
    48  		{
    49  			name: "no exclusions from python -> binary",
    50  			relationship: artifact.Relationship{
    51  				Type: artifact.OwnershipByFileOverlapRelationship,
    52  				From: packageB,
    53  				To:   packageC,
    54  			},
    55  			packages:      pkg.NewCollection(packageB, packageC),
    56  			shouldExclude: false,
    57  		},
    58  		{
    59  			name: "no exclusions for different package names",
    60  			relationship: artifact.Relationship{
    61  				Type: artifact.OwnershipByFileOverlapRelationship,
    62  				From: packageA,
    63  				To:   packageD,
    64  			},
    65  			packages:      pkg.NewCollection(packageA, packageD),
    66  			shouldExclude: false,
    67  		},
    68  	}
    69  
    70  	for _, test := range tests {
    71  		t.Run(test.name, func(t *testing.T) {
    72  			if !Exclude(test.relationship, test.packages) && test.shouldExclude {
    73  				t.Errorf("expected to exclude relationship %+v", test.relationship)
    74  			}
    75  		})
    76  
    77  	}
    78  }