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

     1  package file
     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  )
    11  
    12  func TestCoordinatesSet(t *testing.T) {
    13  
    14  	binA := Coordinates{
    15  		RealPath:     "/bin",
    16  		FileSystemID: "a",
    17  	}
    18  
    19  	binB := Coordinates{
    20  		RealPath:     "/bin",
    21  		FileSystemID: "b",
    22  	}
    23  
    24  	tests := []struct {
    25  		name     string
    26  		input    []Coordinates
    27  		expected []Coordinates
    28  	}{
    29  		{
    30  			name: "de-dup same location",
    31  			input: []Coordinates{
    32  				binA, binA, binA,
    33  			},
    34  			expected: []Coordinates{
    35  				binA,
    36  			},
    37  		},
    38  		{
    39  			name: "dont de-dup different filesystem",
    40  			input: []Coordinates{
    41  				binB, binA,
    42  			},
    43  			expected: []Coordinates{
    44  				binA, binB,
    45  			},
    46  		},
    47  	}
    48  
    49  	for _, test := range tests {
    50  		t.Run(test.name, func(t *testing.T) {
    51  			set := NewCoordinateSet(test.input...)
    52  			assert.Equal(t, test.expected, set.ToSlice())
    53  		})
    54  	}
    55  }
    56  
    57  func TestCoordinateSet_Hash(t *testing.T) {
    58  	etcA := Coordinates{
    59  		RealPath:     "/etc",
    60  		FileSystemID: "a",
    61  	}
    62  
    63  	etcB := Coordinates{
    64  		RealPath:     "/etc",
    65  		FileSystemID: "b",
    66  	}
    67  
    68  	binA := Coordinates{
    69  		RealPath:     "/bin",
    70  		FileSystemID: "a",
    71  	}
    72  
    73  	binB := Coordinates{
    74  		RealPath:     "/bin",
    75  		FileSystemID: "b",
    76  	}
    77  
    78  	tests := []struct {
    79  		name string
    80  		setA CoordinateSet
    81  		setB CoordinateSet
    82  		want assert.ComparisonAssertionFunc
    83  	}{
    84  		{
    85  			name: "empty sets have the same hash",
    86  			setA: NewCoordinateSet(),
    87  			setB: NewCoordinateSet(),
    88  			want: assert.Equal,
    89  		},
    90  		{
    91  			name: "sets with same elements have the same hash",
    92  			setA: NewCoordinateSet(binA, etcA),
    93  			setB: NewCoordinateSet(etcA, binA),
    94  			want: assert.Equal,
    95  		},
    96  		{
    97  			name: "sets with different elements have different hashes",
    98  			setA: NewCoordinateSet(binA, etcA),
    99  			setB: NewCoordinateSet(binA),
   100  			want: assert.NotEqual,
   101  		},
   102  		{
   103  			name: "sets with same paths but different FS IDs have different hashes",
   104  			setA: NewCoordinateSet(etcA, binA),
   105  			setB: NewCoordinateSet(etcB, binB),
   106  			want: assert.NotEqual,
   107  		},
   108  	}
   109  	for _, tt := range tests {
   110  		t.Run(tt.name, func(t *testing.T) {
   111  			gotA, err := artifact.IDByHash(tt.setA)
   112  			require.NoError(t, err)
   113  			gotB, err := artifact.IDByHash(tt.setB)
   114  			require.NoError(t, err)
   115  			tt.want(t, gotA, gotB)
   116  		})
   117  	}
   118  }