Repository navigation
Expand file tree
/
Copy pathrust.spec.ts
More file actions
221 lines (193 loc) · 7.33 KB
/
Copy pathrust.spec.ts
File metadata and controls
221 lines (193 loc) · 7.33 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import fs from 'node:fs/promises';
import { arch } from 'node:os';
import { join } from 'node:path';
import { beforeAll, beforeEach, describe, expect, test, vi } from 'vitest';
import {
CompressionService,
EnvService,
LinkToolService,
} from '../services/index.ts';
import { RustInstallService, RustPrepareService } from './rust.ts';
import { scope } from '~test/http-mock.ts';
import { ensurePaths, rootPath } from '~test/path.ts';
import { checksum, 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://static.rust-lang.org';
const archive = 'rust archive';
describe('cli/tools/rust', () => {
beforeAll(async () => {
await ensurePaths(['tmp', 'home/ubuntu', 'opt/containerbase/bin']);
});
beforeEach(() => {
vi.mocked(arch).mockReturnValue('x64');
execaMock.mockResolvedValue({ failed: false });
});
describe('RustPrepareService', () => {
test('prepare', async () => {
const { svc, child, pathSvc } = await toolContext(RustPrepareService);
const envSvc = await child.getAsync(EnvService);
await expect(svc.prepare()).resolves.toBeUndefined();
expect(await fs.readlink(join(envSvc.userHome, '.cargo'))).toBe(
join(pathSvc.cachePath, '.cargo'),
);
});
test('prepare keeps an existing .cargo link', async () => {
const { svc, child, pathSvc } = await toolContext(RustPrepareService);
const envSvc = await child.getAsync(EnvService);
await expect(svc.prepare()).resolves.toBeUndefined();
await expect(svc.prepare()).resolves.toBeUndefined();
expect(await fs.readlink(join(envSvc.userHome, '.cargo'))).toBe(
join(pathSvc.cachePath, '.cargo'),
);
});
});
describe('RustInstallService', () => {
test.each([
{
hostArch: 'x64',
version: '1.18.0',
file: '/dist/rust-1.18.0-x86_64-unknown-linux-gnu.tar',
target: 'x86_64-unknown-linux-gnu',
ext: 'gz',
},
{
hostArch: 'arm64',
version: '1.19.0',
file: '/dist/rust-1.19.0-aarch64-unknown-linux-gnu.tar',
target: 'aarch64-unknown-linux-gnu',
ext: 'xz',
},
{
hostArch: 'x64',
version: 'nightly-2017-05-04',
file: '/dist/2017-05-04/rust-nightly-x86_64-unknown-linux-gnu.tar',
target: 'x86_64-unknown-linux-gnu',
ext: 'gz',
},
{
hostArch: 'arm64',
version: 'nightly-2017-05-05',
file: '/dist/2017-05-05/rust-nightly-aarch64-unknown-linux-gnu.tar',
target: 'aarch64-unknown-linux-gnu',
ext: 'xz',
},
{
hostArch: 'x64',
version: 'beta',
file: '/dist/rust-beta-x86_64-unknown-linux-gnu.tar',
target: 'x86_64-unknown-linux-gnu',
ext: 'xz',
},
{
hostArch: 'arm64',
version: 'nightly',
file: '/dist/rust-nightly-aarch64-unknown-linux-gnu.tar',
target: 'aarch64-unknown-linux-gnu',
ext: 'xz',
},
] as const)(
'install $version on $target',
async ({ hostArch, version, file, target, ext }) => {
vi.mocked(arch).mockReturnValue(hostArch);
const { svc, pathSvc } = await toolContext(RustInstallService);
scope(baseUrl)
.get(`${file}.${ext}.sha256`)
.reply(200, `${checksum(archive)} rust.tar.${ext}\n`)
.get(`${file}.${ext}`)
.reply(200, archive);
const extract = vi.spyOn(CompressionService.prototype, 'extract');
await expect(svc.install(version)).resolves.toBeUndefined();
expect(extract).toHaveBeenCalledExactlyOnceWith({
file: expect.stringContaining(`.tar.${ext}`),
cwd: expect.any(String),
strip: 1,
});
expect(execaMock).toHaveBeenCalledWith(
expect.stringMatching(/\/install\.sh$/),
[
`--prefix=${pathSvc.versionedToolPath('rust', version)}`,
`--components=cargo,rust-std-${target},rustc`,
],
expect.any(Object),
);
},
);
test('install: removes the temp folder when extraction fails', async () => {
const version = '1.99.0';
const file = `/dist/rust-${version}-x86_64-unknown-linux-gnu.tar.xz`;
const { svc } = await toolContext(RustInstallService);
scope(baseUrl)
.get(`${file}.sha256`)
.reply(200, `${checksum(archive)} rust.tar.xz\n`)
.get(file)
.reply(200, archive);
vi.spyOn(CompressionService.prototype, 'extract').mockRejectedValueOnce(
new Error('extract failed'),
);
await expect(svc.install(version)).rejects.toThrow('extract failed');
const entries = await fs.readdir(rootPath('tmp'));
expect(entries.some((e) => e.startsWith('rust-'))).toBe(false);
});
test('install: rejects an empty checksum', async () => {
const { svc } = await toolContext(RustInstallService);
const file = '/dist/rust-1.97.0-x86_64-unknown-linux-gnu.tar.xz';
scope(baseUrl).get(`${file}.sha256`).reply(200, '');
await expect(svc.install('1.97.0')).rejects.toThrow(
`Checksum not found in ${baseUrl}${file}.sha256`,
);
});
test('install: rejects a checksum mismatch', async () => {
const { svc } = await toolContext(RustInstallService);
const file = '/dist/rust-1.98.0-x86_64-unknown-linux-gnu.tar.xz';
scope(baseUrl)
.get(`${file}.sha256`)
.reply(200, `${checksum('other')} rust.tar.xz\n`)
.get(file)
.times(3)
.reply(200, archive);
await expect(svc.install('1.98.0')).rejects.toThrow('download failed');
});
test('link', async () => {
const { svc, pathSvc } = await toolContext(RustInstallService);
const spy = vi.spyOn(LinkToolService.prototype, 'shellwrapper');
const srcDir = join(pathSvc.versionedToolPath('rust', '1.98.1'), 'bin');
await expect(svc.link('1.98.1')).resolves.toBeUndefined();
expect(spy).toHaveBeenCalledTimes(2);
expect(spy).toHaveBeenCalledWith('rust', { name: 'cargo', srcDir });
expect(spy).toHaveBeenCalledWith('rust', { name: 'rustc', srcDir });
});
test('runs the tool test', async () => {
const { svc } = await toolContext(RustInstallService);
await expect(svc.test('1.98.1')).resolves.toBeUndefined();
expect(execaMock).toHaveBeenCalledWith(
'cargo',
['--version'],
expect.any(Object),
);
expect(execaMock).toHaveBeenCalledWith(
'rustc',
['--version'],
expect.any(Object),
);
});
test.each([
{ version: '1.98.1', valid: true },
{ version: 'beta', valid: true },
{ version: 'nightly', valid: true },
{ version: 'nightly-2026-06-19', valid: true },
{ version: 'stable', valid: false },
{ version: 'nightly-', valid: false },
{ version: 'nightly-2026-6-19', valid: false },
{ version: 'nightly-latest', valid: false },
{ version: 'nightly-2026-06-19/../x', valid: false },
])('validates $version', async ({ version, valid }) => {
const { svc } = await toolContext(RustInstallService);
await expect(svc.validate(version)).resolves.toBe(valid);
});
});
});