Repository navigation
Expand file tree
/
Copy pathprotoc.spec.ts
More file actions
81 lines (66 loc) · 2.71 KB
/
Copy pathprotoc.spec.ts
File metadata and controls
81 lines (66 loc) · 2.71 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
import { arch } from 'node:os';
import { join } from 'node:path';
import { beforeAll, beforeEach, describe, expect, test, vi } from 'vitest';
import { CompressionService, LinkToolService } from '../services/index.ts';
import { ProtocInstallService } from './protoc.ts';
import { scope } from '~test/http-mock.ts';
import { ensurePaths } from '~test/path.ts';
import { toolContext } from '~test/tool.ts';
const { execaMock } = vi.hoisted(() => ({ execaMock: vi.fn() }));
vi.mock('execa', () => ({ execa: execaMock }));
vi.mock('node:os', async (importOriginal) => ({
...(await importOriginal<typeof import('node:os')>()),
arch: vi.fn(() => 'x64'),
}));
const baseUrl = 'https://github.com';
describe('cli/tools/protoc', () => {
beforeAll(async () => {
await ensurePaths(['tmp', 'opt/containerbase/bin']);
});
beforeEach(() => {
vi.mocked(arch).mockReturnValue('x64');
execaMock.mockResolvedValue({ failed: false });
});
test.each([
{ hostArch: 'x64', ghArch: 'x86_64', version: '28.3' },
{ hostArch: 'arm64', ghArch: 'aarch_64', version: '28.4' },
] as const)('install on $ghArch', async ({ hostArch, ghArch, version }) => {
vi.mocked(arch).mockReturnValue(hostArch);
const { svc, pathSvc } = await toolContext(ProtocInstallService);
const filename = `protoc-${version}-linux-${ghArch}.zip`;
scope(baseUrl)
.get(
`/protocolbuffers/protobuf/releases/download/v${version}/${filename}`,
)
.reply(200, 'protoc archive');
const extract = vi.spyOn(CompressionService.prototype, 'extract');
await expect(svc.install(version)).resolves.toBeUndefined();
expect(extract).toHaveBeenCalledExactlyOnceWith({
file: expect.stringContaining(filename),
cwd: pathSvc.versionedToolPath('protoc', version),
});
});
test('link', async () => {
const { svc, pathSvc } = await toolContext(ProtocInstallService);
const spy = vi.spyOn(LinkToolService.prototype, 'shellwrapper');
await expect(svc.link('28.3')).resolves.toBeUndefined();
expect(spy).toHaveBeenCalledExactlyOnceWith('protoc', {
srcDir: join(pathSvc.versionedToolPath('protoc', '28.3'), 'bin'),
});
});
test('runs the tool test', async () => {
const { svc } = await toolContext(ProtocInstallService);
await expect(svc.test('28.3')).resolves.toBeUndefined();
expect(execaMock).toHaveBeenCalledWith(
'protoc',
['--version'],
expect.any(Object),
);
});
test('validate coerces the two part versions', async () => {
const { svc } = await toolContext(ProtocInstallService);
expect(await svc.validate('28.3')).toBe(true);
expect(await svc.validate('28.3.1')).toBe(true);
expect(await svc.validate('not-a-version')).toBe(false);
});
});