github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/syft/pkg/cataloger/package_exclusions_test.go (about)

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