github.com/devseccon/trivy@v0.47.1-0.20231123133102-bd902a0bd996/pkg/fanal/analyzer/language/nodejs/license/license_test.go (about)

     1  package license_test
     2  
     3  import (
     4  	"path/filepath"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/devseccon/trivy/pkg/fanal/analyzer/language/nodejs/license"
    11  	"github.com/devseccon/trivy/pkg/mapfs"
    12  )
    13  
    14  func Test_ParseLicenses(t *testing.T) {
    15  	tests := []struct {
    16  		name    string
    17  		dir     string
    18  		want    map[string][]string
    19  		wantErr string
    20  	}{
    21  		{
    22  			name: "happy",
    23  			dir:  filepath.Join("testdata", "happy"),
    24  			want: map[string][]string{
    25  				"package-a@0.0.1": {"CC-BY-SA-4.0"},
    26  				"package-b@0.0.1": {"MIT"},
    27  				"package-c@0.0.1": {"BSD-3-Clause"},
    28  				"package-d@0.0.1": {"BSD-3-Clause"},
    29  				"package-e@0.0.1": {"(GPL-3.0 OR LGPL-3.0 OR MPL-1.1 OR SEE LICENSE IN LICENSE)"},
    30  			},
    31  		},
    32  	}
    33  
    34  	for _, tt := range tests {
    35  		t.Run(tt.name, func(t *testing.T) {
    36  			fsys := mapfs.New()
    37  			require.NoError(t, fsys.CopyFilesUnder(tt.dir))
    38  
    39  			l := license.NewLicense(0.9)
    40  			licenses, err := l.Traverse(fsys, ".")
    41  			if tt.wantErr != "" {
    42  				assert.ErrorContainsf(t, err, tt.wantErr, tt.name)
    43  				return
    44  			}
    45  			require.NoError(t, err)
    46  			assert.Equal(t, tt.want, licenses)
    47  		})
    48  	}
    49  }
    50  
    51  func Test_IsLicenseRefToFile(t *testing.T) {
    52  	tests := []struct {
    53  		name         string
    54  		input        string
    55  		wantOk       bool
    56  		wantFileName string
    57  	}{
    58  		{
    59  			name:  "no ref to file",
    60  			input: "MIT",
    61  		},
    62  		{
    63  			name:         "empty input",
    64  			wantOk:       true,
    65  			wantFileName: "LICENSE",
    66  		},
    67  		{
    68  			name:         "happy `SEE LICENSE IN`",
    69  			input:        "SEE LICENSE IN LICENSE.md",
    70  			wantOk:       true,
    71  			wantFileName: "LICENSE.md",
    72  		},
    73  		{
    74  			name:   "sad `SEE LICENSE IN`",
    75  			input:  "SEE LICENSE IN ",
    76  			wantOk: false,
    77  		},
    78  		{
    79  			name:         "happy `LicenseRef-`",
    80  			input:        "LicenseRef-LICENSE.txt",
    81  			wantOk:       true,
    82  			wantFileName: "LICENSE.txt",
    83  		},
    84  		{
    85  			name:   "sad `LicenseRef-`",
    86  			input:  "LicenseRef-",
    87  			wantOk: false,
    88  		},
    89  	}
    90  
    91  	for _, tt := range tests {
    92  		t.Run(tt.name, func(t *testing.T) {
    93  			ok, licenseFileName := license.IsLicenseRefToFile(tt.input)
    94  			assert.Equal(t, ok, tt.wantOk)
    95  			assert.Equal(t, licenseFileName, tt.wantFileName)
    96  		})
    97  	}
    98  }