|
| 1 | +import * as core from '@actions/core'; |
| 2 | +import * as cache from '@actions/cache'; |
| 3 | +import * as path from 'path'; |
| 4 | +import * as glob from '@actions/glob'; |
| 5 | + |
| 6 | +import * as utils from '../src/cache-utils'; |
| 7 | +import {restoreCache} from '../src/cache-restore'; |
| 8 | + |
| 9 | +describe('cache-restore', () => { |
| 10 | + process.env['GITHUB_WORKSPACE'] = path.join(__dirname, 'data'); |
| 11 | + if (!process.env.RUNNER_OS) { |
| 12 | + process.env.RUNNER_OS = 'Linux'; |
| 13 | + } |
| 14 | + const platform = process.env.RUNNER_OS; |
| 15 | + const commonPath = '/some/random/path'; |
| 16 | + const npmCachePath = `${commonPath}/npm`; |
| 17 | + const yarn1CachePath = `${commonPath}/yarn1`; |
| 18 | + const yarn2CachePath = `${commonPath}/yarn2`; |
| 19 | + const yarnFileHash = |
| 20 | + 'b8a0bae5243251f7c07dd52d1f78ff78281dfefaded700a176261b6b54fa245b'; |
| 21 | + const npmFileHash = |
| 22 | + 'abf7c9b306a3149dcfba4673e2362755503bcceaab46f0e4e6fee0ade493e20c'; |
| 23 | + const cachesObject = { |
| 24 | + [npmCachePath]: npmFileHash, |
| 25 | + [yarn1CachePath]: yarnFileHash, |
| 26 | + [yarn2CachePath]: yarnFileHash |
| 27 | + }; |
| 28 | + |
| 29 | + function findCacheFolder(command: string) { |
| 30 | + switch (command) { |
| 31 | + case utils.supportedPackageManagers.npm.getCacheFolderCommand: |
| 32 | + return npmCachePath; |
| 33 | + case utils.supportedPackageManagers.yarn1.getCacheFolderCommand: |
| 34 | + return yarn1CachePath; |
| 35 | + case utils.supportedPackageManagers.yarn2.getCacheFolderCommand: |
| 36 | + return yarn2CachePath; |
| 37 | + default: |
| 38 | + return 'packge/not/found'; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + let saveStateSpy: jest.SpyInstance; |
| 43 | + let infoSpy: jest.SpyInstance; |
| 44 | + let debugSpy: jest.SpyInstance; |
| 45 | + let setOutputSpy: jest.SpyInstance; |
| 46 | + let getCommandOutputSpy: jest.SpyInstance; |
| 47 | + let restoreCacheSpy: jest.SpyInstance; |
| 48 | + let hashFilesSpy: jest.SpyInstance; |
| 49 | + |
| 50 | + beforeEach(() => { |
| 51 | + // core |
| 52 | + infoSpy = jest.spyOn(core, 'info'); |
| 53 | + infoSpy.mockImplementation(() => undefined); |
| 54 | + |
| 55 | + debugSpy = jest.spyOn(core, 'debug'); |
| 56 | + debugSpy.mockImplementation(() => undefined); |
| 57 | + |
| 58 | + setOutputSpy = jest.spyOn(core, 'setOutput'); |
| 59 | + setOutputSpy.mockImplementation(() => undefined); |
| 60 | + |
| 61 | + saveStateSpy = jest.spyOn(core, 'saveState'); |
| 62 | + saveStateSpy.mockImplementation(() => undefined); |
| 63 | + |
| 64 | + // glob |
| 65 | + hashFilesSpy = jest.spyOn(glob, 'hashFiles'); |
| 66 | + hashFilesSpy.mockImplementation((pattern: string) => { |
| 67 | + if (pattern.includes('package-lock.json')) { |
| 68 | + return npmFileHash; |
| 69 | + } else if (pattern.includes('yarn.lock')) { |
| 70 | + return yarnFileHash; |
| 71 | + } else { |
| 72 | + return ''; |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + // cache |
| 77 | + restoreCacheSpy = jest.spyOn(cache, 'restoreCache'); |
| 78 | + restoreCacheSpy.mockImplementation( |
| 79 | + (cachePaths: Array<string>, key: string) => { |
| 80 | + if (!cachePaths || cachePaths.length === 0) { |
| 81 | + return undefined; |
| 82 | + } |
| 83 | + |
| 84 | + const cachPath = cachePaths[0]; |
| 85 | + const fileHash = cachesObject[cachPath]; |
| 86 | + |
| 87 | + if (key.includes(fileHash)) { |
| 88 | + return key; |
| 89 | + } |
| 90 | + |
| 91 | + return undefined; |
| 92 | + } |
| 93 | + ); |
| 94 | + |
| 95 | + // cache-utils |
| 96 | + getCommandOutputSpy = jest.spyOn(utils, 'getCommandOutput'); |
| 97 | + }); |
| 98 | + |
| 99 | + describe('Validate provided package manager', () => { |
| 100 | + it.each([['npm7'], ['npm6'], ['yarn1'], ['yarn2'], ['random']])( |
| 101 | + 'Throw an error because %s is not supported', |
| 102 | + async packageManager => { |
| 103 | + await expect(restoreCache(packageManager)).rejects.toThrowError( |
| 104 | + `Caching for '${packageManager}' is not supported` |
| 105 | + ); |
| 106 | + } |
| 107 | + ); |
| 108 | + }); |
| 109 | + |
| 110 | + describe('Restore dependencies', () => { |
| 111 | + it.each([ |
| 112 | + ['yarn', '2.1.2', yarnFileHash], |
| 113 | + ['yarn', '1.2.3', yarnFileHash], |
| 114 | + ['npm', '', npmFileHash] |
| 115 | + ])( |
| 116 | + 'restored dependencies for %s', |
| 117 | + async (packageManager, toolVersion, fileHash) => { |
| 118 | + getCommandOutputSpy.mockImplementation((command: string) => { |
| 119 | + if (command.includes('version')) { |
| 120 | + return toolVersion; |
| 121 | + } else { |
| 122 | + return findCacheFolder(command); |
| 123 | + } |
| 124 | + }); |
| 125 | + |
| 126 | + await restoreCache(packageManager); |
| 127 | + expect(hashFilesSpy).toHaveBeenCalled(); |
| 128 | + expect(infoSpy).toHaveBeenCalledWith( |
| 129 | + `Cache restored from key: ${platform}-${packageManager}-${fileHash}` |
| 130 | + ); |
| 131 | + expect(infoSpy).not.toHaveBeenCalledWith( |
| 132 | + `${packageManager} cache is not found` |
| 133 | + ); |
| 134 | + } |
| 135 | + ); |
| 136 | + }); |
| 137 | + |
| 138 | + describe('Dependencies changed', () => { |
| 139 | + it.each([ |
| 140 | + ['yarn', '2.1.2', yarnFileHash], |
| 141 | + ['yarn', '1.2.3', yarnFileHash], |
| 142 | + ['npm', '', npmFileHash] |
| 143 | + ])( |
| 144 | + 'dependencies are changed %s', |
| 145 | + async (packageManager, toolVersion, fileHash) => { |
| 146 | + getCommandOutputSpy.mockImplementation((command: string) => { |
| 147 | + if (command.includes('version')) { |
| 148 | + return toolVersion; |
| 149 | + } else { |
| 150 | + return findCacheFolder(command); |
| 151 | + } |
| 152 | + }); |
| 153 | + |
| 154 | + restoreCacheSpy.mockImplementationOnce(() => undefined); |
| 155 | + await restoreCache(packageManager); |
| 156 | + expect(hashFilesSpy).toHaveBeenCalled(); |
| 157 | + expect(infoSpy).toHaveBeenCalledWith( |
| 158 | + `${packageManager} cache is not found` |
| 159 | + ); |
| 160 | + } |
| 161 | + ); |
| 162 | + }); |
| 163 | + |
| 164 | + afterEach(() => { |
| 165 | + jest.resetAllMocks(); |
| 166 | + jest.clearAllMocks(); |
| 167 | + }); |
| 168 | +}); |
0 commit comments