github.com/devseccon/trivy@v0.47.1-0.20231123133102-bd902a0bd996/pkg/fanal/analyzer/language/elixir/mix/mix_test.go (about)

     1  package mix
     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_mixLockAnalyzer_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.mix.lock",
    23  			want: &analyzer.AnalysisResult{
    24  				Applications: []types.Application{
    25  					{
    26  						Type:     types.Hex,
    27  						FilePath: "testdata/happy.mix.lock",
    28  						Libraries: types.Packages{
    29  							{
    30  								ID:      "bunt@0.2.0",
    31  								Name:    "bunt",
    32  								Version: "0.2.0",
    33  								Locations: []types.Location{
    34  									{
    35  										StartLine: 2,
    36  										EndLine:   2,
    37  									},
    38  								},
    39  							},
    40  						},
    41  					},
    42  				},
    43  			},
    44  		},
    45  		{
    46  			name:      "empty file",
    47  			inputFile: "testdata/empty.mix.lock",
    48  		},
    49  	}
    50  
    51  	for _, tt := range tests {
    52  		t.Run(tt.name, func(t *testing.T) {
    53  			f, err := os.Open(tt.inputFile)
    54  			require.NoError(t, err)
    55  			defer func() {
    56  				err = f.Close()
    57  				assert.NoError(t, err)
    58  			}()
    59  
    60  			a := mixLockAnalyzer{}
    61  			got, err := a.Analyze(nil, analyzer.AnalysisInput{
    62  				FilePath: tt.inputFile,
    63  				Content:  f,
    64  			})
    65  
    66  			assert.NoError(t, err)
    67  			assert.Equal(t, tt.want, got)
    68  		})
    69  	}
    70  }
    71  
    72  func Test_mixLockAnalyzer_Required(t *testing.T) {
    73  	tests := []struct {
    74  		name     string
    75  		filePath string
    76  		want     bool
    77  	}{
    78  		{
    79  			name:     "happy path",
    80  			filePath: "mix.lock",
    81  			want:     true,
    82  		},
    83  		{
    84  			name:     "sad path",
    85  			filePath: "test.txt",
    86  			want:     false,
    87  		},
    88  	}
    89  	for _, tt := range tests {
    90  		t.Run(tt.name, func(t *testing.T) {
    91  			a := mixLockAnalyzer{}
    92  			got := a.Required(tt.filePath, nil)
    93  			assert.Equal(t, tt.want, got)
    94  		})
    95  	}
    96  }