Skip to content

Commit

Permalink
Provide custom browser arguments (#108)
Browse files Browse the repository at this point in the history
* Provide custom browser arguments via --browserOptions

* avoid parsing the options

---------

Co-authored-by: Michael Eddington <[email protected]>
Co-authored-by: Martin Aeschlimann <[email protected]>
  • Loading branch information
3 people authored Dec 1, 2023
1 parent 8114541 commit f876a3b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,20 @@ vscode-test-web --browserType=chromium --extensionDevelopmentPath=$extensionLoca

Open VS Code in the Browser on a folder with test data from the local disk:

```
```sh
vscode-test-web --browserType=chromium --extensionDevelopmentPath=$extensionLocation $testDataLocation
```

VS Code for the Web will open on a virtual workspace (scheme `vscode-test-web`), backed by a file system provider that gets the file/folder data from the local disk. Changes to the file system are kept in memory and are not written back to disk.

Open VS Code in the Browser with external network access:

```sh
vscode-test-web --browserType=chromium --browserOption=--disable-web-security extensionDevelopmentPath=$extensionLocation
```

This allows the extension being tested to make network requests to external hosts.

Via API:

```ts
Expand Down Expand Up @@ -68,6 +76,7 @@ CLI options:
|Option|Argument Description|
|-----|-----|
| --browser | The browser to launch: `chromium` (default), `firefox`, `webkit` or `none`. |
| --browserOption | Command line argument to use when launching the browser instance. Argument can be provided multiple times. |
| --extensionDevelopmentPath | A path pointing to an extension under development to include. |
| --extensionTestsPath | A path to a test module to run. |
| --quality | `insiders` (default), or `stable`. Ignored when sourcesPath is provided. |
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@
"bugs": {
"url": "https://github.com/microsoft/vscode-test-web/issues"
}
}
}
40 changes: 37 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export interface Options {
*/
browserType: BrowserType;

/**
* Browser command line options.
*/
browserOptions?: string[];

/**
* Absolute path to folder that contains one or more extensions (in subfolders).
* Extension folders include a `package.json` extension manifest.
Expand Down Expand Up @@ -271,6 +276,11 @@ async function openBrowser(endpoint: string, options: Options, configPage?: (pag
}

const args: string[] = [];

if (options.browserOptions) {
args.push(...options.browserOptions);
}

if (process.platform === 'linux' && options.browserType === 'chromium') {
args.push('--no-sandbox');
}
Expand Down Expand Up @@ -385,7 +395,26 @@ function validatePermissions(permissions: unknown): string[] | undefined {
return permissions;
}

console.log(`Invalid permission`);
console.log(`Invalid permission: ${permissions}`);
showHelp();
process.exit(-1);
}

function validateBrowserOptions(browserOptions: unknown): string[] | undefined {
if (browserOptions === undefined) {
return undefined;
}
function isValidOption(p: unknown): p is string {
return typeof p === 'string';
}
if (isValidOption(browserOptions)) {
return [browserOptions];
}
if (Array.isArray(browserOptions) && browserOptions.every(isValidOption)) {
return browserOptions;
}

console.log(`Invalid browser option: ${browserOptions}`);
showHelp();
process.exit(-1);
}
Expand Down Expand Up @@ -497,6 +526,7 @@ function validatePortNumber(port: unknown): number | undefined {

interface CommandLineOptions {
browser?: string;
browserOptions?: string;
browserType?: string;
extensionDevelopmentPath?: string;
extensionTestsPath?: string;
Expand All @@ -521,7 +551,8 @@ interface CommandLineOptions {

function showHelp() {
console.log('Usage:');
console.log(` --browser 'chromium' | 'firefox' | 'webkit' | 'none': The browser to launch. [Optional, defaults to 'chromium']`)
console.log(` --browser 'chromium' | 'firefox' | 'webkit' | 'none': The browser to launch. [Optional, defaults to 'chromium']`);
console.log(` --browserOption option: Command line argument to use when launching the browser instance. [Optional, Multiple]`)
console.log(` --extensionDevelopmentPath path: A path pointing to an extension under development to include. [Optional]`);
console.log(` --extensionTestsPath path: A path to a test module to run. [Optional]`);
console.log(` --quality 'insiders' | 'stable' [Optional, default 'insiders', ignored when running from sources]`);
Expand Down Expand Up @@ -556,7 +587,7 @@ async function cliMain(): Promise<void> {
console.log(`${manifest.name}: ${manifest.version}`);

const options: minimist.Opts = {
string: ['extensionDevelopmentPath', 'extensionTestsPath', 'browser', 'browserType', 'quality', 'version', 'waitForDebugger', 'folder-uri', 'permission', 'extensionPath', 'extensionId', 'sourcesPath', 'host', 'port', 'testRunnerDataDir'],
string: ['extensionDevelopmentPath', 'extensionTestsPath', 'browser', 'browserOption', 'browserType', 'quality', 'version', 'waitForDebugger', 'folder-uri', 'permission', 'extensionPath', 'extensionId', 'sourcesPath', 'host', 'port', 'testRunnerDataDir'],
boolean: ['open-devtools', 'headless', 'hideServerLog', 'printServerLog', 'help', 'verbose', 'coi', 'esm'],
unknown: arg => {
if (arg.startsWith('-')) {
Expand All @@ -573,6 +604,7 @@ async function cliMain(): Promise<void> {
process.exit();
}

const browserOptions = validateBrowserOptions(args.browserOption);
const browserType = validateBrowserType(args);
const extensionTestsPath = await validatePathOrUndefined(args, 'extensionTestsPath', true);
const extensionDevelopmentPath = await validatePathOrUndefined(args, 'extensionDevelopmentPath');
Expand Down Expand Up @@ -612,6 +644,7 @@ async function cliMain(): Promise<void> {
runTests({
extensionTestsPath,
extensionDevelopmentPath,
browserOptions,
browserType,
quality,
devTools,
Expand All @@ -637,6 +670,7 @@ async function cliMain(): Promise<void> {
} else {
open({
extensionDevelopmentPath,
browserOptions,
browserType,
quality,
devTools,
Expand Down

0 comments on commit f876a3b

Please sign in to comment.