mirror of
https://github.com/actions/setup-python.git
synced 2025-04-24 15:32:13 +00:00
Added cache-hit output
This commit is contained in:
parent
422c071dab
commit
c933f3c50e
4 changed files with 50 additions and 0 deletions
|
@ -191,6 +191,37 @@ virtualenvs.path = "{cache-dir}/virtualenvs" # /Users/patrick/Library/Caches/py
|
|||
);
|
||||
});
|
||||
|
||||
describe('Check if handleMatchResult', () => {
|
||||
it.each([
|
||||
['pip', '3.8.12', 'requirements.txt', 'someKey', true],
|
||||
['pipenv', '3.9.1', 'requirements.txt', 'someKey', true],
|
||||
['poetry', '3.8.12', 'requirements.txt', 'someKey', true],
|
||||
['pip', '3.9.2', 'requirements.txt', undefined, false],
|
||||
['pipenv', '3.8.12', 'requirements.txt', undefined, false],
|
||||
['poetry', '3.9.12', 'requirements.txt', undefined, false]
|
||||
])(
|
||||
'sets correct outputs',
|
||||
async (
|
||||
packageManager,
|
||||
pythonVersion,
|
||||
dependencyFile,
|
||||
matchedKey,
|
||||
expectedOutputValue
|
||||
) => {
|
||||
const cacheDistributor = getCacheDistributor(
|
||||
packageManager,
|
||||
pythonVersion,
|
||||
dependencyFile
|
||||
);
|
||||
cacheDistributor.handleMatchResult(matchedKey);
|
||||
expect(setOutputSpy).toHaveBeenCalledWith(
|
||||
'cache-hit',
|
||||
expectedOutputValue
|
||||
);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.resetAllMocks();
|
||||
jest.clearAllMocks();
|
||||
|
|
|
@ -19,6 +19,8 @@ inputs:
|
|||
outputs:
|
||||
python-version:
|
||||
description: "The installed python version. Useful when given a version range as input."
|
||||
cache-hit:
|
||||
description: 'A boolean value to indicate a cache entry was found'
|
||||
runs:
|
||||
using: 'node16'
|
||||
main: 'dist/setup/index.js'
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import * as cache from '@actions/cache';
|
||||
import * as core from '@actions/core';
|
||||
import {PromiseReturnType} from '../utils';
|
||||
|
||||
export enum State {
|
||||
STATE_CACHE_PRIMARY_KEY = 'cache-primary-key',
|
||||
|
@ -41,12 +42,19 @@ abstract class CacheDistributor {
|
|||
restoreKey
|
||||
);
|
||||
|
||||
this.handleMatchResult(matchedKey);
|
||||
}
|
||||
|
||||
public handleMatchResult(
|
||||
matchedKey: PromiseReturnType<typeof cache.restoreCache>
|
||||
) {
|
||||
if (matchedKey) {
|
||||
core.saveState(State.CACHE_MATCHED_KEY, matchedKey);
|
||||
core.info(`Cache restored from key: ${matchedKey}`);
|
||||
} else {
|
||||
core.info(`${this.packageManager} cache is not found`);
|
||||
}
|
||||
core.setOutput('cache-hit', Boolean(matchedKey));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -119,3 +119,12 @@ export function isCacheFeatureAvailable(): boolean {
|
|||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Awaited (typescript 4.5+) polyfill. Not ideal, so use with care
|
||||
export type AwaitedPolyfill<T> = T extends PromiseLike<infer U>
|
||||
? AwaitedPolyfill<U>
|
||||
: T;
|
||||
// Extract return type from promise
|
||||
export type PromiseReturnType<
|
||||
T extends (...args: any) => any
|
||||
> = AwaitedPolyfill<ReturnType<T>>;
|
||||
|
|
Loading…
Add table
Reference in a new issue