diff --git a/__tests__/cache-restore.test.ts b/__tests__/cache-restore.test.ts index 56aabc6f..c97b43b4 100644 --- a/__tests__/cache-restore.test.ts +++ b/__tests__/cache-restore.test.ts @@ -131,6 +131,7 @@ describe('cache-restore', () => { ])( 'restored dependencies for %s', async (packageManager, toolVersion, fileHash) => { + const expectedCacheKey = `node-cache-${platform}-${arch}-${packageManager}-${fileHash}`; getCommandOutputSpy.mockImplementation((command: string) => { if (command.includes('version')) { return toolVersion; @@ -142,12 +143,20 @@ describe('cache-restore', () => { await restoreCache(packageManager, ''); expect(hashFilesSpy).toHaveBeenCalled(); expect(infoSpy).toHaveBeenCalledWith( - `Cache restored from key: node-cache-${platform}-${arch}-${packageManager}-${fileHash}` + `Cache restored from key: ${expectedCacheKey}` ); expect(infoSpy).not.toHaveBeenCalledWith( `${packageManager} cache is not found` ); expect(setOutputSpy).toHaveBeenCalledWith('cache-hit', true); + expect(setOutputSpy).toHaveBeenCalledWith( + 'cache-key', + expectedCacheKey + ); + expect(setOutputSpy).toHaveBeenCalledWith( + 'cache-matched-key', + expectedCacheKey + ); } ); }); @@ -161,6 +170,7 @@ describe('cache-restore', () => { ])( 'dependencies are changed %s', async (packageManager, toolVersion, fileHash) => { + const expectedCacheKey = `node-cache-${platform}-${arch}-${packageManager}-${fileHash}`; getCommandOutputSpy.mockImplementation((command: string) => { if (command.includes('version')) { return toolVersion; @@ -176,6 +186,14 @@ describe('cache-restore', () => { `${packageManager} cache is not found` ); expect(setOutputSpy).toHaveBeenCalledWith('cache-hit', false); + expect(setOutputSpy).toHaveBeenCalledWith( + 'cache-key', + expectedCacheKey + ); + expect(setOutputSpy).toHaveBeenCalledWith( + 'cache-matched-key', + undefined + ); } ); }); @@ -210,6 +228,28 @@ describe('cache-restore', () => { expect(setOutputSpy).toHaveBeenCalledWith('cache-hit', false); }); + + it('sets the cache-matched-key output when cache is found', async () => { + (cache.restoreCache as jest.Mock).mockResolvedValue(cacheKey); + + await restoreCache(packageManager, cacheDependencyPath); + + expect(core.setOutput).toHaveBeenCalledWith( + 'cache-matched-key', + cacheKey + ); + }); + + it('sets the cache-matched-key output to undefined when cache is not found', async () => { + (cache.restoreCache as jest.Mock).mockResolvedValue(undefined); + + await restoreCache(packageManager, cacheDependencyPath); + + expect(core.setOutput).toHaveBeenCalledWith( + 'cache-matched-key', + undefined + ); + }); }); afterEach(() => { diff --git a/action.yml b/action.yml index 99140912..24d36704 100644 --- a/action.yml +++ b/action.yml @@ -32,6 +32,8 @@ outputs: description: 'A boolean value to indicate if a cache was hit.' cache-key: description: 'The key used for the cache.' + cache-matched-key: + description: 'The key used for the cache that resulted in a cache hit.' node-version: description: 'The installed node version.' runs: diff --git a/dist/setup/index.js b/dist/setup/index.js index 962b6896..65f7f279 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -93337,6 +93337,8 @@ const restoreCache = (packageManager, cacheDependencyPath) => __awaiter(void 0, cacheKey = yield cache.restoreCache(cachePaths, primaryKey); } core.setOutput('cache-hit', Boolean(cacheKey)); + core.setOutput('cache-matched-key', cacheKey); + core.debug(`cache-matched-key is ${cacheKey}`); if (!cacheKey) { core.info(`${packageManager} cache is not found`); return; diff --git a/src/cache-restore.ts b/src/cache-restore.ts index 65767510..08eb4402 100644 --- a/src/cache-restore.ts +++ b/src/cache-restore.ts @@ -62,6 +62,8 @@ export const restoreCache = async ( } core.setOutput('cache-hit', Boolean(cacheKey)); + core.setOutput('cache-matched-key', cacheKey); + core.debug(`cache-matched-key is ${cacheKey}`); if (!cacheKey) { core.info(`${packageManager} cache is not found`);