Skip to content

Commit 4279327

Browse files
authored
test: add missing cache unit tests (#44)
* test: add missing cache unit tests * refactor: clean up the tests
1 parent a7b6ce4 commit 4279327

7 files changed

Lines changed: 172 additions & 50 deletions

File tree

.editorconfig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
indent_style = tab
7+
insert_final_newline = true
8+
trim_trailing_whitespace = true
9+
10+
[*.md]
11+
indent_style = space
12+
trim_trailing_whitespace = false
13+
14+
[*.yml]
15+
indent_size = 2
16+
indent_style = space

build/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54217,7 +54217,7 @@ const os_1 = __importDefault(__webpack_require__(87));
5421754217
*/
5421854218
function fromLocalCache(version) {
5421954219
return __awaiter(this, void 0, void 0, function* () {
54220-
return toolCache.find('expo-cli', version, os_1.default.arch());
54220+
return toolCache.find('expo-cli', version);
5422154221
});
5422254222
}
5422354223
exports.fromLocalCache = fromLocalCache;
@@ -54229,7 +54229,7 @@ exports.fromLocalCache = fromLocalCache;
5422954229
*/
5423054230
function toLocalCache(root, version) {
5423154231
return __awaiter(this, void 0, void 0, function* () {
54232-
return toolCache.cacheDir(root, 'expo-cli', version, os_1.default.arch());
54232+
return toolCache.cacheDir(root, 'expo-cli', version);
5423354233
});
5423454234
}
5423554235
exports.toLocalCache = toLocalCache;
@@ -54249,7 +54249,8 @@ function fromRemoteCache(version, packager, customCacheKey) {
5424954249
}
5425054250
}
5425154251
catch (error) {
54252-
core.setFailed(error.message);
54252+
core.setFailed(error);
54253+
throw error;
5425354254
}
5425454255
});
5425554256
}
@@ -54265,7 +54266,8 @@ function toRemoteCache(source, version, packager, customCacheKey) {
5426554266
yield lib_1.saveCache(source, cacheKey);
5426654267
}
5426754268
catch (error) {
54268-
core.setFailed(error.message);
54269+
core.setFailed(error);
54270+
throw error;
5426954271
}
5427054272
});
5427154273
}

src/cache.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import os from 'os';
1111
* @see https://github.com/actions/toolkit/issues/47
1212
*/
1313
export async function fromLocalCache(version: string) {
14-
return toolCache.find('expo-cli', version, os.arch());
14+
return toolCache.find('expo-cli', version);
1515
}
1616

1717
/**
@@ -21,7 +21,7 @@ export async function fromLocalCache(version: string) {
2121
* @see https://github.com/actions/toolkit/issues/47
2222
*/
2323
export async function toLocalCache(root: string, version: string) {
24-
return toolCache.cacheDir(root, 'expo-cli', version, os.arch());
24+
return toolCache.cacheDir(root, 'expo-cli', version);
2525
}
2626

2727
/**
@@ -40,7 +40,8 @@ export async function fromRemoteCache(version: string, packager: string, customC
4040
return target;
4141
}
4242
} catch (error) {
43-
core.setFailed(error.message);
43+
core.setFailed(error);
44+
throw error;
4445
}
4546
}
4647

@@ -54,7 +55,8 @@ export async function toRemoteCache(source: string, version: string, packager: s
5455
try {
5556
await saveCache(source, cacheKey);
5657
} catch (error) {
57-
core.setFailed(error.message);
58+
core.setFailed(error);
59+
throw error;
5860
}
5961
}
6062

tests/cache.test.ts

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import * as remoteCache from '@actions/cache/lib';
2+
import * as core from '@actions/core';
3+
import * as toolCache from '@actions/tool-cache';
4+
import os from 'os';
5+
import * as cache from '../src/cache';
6+
import * as utils from './utils';
7+
8+
describe('fromLocalCache', () => {
9+
it('fetches the version specific cache', async () => {
10+
const path = '/path/to/local/cache';
11+
// todo: check why jest wants `never` instead of `string`
12+
const find = jest.spyOn(toolCache, 'find').mockResolvedValue(path as never);
13+
const result = await cache.fromLocalCache('3.20.1');
14+
expect(result).toBe(path);
15+
expect(find).toBeCalledWith('expo-cli', '3.20.1');
16+
});
17+
});
18+
19+
describe('toLocalCache', () => {
20+
it('stores the version specific cache', async () => {
21+
const path = '/path/to/local/cache';
22+
const root = '/path/from/source';
23+
const cacheDir = jest.spyOn(toolCache, 'cacheDir').mockResolvedValue(path);
24+
const result = await cache.toLocalCache(root, '3.20.1');
25+
expect(result).toBe(path);
26+
expect(cacheDir).toBeCalledWith(root, 'expo-cli', '3.20.1');
27+
});
28+
});
29+
30+
describe('fromRemoteCache', () => {
31+
const spy = {
32+
fail: jest.spyOn(core, 'setFailed').mockImplementation(),
33+
restore: jest.spyOn(remoteCache, 'restoreCache').mockImplementation(),
34+
};
35+
36+
beforeAll(() => {
37+
utils.setEnv('RUNNER_TOOL_CACHE', '/cache/path');
38+
});
39+
40+
afterAll(() => {
41+
utils.restoreEnv();
42+
});
43+
44+
it('restores remote cache with default key', async () => {
45+
expect(await cache.fromRemoteCache('3.20.1', 'yarn')).toBeUndefined();
46+
expect(remoteCache.restoreCache).toBeCalledWith(
47+
`/cache/path/expo-cli/3.20.1/${os.arch()}`,
48+
`expo-cli-${process.platform}-${os.arch()}-yarn-3.20.1`,
49+
`expo-cli-${process.platform}-${os.arch()}-yarn-3.20.1`,
50+
);
51+
});
52+
53+
it('restores remote cache with custom key', async () => {
54+
expect(await cache.fromRemoteCache('3.20.0', 'yarn', 'custom-cache-key')).toBeUndefined();
55+
expect(remoteCache.restoreCache).toBeCalledWith(
56+
`/cache/path/expo-cli/3.20.0/${os.arch()}`,
57+
'custom-cache-key',
58+
'custom-cache-key',
59+
);
60+
});
61+
62+
it('returns path when remote cache exists', async () => {
63+
spy.restore.mockResolvedValueOnce(true);
64+
await expect(cache.fromRemoteCache('3.20.1', 'npm')).resolves.toBe(
65+
`/cache/path/expo-cli/3.20.1/${os.arch()}`,
66+
);
67+
});
68+
69+
it('fails when remote cache throws', async () => {
70+
const error = new Error('Remote cache restore failed');
71+
spy.restore.mockRejectedValueOnce(error);
72+
await expect(cache.fromRemoteCache('3.20.1', 'yarn')).rejects.toBe(error);
73+
expect(spy.fail).toBeCalledWith(error);
74+
});
75+
});
76+
77+
describe('toRemoteCache', () => {
78+
const spy = {
79+
fail: jest.spyOn(core, 'setFailed').mockImplementation(),
80+
save: jest.spyOn(remoteCache, 'saveCache').mockImplementation(),
81+
};
82+
83+
it('saves remote cache with default key', async () => {
84+
expect(await cache.toRemoteCache('/local/path', '3.20.1', 'npm')).toBeUndefined();
85+
expect(remoteCache.saveCache).toBeCalledWith(
86+
'/local/path',
87+
`expo-cli-${process.platform}-${os.arch()}-npm-3.20.1`,
88+
);
89+
});
90+
91+
it('saves remote cache with custom key', async () => {
92+
expect(await cache.toRemoteCache('/local/path', '3.20.1', 'yarn', 'custom-cache-key')).toBeUndefined();
93+
expect(remoteCache.saveCache).toBeCalledWith('/local/path', 'custom-cache-key');
94+
});
95+
96+
it('fails when remote cache throws', async () => {
97+
const error = new Error('Remote cache save failed');
98+
spy.save.mockRejectedValueOnce(error);
99+
await expect(cache.toRemoteCache('/local/path', '3.20.1', 'yarn')).rejects.toBe(error);
100+
expect(spy.fail).toBeCalledWith(error);
101+
});
102+
});

tests/expo.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as core from '@actions/core';
22
import * as cli from '@actions/exec';
33
import * as expo from '../src/expo';
4-
import { setPlatform, restorePlatform, setEnv, restoreEnv } from './utils';
4+
import * as utils from './utils';
55

66
describe('authenticate', () => {
77
const spy = {
@@ -25,7 +25,7 @@ describe('authenticate', () => {
2525
});
2626

2727
it('executes login command with password through environment', async () => {
28-
setEnv('TEST_INCLUDED', 'hellyeah');
28+
utils.setEnv('TEST_INCLUDED', 'hellyeah');
2929
await expo.authenticate('bycedric', 'mypassword');
3030
expect(spy.exec).toBeCalled();
3131
expect(spy.exec.mock.calls[0][0]).toBe('expo');
@@ -36,15 +36,15 @@ describe('authenticate', () => {
3636
EXPO_CLI_PASSWORD: 'mypassword',
3737
},
3838
});
39-
restoreEnv();
39+
utils.restoreEnv();
4040
});
4141

4242
it('executes login command with `.cmd` suffix on windows', async () => {
43-
setPlatform('win32');
43+
utils.setPlatform('win32');
4444
await expo.authenticate('bycedric', 'mypassword');
4545
expect(spy.exec).toBeCalled();
4646
expect(spy.exec.mock.calls[0][0]).toBe('expo.cmd');
47-
restorePlatform();
47+
utils.restorePlatform();
4848
});
4949

5050
it('fails when credentials are incorrect', async () => {

tests/install.test.ts

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jest.mock('libnpm', () => registry);
1414
jest.mock('../src/cache', () => cache);
1515

1616
import * as install from '../src/install';
17-
import { setEnv, restoreEnv } from './utils';
17+
import * as utils from './utils';
1818

1919
describe('resolve', () => {
2020
it('fetches exact version of expo-cli', async () => {
@@ -25,66 +25,66 @@ describe('resolve', () => {
2525
});
2626

2727
describe('install', () => {
28-
it('installs path from cache', async () => {
28+
it('installs path from local cache', async () => {
2929
cache.fromLocalCache.mockResolvedValue('/cache/path');
3030
const expoPath = await install.install({ version: '3.0.10', packager: 'npm' });
3131
expect(expoPath).toBe('/cache/path/node_modules/.bin');
3232
});
3333

34-
it('installs path from packager and cache it', async () => {
35-
process.env['RUNNER_TEMP'] = '/temp/path';
34+
it('installs path from packager and cache it locally', async () => {
35+
utils.setEnv('RUNNER_TEMP', '/temp/path');
3636
cache.fromLocalCache.mockResolvedValue(undefined);
3737
cache.toLocalCache.mockResolvedValue('/cache/path');
3838
const expoPath = await install.install({ version: '3.0.10', packager: 'npm' });
3939
expect(expoPath).toBe('/cache/path/node_modules/.bin');
4040
expect(cache.toLocalCache).toBeCalledWith('/temp/path', '3.0.10');
41+
utils.restoreEnv();
4142
});
42-
});
4343

44-
describe('fromPackager', () => {
45-
afterEach(() => {
46-
restoreEnv();
44+
it('installs path from remote cache', async () => {
45+
cache.fromLocalCache.mockResolvedValue(undefined);
46+
cache.fromRemoteCache.mockResolvedValue('/cache/path');
47+
registry.manifest.mockResolvedValue({ version: '3.20.0' });
48+
const expoPath = await install.install({ version: '3.20.1', packager: 'npm', cache: true });
49+
expect(expoPath).toBe('/cache/path/node_modules/.bin');
50+
expect(cache.fromRemoteCache).toBeCalledWith('3.20.0', 'npm', undefined);
51+
});
52+
53+
it('installs path from packager and cache it remotely', async () => {
54+
utils.setEnv('RUNNER_TEMP', '/temp/path');
55+
cache.fromLocalCache.mockResolvedValue(undefined);
56+
cache.fromRemoteCache.mockResolvedValue(undefined);
57+
cache.toLocalCache.mockResolvedValue('/cache/path');
58+
registry.manifest.mockResolvedValue({ version: '3.20.1' });
59+
const expoPath = await install.install({ version: '3.20.1', packager: 'npm', cache: true });
60+
expect(expoPath).toBe('/cache/path/node_modules/.bin');
61+
expect(cache.toRemoteCache).toBeCalledWith('/cache/path', '3.20.1', 'npm', undefined);
62+
utils.restoreEnv();
4763
});
64+
});
4865

66+
describe('fromPackager', () => {
4967
it('resolves tool path', async () => {
5068
await install.fromPackager('3.0.10', 'npm');
5169
expect(io.which).toBeCalledWith('npm');
5270
});
5371

5472
it('creates temporary folder', async () => {
55-
setEnv('RUNNER_TEMP', '/temp/path');
73+
utils.setEnv('RUNNER_TEMP', '/temp/path');
5674
await install.fromPackager('latest', 'yarn');
5775
expect(io.mkdirP).toBeCalledWith('/temp/path');
76+
utils.restoreEnv();
5877
});
5978

6079
it('installs expo with tool', async () => {
61-
setEnv('RUNNER_TEMP', '/temp/path');
80+
utils.setEnv('RUNNER_TEMP', '/temp/path');
6281
io.which.mockResolvedValue('npm');
6382
const expoPath = await install.fromPackager('beta', 'npm');
6483
expect(expoPath).toBe('/temp/path');
6584
expect(cli.exec).toBeCalled();
6685
expect(cli.exec.mock.calls[0][0]).toBe('npm');
6786
expect(cli.exec.mock.calls[0][1]).toStrictEqual(['add', 'expo-cli@beta']);
6887
expect(cli.exec.mock.calls[0][2]).toMatchObject({ cwd: '/temp/path' });
88+
utils.restoreEnv();
6989
});
7090
});
71-
72-
// todo: move this to cache tests
73-
74-
// describe('fromCache', () => {
75-
// it('uses cache for exact version', async () => {
76-
// toolCache.find.mockResolvedValue('/cache/expo/path');
77-
// const cachePath = await install.fromCache('3.0.10');
78-
// expect(toolCache.find).toBeCalledWith('expo-cli', '3.0.10');
79-
// expect(cachePath).toBe('/cache/expo/path');
80-
// });
81-
// });
82-
83-
// describe('toCache', () => {
84-
// it('uses cache for installed folder', async () => {
85-
// toolCache.cacheDir.mockResolvedValue('/cache/expo/path');
86-
// const cachePath = await install.toCache('3.0.10', '/expo/install/path');
87-
// expect(toolCache.cacheDir).toBeCalledWith('/expo/install/path', 'expo-cli', '3.0.10');
88-
// expect(cachePath).toBe('/cache/expo/path');
89-
// });
90-
// });

tests/system.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as core from '@actions/core';
22
import * as cli from '@actions/exec';
33
import * as system from '../src/system';
4-
import { setPlatform, restorePlatform } from './utils';
4+
import * as utils from './utils';
55

66
describe('patchWatchers', () => {
77
const spy = {
@@ -11,11 +11,11 @@ describe('patchWatchers', () => {
1111
};
1212

1313
afterEach(() => {
14-
restorePlatform();
14+
utils.restorePlatform();
1515
});
1616

1717
it('increses fs inotify settings with sysctl', async () => {
18-
setPlatform('linux');
18+
utils.setPlatform('linux');
1919
await system.patchWatchers();
2020
expect(spy.exec).toBeCalledWith('sudo sysctl fs.inotify.max_user_instances=524288');
2121
expect(spy.exec).toBeCalledWith('sudo sysctl fs.inotify.max_user_watches=524288');
@@ -26,7 +26,7 @@ describe('patchWatchers', () => {
2626
it('warns for unsuccessful patches', async () => {
2727
const error = new Error('Something went wrong');
2828
spy.exec.mockRejectedValueOnce(error);
29-
setPlatform('linux');
29+
utils.setPlatform('linux');
3030
await system.patchWatchers();
3131
expect(core.warning).toBeCalledWith(expect.stringContaining('can\'t patch watchers'));
3232
expect(core.warning).toBeCalledWith(
@@ -35,21 +35,21 @@ describe('patchWatchers', () => {
3535
});
3636

3737
it('skips on windows platform', async () => {
38-
setPlatform('win32');
38+
utils.setPlatform('win32');
3939
await system.patchWatchers();
4040
expect(spy.info).toBeCalledWith(expect.stringContaining('Skipping'));
4141
expect(spy.exec).not.toHaveBeenCalled();
4242
});
4343

4444
it('skips on macos platform', async () => {
45-
setPlatform('darwin');
45+
utils.setPlatform('darwin');
4646
await system.patchWatchers();
4747
expect(spy.info).toBeCalledWith(expect.stringContaining('Skipping'));
4848
expect(spy.exec).not.toHaveBeenCalled();
4949
});
5050

5151
it('runs on linux platform', async () => {
52-
setPlatform('linux');
52+
utils.setPlatform('linux');
5353
await system.patchWatchers();
5454
expect(spy.info).toBeCalledWith(expect.stringContaining('Patching'));
5555
expect(spy.exec).toHaveBeenCalled();

0 commit comments

Comments
 (0)