This commit is contained in:
Jean-Yves LENHOF 2026-01-01 17:31:37 -08:00 committed by GitHub
commit c8852962bf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 366 additions and 18 deletions

View file

@ -0,0 +1,42 @@
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import {cleanPipPackages} from '../src/clean-pip';
describe('cleanPipPackages', () => {
let infoSpy: jest.SpyInstance;
let setFailedSpy: jest.SpyInstance;
let execSpy: jest.SpyInstance;
beforeEach(() => {
infoSpy = jest.spyOn(core, 'info');
infoSpy.mockImplementation(() => undefined);
setFailedSpy = jest.spyOn(core, 'setFailed');
setFailedSpy.mockImplementation(() => undefined);
execSpy = jest.spyOn(exec, 'exec');
execSpy.mockImplementation(() => Promise.resolve(0));
});
afterEach(() => {
jest.resetAllMocks();
jest.clearAllMocks();
});
it('should successfully clean up pip packages', async () => {
await cleanPipPackages();
expect(execSpy).toHaveBeenCalledWith('bash', expect.any(Array));
expect(setFailedSpy).not.toHaveBeenCalled();
});
it('should handle errors and set failed status', async () => {
const error = new Error('Exec failed');
execSpy.mockImplementation(() => Promise.reject(error));
await cleanPipPackages();
expect(execSpy).toHaveBeenCalledWith('bash', expect.any(Array));
expect(setFailedSpy).toHaveBeenCalledWith('Failed to clean up pip packages.');
});
});

View file

@ -1,7 +1,8 @@
import * as core from '@actions/core';
import * as cache from '@actions/cache';
import * as cleanPip from '../src/clean-pip';
import * as exec from '@actions/exec';
import {run} from '../src/cache-save';
import {run} from '../src/post-python';
import {State} from '../src/cache-distributions/cache-distributor';
describe('run', () => {
@ -21,10 +22,13 @@ describe('run', () => {
let saveStateSpy: jest.SpyInstance;
let getStateSpy: jest.SpyInstance;
let getInputSpy: jest.SpyInstance;
let getBooleanInputSpy: jest.SpyInstance;
let setFailedSpy: jest.SpyInstance;
// cache spy
let saveCacheSpy: jest.SpyInstance;
// cleanPipPackages spy
let cleanPipPackagesSpy: jest.SpyInstance;
// exec spy
let getExecOutputSpy: jest.SpyInstance;
@ -59,6 +63,9 @@ describe('run', () => {
getInputSpy = jest.spyOn(core, 'getInput');
getInputSpy.mockImplementation(input => inputs[input]);
getBooleanInputSpy = jest.spyOn(core, 'getBooleanInput');
getBooleanInputSpy.mockImplementation(input => inputs[input]);
getExecOutputSpy = jest.spyOn(exec, 'getExecOutput');
getExecOutputSpy.mockImplementation((input: string) => {
if (input.includes('pip')) {
@ -70,6 +77,9 @@ describe('run', () => {
saveCacheSpy = jest.spyOn(cache, 'saveCache');
saveCacheSpy.mockImplementation(() => undefined);
cleanPipPackagesSpy = jest.spyOn(cleanPip, 'cleanPipPackages');
cleanPipPackagesSpy.mockImplementation(() => undefined);
});
describe('Package manager validation', () => {
@ -258,6 +268,55 @@ describe('run', () => {
});
});
describe('run with postclean option', () => {
it('should clean pip packages when postclean is true', async () => {
inputs['cache'] = '';
inputs['postclean'] = true;
await run();
expect(getBooleanInputSpy).toHaveBeenCalledWith('postclean');
expect(cleanPipPackagesSpy).toHaveBeenCalled();
expect(setFailedSpy).not.toHaveBeenCalled();
});
it('should save cache and clean pip packages when both are enabled', async () => {
inputs['cache'] = 'pip';
inputs['postclean'] = true;
inputs['python-version'] = '3.10.0';
getStateSpy.mockImplementation((name: string) => {
if (name === State.CACHE_MATCHED_KEY) {
return requirementsHash;
} else if (name === State.CACHE_PATHS) {
return JSON.stringify([__dirname]);
} else {
return pipFileLockHash;
}
});
await run();
expect(getInputSpy).toHaveBeenCalled();
expect(getBooleanInputSpy).toHaveBeenCalledWith('postclean');
expect(saveCacheSpy).toHaveBeenCalled();
expect(cleanPipPackagesSpy).toHaveBeenCalled();
expect(setFailedSpy).not.toHaveBeenCalled();
});
it('should not clean pip packages when postclean is false', async () => {
inputs['cache'] = 'pip';
inputs['postclean'] = false;
inputs['python-version'] = '3.10.0';
await run();
expect(getBooleanInputSpy).toHaveBeenCalledWith('postclean');
expect(cleanPipPackagesSpy).not.toHaveBeenCalled();
expect(setFailedSpy).not.toHaveBeenCalled();
});
});
afterEach(() => {
jest.resetAllMocks();
jest.clearAllMocks();