github.com/devseccon/trivy@v0.47.1-0.20231123133102-bd902a0bd996/pkg/fanal/analyzer/language/ruby/gemspec/gemspec_test.go (about) 1 package gemspec 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_gemspecLibraryAnalyzer_Analyze(t *testing.T) { 16 tests := []struct { 17 name string 18 inputFile string 19 includeChecksum bool 20 want *analyzer.AnalysisResult 21 wantErr string 22 }{ 23 { 24 name: "happy path", 25 inputFile: "testdata/multiple_licenses.gemspec", 26 want: &analyzer.AnalysisResult{ 27 Applications: []types.Application{ 28 { 29 Type: types.GemSpec, 30 FilePath: "testdata/multiple_licenses.gemspec", 31 Libraries: types.Packages{ 32 { 33 Name: "test-unit", 34 Version: "3.3.7", 35 Licenses: []string{ 36 "Ruby", 37 "BSDL", 38 "PSFL", 39 }, 40 FilePath: "testdata/multiple_licenses.gemspec", 41 }, 42 }, 43 }, 44 }, 45 }, 46 }, 47 { 48 name: "happy path with checksum", 49 inputFile: "testdata/multiple_licenses.gemspec", 50 includeChecksum: true, 51 want: &analyzer.AnalysisResult{ 52 Applications: []types.Application{ 53 { 54 Type: types.GemSpec, 55 FilePath: "testdata/multiple_licenses.gemspec", 56 Libraries: types.Packages{ 57 { 58 Name: "test-unit", 59 Version: "3.3.7", 60 Licenses: []string{ 61 "Ruby", 62 "BSDL", 63 "PSFL", 64 }, 65 FilePath: "testdata/multiple_licenses.gemspec", 66 Digest: "sha1:6ba7904180fad7e09f224cd3e4d449ea53401fb9", 67 }, 68 }, 69 }, 70 }, 71 }, 72 }, 73 { 74 name: "empty name", 75 inputFile: "testdata/empty_name.gemspec", 76 want: nil, 77 wantErr: "failed to parse", 78 }, 79 } 80 for _, tt := range tests { 81 t.Run(tt.name, func(t *testing.T) { 82 f, err := os.Open(tt.inputFile) 83 require.NoError(t, err) 84 defer f.Close() 85 86 a := gemspecLibraryAnalyzer{} 87 ctx := context.Background() 88 got, err := a.Analyze(ctx, analyzer.AnalysisInput{ 89 FilePath: tt.inputFile, 90 Content: f, 91 Options: analyzer.AnalysisOptions{FileChecksum: tt.includeChecksum}, 92 }) 93 94 if tt.wantErr != "" { 95 require.NotNil(t, err) 96 assert.Contains(t, err.Error(), tt.wantErr) 97 return 98 } 99 assert.NoError(t, err) 100 assert.Equal(t, tt.want, got) 101 }) 102 } 103 } 104 105 func Test_gemspecLibraryAnalyzer_Required(t *testing.T) { 106 tests := []struct { 107 name string 108 filePath string 109 want bool 110 }{ 111 { 112 name: "with default", 113 filePath: "usr/ank/specifications/default/ank.gemspec", 114 want: true, 115 }, 116 { 117 name: "without default", 118 filePath: "usr/ank/specifications/ank.gemspec", 119 want: true, 120 }, 121 { 122 name: "without dot", 123 filePath: "usr/ank/specifications/ankgemspec", 124 want: false, 125 }, 126 { 127 name: "source gemspec", 128 filePath: "/localtRepo/default/ank.gemspec", 129 want: false, 130 }, 131 } 132 for _, tt := range tests { 133 t.Run(tt.name, func(t *testing.T) { 134 a := gemspecLibraryAnalyzer{} 135 got := a.Required(tt.filePath, nil) 136 assert.Equal(t, tt.want, got) 137 }) 138 } 139 }