Skip to content
This repository has been archived by the owner on Dec 6, 2022. It is now read-only.

Commit

Permalink
Add use case for file urls in tests
Browse files Browse the repository at this point in the history
Replaced URI with node's URL class, as it correctly handles file:/// urls out of the box
  • Loading branch information
EricCornelson committed Jun 5, 2019
1 parent 54ffdeb commit 5fa09cc
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 23 deletions.
14 changes: 9 additions & 5 deletions test/int/fixtures/launchProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/

import URI from 'vscode-uri';
import { TestProjectSpec } from '../framework/frameworkTestSupport';
import { IFixture } from './fixture';
import { DefaultFixture } from './defaultFixture';
import { LaunchWebServer } from './launchWebServer';
import { LaunchWebServer, ProvideStaticUrl } from './launchWebServer';
import { LaunchPuppeteer } from '../puppeteer/launchPuppeteer';
import { ExtendedDebugClient } from 'vscode-chrome-debug-core-testsupport';
import { Page, Browser } from 'puppeteer';
import { ITestCallbackContext, IBeforeAndAfterContext } from 'mocha';
import { URL } from 'url';

/** Perform all the steps neccesary to launch a particular project such as:
* - Default fixture/setup
Expand All @@ -20,11 +20,15 @@ import { ITestCallbackContext, IBeforeAndAfterContext } from 'mocha';
export class LaunchProject implements IFixture {
private constructor(
private readonly _defaultFixture: DefaultFixture,
private readonly _launchWebServer: LaunchWebServer,
private readonly _launchWebServer: LaunchWebServer | ProvideStaticUrl,
private readonly _launchPuppeteer: LaunchPuppeteer) { }

public static async create(testContext: IBeforeAndAfterContext | ITestCallbackContext, testSpec: TestProjectSpec): Promise<LaunchProject> {
const launchWebServer = await LaunchWebServer.launch(testSpec);

const launchWebServer = (testSpec.staticUrl) ?
new ProvideStaticUrl(new URL(testSpec.staticUrl), testSpec) :
await LaunchWebServer.launch(testSpec);

const defaultFixture = await DefaultFixture.create(testContext);

const launchPuppeteer = await LaunchPuppeteer.create(defaultFixture.debugClient, launchWebServer.launchConfig);
Expand All @@ -46,7 +50,7 @@ export class LaunchProject implements IFixture {
return this._launchPuppeteer.page;
}

public get url(): URI {
public get url(): URL {
return this._launchWebServer.url;
}

Expand Down
16 changes: 13 additions & 3 deletions test/int/fixtures/launchWebServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/

import URI from 'vscode-uri';
import { createServer } from 'http-server';
import { IFixture } from './fixture';
import { TestProjectSpec } from '../framework/frameworkTestSupport';
import { HttpOrHttpsServer } from '../types/server';
import { ILaunchRequestArgs } from 'vscode-chrome-debug-core';
import { logger } from 'vscode-debugadapter';
import { URL } from 'url';

async function createServerAsync(root: string): Promise<HttpOrHttpsServer> {
const server = createServer({ root });
Expand Down Expand Up @@ -49,9 +49,9 @@ export class LaunchWebServer implements IFixture {
return new LaunchWebServer(await createServerAsync(testSpec.props.webRoot), testSpec);
}

public get url(): URI {
public get url(): URL {
const address = this._server.address();
return URI.parse(`http://localhost:${address.port}/`);
return new URL(`http://localhost:${address.port}/`);
}

public get launchConfig(): ILaunchRequestArgs {
Expand All @@ -65,4 +65,14 @@ export class LaunchWebServer implements IFixture {
public toString(): string {
return `LaunchWebServer`;
}
}

export class ProvideStaticUrl implements IFixture {

public constructor(public readonly url: URL, private readonly testSpec: TestProjectSpec) {}

public get launchConfig(): ILaunchRequestArgs {
return {...this.testSpec.props.launchConfig, url: this.url.href };
}
cleanUp() {}
}
4 changes: 2 additions & 2 deletions test/int/fixtures/testUsing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import { PromiseOrNot } from 'vscode-chrome-debug-core';
import { ITestCallbackContext } from 'mocha';

/** Run a test doing the setup/cleanup indicated by the provided fixtures */
async function testUsingFunction<T extends IFixture>(
function testUsingFunction<T extends IFixture>(
expectation: string,
fixtureProvider: (context: ITestCallbackContext) => PromiseOrNot<T>,
testFunction: (fixtures: T) => Promise<void>) {
testFunction: (fixtures: T) => Promise<void>): Mocha.ITest {
return test(expectation, async function () {
const fixture = await fixtureProvider(this);
try {
Expand Down
12 changes: 4 additions & 8 deletions test/int/framework/frameworkTestSupport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import * as puppeteer from 'puppeteer';
import { ExtendedDebugClient } from 'vscode-chrome-debug-core-testsupport';
import { BreakpointLocation } from '../intTestSupport';
import { ILaunchRequestArgs } from '../../../src/chromeDebugInterfaces';
import { MakePropertyRequired } from '../core-v2/typeUtils';
import { IValidatedMap } from '../core-v2/chrome/collections/validatedMap';
import { DATA_ROOT } from '../testSetup';

Expand All @@ -34,13 +33,13 @@ export interface ProjectSpecProps {
* attached to in order to test the debug adapter)
*/
export class TestProjectSpec {
_props: MakePropertyRequired<ProjectSpecProps, keyof ProjectSpecProps>;
_props: Required<ProjectSpecProps>;
get props() { return this._props; }

/**
* @param props Parameters for the project spec. The only required param is "projectRoot", others will be set to sensible defaults
*/
constructor(props: ProjectSpecProps) {
constructor(props: ProjectSpecProps, public readonly staticUrl?: string) {
const outFiles = props.outFiles || [path.join(props.projectRoot, 'out')];
const webRoot = props.webRoot || props.projectRoot;
this._props = {
Expand All @@ -64,14 +63,11 @@ export class TestProjectSpec {
*
* The path *can only* use forward-slahes "/" as separators
*/
public static fromTestPath(reversedSlashesRelativePath: string, url?: string): TestProjectSpec {
public static fromTestPath(reversedSlashesRelativePath: string, staticUrl?: string): TestProjectSpec {
const pathComponents = reversedSlashesRelativePath.split('/');
const projectAbsolutePath = path.join(...[DATA_ROOT].concat(pathComponents));
let props: ProjectSpecProps = { projectRoot: projectAbsolutePath };
if (url) {
props.url = url;
}
return new TestProjectSpec(props);
return new TestProjectSpec(props, staticUrl);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions test/int/stackTrace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ puppeteerSuite('Stack Traces', TEST_SPEC, (suiteContext) => {
{ name: 'timeoutCallback [app.js]', line: 6, column: 5, source: { fileRelativePath: 'app.js' }, presentationHint: 'normal'},
{ name: '[ setTimeout ]', presentationHint: 'label'},
{ name: 'buttonClick [app.js]', line: 2, column: 5, source: { fileRelativePath: 'app.js' }, presentationHint: 'normal'},
{ name: `onclick [${url.authority}]`, line: 7, column: 49, source: { url }, presentationHint: 'normal'},
{ name: `onclick [${url.host}]`, line: 7, column: 49, source: { url }, presentationHint: 'normal'},
]
});
});
Expand Down Expand Up @@ -117,7 +117,7 @@ puppeteerSuite('Stack Traces', TEST_SPEC, (suiteContext) => {
{ name: 'timeoutCallback [app.js] Line 6', line: 6, column: 5, source: { fileRelativePath: 'app.js' }, presentationHint: 'normal'},
{ name: '[ setTimeout ]', presentationHint: 'label'},
{ name: 'buttonClick [app.js] Line 2', line: 2, column: 5, source: { fileRelativePath: 'app.js' }, presentationHint: 'normal'},
{ name: `onclick [${url.authority}] Line 7`, line: 7, column: 49, source: { url }, presentationHint: 'normal'},
{ name: `onclick [${url.host}] Line 7`, line: 7, column: 49, source: { url }, presentationHint: 'normal'},
]
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import URI from 'vscode-uri';
import * as assert from 'assert';
import * as path from 'path';
import * as testSetup from '../../../testSetup';
import { expect } from 'chai';
import { DebugProtocol } from 'vscode-debugprotocol';
import { BreakpointsWizard } from '../breakpointsWizard';
import { URL } from 'url';

export interface ExpectedSource {
fileRelativePath?: string;
url?: URI;
url?: URL;
evalCode?: boolean;
}

Expand Down Expand Up @@ -50,7 +50,7 @@ export class StackTraceObjectAssertions {
expectedPath = path.join(this._projectRoot, expected.fileRelativePath);
expectedName = path.parse(expectedPath).base;
} else if (expected.url) {
expectedName = expected.url.authority;
expectedName = expected.url.host;
expectedPath = expected.url.toString();
} else if (expected.evalCode === true) {
// Eval code has source that looks like 'VM123'. Check it by regex instead.
Expand Down

0 comments on commit 5fa09cc

Please sign in to comment.