@@ -12,7 +12,12 @@ const getInputMock = jest.spyOn(core, 'getInput').mockImplementation();
12
12
const setFailedMock = jest . spyOn ( core , 'setFailed' ) . mockImplementation ( ) ;
13
13
14
14
const tempDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , 'test-cleanup-' ) ) ;
15
- const cacheRemotePath = path . join ( tempDir , 'cache_remote' ) ;
15
+ const cacheRemotePath = path . join (
16
+ tempDir ,
17
+ 'subdir_1' ,
18
+ 'subdir_2' ,
19
+ 'cache_remote'
20
+ ) ;
16
21
17
22
const cacheFiles = [ 'cache_1.cache' , 'cache_2.cache' , 'cache_3.cache' ] ;
18
23
const minAccessTime = 7 * 24 * 60 * 60 * 1000 ; // 1 week
@@ -283,4 +288,60 @@ describe('cleanup', () => {
283
288
// check that sub directory exists
284
289
expect ( fs . existsSync ( cacheSubPath ) ) . toBe ( true ) ;
285
290
} ) ;
291
+
292
+ it ( 'Cleanup directory with subdirectories and files (recursive=true)' , async ( ) => {
293
+ const cacheSubPath = path . join ( tempDir , 'subdir_1' ) ;
294
+ // Set the action's inputs as return values from core.getInput()
295
+ getInputMock . mockImplementation ( name => {
296
+ switch ( name ) {
297
+ case 'cache-path' :
298
+ return cacheSubPath ;
299
+ case 'restore-keys' :
300
+ return 'cache' ;
301
+ case 'cache-size' :
302
+ return 1 ;
303
+ case 'recursive' :
304
+ return true ;
305
+ default :
306
+ return '' ;
307
+ }
308
+ } ) ;
309
+
310
+ await cleanupImpl . cleanUp ( ) ;
311
+
312
+ expect ( runMock ) . toHaveReturned ( ) ;
313
+ // cache2 and cache3 should be removed
314
+ for ( const cache of cacheFiles . slice ( 1 , 2 ) ) {
315
+ expect ( fs . existsSync ( path . join ( cacheRemotePath , cache ) ) ) . toBe ( false ) ;
316
+ }
317
+ // check that file1 exists
318
+ expect ( fs . existsSync ( path . join ( cacheRemotePath , cacheFiles [ 0 ] ) ) ) . toBe ( true ) ;
319
+ } ) ;
320
+
321
+ it ( 'Cleanup directory with subdirectories and files (recursive=false)' , async ( ) => {
322
+ const cacheSubPath = path . join ( tempDir , 'subdir_1' ) ;
323
+ // Set the action's inputs as return values from core.getInput()
324
+ getInputMock . mockImplementation ( name => {
325
+ switch ( name ) {
326
+ case 'cache-path' :
327
+ return cacheSubPath ;
328
+ case 'restore-keys' :
329
+ return 'cache' ;
330
+ case 'cache-size' :
331
+ return 1 ;
332
+ case 'recursive' :
333
+ return false ;
334
+ default :
335
+ return '' ;
336
+ }
337
+ } ) ;
338
+
339
+ await cleanupImpl . cleanUp ( ) ;
340
+
341
+ expect ( runMock ) . toHaveReturned ( ) ;
342
+ // cache2 and cache3 should be removed
343
+ for ( const cache of cacheFiles ) {
344
+ expect ( fs . existsSync ( path . join ( cacheRemotePath , cache ) ) ) . toBe ( true ) ;
345
+ }
346
+ } ) ;
286
347
} ) ;
0 commit comments