github.com/anchore/syft@v1.38.2/internal/licenses/find_evidence_test.go (about) 1 package licenses 2 3 import ( 4 "context" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/google/licensecheck" 10 "github.com/stretchr/testify/require" 11 ) 12 13 func TestDefaultScanner_FindEvidence(t *testing.T) { 14 testCases := []struct { 15 name string 16 fixture string 17 wantIDs []string // expected license IDs 18 minMatch int // minimum # of matches required 19 }{ 20 { 21 name: "Single licenses are able to be recognized and returned Apache 2.0", 22 fixture: "test-fixtures/apache-license-2.0", 23 wantIDs: []string{"Apache-2.0"}, 24 minMatch: 1, 25 }, 26 { 27 name: "Multiple Licenses are returned as evidence with duplicates at different offset", 28 fixture: "test-fixtures/multi-license", 29 wantIDs: []string{ 30 "MIT", 31 "MIT", 32 "NCSA", 33 "Apache-2.0", 34 "Zlib", 35 "Unlicense", 36 "BSD-2-Clause", 37 "BSD-2-Clause", 38 "BSD-3-Clause", 39 }, 40 minMatch: 2, 41 }, 42 } 43 44 scanner := testScanner() 45 for _, tc := range testCases { 46 t.Run(tc.name, func(t *testing.T) { 47 filePath := filepath.Clean(tc.fixture) 48 f, err := os.Open(filePath) 49 require.NoError(t, err) 50 defer f.Close() 51 52 evidence, content, err := scanner.FindEvidence(context.Background(), f) 53 require.NoError(t, err) 54 require.NotEmpty(t, content) 55 require.GreaterOrEqual(t, len(evidence), tc.minMatch, "expected at least %d matches", tc.minMatch) 56 57 var foundIDs []string 58 for _, ev := range evidence { 59 foundIDs = append(foundIDs, ev.ID) 60 } 61 62 require.ElementsMatch(t, tc.wantIDs, foundIDs, "expected license IDs %v, but got %v", tc.wantIDs, foundIDs) 63 }) 64 } 65 } 66 67 func testScanner() Scanner { 68 return &scanner{ 69 coverageThreshold: DefaultCoverageThreshold, 70 scanner: licensecheck.Scan, 71 } 72 } 73 74 func mustOpen(fixture string) []byte { 75 content, err := os.ReadFile(fixture) 76 if err != nil { 77 panic(err) 78 } 79 80 return content 81 }