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

     1  package pkg
     2  
     3  import (
     4  	"sort"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/anchore/syft/syft/artifact"
    11  	"github.com/anchore/syft/syft/file"
    12  )
    13  
    14  func Test_Hash(t *testing.T) {
    15  
    16  	loc1 := file.NewLocation("place!")
    17  	loc1.FileSystemID = "fs1"
    18  	loc2 := file.NewLocation("place!")
    19  	loc2.FileSystemID = "fs2" // important! there is a different file system ID
    20  
    21  	lic1 := NewLicenseFromLocations("MIT", loc1)
    22  	lic2 := NewLicenseFromLocations("MIT", loc2)
    23  
    24  	lic1.URLs.Add("foo")
    25  	lic2.URLs.Add("bar") // we also want to check the URLs are ignored
    26  
    27  	hash1, err := artifact.IDByHash(lic1)
    28  	require.NoError(t, err)
    29  
    30  	hash2, err := artifact.IDByHash(lic2)
    31  	require.NoError(t, err)
    32  
    33  	assert.Equal(t, hash1, hash2)
    34  }
    35  
    36  func Test_Sort(t *testing.T) {
    37  	tests := []struct {
    38  		name     string
    39  		licenses Licenses
    40  		expected Licenses
    41  	}{
    42  		{
    43  			name:     "empty",
    44  			licenses: []License{},
    45  			expected: []License{},
    46  		},
    47  		{
    48  			name: "single",
    49  			licenses: []License{
    50  				NewLicenseFromLocations("MIT", file.NewLocation("place!")),
    51  			},
    52  			expected: []License{
    53  				NewLicenseFromLocations("MIT", file.NewLocation("place!")),
    54  			},
    55  		},
    56  		{
    57  			name: "multiple",
    58  			licenses: []License{
    59  				NewLicenseFromLocations("MIT", file.NewLocation("place!")),
    60  				NewLicenseFromURLs("MIT", "https://github.com/anchore/syft/blob/main/LICENSE"),
    61  				NewLicenseFromLocations("Apache", file.NewLocation("area!")),
    62  				NewLicenseFromLocations("gpl2+", file.NewLocation("area!")),
    63  			},
    64  			expected: Licenses{
    65  				NewLicenseFromLocations("Apache", file.NewLocation("area!")),
    66  				NewLicenseFromURLs("MIT", "https://github.com/anchore/syft/blob/main/LICENSE"),
    67  				NewLicenseFromLocations("MIT", file.NewLocation("place!")),
    68  				NewLicenseFromLocations("gpl2+", file.NewLocation("area!")),
    69  			},
    70  		},
    71  		{
    72  			name: "multiple with location variants",
    73  			licenses: []License{
    74  				NewLicenseFromLocations("MIT", file.NewLocation("place!")),
    75  				NewLicenseFromLocations("MIT", file.NewLocation("park!")),
    76  				NewLicense("MIT"),
    77  				NewLicense("AAL"),
    78  				NewLicense("Adobe-2006"),
    79  				NewLicenseFromLocations("Apache", file.NewLocation("area!")),
    80  			},
    81  			expected: Licenses{
    82  				NewLicense("AAL"),
    83  				NewLicense("Adobe-2006"),
    84  				NewLicenseFromLocations("Apache", file.NewLocation("area!")),
    85  				NewLicense("MIT"),
    86  				NewLicenseFromLocations("MIT", file.NewLocation("park!")),
    87  				NewLicenseFromLocations("MIT", file.NewLocation("place!")),
    88  			},
    89  		},
    90  	}
    91  
    92  	for _, test := range tests {
    93  		t.Run(test.name, func(t *testing.T) {
    94  			sort.Sort(test.licenses)
    95  			assert.Equal(t, test.expected, test.licenses)
    96  		})
    97  
    98  	}
    99  }