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

     1  package pkg
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/kastenhq/syft/syft/artifact"
    10  	"github.com/kastenhq/syft/syft/file"
    11  )
    12  
    13  func TestRelationshipsEvidentBy(t *testing.T) {
    14  
    15  	c := NewCollection()
    16  
    17  	coordA := file.Coordinates{
    18  		RealPath:     "/somewhere/real",
    19  		FileSystemID: "abc",
    20  	}
    21  	coordC := file.Coordinates{
    22  		RealPath:     "/somewhere/real",
    23  		FileSystemID: "abc",
    24  	}
    25  	coordD := file.Coordinates{
    26  		RealPath:     "/somewhere/real",
    27  		FileSystemID: "abc",
    28  	}
    29  	pkgA := Package{
    30  		Locations: file.NewLocationSet(
    31  			// added!
    32  			file.NewLocationFromCoordinates(coordA).WithAnnotation(EvidenceAnnotationKey, PrimaryEvidenceAnnotation),
    33  			// ignored...
    34  			file.NewLocationFromCoordinates(coordC).WithAnnotation(EvidenceAnnotationKey, SupportingEvidenceAnnotation),
    35  			file.NewLocationFromCoordinates(coordD),
    36  		),
    37  	}
    38  	pkgA.SetID()
    39  	c.Add(pkgA)
    40  
    41  	coordB := file.Coordinates{
    42  		RealPath:     "/somewhere-else/real",
    43  		FileSystemID: "def",
    44  	}
    45  	pkgB := Package{
    46  		Locations: file.NewLocationSet(
    47  			// added!
    48  			file.NewLocationFromCoordinates(coordB).WithAnnotation(EvidenceAnnotationKey, PrimaryEvidenceAnnotation),
    49  		),
    50  	}
    51  	pkgB.SetID()
    52  	c.Add(pkgB)
    53  
    54  	tests := []struct {
    55  		name    string
    56  		catalog *Collection
    57  		want    []artifact.Relationship
    58  	}{
    59  		{
    60  			name:    "go case",
    61  			catalog: c,
    62  			want: []artifact.Relationship{
    63  				{
    64  					From: pkgB,
    65  					To:   coordB,
    66  					Type: artifact.EvidentByRelationship,
    67  				},
    68  				{
    69  					From: pkgA,
    70  					To:   coordA,
    71  					Type: artifact.EvidentByRelationship,
    72  				},
    73  			},
    74  		},
    75  	}
    76  	for _, tt := range tests {
    77  		t.Run(tt.name, func(t *testing.T) {
    78  			actual := RelationshipsEvidentBy(tt.catalog)
    79  			require.Len(t, actual, len(tt.want))
    80  			for i := range actual {
    81  				assert.Equal(t, tt.want[i].From.ID(), actual[i].From.ID(), "from mismatch at index %d", i)
    82  				assert.Equal(t, tt.want[i].To.ID(), actual[i].To.ID(), "to mismatch at index %d", i)
    83  				assert.Equal(t, tt.want[i].Type, actual[i].Type, "type mismatch at index %d", i)
    84  			}
    85  		})
    86  	}
    87  }