Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cache-key as output #1190

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 73 additions & 1 deletion __tests__/cache-restore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
);
}
);
});
Expand All @@ -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;
Expand All @@ -176,10 +186,72 @@ 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
);
}
);
});

describe('Cache key output', () => {
const packageManager = 'npm';
const cacheDependencyPath = 'package-lock.json';
const primaryKey = `node-cache-${platform}-${arch}-${packageManager}-${npmFileHash}`;
const cacheKey = `node-cache-${platform}-${arch}-${packageManager}-abc123`;

beforeEach(() => {
getCommandOutputSpy.mockImplementation(command => {
if (command.includes('npm config get cache')) return npmCachePath;
});
});

it('sets the cache-key output', async () => {
restoreCacheSpy.mockResolvedValue(cacheKey);
await restoreCache(packageManager, cacheDependencyPath);
expect(setOutputSpy).toHaveBeenCalledWith('cache-key', primaryKey);
});

it('sets the cache-hit output to true when cache is found', async () => {
restoreCacheSpy.mockResolvedValue(cacheKey);
await restoreCache(packageManager, cacheDependencyPath);
expect(setOutputSpy).toHaveBeenCalledWith('cache-hit', true);
});

it('sets the cache-hit output to false when cache is not found', async () => {
restoreCacheSpy.mockResolvedValue(undefined);
await restoreCache(packageManager, cacheDependencyPath);

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(() => {
jest.resetAllMocks();
jest.clearAllMocks();
Expand Down
13 changes: 10 additions & 3 deletions __tests__/cache-save.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('run', () => {
let setFailedSpy: jest.SpyInstance;
let getStateSpy: jest.SpyInstance;
let saveCacheSpy: jest.SpyInstance;
let setOutputSpy: jest.SpyInstance;
let getCommandOutputSpy: jest.SpyInstance;
let hashFilesSpy: jest.SpyInstance;
let existsSpy: jest.SpyInstance;
Expand All @@ -53,6 +54,8 @@ describe('run', () => {
saveCacheSpy = jest.spyOn(cache, 'saveCache');
saveCacheSpy.mockImplementation(() => undefined);

setOutputSpy = jest.spyOn(core, 'setOutput');

// glob
hashFilesSpy = jest.spyOn(glob, 'hashFiles');
hashFilesSpy.mockImplementation((pattern: string) => {
Expand Down Expand Up @@ -228,6 +231,7 @@ describe('run', () => {
expect(infoSpy).toHaveBeenLastCalledWith(
`Cache saved with the key: ${npmFileHash}`
);
expect(core.setOutput).toHaveBeenCalledWith('cache-key', npmFileHash);
expect(setFailedSpy).not.toHaveBeenCalled();
});

Expand Down Expand Up @@ -258,6 +262,7 @@ describe('run', () => {
expect(infoSpy).toHaveBeenLastCalledWith(
`Cache saved with the key: ${npmFileHash}`
);
expect(core.setOutput).toHaveBeenCalledWith('cache-key', npmFileHash);
expect(setFailedSpy).not.toHaveBeenCalled();
});

Expand Down Expand Up @@ -288,6 +293,7 @@ describe('run', () => {
expect(infoSpy).toHaveBeenLastCalledWith(
`Cache saved with the key: ${yarnFileHash}`
);
expect(core.setOutput).toHaveBeenCalledWith('cache-key', yarnFileHash);
expect(setFailedSpy).not.toHaveBeenCalled();
});

Expand All @@ -297,9 +303,9 @@ describe('run', () => {
key === State.CachePackageManager
? inputs['cache']
: key === State.CacheMatchedKey
? pnpmFileHash
? 'no-match'
: key === State.CachePrimaryKey
? npmFileHash
? pnpmFileHash
: key === State.CachePaths
? '["/foo/bar"]'
: 'not expected'
Expand All @@ -316,8 +322,9 @@ describe('run', () => {
);
expect(saveCacheSpy).toHaveBeenCalled();
expect(infoSpy).toHaveBeenLastCalledWith(
`Cache saved with the key: ${npmFileHash}`
`Cache saved with the key: ${pnpmFileHash}`
);
expect(core.setOutput).toHaveBeenCalledWith('cache-key', pnpmFileHash);
expect(setFailedSpy).not.toHaveBeenCalled();
});

Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ inputs:
outputs:
cache-hit:
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:
Expand Down
1 change: 1 addition & 0 deletions dist/cache-save/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83729,6 +83729,7 @@ const cachePackages = (packageManager) => __awaiter(void 0, void 0, void 0, func
return;
}
core.info(`Cache saved with the key: ${primaryKey}`);
core.setOutput('cache-key', primaryKey);
});
run(true);

Expand Down
3 changes: 3 additions & 0 deletions dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93326,6 +93326,7 @@ const restoreCache = (packageManager, cacheDependencyPath) => __awaiter(void 0,
const primaryKey = `${keyPrefix}-${fileHash}`;
core.debug(`primary key is ${primaryKey}`);
core.saveState(constants_1.State.CachePrimaryKey, primaryKey);
core.setOutput('cache-key', primaryKey);
const isManagedByYarnBerry = yield (0, cache_utils_1.repoHasYarnBerryManagedDependencies)(packageManagerInfo, cacheDependencyPath);
let cacheKey;
if (isManagedByYarnBerry) {
Expand All @@ -93336,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;
Expand Down
3 changes: 3 additions & 0 deletions src/cache-restore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const restoreCache = async (
core.debug(`primary key is ${primaryKey}`);

core.saveState(State.CachePrimaryKey, primaryKey);
core.setOutput('cache-key', primaryKey);

const isManagedByYarnBerry = await repoHasYarnBerryManagedDependencies(
packageManagerInfo,
Expand All @@ -61,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`);
Expand Down
1 change: 1 addition & 0 deletions src/cache-save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const cachePackages = async (packageManager: string) => {
}

core.info(`Cache saved with the key: ${primaryKey}`);
core.setOutput('cache-key', primaryKey);
};

run(true);