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

     1  package pkg
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/anchore/syft/internal"
     9  	"github.com/anchore/syft/syft/file"
    10  	"github.com/anchore/syft/syft/license"
    11  )
    12  
    13  func TestLicenseSet_Add(t *testing.T) {
    14  	tests := []struct {
    15  		name     string
    16  		licenses []License
    17  		want     []License
    18  	}{
    19  		{
    20  			name: "add one simple license",
    21  			licenses: []License{
    22  				NewLicense("MIT"),
    23  			},
    24  			want: []License{
    25  				NewLicense("MIT"),
    26  			},
    27  		},
    28  		{
    29  			name: "add multiple simple licenses",
    30  			licenses: []License{
    31  				NewLicense("MIT"),
    32  				NewLicense("MIT"),
    33  				NewLicense("Apache-2.0"),
    34  			},
    35  			want: []License{
    36  				NewLicense("Apache-2.0"),
    37  				NewLicense("MIT"),
    38  			},
    39  		},
    40  		{
    41  			name: "attempt to add a license with no name",
    42  			licenses: []License{
    43  				NewLicense(""),
    44  			},
    45  			want: nil,
    46  		},
    47  		{
    48  			name: "keep multiple licenses sorted",
    49  			licenses: []License{
    50  				NewLicense("MIT"),
    51  				NewLicense("Apache-2.0"),
    52  			},
    53  			want: []License{
    54  				NewLicense("Apache-2.0"),
    55  				NewLicense("MIT"),
    56  			},
    57  		},
    58  		{
    59  			name: "deduplicate licenses with locations",
    60  			licenses: []License{
    61  				NewLicenseFromLocations("MIT", file.NewLocationFromCoordinates(file.Coordinates{RealPath: "/place", FileSystemID: "1"})),
    62  				NewLicenseFromLocations("MIT", file.NewLocationFromCoordinates(file.Coordinates{RealPath: "/place", FileSystemID: "1"})),
    63  				NewLicenseFromLocations("MIT", file.NewLocationFromCoordinates(file.Coordinates{RealPath: "/place", FileSystemID: "2"})),
    64  			},
    65  			want: []License{
    66  				NewLicenseFromLocations(
    67  					"MIT",
    68  					file.NewLocationFromCoordinates(file.Coordinates{RealPath: "/place", FileSystemID: "1"}),
    69  					file.NewLocationFromCoordinates(file.Coordinates{RealPath: "/place", FileSystemID: "2"}),
    70  				),
    71  			},
    72  		},
    73  		{
    74  			name: "same licenses with different locations",
    75  			licenses: []License{
    76  				NewLicense("MIT"),
    77  				NewLicenseFromLocations("MIT", file.NewLocationFromCoordinates(file.Coordinates{RealPath: "/place", FileSystemID: "2"})),
    78  				NewLicenseFromLocations("MIT", file.NewLocationFromCoordinates(file.Coordinates{RealPath: "/place", FileSystemID: "1"})),
    79  			},
    80  			want: []License{
    81  				NewLicenseFromLocations(
    82  					"MIT",
    83  					file.NewLocationFromCoordinates(file.Coordinates{RealPath: "/place", FileSystemID: "1"}),
    84  					file.NewLocationFromCoordinates(file.Coordinates{RealPath: "/place", FileSystemID: "2"}),
    85  				),
    86  			},
    87  		},
    88  		{
    89  			name: "same license from different sources",
    90  			licenses: []License{
    91  				NewLicense("MIT"),
    92  				NewLicenseFromLocations("MIT", file.NewLocation("/place")),
    93  				NewLicenseFromURLs("MIT", "https://example.com"),
    94  			},
    95  			want: []License{
    96  				{
    97  					Value:          "MIT",
    98  					SPDXExpression: "MIT",
    99  					Type:           license.Declared,
   100  					URLs:           internal.NewStringSet("https://example.com"),
   101  					Locations:      file.NewLocationSet(file.NewLocation("/place")),
   102  				},
   103  			},
   104  		},
   105  		{
   106  			name: "different licenses from different sources with different types constitute two licenses",
   107  			licenses: []License{
   108  				NewLicenseFromType("MIT", license.Concluded),
   109  				NewLicenseFromType("MIT", license.Declared),
   110  				NewLicenseFromLocations("MIT", file.NewLocation("/place")),
   111  				NewLicenseFromURLs("MIT", "https://example.com"),
   112  			},
   113  			want: []License{
   114  				{
   115  					Value:          "MIT",
   116  					SPDXExpression: "MIT",
   117  					Type:           license.Concluded,
   118  					URLs:           internal.NewStringSet(),
   119  					Locations:      file.NewLocationSet(),
   120  				},
   121  				{
   122  					Value:          "MIT",
   123  					SPDXExpression: "MIT",
   124  					Type:           license.Declared,
   125  					URLs:           internal.NewStringSet("https://example.com"),
   126  					Locations:      file.NewLocationSet(file.NewLocation("/place")),
   127  				},
   128  			},
   129  		},
   130  	}
   131  	for _, tt := range tests {
   132  		t.Run(tt.name, func(t *testing.T) {
   133  			s := NewLicenseSet()
   134  			s.Add(tt.licenses...)
   135  			testMe := s.ToSlice()
   136  			assert.Equal(t, tt.want, testMe)
   137  		})
   138  	}
   139  }