2022-05-25 06:07:29 -04:00
|
|
|
import * as cache from '@actions/cache';
|
|
|
|
import * as core from '@actions/core';
|
|
|
|
import * as glob from '@actions/glob';
|
|
|
|
|
|
|
|
import * as cacheRestore from '../src/cache-restore';
|
|
|
|
import * as cacheUtils from '../src/cache-utils';
|
|
|
|
import {PackageManagerInfo} from '../src/package-managers';
|
|
|
|
|
|
|
|
describe('restoreCache', () => {
|
|
|
|
//Arrange
|
2023-03-08 03:45:16 -05:00
|
|
|
const hashFilesSpy = jest.spyOn(glob, 'hashFiles');
|
|
|
|
const getCacheDirectoryPathSpy = jest.spyOn(
|
2022-05-25 06:07:29 -04:00
|
|
|
cacheUtils,
|
|
|
|
'getCacheDirectoryPath'
|
|
|
|
);
|
2023-03-08 03:45:16 -05:00
|
|
|
const restoreCacheSpy = jest.spyOn(cache, 'restoreCache');
|
|
|
|
const infoSpy = jest.spyOn(core, 'info');
|
|
|
|
const setOutputSpy = jest.spyOn(core, 'setOutput');
|
2022-05-25 06:07:29 -04:00
|
|
|
|
2022-10-17 12:33:22 -04:00
|
|
|
const versionSpec = '1.13.1';
|
2022-05-25 06:07:29 -04:00
|
|
|
const packageManager = 'default';
|
|
|
|
const cacheDependencyPath = 'path';
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
getCacheDirectoryPathSpy.mockImplementation(
|
|
|
|
(PackageManager: PackageManagerInfo) => {
|
|
|
|
return new Promise<string[]>(resolve => {
|
|
|
|
resolve(['cache_directory_path', 'cache_directory_path']);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw if dependency file path is not valid', async () => {
|
|
|
|
//Arrange
|
|
|
|
hashFilesSpy.mockImplementation((somePath: string) => {
|
|
|
|
return new Promise<string>(resolve => {
|
|
|
|
resolve('');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
//Act + Assert
|
2023-03-08 03:45:16 -05:00
|
|
|
await expect(async () => {
|
2023-09-15 16:43:13 -04:00
|
|
|
await cacheRestore.restoreModCache(
|
2022-10-17 12:33:22 -04:00
|
|
|
versionSpec,
|
|
|
|
packageManager,
|
|
|
|
cacheDependencyPath
|
|
|
|
);
|
2023-03-08 03:45:16 -05:00
|
|
|
}).rejects.toThrow(
|
2022-05-25 06:07:29 -04:00
|
|
|
'Some specified paths were not resolved, unable to cache dependencies.'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should inform if cache hit is not occured', async () => {
|
|
|
|
//Arrange
|
|
|
|
hashFilesSpy.mockImplementation((somePath: string) => {
|
|
|
|
return new Promise<string>(resolve => {
|
|
|
|
resolve('file_hash');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
restoreCacheSpy.mockImplementation(() => {
|
|
|
|
return new Promise<string>(resolve => {
|
|
|
|
resolve('');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
//Act + Assert
|
2023-09-15 16:43:13 -04:00
|
|
|
await cacheRestore.restoreModCache(
|
2022-10-17 12:33:22 -04:00
|
|
|
versionSpec,
|
|
|
|
packageManager,
|
|
|
|
cacheDependencyPath
|
|
|
|
);
|
2023-03-08 03:45:16 -05:00
|
|
|
expect(infoSpy).toHaveBeenCalledWith(`Cache is not found`);
|
2022-05-25 06:07:29 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should set output if cache hit is occured', async () => {
|
|
|
|
//Arrange
|
|
|
|
hashFilesSpy.mockImplementation((somePath: string) => {
|
|
|
|
return new Promise<string>(resolve => {
|
|
|
|
resolve('file_hash');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
restoreCacheSpy.mockImplementation(() => {
|
|
|
|
return new Promise<string>(resolve => {
|
|
|
|
resolve('cache_key');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
//Act + Assert
|
2023-09-15 16:43:13 -04:00
|
|
|
await cacheRestore.restoreModCache(
|
2022-10-17 12:33:22 -04:00
|
|
|
versionSpec,
|
|
|
|
packageManager,
|
|
|
|
cacheDependencyPath
|
|
|
|
);
|
2023-03-08 03:45:16 -05:00
|
|
|
expect(setOutputSpy).toHaveBeenCalledWith('cache-hit', true);
|
2022-05-25 06:07:29 -04:00
|
|
|
});
|
|
|
|
});
|