Repository navigation
Expand file tree
/
Copy pathflutter.spec.ts
More file actions
119 lines (100 loc) · 4.15 KB
/
Copy pathflutter.spec.ts
File metadata and controls
119 lines (100 loc) · 4.15 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
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,
PathService,
} from '../services/index.ts';
import { FlutterInstallService, FlutterPrepareService } from './flutter.ts';
import { scope } from '~test/http-mock.ts';
import { ensurePaths } 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://github.com';
const tarball = 'flutter archive';
describe('cli/tools/flutter', () => {
beforeAll(async () => {
await ensurePaths(['tmp', 'home/ubuntu', 'root', 'opt/containerbase/bin']);
});
beforeEach(() => {
vi.mocked(arch).mockReturnValue('x64');
execaMock.mockResolvedValue({ failed: false });
});
describe('FlutterInstallService', () => {
test.each([
{ hostArch: 'x64', ghArch: 'x86_64', version: '3.24.0' },
{ hostArch: 'arm64', ghArch: 'aarch64', version: '3.24.1' },
] as const)('install on $ghArch', async ({ hostArch, ghArch, version }) => {
vi.mocked(arch).mockReturnValue(hostArch);
const { svc, pathSvc } = await toolContext(FlutterInstallService);
const filename = `flutter-${version}-${ghArch}.tar.xz`;
const releaseUrl = `/containerbase/flutter-prebuild/releases/download/${version}`;
scope(baseUrl)
.get(`${releaseUrl}/${filename}.sha512`)
.reply(200, `${checksum(tarball, 'sha512')}\n`)
.get(`${releaseUrl}/${filename}`)
.reply(200, tarball);
const extract = vi.spyOn(CompressionService.prototype, 'extract');
await expect(svc.install(version)).resolves.toBeUndefined();
expect(extract).toHaveBeenCalledExactlyOnceWith({
file: expect.stringContaining(filename),
cwd: pathSvc.toolPath('flutter'),
});
});
test('link', async () => {
const { svc, pathSvc } = await toolContext(FlutterInstallService);
const spy = vi.spyOn(LinkToolService.prototype, 'shellwrapper');
await expect(svc.link('3.24.0')).resolves.toBeUndefined();
expect(spy).toHaveBeenCalledExactlyOnceWith('flutter', {
srcDir: join(pathSvc.versionedToolPath('flutter', '3.24.0'), 'bin'),
args: '--no-version-check',
});
});
test('runs the tool test', async () => {
const { svc } = await toolContext(FlutterInstallService);
await expect(svc.test('3.24.0')).resolves.toBeUndefined();
expect(execaMock).toHaveBeenCalledWith(
'flutter',
['--version'],
expect.any(Object),
);
});
});
describe('FlutterPrepareService', () => {
test('initialize and prepare', async () => {
const { svc, child } = await toolContext(FlutterPrepareService);
const pathSvc = await child.getAsync(PathService);
const envSvc = await child.getAsync(EnvService);
await expect(svc.initialize()).resolves.toBeUndefined();
expect(
await fs.readFile(join(pathSvc.cachePath, '.flutter'), 'utf8'),
).toBe('{ "firstRun": false, "enabled": false }\n');
expect(
await fs.readFile(
join(pathSvc.cachePath, '.flutter_tool_state'),
'utf8',
),
).toBe('{ "is-bot": false, "redisplay-welcome-message": false }\n');
await expect(svc.prepare()).resolves.toBeUndefined();
expect(
await fs.readFile(join(envSvc.rootDir, 'root', '.flutter'), 'utf8'),
).toBe('{ "firstRun": false, "enabled": false }');
expect(await fs.readlink(join(envSvc.userHome, '.flutter'))).toBe(
join(pathSvc.cachePath, '.flutter'),
);
expect(
await fs.readlink(join(envSvc.userHome, '.flutter_tool_state')),
).toBe(join(pathSvc.cachePath, '.flutter_tool_state'));
// second run is a no-op, it would throw on existing symlinks otherwise
await expect(svc.prepare()).resolves.toBeUndefined();
});
});
});