github.com/lineaje-labs/syft@v0.98.1-0.20231227153149-9e393f60ff1b/syft/pkg/cataloger/r/package_test.go (about) 1 package r 2 3 import ( 4 "testing" 5 6 "github.com/anchore/syft/syft/pkg" 7 ) 8 9 func Test_NewPackageLicenses(t *testing.T) { 10 testCases := []struct { 11 name string 12 pd parseData 13 want []pkg.License 14 }{ 15 { 16 "License field with single valid spdx", 17 parseData{ 18 Package: "Foo", 19 Version: "1", 20 License: "MIT", 21 }, 22 []pkg.License{ 23 pkg.NewLicense("MIT"), 24 }, 25 }, 26 { 27 "License field with single version separator no +", 28 parseData{ 29 Package: "Bar", 30 Version: "2", 31 License: "LGPL (== 2.0)", 32 }, 33 []pkg.License{ 34 pkg.NewLicense("LGPL2.0"), 35 }, 36 }, 37 { 38 "License field with multiple version separator", 39 parseData{ 40 Package: "Bar", 41 Version: "2", 42 License: "LGPL (>= 2.0, < 3)", 43 }, 44 []pkg.License{ 45 pkg.NewLicense("LGPL2.0+"), 46 }, 47 }, 48 { 49 "License field with file reference", 50 parseData{ 51 Package: "Baz", 52 Version: "3", 53 License: "GPL-2 + file LICENSE", 54 }, 55 []pkg.License{ 56 pkg.NewLicense("GPL-2"), 57 }, 58 }, 59 { 60 "License field which covers no case", 61 parseData{ 62 Package: "Baz", 63 Version: "3", 64 License: "Mozilla Public License", 65 }, 66 []pkg.License{ 67 pkg.NewLicense("Mozilla Public License"), 68 }, 69 }, 70 { 71 "License field with multiple cases", 72 parseData{ 73 Package: "Baz", 74 Version: "3", 75 License: "GPL-2 | file LICENSE | LGPL (>= 2.0)", 76 }, 77 []pkg.License{ 78 pkg.NewLicense("GPL-2"), 79 pkg.NewLicense("LGPL2.0+"), 80 }, 81 }, 82 } 83 84 for _, tt := range testCases { 85 t.Run(tt.name, func(t *testing.T) { 86 got := parseLicenseData(tt.pd.License) 87 if len(got) != len(tt.want) { 88 t.Errorf("unexpected number of licenses: got=%d, want=%d", len(got), len(tt.want)) 89 } 90 91 for _, wantLicense := range tt.want { 92 found := false 93 for _, gotLicense := range got { 94 if wantLicense.Type == gotLicense.Type && 95 wantLicense.SPDXExpression == gotLicense.SPDXExpression && 96 wantLicense.Value == gotLicense.Value { 97 found = true 98 } 99 } 100 if !found { 101 t.Errorf("could not find expected license: %+v; got: %+v", wantLicense, got) 102 } 103 } 104 }) 105 } 106 }