3
0
Fork 0
mirror of https://gitea.com/actions/setup-python.git synced 2024-11-29 05:29:34 +01:00

Tests: do not mock getInput with an incompatible implementation

This commit is contained in:
Aarni Koskela 2023-11-10 15:33:21 +02:00
parent 3cfd90a803
commit 7e8faf07b9

View file

@ -20,7 +20,6 @@ describe('run', () => {
let debugSpy: jest.SpyInstance; let debugSpy: jest.SpyInstance;
let saveStateSpy: jest.SpyInstance; let saveStateSpy: jest.SpyInstance;
let getStateSpy: jest.SpyInstance; let getStateSpy: jest.SpyInstance;
let getInputSpy: jest.SpyInstance;
let setFailedSpy: jest.SpyInstance; let setFailedSpy: jest.SpyInstance;
// cache spy // cache spy
@ -29,10 +28,17 @@ describe('run', () => {
// exec spy // exec spy
let getExecOutputSpy: jest.SpyInstance; let getExecOutputSpy: jest.SpyInstance;
let inputs = {} as any; function setInput(name: string, value: string): void {
process.env[`INPUT_${name.replace(/ /g, '_').toUpperCase()}`] = value;
}
beforeEach(() => { beforeEach(() => {
process.env['RUNNER_OS'] = process.env['RUNNER_OS'] ?? 'linux'; process.env['RUNNER_OS'] = process.env['RUNNER_OS'] ?? 'linux';
for(const key in process.env) {
if(key.startsWith('INPUT_')) {
delete process.env[key];
}
}
infoSpy = jest.spyOn(core, 'info'); infoSpy = jest.spyOn(core, 'info');
infoSpy.mockImplementation(input => undefined); infoSpy.mockImplementation(input => undefined);
@ -56,9 +62,6 @@ describe('run', () => {
setFailedSpy = jest.spyOn(core, 'setFailed'); setFailedSpy = jest.spyOn(core, 'setFailed');
getInputSpy = jest.spyOn(core, 'getInput');
getInputSpy.mockImplementation(input => inputs[input]);
getExecOutputSpy = jest.spyOn(exec, 'getExecOutput'); getExecOutputSpy = jest.spyOn(exec, 'getExecOutput');
getExecOutputSpy.mockImplementation((input: string) => { getExecOutputSpy.mockImplementation((input: string) => {
if (input.includes('pip')) { if (input.includes('pip')) {
@ -74,10 +77,9 @@ describe('run', () => {
describe('Package manager validation', () => { describe('Package manager validation', () => {
it('Package manager is not provided, skip caching', async () => { it('Package manager is not provided, skip caching', async () => {
inputs['cache'] = ''; setInput('cache', '');
await run(); await run();
expect(getInputSpy).toHaveBeenCalled();
expect(infoSpy).not.toHaveBeenCalled(); expect(infoSpy).not.toHaveBeenCalled();
expect(saveCacheSpy).not.toHaveBeenCalled(); expect(saveCacheSpy).not.toHaveBeenCalled();
expect(setFailedSpy).not.toHaveBeenCalled(); expect(setFailedSpy).not.toHaveBeenCalled();
@ -86,12 +88,11 @@ describe('run', () => {
describe('Validate unchanged cache is not saved', () => { describe('Validate unchanged cache is not saved', () => {
it('should not save cache for pip', async () => { it('should not save cache for pip', async () => {
inputs['cache'] = 'pip'; setInput('cache', 'pip');
inputs['python-version'] = '3.10.0'; setInput('python-version', '3.10.0');
await run(); await run();
expect(getInputSpy).toHaveBeenCalled();
expect(debugSpy).toHaveBeenCalledWith( expect(debugSpy).toHaveBeenCalledWith(
`paths for caching are ${__dirname}` `paths for caching are ${__dirname}`
); );
@ -103,12 +104,11 @@ describe('run', () => {
}); });
it('should not save cache for pipenv', async () => { it('should not save cache for pipenv', async () => {
inputs['cache'] = 'pipenv'; setInput('cache', 'pipenv');
inputs['python-version'] = '3.10.0'; setInput('python-version', '3.10.0');
await run(); await run();
expect(getInputSpy).toHaveBeenCalled();
expect(debugSpy).toHaveBeenCalledWith( expect(debugSpy).toHaveBeenCalledWith(
`paths for caching are ${__dirname}` `paths for caching are ${__dirname}`
); );
@ -122,8 +122,8 @@ describe('run', () => {
describe('action saves the cache', () => { describe('action saves the cache', () => {
it('saves cache from pip', async () => { it('saves cache from pip', async () => {
inputs['cache'] = 'pip'; setInput('cache', 'pip');
inputs['python-version'] = '3.10.0'; setInput('python-version', '3.10.0');
getStateSpy.mockImplementation((name: string) => { getStateSpy.mockImplementation((name: string) => {
if (name === State.CACHE_MATCHED_KEY) { if (name === State.CACHE_MATCHED_KEY) {
return requirementsHash; return requirementsHash;
@ -136,7 +136,6 @@ describe('run', () => {
await run(); await run();
expect(getInputSpy).toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(3); expect(getStateSpy).toHaveBeenCalledTimes(3);
expect(infoSpy).not.toHaveBeenCalledWith( expect(infoSpy).not.toHaveBeenCalledWith(
`Cache hit occurred on the primary key ${requirementsHash}, not saving cache.` `Cache hit occurred on the primary key ${requirementsHash}, not saving cache.`
@ -149,8 +148,8 @@ describe('run', () => {
}); });
it('saves cache from pipenv', async () => { it('saves cache from pipenv', async () => {
inputs['cache'] = 'pipenv'; setInput('cache', 'pipenv');
inputs['python-version'] = '3.10.0'; setInput('python-version', '3.10.0');
getStateSpy.mockImplementation((name: string) => { getStateSpy.mockImplementation((name: string) => {
if (name === State.CACHE_MATCHED_KEY) { if (name === State.CACHE_MATCHED_KEY) {
return pipFileLockHash; return pipFileLockHash;
@ -163,7 +162,6 @@ describe('run', () => {
await run(); await run();
expect(getInputSpy).toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(3); expect(getStateSpy).toHaveBeenCalledTimes(3);
expect(infoSpy).not.toHaveBeenCalledWith( expect(infoSpy).not.toHaveBeenCalledWith(
`Cache hit occurred on the primary key ${pipFileLockHash}, not saving cache.` `Cache hit occurred on the primary key ${pipFileLockHash}, not saving cache.`
@ -176,8 +174,8 @@ describe('run', () => {
}); });
it('saves cache from poetry', async () => { it('saves cache from poetry', async () => {
inputs['cache'] = 'poetry'; setInput('cache', 'poetry');
inputs['python-version'] = '3.10.0'; setInput('python-version', '3.10.0');
getStateSpy.mockImplementation((name: string) => { getStateSpy.mockImplementation((name: string) => {
if (name === State.CACHE_MATCHED_KEY) { if (name === State.CACHE_MATCHED_KEY) {
return poetryLockHash; return poetryLockHash;
@ -190,7 +188,6 @@ describe('run', () => {
await run(); await run();
expect(getInputSpy).toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(3); expect(getStateSpy).toHaveBeenCalledTimes(3);
expect(infoSpy).not.toHaveBeenCalledWith( expect(infoSpy).not.toHaveBeenCalledWith(
`Cache hit occurred on the primary key ${poetryLockHash}, not saving cache.` `Cache hit occurred on the primary key ${poetryLockHash}, not saving cache.`
@ -203,8 +200,8 @@ describe('run', () => {
}); });
it('saves with -1 cacheId , should not fail workflow', async () => { it('saves with -1 cacheId , should not fail workflow', async () => {
inputs['cache'] = 'poetry'; setInput('cache', 'poetry');
inputs['python-version'] = '3.10.0'; setInput('python-version', '3.10.0');
getStateSpy.mockImplementation((name: string) => { getStateSpy.mockImplementation((name: string) => {
if (name === State.STATE_CACHE_PRIMARY_KEY) { if (name === State.STATE_CACHE_PRIMARY_KEY) {
return poetryLockHash; return poetryLockHash;
@ -221,7 +218,6 @@ describe('run', () => {
await run(); await run();
expect(getInputSpy).toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(3); expect(getStateSpy).toHaveBeenCalledTimes(3);
expect(infoSpy).not.toHaveBeenCalled(); expect(infoSpy).not.toHaveBeenCalled();
expect(saveCacheSpy).toHaveBeenCalled(); expect(saveCacheSpy).toHaveBeenCalled();
@ -232,8 +228,8 @@ describe('run', () => {
}); });
it('saves with error from toolkit, should not fail the workflow', async () => { it('saves with error from toolkit, should not fail the workflow', async () => {
inputs['cache'] = 'npm'; setInput('cache', 'npm');
inputs['python-version'] = '3.10.0'; setInput('python-version', '3.10.0');
getStateSpy.mockImplementation((name: string) => { getStateSpy.mockImplementation((name: string) => {
if (name === State.STATE_CACHE_PRIMARY_KEY) { if (name === State.STATE_CACHE_PRIMARY_KEY) {
return poetryLockHash; return poetryLockHash;
@ -250,7 +246,6 @@ describe('run', () => {
await run(); await run();
expect(getInputSpy).toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(3); expect(getStateSpy).toHaveBeenCalledTimes(3);
expect(infoSpy).not.toHaveBeenCalledWith(); expect(infoSpy).not.toHaveBeenCalledWith();
expect(saveCacheSpy).toHaveBeenCalled(); expect(saveCacheSpy).toHaveBeenCalled();
@ -261,6 +256,5 @@ describe('run', () => {
afterEach(() => { afterEach(() => {
jest.resetAllMocks(); jest.resetAllMocks();
jest.clearAllMocks(); jest.clearAllMocks();
inputs = {};
}); });
}); });