Added cache-hit output

This commit is contained in:
dhvcc 2022-04-03 02:33:19 +03:00
parent 422c071dab
commit c933f3c50e
4 changed files with 50 additions and 0 deletions

View file

@ -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();

View file

@ -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'

View file

@ -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));
}
}

View file

@ -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>>;