Skip to content

Commit 21c0493

Browse files
authored
Cache hit output (#373)
* Remove useless await in getCacheDistributor calls * Added cache-hit output * Build action with cache-hit output * Remove PromiseReturnType, add matchedKey == primaryKey check * Update cache-distributor.ts * Fix tests and rebuild
1 parent 7933d5a commit 21c0493

File tree

5 files changed

+68
-17
lines changed

5 files changed

+68
-17
lines changed

__tests__/cache-restore.test.ts

+39-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ virtualenvs.path = "{cache-dir}/virtualenvs" # /Users/patrick/Library/Caches/py
2727
let debugSpy: jest.SpyInstance;
2828
let saveSatetSpy: jest.SpyInstance;
2929
let getStateSpy: jest.SpyInstance;
30+
let setOutputSpy: jest.SpyInstance;
3031

3132
// cache spy
3233
let restoreCacheSpy: jest.SpyInstance;
@@ -64,6 +65,9 @@ virtualenvs.path = "{cache-dir}/virtualenvs" # /Users/patrick/Library/Caches/py
6465
return {stdout: '', stderr: 'Error occured', exitCode: 2};
6566
});
6667

68+
setOutputSpy = jest.spyOn(core, 'setOutput');
69+
setOutputSpy.mockImplementation(input => undefined);
70+
6771
restoreCacheSpy = jest.spyOn(cache, 'restoreCache');
6872
restoreCacheSpy.mockImplementation(
6973
(cachePaths: string[], primaryKey: string, restoreKey?: string) => {
@@ -100,7 +104,7 @@ virtualenvs.path = "{cache-dir}/virtualenvs" # /Users/patrick/Library/Caches/py
100104
])(
101105
'restored dependencies for %s by primaryKey',
102106
async (packageManager, pythonVersion, dependencyFile, fileHash) => {
103-
const cacheDistributor = await getCacheDistributor(
107+
const cacheDistributor = getCacheDistributor(
104108
packageManager,
105109
pythonVersion,
106110
dependencyFile
@@ -126,7 +130,7 @@ virtualenvs.path = "{cache-dir}/virtualenvs" # /Users/patrick/Library/Caches/py
126130
dependencyFile,
127131
cacheDependencyPath
128132
) => {
129-
const cacheDistributor = await getCacheDistributor(
133+
const cacheDistributor = getCacheDistributor(
130134
packageManager,
131135
pythonVersion,
132136
dependencyFile
@@ -162,7 +166,7 @@ virtualenvs.path = "{cache-dir}/virtualenvs" # /Users/patrick/Library/Caches/py
162166
return primaryKey !== fileHash && restoreKey ? pipFileLockHash : '';
163167
}
164168
);
165-
const cacheDistributor = await getCacheDistributor(
169+
const cacheDistributor = getCacheDistributor(
166170
packageManager,
167171
pythonVersion,
168172
dependencyFile
@@ -187,6 +191,38 @@ virtualenvs.path = "{cache-dir}/virtualenvs" # /Users/patrick/Library/Caches/py
187191
);
188192
});
189193

194+
describe('Check if handleMatchResult', () => {
195+
it.each([
196+
['pip', '3.8.12', 'requirements.txt', 'someKey', 'someKey', true],
197+
['pipenv', '3.9.1', 'requirements.txt', 'someKey', 'someKey', true],
198+
['poetry', '3.8.12', 'requirements.txt', 'someKey', 'someKey', true],
199+
['pip', '3.9.2', 'requirements.txt', undefined, 'someKey', false],
200+
['pipenv', '3.8.12', 'requirements.txt', undefined, 'someKey', false],
201+
['poetry', '3.9.12', 'requirements.txt', undefined, 'someKey', false]
202+
])(
203+
'sets correct outputs',
204+
async (
205+
packageManager,
206+
pythonVersion,
207+
dependencyFile,
208+
matchedKey,
209+
restoredKey,
210+
expectedOutputValue
211+
) => {
212+
const cacheDistributor = getCacheDistributor(
213+
packageManager,
214+
pythonVersion,
215+
dependencyFile
216+
);
217+
cacheDistributor.handleMatchResult(matchedKey, restoredKey);
218+
expect(setOutputSpy).toHaveBeenCalledWith(
219+
'cache-hit',
220+
expectedOutputValue
221+
);
222+
}
223+
);
224+
});
225+
190226
afterEach(() => {
191227
jest.resetAllMocks();
192228
jest.clearAllMocks();

action.yml

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ inputs:
1919
outputs:
2020
python-version:
2121
description: "The installed python version. Useful when given a version range as input."
22+
cache-hit:
23+
description: 'A boolean value to indicate a cache entry was found'
2224
runs:
2325
using: 'node16'
2426
main: 'dist/setup/index.js'

dist/cache-save/index.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -37234,15 +37234,19 @@ class CacheDistributor {
3723437234
core.saveState(State.CACHE_PATHS, cachePath);
3723537235
core.saveState(State.STATE_CACHE_PRIMARY_KEY, primaryKey);
3723637236
const matchedKey = yield cache.restoreCache(cachePath, primaryKey, restoreKey);
37237-
if (matchedKey) {
37238-
core.saveState(State.CACHE_MATCHED_KEY, matchedKey);
37239-
core.info(`Cache restored from key: ${matchedKey}`);
37240-
}
37241-
else {
37242-
core.info(`${this.packageManager} cache is not found`);
37243-
}
37237+
this.handleMatchResult(matchedKey, primaryKey);
3724437238
});
3724537239
}
37240+
handleMatchResult(matchedKey, primaryKey) {
37241+
if (matchedKey) {
37242+
core.saveState(State.CACHE_MATCHED_KEY, matchedKey);
37243+
core.info(`Cache restored from key: ${matchedKey}`);
37244+
}
37245+
else {
37246+
core.info(`${this.packageManager} cache is not found`);
37247+
}
37248+
core.setOutput('cache-hit', matchedKey === primaryKey);
37249+
}
3724637250
}
3724737251
exports.default = CacheDistributor;
3724837252

dist/setup/index.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -42579,15 +42579,19 @@ class CacheDistributor {
4257942579
core.saveState(State.CACHE_PATHS, cachePath);
4258042580
core.saveState(State.STATE_CACHE_PRIMARY_KEY, primaryKey);
4258142581
const matchedKey = yield cache.restoreCache(cachePath, primaryKey, restoreKey);
42582-
if (matchedKey) {
42583-
core.saveState(State.CACHE_MATCHED_KEY, matchedKey);
42584-
core.info(`Cache restored from key: ${matchedKey}`);
42585-
}
42586-
else {
42587-
core.info(`${this.packageManager} cache is not found`);
42588-
}
42582+
this.handleMatchResult(matchedKey, primaryKey);
4258942583
});
4259042584
}
42585+
handleMatchResult(matchedKey, primaryKey) {
42586+
if (matchedKey) {
42587+
core.saveState(State.CACHE_MATCHED_KEY, matchedKey);
42588+
core.info(`Cache restored from key: ${matchedKey}`);
42589+
}
42590+
else {
42591+
core.info(`${this.packageManager} cache is not found`);
42592+
}
42593+
core.setOutput('cache-hit', matchedKey === primaryKey);
42594+
}
4259142595
}
4259242596
exports.default = CacheDistributor;
4259342597

src/cache-distributions/cache-distributor.ts

+5
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,17 @@ abstract class CacheDistributor {
4141
restoreKey
4242
);
4343

44+
this.handleMatchResult(matchedKey, primaryKey);
45+
}
46+
47+
public handleMatchResult(matchedKey: string | undefined, primaryKey: string) {
4448
if (matchedKey) {
4549
core.saveState(State.CACHE_MATCHED_KEY, matchedKey);
4650
core.info(`Cache restored from key: ${matchedKey}`);
4751
} else {
4852
core.info(`${this.packageManager} cache is not found`);
4953
}
54+
core.setOutput('cache-hit', matchedKey === primaryKey);
5055
}
5156
}
5257

0 commit comments

Comments
 (0)