Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/test/sanity/src/cli.test.ts
5241 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import assert from 'assert';
7
import { Browser } from 'playwright';
8
import { TestContext } from './context.js';
9
import { GitHubAuth } from './githubAuth.js';
10
import { UITest } from './uiTest.js';
11
12
export function setup(context: TestContext) {
13
context.test('cli-alpine-arm64', ['alpine', 'arm64'], async () => {
14
const dir = await context.downloadAndUnpack('cli-alpine-arm64');
15
const entryPoint = context.getCliEntryPoint(dir);
16
await testCliApp(entryPoint);
17
});
18
19
context.test('cli-alpine-x64', ['alpine', 'x64'], async () => {
20
const dir = await context.downloadAndUnpack('cli-alpine-x64');
21
const entryPoint = context.getCliEntryPoint(dir);
22
await testCliApp(entryPoint);
23
});
24
25
context.test('cli-darwin-arm64', ['darwin', 'arm64'], async () => {
26
const dir = await context.downloadAndUnpack('cli-darwin-arm64');
27
context.validateAllCodesignSignatures(dir);
28
const entryPoint = context.getCliEntryPoint(dir);
29
await testCliApp(entryPoint);
30
});
31
32
context.test('cli-darwin-x64', ['darwin', 'x64'], async () => {
33
const dir = await context.downloadAndUnpack('cli-darwin-x64');
34
context.validateAllCodesignSignatures(dir);
35
const entryPoint = context.getCliEntryPoint(dir);
36
await testCliApp(entryPoint);
37
});
38
39
context.test('cli-linux-arm64', ['linux', 'arm64'], async () => {
40
const dir = await context.downloadAndUnpack('cli-linux-arm64');
41
const entryPoint = context.getCliEntryPoint(dir);
42
await testCliApp(entryPoint);
43
});
44
45
context.test('cli-linux-armhf', ['linux', 'arm32'], async () => {
46
const dir = await context.downloadAndUnpack('cli-linux-armhf');
47
const entryPoint = context.getCliEntryPoint(dir);
48
await testCliApp(entryPoint);
49
});
50
51
context.test('cli-linux-x64', ['linux', 'x64'], async () => {
52
const dir = await context.downloadAndUnpack('cli-linux-x64');
53
const entryPoint = context.getCliEntryPoint(dir);
54
await testCliApp(entryPoint);
55
});
56
57
context.test('cli-win32-arm64', ['windows', 'arm64'], async () => {
58
const dir = await context.downloadAndUnpack('cli-win32-arm64');
59
context.validateAllAuthenticodeSignatures(dir);
60
const entryPoint = context.getCliEntryPoint(dir);
61
await testCliApp(entryPoint);
62
});
63
64
context.test('cli-win32-x64', ['windows', 'x64'], async () => {
65
const dir = await context.downloadAndUnpack('cli-win32-x64');
66
context.validateAllAuthenticodeSignatures(dir);
67
const entryPoint = context.getCliEntryPoint(dir);
68
await testCliApp(entryPoint);
69
});
70
71
async function testCliApp(entryPoint: string) {
72
if (context.options.downloadOnly) {
73
return;
74
}
75
76
const result = context.runNoErrors(entryPoint, '--version');
77
const version = result.stdout.trim().match(/\(commit ([a-f0-9]+)\)/)?.[1];
78
assert.strictEqual(version, context.options.commit, `Expected commit ${context.options.commit} but got ${version}`);
79
80
if (!context.capabilities.has('github-account')) {
81
return;
82
}
83
84
const cliDataDir = context.createTempDir();
85
const test = new UITest(context);
86
const auth = new GitHubAuth(context);
87
let browser: Browser | undefined;
88
89
context.log('Logging out of Dev Tunnel to ensure fresh authentication');
90
context.run(entryPoint, '--cli-data-dir', cliDataDir, 'tunnel', 'user', 'logout');
91
92
context.log('Starting Dev Tunnel to local server using CLI');
93
await context.runCliApp('CLI', entryPoint,
94
[
95
'--cli-data-dir', cliDataDir,
96
'tunnel',
97
'--accept-server-license-terms',
98
'--server-data-dir', context.createTempDir(),
99
'--extensions-dir', test.extensionsDir,
100
'--verbose'
101
],
102
async (line) => {
103
const deviceCode = /To grant access .* use code ([A-Z0-9-]+)/.exec(line)?.[1];
104
if (deviceCode) {
105
context.log(`Device code detected: ${deviceCode}, starting device flow authentication`);
106
browser = await context.launchBrowser();
107
await auth.runDeviceCodeFlow(browser, deviceCode);
108
return;
109
}
110
111
const tunnelUrl = /Open this link in your browser (https?:\/\/[^\s]+)/.exec(line)?.[1];
112
if (tunnelUrl) {
113
const tunnelId = new URL(tunnelUrl).pathname.split('/').pop()!;
114
const url = context.getTunnelUrl(tunnelUrl, test.workspaceDir);
115
context.log(`CLI started successfully with tunnel URL: ${url}`);
116
117
if (!browser) {
118
throw new Error('Browser instance is not available');
119
}
120
121
context.log(`Navigating to ${url}`);
122
const page = await context.getPage(browser.newPage());
123
await page.goto(url);
124
125
context.log('Waiting for the workbench to load');
126
await page.waitForSelector('.monaco-workbench');
127
128
context.log('Selecting GitHub Account');
129
await page.locator('span.monaco-highlighted-label', { hasText: 'GitHub' }).click();
130
131
context.log('Clicking Allow on confirmation dialog');
132
await page.getByRole('button', { name: 'Allow' }).click();
133
134
await auth.runUserWebFlow(page);
135
136
context.log('Waiting for connection to be established');
137
await page.getByRole('button', { name: `remote ${tunnelId}` }).waitFor({ timeout: 5 * 60 * 1000 });
138
139
await test.run(page);
140
141
context.log('Closing browser');
142
await browser.close();
143
144
test.validate();
145
return true;
146
}
147
}
148
);
149
}
150
}
151
152