■ ■ ■ ■ ■ ■
syft/pkg/cataloger/portage/license_test.go
| 1 | + | package portage |
| 2 | + | |
| 3 | + | import ( |
| 4 | + | "strings" |
| 5 | + | "testing" |
| 6 | + | |
| 7 | + | "github.com/stretchr/testify/assert" |
| 8 | + | ) |
| 9 | + | |
| 10 | + | // you can get a good sense of test fixtures with: |
| 11 | + | // docker run --rm -it gentoo/stage3 bash -c 'find var/db/pkg/ | grep LICENSE | xargs cat' |
| 12 | + | |
| 13 | + | func Test_extractLicenses(t *testing.T) { |
| 14 | + | |
| 15 | + | tests := []struct { |
| 16 | + | name string |
| 17 | + | license string |
| 18 | + | want []string |
| 19 | + | }{ |
| 20 | + | { |
| 21 | + | name: "empty", |
| 22 | + | license: "", |
| 23 | + | want: []string{}, |
| 24 | + | }, |
| 25 | + | { |
| 26 | + | name: "single", |
| 27 | + | license: "GPL-2", |
| 28 | + | want: []string{"GPL-2"}, |
| 29 | + | }, |
| 30 | + | { |
| 31 | + | name: "multiple", |
| 32 | + | license: "GPL-2 GPL-3 ", // note the extra space |
| 33 | + | want: []string{"GPL-2", "GPL-3"}, |
| 34 | + | }, |
| 35 | + | // the following cases are NOT valid interpretations, but capture the behavior today. |
| 36 | + | // when we follow up later with SPDX license expressions, this can be fixed then. |
| 37 | + | { |
| 38 | + | name: "license choices", |
| 39 | + | license: "|| ( GPL-2 GPL-3 )", |
| 40 | + | // should allow for expression of "NONE OR (GPL-2 OR GPL-3)" or "GPL-2 OR GPL-3", |
| 41 | + | // I'm not certain which is correct (NONE isn't allowed, right?) |
| 42 | + | want: []string{"GPL-2", "GPL-3"}, |
| 43 | + | }, |
| 44 | + | { |
| 45 | + | name: "license choices with use flag", |
| 46 | + | license: "LGPL-2.1+ tools? ( GPL-2+ )", |
| 47 | + | want: []string{"GPL-2+", "LGPL-2.1+"}, // should allow for expression of "LGPL-2.1+ OR (LGPL-2.1+ AND GPL-2+)" |
| 48 | + | }, |
| 49 | + | { |
| 50 | + | name: "license choices with unknown suffix", |
| 51 | + | license: "GPL-3+ LGPL-3+ || ( GPL-3+ libgcc libstdc++ gcc-runtime-library-exception-3.1 ) FDL-1.3+", |
| 52 | + | want: []string{ |
| 53 | + | "FDL-1.3+", // is it right to include this? what does this represent since a useflag was not specified? |
| 54 | + | "GPL-3+", |
| 55 | + | "LGPL-3+", |
| 56 | + | "gcc-runtime-library-exception-3.1", |
| 57 | + | "libgcc", |
| 58 | + | "libstdc++", |
| 59 | + | }, |
| 60 | + | }, |
| 61 | + | } |
| 62 | + | for _, tt := range tests { |
| 63 | + | t.Run(tt.name, func(t *testing.T) { |
| 64 | + | assert.Equalf(t, tt.want, extractLicenses(strings.NewReader(tt.license)), "extractLicenses(%v)", tt.license) |
| 65 | + | }) |
| 66 | + | } |
| 67 | + | } |
| 68 | + | |