-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add missing cache unit tests (#44)
* test: add missing cache unit tests * refactor: clean up the tests
- Loading branch information
Showing
7 changed files
with
172 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
indent_style = tab | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
|
||
[*.md] | ||
indent_style = space | ||
trim_trailing_whitespace = false | ||
|
||
[*.yml] | ||
indent_size = 2 | ||
indent_style = space |
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,102 @@ | ||
import * as remoteCache from '@actions/cache/lib'; | ||
import * as core from '@actions/core'; | ||
import * as toolCache from '@actions/tool-cache'; | ||
import os from 'os'; | ||
import * as cache from '../src/cache'; | ||
import * as utils from './utils'; | ||
|
||
describe('fromLocalCache', () => { | ||
it('fetches the version specific cache', async () => { | ||
const path = '/path/to/local/cache'; | ||
// todo: check why jest wants `never` instead of `string` | ||
const find = jest.spyOn(toolCache, 'find').mockResolvedValue(path as never); | ||
const result = await cache.fromLocalCache('3.20.1'); | ||
expect(result).toBe(path); | ||
expect(find).toBeCalledWith('expo-cli', '3.20.1'); | ||
}); | ||
}); | ||
|
||
describe('toLocalCache', () => { | ||
it('stores the version specific cache', async () => { | ||
const path = '/path/to/local/cache'; | ||
const root = '/path/from/source'; | ||
const cacheDir = jest.spyOn(toolCache, 'cacheDir').mockResolvedValue(path); | ||
const result = await cache.toLocalCache(root, '3.20.1'); | ||
expect(result).toBe(path); | ||
expect(cacheDir).toBeCalledWith(root, 'expo-cli', '3.20.1'); | ||
}); | ||
}); | ||
|
||
describe('fromRemoteCache', () => { | ||
const spy = { | ||
fail: jest.spyOn(core, 'setFailed').mockImplementation(), | ||
restore: jest.spyOn(remoteCache, 'restoreCache').mockImplementation(), | ||
}; | ||
|
||
beforeAll(() => { | ||
utils.setEnv('RUNNER_TOOL_CACHE', '/cache/path'); | ||
}); | ||
|
||
afterAll(() => { | ||
utils.restoreEnv(); | ||
}); | ||
|
||
it('restores remote cache with default key', async () => { | ||
expect(await cache.fromRemoteCache('3.20.1', 'yarn')).toBeUndefined(); | ||
expect(remoteCache.restoreCache).toBeCalledWith( | ||
`/cache/path/expo-cli/3.20.1/${os.arch()}`, | ||
`expo-cli-${process.platform}-${os.arch()}-yarn-3.20.1`, | ||
`expo-cli-${process.platform}-${os.arch()}-yarn-3.20.1`, | ||
); | ||
}); | ||
|
||
it('restores remote cache with custom key', async () => { | ||
expect(await cache.fromRemoteCache('3.20.0', 'yarn', 'custom-cache-key')).toBeUndefined(); | ||
expect(remoteCache.restoreCache).toBeCalledWith( | ||
`/cache/path/expo-cli/3.20.0/${os.arch()}`, | ||
'custom-cache-key', | ||
'custom-cache-key', | ||
); | ||
}); | ||
|
||
it('returns path when remote cache exists', async () => { | ||
spy.restore.mockResolvedValueOnce(true); | ||
await expect(cache.fromRemoteCache('3.20.1', 'npm')).resolves.toBe( | ||
`/cache/path/expo-cli/3.20.1/${os.arch()}`, | ||
); | ||
}); | ||
|
||
it('fails when remote cache throws', async () => { | ||
const error = new Error('Remote cache restore failed'); | ||
spy.restore.mockRejectedValueOnce(error); | ||
await expect(cache.fromRemoteCache('3.20.1', 'yarn')).rejects.toBe(error); | ||
expect(spy.fail).toBeCalledWith(error); | ||
}); | ||
}); | ||
|
||
describe('toRemoteCache', () => { | ||
const spy = { | ||
fail: jest.spyOn(core, 'setFailed').mockImplementation(), | ||
save: jest.spyOn(remoteCache, 'saveCache').mockImplementation(), | ||
}; | ||
|
||
it('saves remote cache with default key', async () => { | ||
expect(await cache.toRemoteCache('/local/path', '3.20.1', 'npm')).toBeUndefined(); | ||
expect(remoteCache.saveCache).toBeCalledWith( | ||
'/local/path', | ||
`expo-cli-${process.platform}-${os.arch()}-npm-3.20.1`, | ||
); | ||
}); | ||
|
||
it('saves remote cache with custom key', async () => { | ||
expect(await cache.toRemoteCache('/local/path', '3.20.1', 'yarn', 'custom-cache-key')).toBeUndefined(); | ||
expect(remoteCache.saveCache).toBeCalledWith('/local/path', 'custom-cache-key'); | ||
}); | ||
|
||
it('fails when remote cache throws', async () => { | ||
const error = new Error('Remote cache save failed'); | ||
spy.save.mockRejectedValueOnce(error); | ||
await expect(cache.toRemoteCache('/local/path', '3.20.1', 'yarn')).rejects.toBe(error); | ||
expect(spy.fail).toBeCalledWith(error); | ||
}); | ||
}); |
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