@@ -162,6 +162,95 @@ describe('gitleaks', () => {
162
162
expect ( result . steps ) . to . have . lengthOf ( 1 ) ;
163
163
expect ( result . steps [ 0 ] . error ) . to . be . true ;
164
164
expect ( stepSpy . calledWith ( '\nFound secret in file.txt\nWarning: potential leak' ) ) . to . be . true ;
165
- } ) ;
165
+ } ) ;
166
+
167
+ it ( 'should handle gitleaks execution failure' , async ( ) => {
168
+ stubs . getAPIs . returns ( { gitleaks : { enabled : true } } ) ;
169
+
170
+ const gitRootCommitMock = {
171
+ exitCode : 0 ,
172
+ stdout : 'rootcommit123\n' ,
173
+ stderr : ''
174
+ } ;
175
+
176
+ const gitleaksMock = {
177
+ exitCode : 1 ,
178
+ stdout : '' ,
179
+ stderr : 'Command failed'
180
+ } ;
181
+
182
+ stubs . spawn
183
+ . onFirstCall ( ) . returns ( {
184
+ on : ( event , cb ) => {
185
+ if ( event === 'close' ) cb ( gitRootCommitMock . exitCode ) ;
186
+ return { stdout : { on : ( ) => { } } , stderr : { on : ( ) => { } } } ;
187
+ } ,
188
+ stdout : { on : ( _ , cb ) => cb ( gitRootCommitMock . stdout ) } ,
189
+ stderr : { on : ( _ , cb ) => cb ( gitRootCommitMock . stderr ) }
190
+ } )
191
+ . onSecondCall ( ) . returns ( {
192
+ on : ( event , cb ) => {
193
+ if ( event === 'close' ) cb ( gitleaksMock . exitCode ) ;
194
+ return { stdout : { on : ( ) => { } } , stderr : { on : ( ) => { } } } ;
195
+ } ,
196
+ stdout : { on : ( _ , cb ) => cb ( gitleaksMock . stdout ) } ,
197
+ stderr : { on : ( _ , cb ) => cb ( gitleaksMock . stderr ) }
198
+ } ) ;
199
+
200
+ const result = await exec ( req , action ) ;
201
+
202
+ expect ( result . error ) . to . be . true ;
203
+ expect ( result . steps ) . to . have . lengthOf ( 1 ) ;
204
+ expect ( result . steps [ 0 ] . error ) . to . be . true ;
205
+ expect ( stepSpy . calledWith ( 'failed to run gitleaks, please contact an administrator\n' ) ) . to . be . true ;
206
+ } ) ;
207
+
208
+ it ( 'should handle custom config path' , async ( ) => {
209
+ stubs . getAPIs . returns ( {
210
+ gitleaks : {
211
+ enabled : true ,
212
+ configPath : `../fixtures/gitleaks-config.toml`
213
+ }
214
+ } ) ;
215
+
216
+ stubs . fs . stat . resolves ( { isFile : ( ) => true } ) ;
217
+ stubs . fs . access . resolves ( ) ;
218
+
219
+ const gitRootCommitMock = {
220
+ exitCode : 0 ,
221
+ stdout : 'rootcommit123\n' ,
222
+ stderr : ''
223
+ } ;
224
+
225
+ const gitleaksMock = {
226
+ exitCode : 0 ,
227
+ stdout : '' ,
228
+ stderr : 'No leaks found'
229
+ } ;
230
+
231
+ stubs . spawn
232
+ . onFirstCall ( ) . returns ( {
233
+ on : ( event , cb ) => {
234
+ if ( event === 'close' ) cb ( gitRootCommitMock . exitCode ) ;
235
+ return { stdout : { on : ( ) => { } } , stderr : { on : ( ) => { } } } ;
236
+ } ,
237
+ stdout : { on : ( _ , cb ) => cb ( gitRootCommitMock . stdout ) } ,
238
+ stderr : { on : ( _ , cb ) => cb ( gitRootCommitMock . stderr ) }
239
+ } )
240
+ . onSecondCall ( ) . returns ( {
241
+ on : ( event , cb ) => {
242
+ if ( event === 'close' ) cb ( gitleaksMock . exitCode ) ;
243
+ return { stdout : { on : ( ) => { } } , stderr : { on : ( ) => { } } } ;
244
+ } ,
245
+ stdout : { on : ( _ , cb ) => cb ( gitleaksMock . stdout ) } ,
246
+ stderr : { on : ( _ , cb ) => cb ( gitleaksMock . stderr ) }
247
+ } ) ;
248
+
249
+ const result = await exec ( req , action ) ;
250
+
251
+ expect ( result . error ) . to . be . false ;
252
+ expect ( result . steps [ 0 ] . error ) . to . be . false ;
253
+ expect ( stubs . spawn . secondCall . args [ 1 ] ) . to . include ( '--config=../fixtures/gitleaks-config.toml' ) ;
254
+ } ) ;
166
255
} ) ;
167
256
} ) ;
0 commit comments