-
Notifications
You must be signed in to change notification settings - Fork 28
/
launcher.ts
362 lines (322 loc) · 13.9 KB
/
launcher.ts
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import os from 'node:os'
import fs from 'node:fs/promises'
import path from 'node:path'
import { format } from 'node:util'
import downloadBundle from 'download'
import logger from '@wdio/logger'
import { request } from 'undici'
import { download } from '@vscode/test-electron'
import { SevereServiceError } from 'webdriverio'
import { launcher as ChromedriverServiceLauncher } from 'wdio-chromedriver-service'
import type { Options, Capabilities } from '@wdio/types'
import startServer from './server/index.js'
import {
validatePlatform, fileExist, directoryExists, isMultiremote,
isChrome
} from './utils.js'
import {
DEFAULT_CHANNEL, VSCODE_RELEASES, VSCODE_MANIFEST_URL, CHROMEDRIVER_RELEASES,
CHROMEDRIVER_DOWNLOAD_PATH, DEFAULT_CACHE_PATH, VSCODE_CAPABILITY_KEY,
VSCODE_WEB_STANDALONE, DEFAULT_VSCODE_WEB_HOSTNAME
} from './constants.js'
import type {
ServiceOptions, ServiceCapability, VSCodeCapabilities, WebStandaloneResponse,
Bundle
} from './types'
interface BundeInformation {
chromedriver: string
vscode: string
}
interface Manifest {
registrations: Registration[]
}
interface Registration {
version: string
component: {
git: {
name: string
}
}
}
type Versions = { [desiredVersion: string]: BundeInformation | undefined }
const VERSIONS_TXT = 'versions.txt'
const log = logger('wdio-vscode-service/launcher')
export default class VSCodeServiceLauncher extends ChromedriverServiceLauncher {
private _cachePath: string
private _vscodeServerPort?: number
constructor (
private _options: ServiceOptions,
private _capabilities: Capabilities.Capabilities,
config: Options.Testrunner
) {
super(_options, _capabilities, config)
this._cachePath = this._options.cachePath || DEFAULT_CACHE_PATH
this._mapCapabilities = () => {}
}
// @ts-expect-error this service uses provided params
async onPrepare (_: never, capabilities: Capabilities.RemoteCapabilities) {
const caps: VSCodeCapabilities[] = Array.isArray(capabilities)
? capabilities.map((c) => ((c as Capabilities.W3CCapabilities).alwaysMatch || c) as VSCodeCapabilities)
: Object.values(capabilities).map((c) => c.capabilities as VSCodeCapabilities)
/**
* check if for given version we already have all bundles
* and continue without download if possible
*/
const versionsFilePath = path.join(this._cachePath, VERSIONS_TXT)
const versionsFileExist = await fileExist(versionsFilePath)
for (const cap of caps) {
/**
* skip setup if user is not using VSCode as capability
*/
if (typeof cap.browserName !== 'string' || !cap[VSCODE_CAPABILITY_KEY]) {
continue
}
const version = cap[VSCODE_CAPABILITY_KEY].version || cap.browserVersion || DEFAULT_CHANNEL
/**
* setup VSCode Desktop
*/
if (cap.browserName === 'vscode') {
await this._setupVSCodeDesktop(versionsFileExist, versionsFilePath, version, cap)
continue
}
/**
* setup VSCode Web
*/
await this._setupVSCodeWeb(version, cap)
this._mapBrowserCapabilities(this.options as ServiceOptions)
}
return super.onPrepare()
}
/**
* Set up VSCode for web testing
* @param versionsFileExist true if we already have information stored about cached VSCode and Chromedriver bundles
* @param versionsFilePath string with path to cached directory
* @param cap capabilities used for this test run
*/
private async _setupVSCodeWeb (
version: string,
cap: VSCodeCapabilities
) {
/**
* no need to do any work if we already started the server
*/
if (this._vscodeServerPort || !cap[VSCODE_CAPABILITY_KEY]) {
return
}
try {
const vscodeStandalone = await this._fetchVSCodeWebStandalone(version)
const port = await startServer(vscodeStandalone, cap[VSCODE_CAPABILITY_KEY])
cap[VSCODE_CAPABILITY_KEY].serverOptions = {
...(cap[VSCODE_CAPABILITY_KEY].serverOptions || {
hostname: DEFAULT_VSCODE_WEB_HOSTNAME
}),
port
}
} catch (err: any) {
throw new SevereServiceError(`Couldn't start server for VSCode Web: ${err.message}`)
}
}
/**
* Set up VSCode for desktop testing
* @param versionsFileExist true if we already have information stored about cached VSCode and Chromedriver bundles
* @param versionsFilePath string with path to cached directory
* @param cap capabilities used for this test run
*/
private async _setupVSCodeDesktop (
versionsFileExist: boolean,
versionsFilePath: string,
version: string,
cap: VSCodeCapabilities
) {
if (!cap[VSCODE_CAPABILITY_KEY]) {
throw new Error(`No key "${VSCODE_CAPABILITY_KEY}" found in caps`)
}
if (versionsFileExist) {
const content = JSON.parse((await fs.readFile(versionsFilePath)).toString()) as Versions
const chromedriverPath = path.join(this._cachePath, `chromedriver-${content[version]?.chromedriver}`)
const vscodePath = (
cap[VSCODE_CAPABILITY_KEY]?.binary
|| path.join(this._cachePath, `vscode-${process.platform}-${process.arch}-${content[version]?.vscode}`)
)
if (content[version] && await fileExist(chromedriverPath) && await fileExist(vscodePath)) {
log.info(
`Skipping download, bundles for VSCode v${content[version]?.vscode} `
+ `and Chromedriver v${content[version]?.chromedriver} already exist`
)
Object.assign(cap, this.options)
cap[VSCODE_CAPABILITY_KEY].binary = (
cap[VSCODE_CAPABILITY_KEY].binary
|| await this._downloadVSCode(content[version]?.vscode as string)
)
this.chromedriverCustomPath = chromedriverPath
return
}
}
const [vscodeVersion, chromedriverVersion, chromedriverPath] = await this._setupChromedriver(version)
this.chromedriverCustomPath = chromedriverPath
const serviceArgs: ServiceCapability = {
chromedriver: { version: chromedriverVersion, path: chromedriverPath },
vscode: {
version: vscodeVersion,
path: cap[VSCODE_CAPABILITY_KEY]?.binary || await this._downloadVSCode(vscodeVersion)
}
}
Object.assign(cap, this.options)
cap[VSCODE_CAPABILITY_KEY].binary = serviceArgs.vscode.path
await this._updateVersionsTxt(version, serviceArgs, versionsFileExist)
}
/**
* Downloads Chromedriver bundle for given VSCode version
* @param desiredReleaseChannel either release channel (e.g. "stable" or "insiders")
* or a concrete version e.g. 1.66.0
* @returns "insiders" if `desiredReleaseChannel` is set to this otherwise a concrete version
*/
private async _setupChromedriver (desiredReleaseChannel: string) {
const version = await this._fetchVSCodeVersion(desiredReleaseChannel)
try {
const chromedriverVersion = await this._fetchChromedriverVersion(version)
log.info(`Download Chromedriver (v${chromedriverVersion})`)
await downloadBundle(
format(CHROMEDRIVER_DOWNLOAD_PATH, chromedriverVersion, validatePlatform(chromedriverVersion)),
this._cachePath,
{ extract: true, strip: 1 }
)
const ext = os.platform().startsWith('win') ? '.exe' : ''
const chromedriverPath = path.join(this._cachePath, `chromedriver-${chromedriverVersion}${ext}`)
await fs.rename(path.join(this._cachePath, `chromedriver${ext}`), chromedriverPath)
/**
* return 'insiders' if desired release channel
*/
return version === 'main'
? [desiredReleaseChannel, chromedriverVersion, chromedriverPath]
: [version, chromedriverVersion, chromedriverPath]
} catch (err: any) {
throw new SevereServiceError(`Couldn't set up Chromedriver ${err.message}`)
}
}
/**
* Download VSCode bundle
* @param version VSCode version
* @returns path to downloaded VSCode bundle
*/
private async _downloadVSCode (version: string) {
try {
log.info(`Download VSCode binary (${version})`)
return await download({
cachePath: this._cachePath,
version
})
} catch (err: any) {
throw new SevereServiceError(`Couldn't set up VSCode: ${err.message}`)
}
}
/**
* Get VSCode version based on desired channel or validate version if provided
* @param desiredReleaseChannel either release channel (e.g. "stable" or "insiders")
* or a concrete version e.g. 1.66.0
* @returns "main" if `desiredReleaseChannel` is "insiders" otherwise a concrete VSCode version
*/
private async _fetchVSCodeVersion (desiredReleaseChannel?: string) {
if (desiredReleaseChannel === 'insiders') {
return 'main'
}
try {
log.info(`Fetch releases from ${VSCODE_RELEASES}`)
const { body: versions } = await request(VSCODE_RELEASES, {})
const availableVersions: string[] = await versions.json() as string[]
if (desiredReleaseChannel) {
/**
* validate provided VSCode version
*/
const newDesiredReleaseChannel = desiredReleaseChannel === 'stable'
? availableVersions[0]
: desiredReleaseChannel
if (!availableVersions.includes(newDesiredReleaseChannel)) {
throw new Error(
`Desired version "${newDesiredReleaseChannel}" is not existent, available versions:`
+ `${availableVersions.slice(0, 5).join(', ')}..., see ${VSCODE_RELEASES}`
)
}
return newDesiredReleaseChannel
}
return availableVersions[0]
} catch (err: any) {
throw new SevereServiceError(`Couldn't fetch latest VSCode: ${err.message}`)
}
}
/**
* Fetches required Chromedriver version for given VSCode version
* @param vscodeVersion branch or tag version of VSCode repository
* @returns required Chromedriver version
*/
private async _fetchChromedriverVersion (vscodeVersion: string) {
try {
const { body } = await request(format(VSCODE_MANIFEST_URL, vscodeVersion), {})
const manifest = await body.json() as Manifest
const chromium = manifest.registrations.find((r: any) => r.component.git.name === 'chromium')
if (!chromium) {
throw new Error('Can\'t find chromium version in manifest response')
}
const { body: chromedriverVersion } = await request(
format(CHROMEDRIVER_RELEASES, chromium.version.split('.')[0]),
{}
)
return await chromedriverVersion.text()
} catch (err: any) {
throw new SevereServiceError(`Couldn't fetch Chromedriver version: ${err.message}`)
}
}
/**
* Fetches VSCode Web files
* ToDo(Christian): allow to define a local VSCode development path
* to be able to skip this part
*/
private async _fetchVSCodeWebStandalone (vscodeVersion: string): Promise<Bundle> {
if (vscodeVersion !== 'stable' && vscodeVersion !== 'insiders') {
throw new Error('Running VSCode in the browser is only supported for "stable" and "insiders" version')
}
try {
const { body } = await request(format(VSCODE_WEB_STANDALONE, vscodeVersion), {})
const info = await body.json() as WebStandaloneResponse
const folder = path.join(this._cachePath, `vscode-web-${vscodeVersion}-${info.version}`)
if (!(await directoryExists(folder))) {
await downloadBundle(info.url, folder, { extract: true, strip: 1 })
}
return { path: folder, vscodeVersion, version: info.version }
} catch (err: any) {
throw new SevereServiceError(`Couldn't set up VSCode Web: ${err.message}`)
}
}
private async _updateVersionsTxt (version: string, serviceArgs: ServiceCapability, versionsFileExist: boolean) {
const newContent: Versions = {
[version]: {
chromedriver: serviceArgs.chromedriver.version,
vscode: serviceArgs.vscode.version
}
}
const versionsTxtPath = path.join(this._cachePath, VERSIONS_TXT)
if (!versionsFileExist) {
return fs.writeFile(
versionsTxtPath,
JSON.stringify(newContent, null, 4),
'utf-8'
)
}
const content = JSON.parse((await fs.readFile(versionsTxtPath, 'utf-8')).toString())
return fs.writeFile(
versionsTxtPath,
JSON.stringify({ ...content, ...newContent }, null, 4),
'utf-8'
)
}
private _mapBrowserCapabilities (options: ServiceOptions) {
if (isMultiremote(this._capabilities)) {
throw new SevereServiceError('This service deson\'t support multiremote yet')
}
for (const cap of this._capabilities as any as Capabilities.Capabilities[]) {
if (isChrome(cap)) {
Object.assign(cap, options)
}
}
}
}