Skip to content

Commit bea5baf

Browse files
authoredAug 10, 2023
change getinput to getstate for cache (#816)
1 parent d82f92a commit bea5baf

File tree

6 files changed

+62
-32
lines changed

6 files changed

+62
-32
lines changed
 

‎__tests__/cache-save.test.ts

+53-30
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ describe('run', () => {
9292

9393
it('Package manager is not valid, skip caching', async () => {
9494
inputs['cache'] = 'yarn3';
95+
getStateSpy.mockImplementation(key =>
96+
key === State.CachePackageManager ? inputs['cache'] : ''
97+
);
9598

9699
await run();
97100

@@ -108,7 +111,9 @@ describe('run', () => {
108111
it('should not save cache for yarn1', async () => {
109112
inputs['cache'] = 'yarn';
110113
getStateSpy.mockImplementation(key =>
111-
key === State.CachePrimaryKey || key === State.CacheMatchedKey
114+
key === State.CachePackageManager
115+
? inputs['cache']
116+
: key === State.CachePrimaryKey || key === State.CacheMatchedKey
112117
? yarnFileHash
113118
: key === State.CachePaths
114119
? '["/foo/bar"]'
@@ -117,8 +122,8 @@ describe('run', () => {
117122

118123
await run();
119124

120-
expect(getInputSpy).toHaveBeenCalled();
121-
expect(getStateSpy).toHaveBeenCalledTimes(3);
125+
expect(getInputSpy).not.toHaveBeenCalled();
126+
expect(getStateSpy).toHaveBeenCalledTimes(4);
122127
expect(getCommandOutputSpy).toHaveBeenCalledTimes(0);
123128
expect(debugSpy).toHaveBeenCalledTimes(0);
124129
expect(infoSpy).toHaveBeenCalledWith(
@@ -130,7 +135,9 @@ describe('run', () => {
130135
it('should not save cache for yarn2', async () => {
131136
inputs['cache'] = 'yarn';
132137
getStateSpy.mockImplementation(key =>
133-
key === State.CachePrimaryKey || key === State.CacheMatchedKey
138+
key === State.CachePackageManager
139+
? inputs['cache']
140+
: key === State.CachePrimaryKey || key === State.CacheMatchedKey
134141
? yarnFileHash
135142
: key === State.CachePaths
136143
? '["/foo/bar"]'
@@ -139,8 +146,8 @@ describe('run', () => {
139146

140147
await run();
141148

142-
expect(getInputSpy).toHaveBeenCalled();
143-
expect(getStateSpy).toHaveBeenCalledTimes(3);
149+
expect(getInputSpy).not.toHaveBeenCalled();
150+
expect(getStateSpy).toHaveBeenCalledTimes(4);
144151
expect(getCommandOutputSpy).toHaveBeenCalledTimes(0);
145152
expect(debugSpy).toHaveBeenCalledTimes(0);
146153
expect(infoSpy).toHaveBeenCalledWith(
@@ -152,7 +159,9 @@ describe('run', () => {
152159
it('should not save cache for npm', async () => {
153160
inputs['cache'] = 'npm';
154161
getStateSpy.mockImplementation(key =>
155-
key === State.CachePrimaryKey || key === State.CacheMatchedKey
162+
key === State.CachePackageManager
163+
? inputs['cache']
164+
: key === State.CachePrimaryKey || key === State.CacheMatchedKey
156165
? yarnFileHash
157166
: key === State.CachePaths
158167
? '["/foo/bar"]'
@@ -162,8 +171,8 @@ describe('run', () => {
162171

163172
await run();
164173

165-
expect(getInputSpy).toHaveBeenCalled();
166-
expect(getStateSpy).toHaveBeenCalledTimes(3);
174+
expect(getInputSpy).not.toHaveBeenCalled();
175+
expect(getStateSpy).toHaveBeenCalledTimes(4);
167176
expect(getCommandOutputSpy).toHaveBeenCalledTimes(0);
168177
expect(debugSpy).toHaveBeenCalledTimes(0);
169178
expect(setFailedSpy).not.toHaveBeenCalled();
@@ -172,7 +181,9 @@ describe('run', () => {
172181
it('should not save cache for pnpm', async () => {
173182
inputs['cache'] = 'pnpm';
174183
getStateSpy.mockImplementation(key =>
175-
key === State.CachePrimaryKey || key === State.CacheMatchedKey
184+
key === State.CachePackageManager
185+
? inputs['cache']
186+
: key === State.CachePrimaryKey || key === State.CacheMatchedKey
176187
? yarnFileHash
177188
: key === State.CachePaths
178189
? '["/foo/bar"]'
@@ -181,8 +192,8 @@ describe('run', () => {
181192

182193
await run();
183194

184-
expect(getInputSpy).toHaveBeenCalled();
185-
expect(getStateSpy).toHaveBeenCalledTimes(3);
195+
expect(getInputSpy).not.toHaveBeenCalled();
196+
expect(getStateSpy).toHaveBeenCalledTimes(4);
186197
expect(getCommandOutputSpy).toHaveBeenCalledTimes(0);
187198
expect(debugSpy).toHaveBeenCalledTimes(0);
188199
expect(setFailedSpy).not.toHaveBeenCalled();
@@ -193,7 +204,9 @@ describe('run', () => {
193204
it('saves cache from yarn 1', async () => {
194205
inputs['cache'] = 'yarn';
195206
getStateSpy.mockImplementation((key: string) =>
196-
key === State.CacheMatchedKey
207+
key === State.CachePackageManager
208+
? inputs['cache']
209+
: key === State.CacheMatchedKey
197210
? yarnFileHash
198211
: key === State.CachePrimaryKey
199212
? npmFileHash
@@ -204,8 +217,8 @@ describe('run', () => {
204217

205218
await run();
206219

207-
expect(getInputSpy).toHaveBeenCalled();
208-
expect(getStateSpy).toHaveBeenCalledTimes(3);
220+
expect(getInputSpy).not.toHaveBeenCalled();
221+
expect(getStateSpy).toHaveBeenCalledTimes(4);
209222
expect(getCommandOutputSpy).toHaveBeenCalledTimes(0);
210223
expect(debugSpy).toHaveBeenCalledTimes(0);
211224
expect(infoSpy).not.toHaveBeenCalledWith(
@@ -221,7 +234,9 @@ describe('run', () => {
221234
it('saves cache from yarn 2', async () => {
222235
inputs['cache'] = 'yarn';
223236
getStateSpy.mockImplementation((key: string) =>
224-
key === State.CacheMatchedKey
237+
key === State.CachePackageManager
238+
? inputs['cache']
239+
: key === State.CacheMatchedKey
225240
? yarnFileHash
226241
: key === State.CachePrimaryKey
227242
? npmFileHash
@@ -232,8 +247,8 @@ describe('run', () => {
232247

233248
await run();
234249

235-
expect(getInputSpy).toHaveBeenCalled();
236-
expect(getStateSpy).toHaveBeenCalledTimes(3);
250+
expect(getInputSpy).not.toHaveBeenCalled();
251+
expect(getStateSpy).toHaveBeenCalledTimes(4);
237252
expect(getCommandOutputSpy).toHaveBeenCalledTimes(0);
238253
expect(debugSpy).toHaveBeenCalledTimes(0);
239254
expect(infoSpy).not.toHaveBeenCalledWith(
@@ -249,7 +264,9 @@ describe('run', () => {
249264
it('saves cache from npm', async () => {
250265
inputs['cache'] = 'npm';
251266
getStateSpy.mockImplementation((key: string) =>
252-
key === State.CacheMatchedKey
267+
key === State.CachePackageManager
268+
? inputs['cache']
269+
: key === State.CacheMatchedKey
253270
? npmFileHash
254271
: key === State.CachePrimaryKey
255272
? yarnFileHash
@@ -260,8 +277,8 @@ describe('run', () => {
260277

261278
await run();
262279

263-
expect(getInputSpy).toHaveBeenCalled();
264-
expect(getStateSpy).toHaveBeenCalledTimes(3);
280+
expect(getInputSpy).not.toHaveBeenCalled();
281+
expect(getStateSpy).toHaveBeenCalledTimes(4);
265282
expect(getCommandOutputSpy).toHaveBeenCalledTimes(0);
266283
expect(debugSpy).toHaveBeenCalledTimes(0);
267284
expect(infoSpy).not.toHaveBeenCalledWith(
@@ -277,7 +294,9 @@ describe('run', () => {
277294
it('saves cache from pnpm', async () => {
278295
inputs['cache'] = 'pnpm';
279296
getStateSpy.mockImplementation((key: string) =>
280-
key === State.CacheMatchedKey
297+
key === State.CachePackageManager
298+
? inputs['cache']
299+
: key === State.CacheMatchedKey
281300
? pnpmFileHash
282301
: key === State.CachePrimaryKey
283302
? npmFileHash
@@ -288,8 +307,8 @@ describe('run', () => {
288307

289308
await run();
290309

291-
expect(getInputSpy).toHaveBeenCalled();
292-
expect(getStateSpy).toHaveBeenCalledTimes(3);
310+
expect(getInputSpy).not.toHaveBeenCalled();
311+
expect(getStateSpy).toHaveBeenCalledTimes(4);
293312
expect(getCommandOutputSpy).toHaveBeenCalledTimes(0);
294313
expect(debugSpy).toHaveBeenCalledTimes(0);
295314
expect(infoSpy).not.toHaveBeenCalledWith(
@@ -305,7 +324,9 @@ describe('run', () => {
305324
it('save with -1 cacheId , should not fail workflow', async () => {
306325
inputs['cache'] = 'npm';
307326
getStateSpy.mockImplementation((key: string) =>
308-
key === State.CacheMatchedKey
327+
key === State.CachePackageManager
328+
? inputs['cache']
329+
: key === State.CacheMatchedKey
309330
? npmFileHash
310331
: key === State.CachePrimaryKey
311332
? yarnFileHash
@@ -319,8 +340,8 @@ describe('run', () => {
319340

320341
await run();
321342

322-
expect(getInputSpy).toHaveBeenCalled();
323-
expect(getStateSpy).toHaveBeenCalledTimes(3);
343+
expect(getInputSpy).not.toHaveBeenCalled();
344+
expect(getStateSpy).toHaveBeenCalledTimes(4);
324345
expect(getCommandOutputSpy).toHaveBeenCalledTimes(0);
325346
expect(debugSpy).toHaveBeenCalledTimes(0);
326347
expect(infoSpy).not.toHaveBeenCalledWith(
@@ -336,7 +357,9 @@ describe('run', () => {
336357
it('saves with error from toolkit, should fail workflow', async () => {
337358
inputs['cache'] = 'npm';
338359
getStateSpy.mockImplementation((key: string) =>
339-
key === State.CacheMatchedKey
360+
key === State.CachePackageManager
361+
? inputs['cache']
362+
: key === State.CacheMatchedKey
340363
? npmFileHash
341364
: key === State.CachePrimaryKey
342365
? yarnFileHash
@@ -350,8 +373,8 @@ describe('run', () => {
350373

351374
await run();
352375

353-
expect(getInputSpy).toHaveBeenCalled();
354-
expect(getStateSpy).toHaveBeenCalledTimes(3);
376+
expect(getInputSpy).not.toHaveBeenCalled();
377+
expect(getStateSpy).toHaveBeenCalledTimes(4);
355378
expect(getCommandOutputSpy).toHaveBeenCalledTimes(0);
356379
expect(debugSpy).toHaveBeenCalledTimes(0);
357380
expect(infoSpy).not.toHaveBeenCalledWith(

‎dist/cache-save/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -60381,7 +60381,7 @@ process.on('uncaughtException', e => {
6038160381
function run() {
6038260382
return __awaiter(this, void 0, void 0, function* () {
6038360383
try {
60384-
const cacheLock = core.getInput('cache');
60384+
const cacheLock = core.getState(constants_1.State.CachePackageManager);
6038560385
yield cachePackages(cacheLock);
6038660386
}
6038760387
catch (error) {
@@ -60692,6 +60692,7 @@ var LockType;
6069260692
})(LockType = exports.LockType || (exports.LockType = {}));
6069360693
var State;
6069460694
(function (State) {
60695+
State["CachePackageManager"] = "SETUP_NODE_CACHE_PACKAGE_MANAGER";
6069560696
State["CachePrimaryKey"] = "CACHE_KEY";
6069660697
State["CacheMatchedKey"] = "CACHE_RESULT";
6069760698
State["CachePaths"] = "CACHE_PATHS";

‎dist/setup/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -71479,6 +71479,7 @@ var LockType;
7147971479
})(LockType = exports.LockType || (exports.LockType = {}));
7148071480
var State;
7148171481
(function (State) {
71482+
State["CachePackageManager"] = "SETUP_NODE_CACHE_PACKAGE_MANAGER";
7148271483
State["CachePrimaryKey"] = "CACHE_KEY";
7148371484
State["CacheMatchedKey"] = "CACHE_RESULT";
7148471485
State["CachePaths"] = "CACHE_PATHS";
@@ -72209,6 +72210,7 @@ const cache_restore_1 = __nccwpck_require__(9517);
7220972210
const cache_utils_1 = __nccwpck_require__(1678);
7221072211
const installer_factory_1 = __nccwpck_require__(5617);
7221172212
const util_1 = __nccwpck_require__(2629);
72213+
const constants_1 = __nccwpck_require__(9042);
7221272214
function run() {
7221372215
return __awaiter(this, void 0, void 0, function* () {
7221472216
try {
@@ -72249,6 +72251,7 @@ function run() {
7224972251
auth.configAuthentication(registryUrl, alwaysAuth);
7225072252
}
7225172253
if (cache && cache_utils_1.isCacheFeatureAvailable()) {
72254+
core.saveState(constants_1.State.CachePackageManager, cache);
7225272255
const cacheDependencyPath = core.getInput('cache-dependency-path');
7225372256
yield cache_restore_1.restoreCache(cache, cacheDependencyPath);
7225472257
}

‎src/cache-save.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ process.on('uncaughtException', e => {
1616

1717
export async function run() {
1818
try {
19-
const cacheLock = core.getInput('cache');
19+
const cacheLock = core.getState(State.CachePackageManager);
2020
await cachePackages(cacheLock);
2121
} catch (error) {
2222
core.setFailed(error.message);

‎src/constants.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export enum LockType {
55
}
66

77
export enum State {
8+
CachePackageManager = 'SETUP_NODE_CACHE_PACKAGE_MANAGER',
89
CachePrimaryKey = 'CACHE_KEY',
910
CacheMatchedKey = 'CACHE_RESULT',
1011
CachePaths = 'CACHE_PATHS'

‎src/main.ts

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {restoreCache} from './cache-restore';
99
import {isCacheFeatureAvailable} from './cache-utils';
1010
import {getNodejsDistribution} from './distributions/installer-factory';
1111
import {parseNodeVersionFile, printEnvDetailsAndSetOutput} from './util';
12+
import {State} from './constants';
1213

1314
export async function run() {
1415
try {
@@ -60,6 +61,7 @@ export async function run() {
6061
}
6162

6263
if (cache && isCacheFeatureAvailable()) {
64+
core.saveState(State.CachePackageManager, cache);
6365
const cacheDependencyPath = core.getInput('cache-dependency-path');
6466
await restoreCache(cache, cacheDependencyPath);
6567
}

0 commit comments

Comments
 (0)
Failed to load comments.