forked from actions/setup-node
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
38d335a
commit 01aec7b
Showing
15 changed files
with
23,229 additions
and
11,714 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
import * as core from '@actions/core'; | ||
import * as cache from '@actions/cache'; | ||
import * as path from 'path'; | ||
import * as glob from '@actions/glob'; | ||
|
||
import * as utils from '../src/cache-utils'; | ||
import {restoreCache} from '../src/cache-restore'; | ||
|
||
describe('cache-restore', () => { | ||
process.env['GITHUB_WORKSPACE'] = path.join(__dirname, 'data'); | ||
if (!process.env.RUNNER_OS) { | ||
process.env.RUNNER_OS = 'Linux'; | ||
} | ||
const platform = process.env.RUNNER_OS; | ||
const commonPath = '/some/random/path'; | ||
const npmCachePath = `${commonPath}/npm`; | ||
const yarn1CachePath = `${commonPath}/yarn1`; | ||
const yarn2CachePath = `${commonPath}/yarn2`; | ||
const yarnFileHash = | ||
'b8a0bae5243251f7c07dd52d1f78ff78281dfefaded700a176261b6b54fa245b'; | ||
const npmFileHash = | ||
'abf7c9b306a3149dcfba4673e2362755503bcceaab46f0e4e6fee0ade493e20c'; | ||
const cachesObject = { | ||
[npmCachePath]: npmFileHash, | ||
[yarn1CachePath]: yarnFileHash, | ||
[yarn2CachePath]: yarnFileHash | ||
}; | ||
|
||
let packageManagerVersion = '1.2.3'; | ||
|
||
let saveStateSpy: jest.SpyInstance; | ||
let infoSpy: jest.SpyInstance; | ||
let debugSpy: jest.SpyInstance; | ||
let setOutputSpy: jest.SpyInstance; | ||
let getCommandOutputSpy: jest.SpyInstance; | ||
let restoreCacheSpy: jest.SpyInstance; | ||
let hashFilesSpy: jest.SpyInstance; | ||
|
||
beforeEach(() => { | ||
// core | ||
infoSpy = jest.spyOn(core, 'info'); | ||
infoSpy.mockImplementation(() => undefined); | ||
|
||
debugSpy = jest.spyOn(core, 'debug'); | ||
debugSpy.mockImplementation(() => undefined); | ||
|
||
setOutputSpy = jest.spyOn(core, 'setOutput'); | ||
setOutputSpy.mockImplementation(() => undefined); | ||
|
||
saveStateSpy = jest.spyOn(core, 'saveState'); | ||
saveStateSpy.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 ''; | ||
} | ||
}); | ||
|
||
// cache | ||
restoreCacheSpy = jest.spyOn(cache, 'restoreCache'); | ||
restoreCacheSpy.mockImplementation( | ||
(cachePaths: Array<string>, key: string) => { | ||
if (!cachePaths || cachePaths.length === 0) { | ||
return undefined; | ||
} | ||
|
||
const cachPath = cachePaths[0]; | ||
const fileHash = cachesObject[cachPath]; | ||
|
||
if (key.includes(fileHash)) { | ||
return key; | ||
} | ||
|
||
return undefined; | ||
} | ||
); | ||
|
||
// cache-utils | ||
getCommandOutputSpy = jest.spyOn(utils, 'getCommandOutput'); | ||
getCommandOutputSpy.mockImplementation((command: string) => { | ||
if (command.includes('version')) { | ||
return packageManagerVersion; | ||
} else { | ||
switch (command) { | ||
case utils.supportedPackageManagers.npm.getCacheFolderCommand: | ||
return npmCachePath; | ||
case utils.supportedPackageManagers.yarn1.getCacheFolderCommand: | ||
return yarn1CachePath; | ||
case utils.supportedPackageManagers.yarn2.getCacheFolderCommand: | ||
return yarn2CachePath; | ||
default: | ||
return 'packge/not/found'; | ||
} | ||
} | ||
}); | ||
}); | ||
it.each([['npm7'], ['npm7'], ['yarn1'], ['yarn2'], ['random']])( | ||
'it will throw an error because %s is not supported', | ||
async packageManager => { | ||
await expect(restoreCache(packageManager)).rejects.toThrowError( | ||
`Caching for '${packageManager}' is not supported` | ||
); | ||
} | ||
); | ||
|
||
it.each([ | ||
['yarn', '2.1.2', yarnFileHash], | ||
['yarn', '1.2.3', yarnFileHash], | ||
['npm', '', npmFileHash] | ||
])( | ||
'restored packages for %s', | ||
async (packageManager, toolVersion, fileHash) => { | ||
if (toolVersion) { | ||
packageManagerVersion = toolVersion; | ||
} | ||
|
||
await restoreCache(packageManager); | ||
expect(infoSpy).toHaveBeenLastCalledWith( | ||
`Cache restored from key: ${platform}-${packageManager}-${fileHash}` | ||
); | ||
} | ||
); | ||
|
||
it.each([ | ||
['yarn', '2.1.2', yarnFileHash], | ||
['yarn', '1.2.3', yarnFileHash], | ||
['npm', '', npmFileHash] | ||
])( | ||
'restored packages for %s', | ||
async (packageManager, toolVersion, fileHash) => { | ||
if (toolVersion) { | ||
packageManagerVersion = toolVersion; | ||
} | ||
restoreCacheSpy.mockImplementationOnce(() => undefined); | ||
await restoreCache(packageManager); | ||
expect(infoSpy).not.toHaveBeenLastCalledWith( | ||
`Cache restored from key: ${platform}-${packageManager}-${fileHash}` | ||
); | ||
} | ||
); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
jest.clearAllMocks(); | ||
//jest.restoreAllMocks(); | ||
}); | ||
}); |
Oops, something went wrong.