mirror of
https://github.com/actions/setup-python.git
synced 2026-01-19 19:38:56 +08:00
Merge 0c7b7994e2 into 4f41a90a1f
This commit is contained in:
commit
c8852962bf
10 changed files with 366 additions and 18 deletions
42
__tests__/clean-pip.test.ts
Normal file
42
__tests__/clean-pip.test.ts
Normal 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.');
|
||||
});
|
||||
});
|
||||
|
|
@ -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();
|
||||
Loading…
Add table
Add a link
Reference in a new issue