From c933f3c50ea2d98bc61f4112402f951e73d26ed8 Mon Sep 17 00:00:00 2001 From: dhvcc <1337kwiz@gmail.com> Date: Sun, 3 Apr 2022 02:33:19 +0300 Subject: [PATCH] Added cache-hit output --- __tests__/cache-restore.test.ts | 31 ++++++++++++++++++++ action.yml | 2 ++ src/cache-distributions/cache-distributor.ts | 8 +++++ src/utils.ts | 9 ++++++ 4 files changed, 50 insertions(+) diff --git a/__tests__/cache-restore.test.ts b/__tests__/cache-restore.test.ts index 7b4dcf02..55cc571b 100644 --- a/__tests__/cache-restore.test.ts +++ b/__tests__/cache-restore.test.ts @@ -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(); diff --git a/action.yml b/action.yml index bd9ee686..bda521dd 100644 --- a/action.yml +++ b/action.yml @@ -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' diff --git a/src/cache-distributions/cache-distributor.ts b/src/cache-distributions/cache-distributor.ts index ef0be0ef..3b30076c 100644 --- a/src/cache-distributions/cache-distributor.ts +++ b/src/cache-distributions/cache-distributor.ts @@ -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 + ) { 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)); } } diff --git a/src/utils.ts b/src/utils.ts index eb3a1ba6..f0dca7e1 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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 extends PromiseLike + ? AwaitedPolyfill + : T; +// Extract return type from promise +export type PromiseReturnType< + T extends (...args: any) => any +> = AwaitedPolyfill>;