This commit is contained in:
Alex Dunae 2025-06-19 10:48:23 +10:00 committed by GitHub
commit 0eec1c2c87
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 95 additions and 4 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,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();

View file

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