Skip to content

Commit aae1567

Browse files
committed
Tests: do not mock getInput with an incompatible implementation
1 parent 448593e commit aae1567

1 file changed

Lines changed: 23 additions & 29 deletions

File tree

__tests__/cache-save.test.ts

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ describe('run', () => {
2020
let debugSpy: jest.SpyInstance;
2121
let saveStateSpy: jest.SpyInstance;
2222
let getStateSpy: jest.SpyInstance;
23-
let getInputSpy: jest.SpyInstance;
2423
let setFailedSpy: jest.SpyInstance;
2524

2625
// cache spy
@@ -29,10 +28,17 @@ describe('run', () => {
2928
// exec spy
3029
let getExecOutputSpy: jest.SpyInstance;
3130

32-
let inputs = {} as any;
31+
function setInput(name: string, value: string): void {
32+
process.env[`INPUT_${name.replace(/ /g, '_').toUpperCase()}`] = value;
33+
}
3334

3435
beforeEach(() => {
3536
process.env['RUNNER_OS'] = process.env['RUNNER_OS'] ?? 'linux';
37+
for(const key in process.env) {
38+
if(key.startsWith('INPUT_')) {
39+
delete process.env[key];
40+
}
41+
}
3642

3743
infoSpy = jest.spyOn(core, 'info');
3844
infoSpy.mockImplementation(input => undefined);
@@ -56,9 +62,6 @@ describe('run', () => {
5662

5763
setFailedSpy = jest.spyOn(core, 'setFailed');
5864

59-
getInputSpy = jest.spyOn(core, 'getInput');
60-
getInputSpy.mockImplementation(input => inputs[input]);
61-
6265
getExecOutputSpy = jest.spyOn(exec, 'getExecOutput');
6366
getExecOutputSpy.mockImplementation((input: string) => {
6467
if (input.includes('pip')) {
@@ -74,10 +77,9 @@ describe('run', () => {
7477

7578
describe('Package manager validation', () => {
7679
it('Package manager is not provided, skip caching', async () => {
77-
inputs['cache'] = '';
80+
setInput('cache', '');
7881
await run();
7982

80-
expect(getInputSpy).toHaveBeenCalled();
8183
expect(infoSpy).not.toHaveBeenCalled();
8284
expect(saveCacheSpy).not.toHaveBeenCalled();
8385
expect(setFailedSpy).not.toHaveBeenCalled();
@@ -86,12 +88,11 @@ describe('run', () => {
8688

8789
describe('Validate unchanged cache is not saved', () => {
8890
it('should not save cache for pip', async () => {
89-
inputs['cache'] = 'pip';
90-
inputs['python-version'] = '3.10.0';
91+
setInput('cache', 'pip');
92+
setInput('python-version', '3.10.0');
9193

9294
await run();
9395

94-
expect(getInputSpy).toHaveBeenCalled();
9596
expect(debugSpy).toHaveBeenCalledWith(
9697
`paths for caching are ${__dirname}`
9798
);
@@ -103,12 +104,11 @@ describe('run', () => {
103104
});
104105

105106
it('should not save cache for pipenv', async () => {
106-
inputs['cache'] = 'pipenv';
107-
inputs['python-version'] = '3.10.0';
107+
setInput('cache', 'pipenv');
108+
setInput('python-version', '3.10.0');
108109

109110
await run();
110111

111-
expect(getInputSpy).toHaveBeenCalled();
112112
expect(debugSpy).toHaveBeenCalledWith(
113113
`paths for caching are ${__dirname}`
114114
);
@@ -122,8 +122,8 @@ describe('run', () => {
122122

123123
describe('action saves the cache', () => {
124124
it('saves cache from pip', async () => {
125-
inputs['cache'] = 'pip';
126-
inputs['python-version'] = '3.10.0';
125+
setInput('cache', 'pip');
126+
setInput('python-version', '3.10.0');
127127
getStateSpy.mockImplementation((name: string) => {
128128
if (name === State.CACHE_MATCHED_KEY) {
129129
return requirementsHash;
@@ -136,7 +136,6 @@ describe('run', () => {
136136

137137
await run();
138138

139-
expect(getInputSpy).toHaveBeenCalled();
140139
expect(getStateSpy).toHaveBeenCalledTimes(3);
141140
expect(infoSpy).not.toHaveBeenCalledWith(
142141
`Cache hit occurred on the primary key ${requirementsHash}, not saving cache.`
@@ -149,8 +148,8 @@ describe('run', () => {
149148
});
150149

151150
it('saves cache from pipenv', async () => {
152-
inputs['cache'] = 'pipenv';
153-
inputs['python-version'] = '3.10.0';
151+
setInput('cache', 'pipenv');
152+
setInput('python-version', '3.10.0');
154153
getStateSpy.mockImplementation((name: string) => {
155154
if (name === State.CACHE_MATCHED_KEY) {
156155
return pipFileLockHash;
@@ -163,7 +162,6 @@ describe('run', () => {
163162

164163
await run();
165164

166-
expect(getInputSpy).toHaveBeenCalled();
167165
expect(getStateSpy).toHaveBeenCalledTimes(3);
168166
expect(infoSpy).not.toHaveBeenCalledWith(
169167
`Cache hit occurred on the primary key ${pipFileLockHash}, not saving cache.`
@@ -176,8 +174,8 @@ describe('run', () => {
176174
});
177175

178176
it('saves cache from poetry', async () => {
179-
inputs['cache'] = 'poetry';
180-
inputs['python-version'] = '3.10.0';
177+
setInput('cache', 'poetry');
178+
setInput('python-version', '3.10.0');
181179
getStateSpy.mockImplementation((name: string) => {
182180
if (name === State.CACHE_MATCHED_KEY) {
183181
return poetryLockHash;
@@ -190,7 +188,6 @@ describe('run', () => {
190188

191189
await run();
192190

193-
expect(getInputSpy).toHaveBeenCalled();
194191
expect(getStateSpy).toHaveBeenCalledTimes(3);
195192
expect(infoSpy).not.toHaveBeenCalledWith(
196193
`Cache hit occurred on the primary key ${poetryLockHash}, not saving cache.`
@@ -203,8 +200,8 @@ describe('run', () => {
203200
});
204201

205202
it('saves with -1 cacheId , should not fail workflow', async () => {
206-
inputs['cache'] = 'poetry';
207-
inputs['python-version'] = '3.10.0';
203+
setInput('cache', 'poetry');
204+
setInput('python-version', '3.10.0');
208205
getStateSpy.mockImplementation((name: string) => {
209206
if (name === State.STATE_CACHE_PRIMARY_KEY) {
210207
return poetryLockHash;
@@ -221,7 +218,6 @@ describe('run', () => {
221218

222219
await run();
223220

224-
expect(getInputSpy).toHaveBeenCalled();
225221
expect(getStateSpy).toHaveBeenCalledTimes(3);
226222
expect(infoSpy).not.toHaveBeenCalled();
227223
expect(saveCacheSpy).toHaveBeenCalled();
@@ -232,8 +228,8 @@ describe('run', () => {
232228
});
233229

234230
it('saves with error from toolkit, should not fail the workflow', async () => {
235-
inputs['cache'] = 'npm';
236-
inputs['python-version'] = '3.10.0';
231+
setInput('cache', 'npm');
232+
setInput('python-version', '3.10.0');
237233
getStateSpy.mockImplementation((name: string) => {
238234
if (name === State.STATE_CACHE_PRIMARY_KEY) {
239235
return poetryLockHash;
@@ -250,7 +246,6 @@ describe('run', () => {
250246

251247
await run();
252248

253-
expect(getInputSpy).toHaveBeenCalled();
254249
expect(getStateSpy).toHaveBeenCalledTimes(3);
255250
expect(infoSpy).not.toHaveBeenCalledWith();
256251
expect(saveCacheSpy).toHaveBeenCalled();
@@ -261,6 +256,5 @@ describe('run', () => {
261256
afterEach(() => {
262257
jest.resetAllMocks();
263258
jest.clearAllMocks();
264-
inputs = {};
265259
});
266260
});

0 commit comments

Comments
 (0)