github.com/devseccon/trivy@v0.47.1-0.20231123133102-bd902a0bd996/pkg/fanal/analyzer/language/java/gradle/lockfile_test.go (about)

     1  package gradle
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/devseccon/trivy/pkg/fanal/analyzer"
    11  	"github.com/devseccon/trivy/pkg/fanal/types"
    12  )
    13  
    14  func Test_gradleLockAnalyzer_Analyze(t *testing.T) {
    15  	tests := []struct {
    16  		name      string
    17  		inputFile string
    18  		want      *analyzer.AnalysisResult
    19  	}{
    20  		{
    21  			name:      "happy path",
    22  			inputFile: "testdata/happy.lockfile",
    23  			want: &analyzer.AnalysisResult{
    24  				Applications: []types.Application{
    25  					{
    26  						Type:     types.Gradle,
    27  						FilePath: "testdata/happy.lockfile",
    28  						Libraries: types.Packages{
    29  							{
    30  								Name:    "com.example:example",
    31  								Version: "0.0.1",
    32  							},
    33  						},
    34  					},
    35  				},
    36  			},
    37  		},
    38  		{
    39  			name:      "empty file",
    40  			inputFile: "testdata/empty.lockfile",
    41  		},
    42  	}
    43  
    44  	for _, tt := range tests {
    45  		t.Run(tt.name, func(t *testing.T) {
    46  			f, err := os.Open(tt.inputFile)
    47  			require.NoError(t, err)
    48  			defer func() {
    49  				err = f.Close()
    50  				assert.NoError(t, err)
    51  			}()
    52  
    53  			a := gradleLockAnalyzer{}
    54  			got, err := a.Analyze(nil, analyzer.AnalysisInput{
    55  				FilePath: tt.inputFile,
    56  				Content:  f,
    57  			})
    58  
    59  			assert.NoError(t, err)
    60  			assert.Equal(t, tt.want, got)
    61  		})
    62  	}
    63  }
    64  
    65  func Test_nugetLibraryAnalyzer_Required(t *testing.T) {
    66  	tests := []struct {
    67  		name     string
    68  		filePath string
    69  		want     bool
    70  	}{
    71  		{
    72  			name:     "default name",
    73  			filePath: "test/gradle.lockfile",
    74  			want:     true,
    75  		},
    76  		{
    77  			name:     "name with prefix",
    78  			filePath: "test/settings-gradle.lockfile",
    79  			want:     true,
    80  		},
    81  		{
    82  			name:     "txt",
    83  			filePath: "test/test.txt",
    84  			want:     false,
    85  		},
    86  	}
    87  	for _, tt := range tests {
    88  		t.Run(tt.name, func(t *testing.T) {
    89  			a := gradleLockAnalyzer{}
    90  			got := a.Required(tt.filePath, nil)
    91  			assert.Equal(t, tt.want, got)
    92  		})
    93  	}
    94  }