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

     1  package pnpm
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/devseccon/trivy/pkg/fanal/analyzer"
    12  	"github.com/devseccon/trivy/pkg/fanal/types"
    13  )
    14  
    15  func Test_pnpmPkgLibraryAnalyzer_Analyze(t *testing.T) {
    16  	tests := []struct {
    17  		name      string
    18  		inputFile string
    19  		want      *analyzer.AnalysisResult
    20  		wantErr   string
    21  	}{
    22  		{
    23  			name:      "happy path",
    24  			inputFile: "testdata/pnpm-lock.yaml",
    25  			want: &analyzer.AnalysisResult{
    26  				Applications: []types.Application{
    27  					{
    28  						Type:     types.Pnpm,
    29  						FilePath: "testdata/pnpm-lock.yaml",
    30  						Libraries: types.Packages{
    31  							{
    32  								ID:      "lodash@4.17.21",
    33  								Name:    "lodash",
    34  								Version: "4.17.21",
    35  							},
    36  						},
    37  					},
    38  				},
    39  			},
    40  		},
    41  	}
    42  	for _, tt := range tests {
    43  		t.Run(tt.name, func(t *testing.T) {
    44  			f, err := os.Open(tt.inputFile)
    45  			require.NoError(t, err)
    46  			defer f.Close()
    47  
    48  			a := pnpmLibraryAnalyzer{}
    49  			ctx := context.Background()
    50  			got, err := a.Analyze(ctx, analyzer.AnalysisInput{
    51  				FilePath: tt.inputFile,
    52  				Content:  f,
    53  			})
    54  
    55  			if tt.wantErr != "" {
    56  				require.NotNil(t, err)
    57  				assert.Contains(t, err.Error(), tt.wantErr)
    58  				return
    59  			}
    60  
    61  			assert.NoError(t, err)
    62  			assert.Equal(t, tt.want, got)
    63  		})
    64  	}
    65  }