8000 Added cache-hit output · dhvcc/setup-python@c933f3c · GitHub
[go: up one dir, main page]

Skip to content

Commit c933f3c

Browse files
committed
Added cache-hit output
1 parent 422c071 commit c933f3c

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

__tests__/cache-restore.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,37 @@ virtualenvs.path = "{cache-dir}/virtualenvs" # /Users/patrick/Library/Caches/py
191191
);
192192
});
193193

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

action.yml

Lines changed: 2 additions & 0 deletions
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'

src/cache-distributions/cache-distributor.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as cache from '@actions/cache';
22
import * as core from '@actions/core';
3+
import {PromiseReturnType} from '../utils';
34

45
export enum State {
56
STATE_CACHE_PRIMARY_KEY = 'cache-primary-key',
@@ -41,12 +42,19 @@ abstract class CacheDistributor {
4142
restoreKey
4243
);
4344

45+
this.handleMatchResult(matchedKey);
46+
}
47+
48+
public handleMatchResult(
49+
matchedKey: PromiseReturnType<typeof cache.restoreCache>
50+
) {
4451
if (matchedKey) {
4552
core.saveState(State.CACHE_MATCHED_KEY, matchedKey);
4653
core.info(`Cache restored from key: ${matchedKey}`);
4754
} else {
4855
core.info(`${this.packageManager} cache is not found`);
4956
}
57+
core.setOutput('cache-hit', Boolean(matchedKey));
5058
}
5159
}
5260

src/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,12 @@ export function isCacheFeatureAvailable(): boolean {
119119

120120
return true;
121121
}
122+
123+
// Awaited (typescript 4.5+) polyfill. Not ideal, so use with care
124+
export type AwaitedPolyfill<T> = T extends PromiseLike<infer U>
125+
? AwaitedPolyfill<U>
126+
: T;
127+
// Extract return type from promise
128+
export type PromiseReturnType<
129+
T extends (...args: any) => any
130+
> = AwaitedPolyfill<ReturnType<T>>;

0 commit comments

Comments
 (0)
0