github.com/devseccon/trivy@v0.47.1-0.20231123133102-bd902a0bd996/pkg/fanal/analyzer/language/python/pip/pip_test.go (about) 1 package pip 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_pipAnalyzer_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/requirements.txt", 25 want: &analyzer.AnalysisResult{ 26 Applications: []types.Application{ 27 { 28 Type: types.Pip, 29 FilePath: "testdata/requirements.txt", 30 Libraries: types.Packages{ 31 { 32 Name: "click", 33 Version: "8.0.0", 34 }, 35 { 36 Name: "Flask", 37 Version: "2.0.0", 38 }, 39 { 40 Name: "itsdangerous", 41 Version: "2.0.0", 42 }, 43 }, 44 }, 45 }, 46 }, 47 }, 48 { 49 name: "happy path with not related filename", 50 inputFile: "testdata/not-related.txt", 51 want: nil, 52 }, 53 } 54 for _, tt := range tests { 55 t.Run(tt.name, func(t *testing.T) { 56 f, err := os.Open(tt.inputFile) 57 require.NoError(t, err) 58 defer f.Close() 59 60 a := pipLibraryAnalyzer{} 61 ctx := context.Background() 62 got, err := a.Analyze(ctx, analyzer.AnalysisInput{ 63 FilePath: tt.inputFile, 64 Content: f, 65 }) 66 67 if tt.wantErr != "" { 68 require.NotNil(t, err) 69 assert.Contains(t, err.Error(), tt.wantErr) 70 return 71 } 72 assert.NoError(t, err) 73 assert.Equal(t, tt.want, got) 74 }) 75 } 76 } 77 78 func Test_pipAnalyzer_Required(t *testing.T) { 79 tests := []struct { 80 name string 81 filePath string 82 want bool 83 }{ 84 { 85 name: "happy", 86 filePath: "test/requirements.txt", 87 want: true, 88 }, 89 { 90 name: "sad", 91 filePath: "a/b/c/d/test.sum", 92 want: false, 93 }, 94 } 95 for _, tt := range tests { 96 t.Run(tt.name, func(t *testing.T) { 97 a := pipLibraryAnalyzer{} 98 got := a.Required(tt.filePath, nil) 99 assert.Equal(t, tt.want, got) 100 }) 101 } 102 }