2021-06-16 14:52:44 +08:00
|
|
|
import * as core from '@actions/core';
|
|
|
|
import * as cache from '@actions/cache';
|
|
|
|
import * as glob from '@actions/glob';
|
2021-07-15 03:11:07 +08:00
|
|
|
import fs from 'fs';
|
2021-06-16 14:52:44 +08:00
|
|
|
import path from 'path';
|
|
|
|
|
|
|
|
import * as utils from '../src/cache-utils';
|
|
|
|
import {run} from '../src/cache-save';
|
|
|
|
import {State} from '../src/constants';
|
|
|
|
|
|
|
|
describe('run', () => {
|
|
|
|
const yarnFileHash =
|
|
|
|
'b8a0bae5243251f7c07dd52d1f78ff78281dfefaded700a176261b6b54fa245b';
|
|
|
|
const npmFileHash =
|
|
|
|
'abf7c9b306a3149dcfba4673e2362755503bcceaab46f0e4e6fee0ade493e20c';
|
2021-06-30 23:44:51 +08:00
|
|
|
const pnpmFileHash =
|
|
|
|
'26309058093e84713f38869c50cf1cee9b08155ede874ec1b44ce3fca8c68c70';
|
2021-06-16 14:52:44 +08:00
|
|
|
const commonPath = '/some/random/path';
|
|
|
|
process.env['GITHUB_WORKSPACE'] = path.join(__dirname, 'data');
|
|
|
|
|
2023-03-08 16:47:38 +08:00
|
|
|
const inputs = {} as any;
|
2021-06-16 14:52:44 +08:00
|
|
|
|
|
|
|
let getInputSpy: jest.SpyInstance;
|
|
|
|
let infoSpy: jest.SpyInstance;
|
|
|
|
let warningSpy: jest.SpyInstance;
|
|
|
|
let debugSpy: jest.SpyInstance;
|
|
|
|
let setFailedSpy: jest.SpyInstance;
|
|
|
|
let getStateSpy: jest.SpyInstance;
|
|
|
|
let saveCacheSpy: jest.SpyInstance;
|
|
|
|
let getCommandOutputSpy: jest.SpyInstance;
|
|
|
|
let hashFilesSpy: jest.SpyInstance;
|
2021-07-15 03:11:07 +08:00
|
|
|
let existsSpy: jest.SpyInstance;
|
2021-06-16 14:52:44 +08:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
getInputSpy = jest.spyOn(core, 'getInput');
|
|
|
|
getInputSpy.mockImplementation((name: string) => inputs[name]);
|
|
|
|
|
|
|
|
infoSpy = jest.spyOn(core, 'info');
|
|
|
|
infoSpy.mockImplementation(() => undefined);
|
|
|
|
|
|
|
|
warningSpy = jest.spyOn(core, 'warning');
|
|
|
|
warningSpy.mockImplementation(() => undefined);
|
|
|
|
|
|
|
|
setFailedSpy = jest.spyOn(core, 'setFailed');
|
|
|
|
setFailedSpy.mockImplementation(() => undefined);
|
|
|
|
|
|
|
|
debugSpy = jest.spyOn(core, 'debug');
|
|
|
|
debugSpy.mockImplementation(() => undefined);
|
|
|
|
|
|
|
|
getStateSpy = jest.spyOn(core, 'getState');
|
|
|
|
|
|
|
|
// cache
|
|
|
|
saveCacheSpy = jest.spyOn(cache, 'saveCache');
|
|
|
|
saveCacheSpy.mockImplementation(() => undefined);
|
|
|
|
|
|
|
|
// glob
|
|
|
|
hashFilesSpy = jest.spyOn(glob, 'hashFiles');
|
|
|
|
hashFilesSpy.mockImplementation((pattern: string) => {
|
|
|
|
if (pattern.includes('package-lock.json')) {
|
|
|
|
return npmFileHash;
|
|
|
|
} else if (pattern.includes('yarn.lock')) {
|
|
|
|
return yarnFileHash;
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-07-15 03:11:07 +08:00
|
|
|
existsSpy = jest.spyOn(fs, 'existsSync');
|
|
|
|
existsSpy.mockImplementation(() => true);
|
|
|
|
|
2021-06-16 14:52:44 +08:00
|
|
|
// utils
|
|
|
|
getCommandOutputSpy = jest.spyOn(utils, 'getCommandOutput');
|
|
|
|
});
|
|
|
|
|
2021-07-15 19:40:08 +08:00
|
|
|
afterEach(() => {
|
|
|
|
existsSpy.mockRestore();
|
|
|
|
});
|
|
|
|
|
2021-06-16 14:52:44 +08:00
|
|
|
describe('Package manager validation', () => {
|
|
|
|
it('Package manager is not provided, skip caching', async () => {
|
|
|
|
inputs['cache'] = '';
|
|
|
|
|
|
|
|
await run();
|
|
|
|
|
|
|
|
expect(setFailedSpy).not.toHaveBeenCalled();
|
|
|
|
expect(infoSpy).not.toHaveBeenCalled();
|
|
|
|
expect(saveCacheSpy).not.toHaveBeenCalled();
|
|
|
|
expect(debugSpy).toHaveBeenLastCalledWith(
|
|
|
|
"Caching for '' is not supported"
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Package manager is not valid, skip caching', async () => {
|
|
|
|
inputs['cache'] = 'yarn3';
|
2023-08-10 22:32:24 +08:00
|
|
|
getStateSpy.mockImplementation(key =>
|
|
|
|
key === State.CachePackageManager ? inputs['cache'] : ''
|
|
|
|
);
|
2021-06-16 14:52:44 +08:00
|
|
|
|
|
|
|
await run();
|
|
|
|
|
|
|
|
expect(setFailedSpy).not.toHaveBeenCalled();
|
|
|
|
expect(infoSpy).not.toHaveBeenCalled();
|
|
|
|
expect(saveCacheSpy).not.toHaveBeenCalled();
|
|
|
|
expect(debugSpy).toHaveBeenLastCalledWith(
|
|
|
|
"Caching for 'yarn3' is not supported"
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Validate unchanged cache is not saved', () => {
|
|
|
|
it('should not save cache for yarn1', async () => {
|
|
|
|
inputs['cache'] = 'yarn';
|
2023-06-21 23:52:17 +08:00
|
|
|
getStateSpy.mockImplementation(key =>
|
2023-08-10 22:32:24 +08:00
|
|
|
key === State.CachePackageManager
|
|
|
|
? inputs['cache']
|
|
|
|
: key === State.CachePrimaryKey || key === State.CacheMatchedKey
|
2023-06-21 23:52:17 +08:00
|
|
|
? yarnFileHash
|
|
|
|
: key === State.CachePaths
|
|
|
|
? '["/foo/bar"]'
|
|
|
|
: 'not expected'
|
|
|
|
);
|
2021-06-16 14:52:44 +08:00
|
|
|
|
|
|
|
await run();
|
|
|
|
|
2023-08-10 22:32:24 +08:00
|
|
|
expect(getInputSpy).not.toHaveBeenCalled();
|
|
|
|
expect(getStateSpy).toHaveBeenCalledTimes(4);
|
2023-06-21 23:52:17 +08:00
|
|
|
expect(getCommandOutputSpy).toHaveBeenCalledTimes(0);
|
|
|
|
expect(debugSpy).toHaveBeenCalledTimes(0);
|
2021-06-16 14:52:44 +08:00
|
|
|
expect(infoSpy).toHaveBeenCalledWith(
|
|
|
|
`Cache hit occurred on the primary key ${yarnFileHash}, not saving cache.`
|
|
|
|
);
|
|
|
|
expect(setFailedSpy).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not save cache for yarn2', async () => {
|
|
|
|
inputs['cache'] = 'yarn';
|
2023-06-21 23:52:17 +08:00
|
|
|
getStateSpy.mockImplementation(key =>
|
2023-08-10 22:32:24 +08:00
|
|
|
key === State.CachePackageManager
|
|
|
|
? inputs['cache']
|
|
|
|
: key === State.CachePrimaryKey || key === State.CacheMatchedKey
|
2023-06-21 23:52:17 +08:00
|
|
|
? yarnFileHash
|
|
|
|
: key === State.CachePaths
|
|
|
|
? '["/foo/bar"]'
|
|
|
|
: 'not expected'
|
|
|
|
);
|
2021-06-16 14:52:44 +08:00
|
|
|
|
|
|
|
await run();
|
|
|
|
|
2023-08-10 22:32:24 +08:00
|
|
|
expect(getInputSpy).not.toHaveBeenCalled();
|
|
|
|
expect(getStateSpy).toHaveBeenCalledTimes(4);
|
2023-06-21 23:52:17 +08:00
|
|
|
expect(getCommandOutputSpy).toHaveBeenCalledTimes(0);
|
|
|
|
expect(debugSpy).toHaveBeenCalledTimes(0);
|
2021-06-16 14:52:44 +08:00
|
|
|
expect(infoSpy).toHaveBeenCalledWith(
|
|
|
|
`Cache hit occurred on the primary key ${yarnFileHash}, not saving cache.`
|
|
|
|
);
|
|
|
|
expect(setFailedSpy).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not save cache for npm', async () => {
|
|
|
|
inputs['cache'] = 'npm';
|
2023-06-21 23:52:17 +08:00
|
|
|
getStateSpy.mockImplementation(key =>
|
2023-08-10 22:32:24 +08:00
|
|
|
key === State.CachePackageManager
|
|
|
|
? inputs['cache']
|
|
|
|
: key === State.CachePrimaryKey || key === State.CacheMatchedKey
|
2023-06-21 23:52:17 +08:00
|
|
|
? yarnFileHash
|
|
|
|
: key === State.CachePaths
|
|
|
|
? '["/foo/bar"]'
|
|
|
|
: 'not expected'
|
|
|
|
);
|
2021-06-16 14:52:44 +08:00
|
|
|
getCommandOutputSpy.mockImplementationOnce(() => `${commonPath}/npm`);
|
|
|
|
|
|
|
|
await run();
|
|
|
|
|
2023-08-10 22:32:24 +08:00
|
|
|
expect(getInputSpy).not.toHaveBeenCalled();
|
|
|
|
expect(getStateSpy).toHaveBeenCalledTimes(4);
|
2023-06-21 23:52:17 +08:00
|
|
|
expect(getCommandOutputSpy).toHaveBeenCalledTimes(0);
|
|
|
|
expect(debugSpy).toHaveBeenCalledTimes(0);
|
2021-06-16 14:52:44 +08:00
|
|
|
expect(setFailedSpy).not.toHaveBeenCalled();
|
|
|
|
});
|
2021-06-30 23:44:51 +08:00
|
|
|
|
2021-07-14 20:01:18 +08:00
|
|
|
it('should not save cache for pnpm', async () => {
|
2021-06-30 23:44:51 +08:00
|
|
|
inputs['cache'] = 'pnpm';
|
2023-06-21 23:52:17 +08:00
|
|
|
getStateSpy.mockImplementation(key =>
|
2023-08-10 22:32:24 +08:00
|
|
|
key === State.CachePackageManager
|
|
|
|
? inputs['cache']
|
|
|
|
: key === State.CachePrimaryKey || key === State.CacheMatchedKey
|
2023-06-21 23:52:17 +08:00
|
|
|
? yarnFileHash
|
|
|
|
: key === State.CachePaths
|
|
|
|
? '["/foo/bar"]'
|
|
|
|
: 'not expected'
|
|
|
|
);
|
2021-06-30 23:44:51 +08:00
|
|
|
|
|
|
|
await run();
|
|
|
|
|
2023-08-10 22:32:24 +08:00
|
|
|
expect(getInputSpy).not.toHaveBeenCalled();
|
|
|
|
expect(getStateSpy).toHaveBeenCalledTimes(4);
|
2023-06-21 23:52:17 +08:00
|
|
|
expect(getCommandOutputSpy).toHaveBeenCalledTimes(0);
|
|
|
|
expect(debugSpy).toHaveBeenCalledTimes(0);
|
2021-06-30 23:44:51 +08:00
|
|
|
expect(setFailedSpy).not.toHaveBeenCalled();
|
|
|
|
});
|
2021-06-16 14:52:44 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('action saves the cache', () => {
|
|
|
|
it('saves cache from yarn 1', async () => {
|
|
|
|
inputs['cache'] = 'yarn';
|
2023-06-21 23:52:17 +08:00
|
|
|
getStateSpy.mockImplementation((key: string) =>
|
2023-08-10 22:32:24 +08:00
|
|
|
key === State.CachePackageManager
|
|
|
|
? inputs['cache']
|
|
|
|
: key === State.CacheMatchedKey
|
2023-06-21 23:52:17 +08:00
|
|
|
? yarnFileHash
|
|
|
|
: key === State.CachePrimaryKey
|
|
|
|
? npmFileHash
|
|
|
|
: key === State.CachePaths
|
|
|
|
? '["/foo/bar"]'
|
|
|
|
: 'not expected'
|
|
|
|
);
|
2021-06-16 14:52:44 +08:00
|
|
|
|
|
|
|
await run();
|
|
|
|
|
2023-08-10 22:32:24 +08:00
|
|
|
expect(getInputSpy).not.toHaveBeenCalled();
|
|
|
|
expect(getStateSpy).toHaveBeenCalledTimes(4);
|
2023-06-21 23:52:17 +08:00
|
|
|
expect(getCommandOutputSpy).toHaveBeenCalledTimes(0);
|
|
|
|
expect(debugSpy).toHaveBeenCalledTimes(0);
|
2021-06-16 14:52:44 +08:00
|
|
|
expect(infoSpy).not.toHaveBeenCalledWith(
|
|
|
|
`Cache hit occurred on the primary key ${yarnFileHash}, not saving cache.`
|
|
|
|
);
|
|
|
|
expect(saveCacheSpy).toHaveBeenCalled();
|
|
|
|
expect(infoSpy).toHaveBeenLastCalledWith(
|
|
|
|
`Cache saved with the key: ${npmFileHash}`
|
|
|
|
);
|
|
|
|
expect(setFailedSpy).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('saves cache from yarn 2', async () => {
|
|
|
|
inputs['cache'] = 'yarn';
|
2023-06-21 23:52:17 +08:00
|
|
|
getStateSpy.mockImplementation((key: string) =>
|
2023-08-10 22:32:24 +08:00
|
|
|
key === State.CachePackageManager
|
|
|
|
? inputs['cache']
|
|
|
|
: key === State.CacheMatchedKey
|
2023-06-21 23:52:17 +08:00
|
|
|
? yarnFileHash
|
|
|
|
: key === State.CachePrimaryKey
|
|
|
|
? npmFileHash
|
|
|
|
: key === State.CachePaths
|
|
|
|
? '["/foo/bar"]'
|
|
|
|
: 'not expected'
|
|
|
|
);
|
2021-06-16 14:52:44 +08:00
|
|
|
|
|
|
|
await run();
|
|
|
|
|
2023-08-10 22:32:24 +08:00
|
|
|
expect(getInputSpy).not.toHaveBeenCalled();
|
|
|
|
expect(getStateSpy).toHaveBeenCalledTimes(4);
|
2023-06-21 23:52:17 +08:00
|
|
|
expect(getCommandOutputSpy).toHaveBeenCalledTimes(0);
|
|
|
|
expect(debugSpy).toHaveBeenCalledTimes(0);
|
2021-06-16 14:52:44 +08:00
|
|
|
expect(infoSpy).not.toHaveBeenCalledWith(
|
|
|
|
`Cache hit occurred on the primary key ${yarnFileHash}, not saving cache.`
|
|
|
|
);
|
|
|
|
expect(saveCacheSpy).toHaveBeenCalled();
|
|
|
|
expect(infoSpy).toHaveBeenLastCalledWith(
|
|
|
|
`Cache saved with the key: ${npmFileHash}`
|
|
|
|
);
|
|
|
|
expect(setFailedSpy).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('saves cache from npm', async () => {
|
|
|
|
inputs['cache'] = 'npm';
|
2023-06-21 23:52:17 +08:00
|
|
|
getStateSpy.mockImplementation((key: string) =>
|
2023-08-10 22:32:24 +08:00
|
|
|
key === State.CachePackageManager
|
|
|
|
? inputs['cache']
|
|
|
|
: key === State.CacheMatchedKey
|
2023-06-21 23:52:17 +08:00
|
|
|
? npmFileHash
|
|
|
|
: key === State.CachePrimaryKey
|
|
|
|
? yarnFileHash
|
|
|
|
: key === State.CachePaths
|
|
|
|
? '["/foo/bar"]'
|
|
|
|
: 'not expected'
|
|
|
|
);
|
2021-06-16 14:52:44 +08:00
|
|
|
|
|
|
|
await run();
|
|
|
|
|
2023-08-10 22:32:24 +08:00
|
|
|
expect(getInputSpy).not.toHaveBeenCalled();
|
|
|
|
expect(getStateSpy).toHaveBeenCalledTimes(4);
|
2023-06-21 23:52:17 +08:00
|
|
|
expect(getCommandOutputSpy).toHaveBeenCalledTimes(0);
|
|
|
|
expect(debugSpy).toHaveBeenCalledTimes(0);
|
2021-06-16 14:52:44 +08:00
|
|
|
expect(infoSpy).not.toHaveBeenCalledWith(
|
|
|
|
`Cache hit occurred on the primary key ${npmFileHash}, not saving cache.`
|
|
|
|
);
|
|
|
|
expect(saveCacheSpy).toHaveBeenCalled();
|
|
|
|
expect(infoSpy).toHaveBeenLastCalledWith(
|
|
|
|
`Cache saved with the key: ${yarnFileHash}`
|
|
|
|
);
|
|
|
|
expect(setFailedSpy).not.toHaveBeenCalled();
|
|
|
|
});
|
2021-06-30 23:44:51 +08:00
|
|
|
|
|
|
|
it('saves cache from pnpm', async () => {
|
|
|
|
inputs['cache'] = 'pnpm';
|
2023-06-21 23:52:17 +08:00
|
|
|
getStateSpy.mockImplementation((key: string) =>
|
2023-08-10 22:32:24 +08:00
|
|
|
key === State.CachePackageManager
|
|
|
|
? inputs['cache']
|
|
|
|
: key === State.CacheMatchedKey
|
2023-06-21 23:52:17 +08:00
|
|
|
? pnpmFileHash
|
|
|
|
: key === State.CachePrimaryKey
|
|
|
|
? npmFileHash
|
|
|
|
: key === State.CachePaths
|
|
|
|
? '["/foo/bar"]'
|
|
|
|
: 'not expected'
|
|
|
|
);
|
2021-06-30 23:44:51 +08:00
|
|
|
|
|
|
|
await run();
|
|
|
|
|
2023-08-10 22:32:24 +08:00
|
|
|
expect(getInputSpy).not.toHaveBeenCalled();
|
|
|
|
expect(getStateSpy).toHaveBeenCalledTimes(4);
|
2023-06-21 23:52:17 +08:00
|
|
|
expect(getCommandOutputSpy).toHaveBeenCalledTimes(0);
|
|
|
|
expect(debugSpy).toHaveBeenCalledTimes(0);
|
2021-06-30 23:44:51 +08:00
|
|
|
expect(infoSpy).not.toHaveBeenCalledWith(
|
|
|
|
`Cache hit occurred on the primary key ${pnpmFileHash}, not saving cache.`
|
|
|
|
);
|
|
|
|
expect(saveCacheSpy).toHaveBeenCalled();
|
|
|
|
expect(infoSpy).toHaveBeenLastCalledWith(
|
|
|
|
`Cache saved with the key: ${npmFileHash}`
|
|
|
|
);
|
|
|
|
expect(setFailedSpy).not.toHaveBeenCalled();
|
|
|
|
});
|
2022-06-27 16:19:35 +08:00
|
|
|
|
2022-06-27 16:17:28 +08:00
|
|
|
it('save with -1 cacheId , should not fail workflow', async () => {
|
|
|
|
inputs['cache'] = 'npm';
|
2023-06-21 23:52:17 +08:00
|
|
|
getStateSpy.mockImplementation((key: string) =>
|
2023-08-10 22:32:24 +08:00
|
|
|
key === State.CachePackageManager
|
|
|
|
? inputs['cache']
|
|
|
|
: key === State.CacheMatchedKey
|
2023-06-21 23:52:17 +08:00
|
|
|
? npmFileHash
|
|
|
|
: key === State.CachePrimaryKey
|
|
|
|
? yarnFileHash
|
|
|
|
: key === State.CachePaths
|
|
|
|
? '["/foo/bar"]'
|
|
|
|
: 'not expected'
|
|
|
|
);
|
2022-06-27 16:17:28 +08:00
|
|
|
saveCacheSpy.mockImplementation(() => {
|
|
|
|
return -1;
|
|
|
|
});
|
|
|
|
|
|
|
|
await run();
|
|
|
|
|
2023-08-10 22:32:24 +08:00
|
|
|
expect(getInputSpy).not.toHaveBeenCalled();
|
|
|
|
expect(getStateSpy).toHaveBeenCalledTimes(4);
|
2023-06-21 23:52:17 +08:00
|
|
|
expect(getCommandOutputSpy).toHaveBeenCalledTimes(0);
|
|
|
|
expect(debugSpy).toHaveBeenCalledTimes(0);
|
2022-06-27 16:17:28 +08:00
|
|
|
expect(infoSpy).not.toHaveBeenCalledWith(
|
|
|
|
`Cache hit occurred on the primary key ${npmFileHash}, not saving cache.`
|
|
|
|
);
|
|
|
|
expect(saveCacheSpy).toHaveBeenCalled();
|
|
|
|
expect(infoSpy).not.toHaveBeenLastCalledWith(
|
|
|
|
`Cache saved with the key: ${yarnFileHash}`
|
|
|
|
);
|
|
|
|
expect(setFailedSpy).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('saves with error from toolkit, should fail workflow', async () => {
|
|
|
|
inputs['cache'] = 'npm';
|
2023-06-21 23:52:17 +08:00
|
|
|
getStateSpy.mockImplementation((key: string) =>
|
2023-08-10 22:32:24 +08:00
|
|
|
key === State.CachePackageManager
|
|
|
|
? inputs['cache']
|
|
|
|
: key === State.CacheMatchedKey
|
2023-06-21 23:52:17 +08:00
|
|
|
? npmFileHash
|
|
|
|
: key === State.CachePrimaryKey
|
|
|
|
? yarnFileHash
|
|
|
|
: key === State.CachePaths
|
|
|
|
? '["/foo/bar"]'
|
|
|
|
: 'not expected'
|
|
|
|
);
|
2022-06-27 16:17:28 +08:00
|
|
|
saveCacheSpy.mockImplementation(() => {
|
|
|
|
throw new cache.ValidationError('Validation failed');
|
|
|
|
});
|
|
|
|
|
|
|
|
await run();
|
|
|
|
|
2023-08-10 22:32:24 +08:00
|
|
|
expect(getInputSpy).not.toHaveBeenCalled();
|
|
|
|
expect(getStateSpy).toHaveBeenCalledTimes(4);
|
2023-06-21 23:52:17 +08:00
|
|
|
expect(getCommandOutputSpy).toHaveBeenCalledTimes(0);
|
|
|
|
expect(debugSpy).toHaveBeenCalledTimes(0);
|
2022-06-27 16:17:28 +08:00
|
|
|
expect(infoSpy).not.toHaveBeenCalledWith(
|
|
|
|
`Cache hit occurred on the primary key ${npmFileHash}, not saving cache.`
|
|
|
|
);
|
|
|
|
expect(saveCacheSpy).toHaveBeenCalled();
|
|
|
|
expect(setFailedSpy).toHaveBeenCalled();
|
|
|
|
});
|
2021-06-16 14:52:44 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.resetAllMocks();
|
|
|
|
jest.clearAllMocks();
|
|
|
|
});
|
|
|
|
});
|