Add output cache-matched-key

This commit is contained in:
Alex Dunae 2025-01-15 12:03:24 -08:00
parent 82af78e9c4
commit d23de777e0
4 changed files with 47 additions and 1 deletions

View File

@ -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(() => {

View File

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

2
dist/setup/index.js vendored
View File

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

View File

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