-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.ts
More file actions
129 lines (110 loc) · 3.44 KB
/
Copy pathindex.test.ts
File metadata and controls
129 lines (110 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import type { arch, platform } from 'node:os';
import type { addPath, getInput, setFailed } from '@actions/core';
import type { exec } from '@actions/exec';
import type {
cacheFile,
downloadTool,
extractTar,
extractZip,
find,
} from '@actions/tool-cache';
import { jest } from '@jest/globals';
const mockedCore: {
getInput: jest.MockedFunction<typeof getInput>;
addPath: jest.MockedFunction<typeof addPath>;
setFailed: jest.MockedFunction<typeof setFailed>;
} = {
getInput: jest.fn(),
addPath: jest.fn(),
setFailed: jest.fn(),
};
const mockedExec: {
exec: jest.MockedFunction<typeof exec>;
} = {
exec: jest.fn(),
};
const mockedTc: {
downloadTool: jest.MockedFunction<typeof downloadTool>;
extractTar: jest.MockedFunction<typeof extractTar>;
extractZip: jest.MockedFunction<typeof extractZip>;
cacheFile: jest.MockedFunction<typeof cacheFile>;
find: jest.MockedFunction<typeof find>;
} = {
downloadTool: jest.fn(),
extractTar: jest.fn(),
extractZip: jest.fn(),
cacheFile: jest.fn(),
find: jest.fn(),
};
const mockedOs: {
platform: jest.MockedFunction<typeof platform>;
arch: jest.MockedFunction<typeof arch>;
} = {
platform: jest.fn(),
arch: jest.fn(),
};
jest.unstable_mockModule('@actions/core', () => mockedCore);
jest.unstable_mockModule('@actions/exec', () => mockedExec);
jest.unstable_mockModule('@actions/tool-cache', () => mockedTc);
jest.unstable_mockModule('node:os', () => mockedOs);
const { run } = await import('./index.js');
beforeEach(() => {
jest.resetAllMocks();
});
const cliName = 'cli-name';
const cliVersion = '1.2.3';
const pathToTarball = 'path/to/tarball';
const pathToCLI = 'path/to/cli';
const platforms = ['darwin', 'win32', 'linux'];
describe.each(platforms)('when platform is %p', (platform) => {
beforeEach(() => {
mockedOs.platform.mockReturnValue(platform as NodeJS.Platform);
mockedOs.arch.mockReturnValue('arm64' as NodeJS.Architecture);
mockedCore.getInput.mockImplementation((input) => {
switch (input) {
case 'cli-version':
return cliVersion;
case 'cli-name':
return cliName;
default:
throw Error(`Invalid input: ${input}`);
}
});
});
it('downloads, extracts, and adds CLI to PATH', async () => {
mockedTc.downloadTool.mockResolvedValueOnce(pathToTarball);
const isWin32 = platform === 'win32';
const extract = isWin32 ? mockedTc.extractZip : mockedTc.extractTar;
extract.mockResolvedValueOnce(pathToCLI);
await run();
expect(mockedTc.downloadTool).toHaveBeenCalledWith(
expect.stringContaining(
`https://github.com/cli/cli/releases/download/v${cliVersion}/gh_${cliVersion}_`,
),
);
expect(extract).toHaveBeenCalledWith(pathToTarball);
expect(mockedExec.exec).toHaveBeenCalledWith('mv', [
expect.stringContaining('/bin/gh'),
expect.stringContaining(`/bin/${cliName}`),
]);
expect(mockedTc.cacheFile).toHaveBeenCalledWith(
expect.stringContaining(`/bin/${cliName}`),
isWin32 ? `${cliName}.exe` : cliName,
cliName,
cliVersion,
);
expect(mockedCore.addPath).toHaveBeenCalledWith(
expect.stringContaining(pathToCLI),
);
});
});
describe('error', () => {
it('throws error', async () => {
const message = 'error';
mockedCore.getInput.mockImplementationOnce(() => {
throw new Error(message);
});
await run();
expect(mockedCore.setFailed).toHaveBeenCalledWith(message);
});
});