github.com/devseccon/trivy@v0.47.1-0.20231123133102-bd902a0bd996/pkg/fanal/analyzer/language/php/composer/composer_test.go (about)

     1  package composer
     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_composerAnalyzer_PostAnalyze(t *testing.T) {
    16  	tests := []struct {
    17  		name string
    18  		dir  string
    19  		want *analyzer.AnalysisResult
    20  	}{
    21  		{
    22  			name: "happy path",
    23  			dir:  "testdata/happy",
    24  			want: &analyzer.AnalysisResult{
    25  				Applications: []types.Application{
    26  					{
    27  						Type:     types.Composer,
    28  						FilePath: "composer.lock",
    29  						Libraries: types.Packages{
    30  							{
    31  								ID:       "pear/log@1.13.3",
    32  								Name:     "pear/log",
    33  								Version:  "1.13.3",
    34  								Indirect: false,
    35  								Licenses: []string{"MIT"},
    36  								Locations: []types.Location{
    37  									{
    38  										StartLine: 9,
    39  										EndLine:   68,
    40  									},
    41  								},
    42  								DependsOn: []string{"pear/pear_exception@v1.0.2"},
    43  							},
    44  							{
    45  								ID:       "pear/pear_exception@v1.0.2",
    46  								Name:     "pear/pear_exception",
    47  								Version:  "v1.0.2",
    48  								Indirect: true,
    49  								Licenses: []string{"BSD-2-Clause"},
    50  								Locations: []types.Location{
    51  									{
    52  										StartLine: 69,
    53  										EndLine:   127,
    54  									},
    55  								},
    56  							},
    57  						},
    58  					},
    59  				},
    60  			},
    61  		},
    62  		{
    63  			name: "no composer.json",
    64  			dir:  "testdata/no-composer-json",
    65  			want: &analyzer.AnalysisResult{
    66  				Applications: []types.Application{
    67  					{
    68  						Type:     types.Composer,
    69  						FilePath: "composer.lock",
    70  						Libraries: types.Packages{
    71  							{
    72  								ID:       "pear/log@1.13.3",
    73  								Name:     "pear/log",
    74  								Version:  "1.13.3",
    75  								Indirect: false,
    76  								Licenses: []string{"MIT"},
    77  								Locations: []types.Location{
    78  									{
    79  										StartLine: 9,
    80  										EndLine:   68,
    81  									},
    82  								},
    83  								DependsOn: []string{"pear/pear_exception@v1.0.2"},
    84  							},
    85  							{
    86  								ID:       "pear/pear_exception@v1.0.2",
    87  								Name:     "pear/pear_exception",
    88  								Version:  "v1.0.2",
    89  								Indirect: false,
    90  								Licenses: []string{"BSD-2-Clause"},
    91  								Locations: []types.Location{
    92  									{
    93  										StartLine: 69,
    94  										EndLine:   127,
    95  									},
    96  								},
    97  							},
    98  						},
    99  					},
   100  				},
   101  			},
   102  		},
   103  		{
   104  			name: "wrong composer.json",
   105  			dir:  "testdata/wrong-composer-json",
   106  			want: &analyzer.AnalysisResult{
   107  				Applications: []types.Application{
   108  					{
   109  						Type:     types.Composer,
   110  						FilePath: "composer.lock",
   111  						Libraries: types.Packages{
   112  							{
   113  								ID:       "pear/log@1.13.3",
   114  								Name:     "pear/log",
   115  								Version:  "1.13.3",
   116  								Indirect: false,
   117  								Licenses: []string{"MIT"},
   118  								Locations: []types.Location{
   119  									{
   120  										StartLine: 9,
   121  										EndLine:   68,
   122  									},
   123  								},
   124  								DependsOn: []string{"pear/pear_exception@v1.0.2"},
   125  							},
   126  							{
   127  								ID:       "pear/pear_exception@v1.0.2",
   128  								Name:     "pear/pear_exception",
   129  								Version:  "v1.0.2",
   130  								Indirect: false,
   131  								Licenses: []string{"BSD-2-Clause"},
   132  								Locations: []types.Location{
   133  									{
   134  										StartLine: 69,
   135  										EndLine:   127,
   136  									},
   137  								},
   138  							},
   139  						},
   140  					},
   141  				},
   142  			},
   143  		},
   144  		{
   145  			name: "broken composer.lock",
   146  			dir:  "testdata/sad",
   147  			want: &analyzer.AnalysisResult{},
   148  		},
   149  	}
   150  
   151  	for _, tt := range tests {
   152  		t.Run(tt.name, func(t *testing.T) {
   153  			a, err := newComposerAnalyzer(analyzer.AnalyzerOptions{})
   154  			require.NoError(t, err)
   155  
   156  			got, err := a.PostAnalyze(context.Background(), analyzer.PostAnalysisInput{
   157  				FS: os.DirFS(tt.dir),
   158  			})
   159  
   160  			assert.NoError(t, err)
   161  			assert.Equal(t, tt.want, got)
   162  		})
   163  	}
   164  }