Skip to content

Commit c48c9bd

Browse files
committed
extend cleanup
1 parent a6a6f8e commit c48c9bd

File tree

11 files changed

+5422
-4141
lines changed

11 files changed

+5422
-4141
lines changed

.github/actions/cache/__tests__/cleanup.test.js

+62-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ const getInputMock = jest.spyOn(core, 'getInput').mockImplementation();
1212
const setFailedMock = jest.spyOn(core, 'setFailed').mockImplementation();
1313

1414
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+
);
1621

1722
const cacheFiles = ['cache_1.cache', 'cache_2.cache', 'cache_3.cache'];
1823
const minAccessTime = 7 * 24 * 60 * 60 * 1000; // 1 week
@@ -283,4 +288,60 @@ describe('cleanup', () => {
283288
// check that sub directory exists
284289
expect(fs.existsSync(cacheSubPath)).toBe(true);
285290
});
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+
});
286347
});

.github/actions/cache/__tests__/save.test.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ describe('save', () => {
9595

9696
it('Cache files: absent remote cache dir', async () => {
9797
// Set the action's inputs as return values from core.getInput()
98-
const cacheRemotePathAbsent = path.join(tempDir, 'cache_remote_absent');
98+
const cacheRemotePathAbsent = path.join(
99+
tempDir,
100+
'cache_remote_absent',
101+
'subdir'
102+
);
99103
getInputMock.mockImplementation(name => {
100104
switch (name) {
101105
case 'cache-path':

.github/actions/cache/cleanup/action.yml

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ inputs:
2828
evicted to limit the total cache storage'
2929
default: 50
3030
required: false
31+
recursive:
32+
description: 'Scan the current directory only or all subdirectories'
33+
default: false
34+
required: false
3135

3236
runs:
3337
using: 'node20'

0 commit comments

Comments
 (0)