This commit is contained in:
Rob Lourens 2015-11-05 14:02:52 -08:00
Родитель 2075f779c5
Коммит 0d9af4556e
5 изменённых файлов: 130 добавлений и 19 удалений

Просмотреть файл

@ -50,7 +50,6 @@ export class PathTransformer implements IDebugTransformer {
}
public scriptParsed(event: DebugProtocol.Event): void {
// maybe cache canonicalized form? what about scripts with query args
const webkitUrl: string = event.body.scriptUrl;
const clientUrl = utils.webkitUrlToClientUrl(this._clientCWD, webkitUrl);
this._clientUrlToWebkitUrl.set(clientUrl, webkitUrl);

Просмотреть файл

@ -0,0 +1,94 @@
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
import * as assert from 'assert';
import * as mockery from 'mockery';
import * as testUtils from '../testUtils';
import {PathTransformer as _PathTransformer} from '../../adapter/pathTransformer';
const MODULE_UNDER_TEST = '../../adapter/pathTransformer';
function createTransformer(): _PathTransformer {
return new (require(MODULE_UNDER_TEST).PathTransformer)();
}
suite('PathTransformer', () => {
const TARGET_URL = 'http://mysite.com/scripts/script1.js';
const CLIENT_URL = 'c:/projects/mysite/scripts/script1.js';
let utilsMock: Sinon.SinonMock;
let transformer: _PathTransformer;
setup(() => {
testUtils.setupUnhandledRejectionListener();
mockery.enable({ useCleanCache: true, warnOnReplace: false });
mockery.registerAllowables([MODULE_UNDER_TEST]);
// Mock the utils functions
const mockedObj = testUtils.getDefaultUtilitiesMock();
utilsMock = testUtils.getSinonMock(mockedObj);
utilsMock.expects('webkitUrlToClientUrl')
.once()
.withExactArgs(/*cwd=*/undefined, TARGET_URL).returns(CLIENT_URL);
mockery.registerMock('../webkit/utilities', mockedObj);
transformer = createTransformer();
});
teardown(() => {
testUtils.removeUnhandledRejectionListener();
mockery.deregisterAll();
mockery.disable();
});
suite('setBreakpoints()', () => {
let SET_BP_ARGS;
const EXPECTED_SET_BP_ARGS = { source: { path: TARGET_URL } };
setup(() => {
// This will be modified by the test, so restore it before each
SET_BP_ARGS = { source: { path: CLIENT_URL } };
});
test('resolves correctly when it can map the client script to the target script', () => {
utilsMock.expects('canonicalizeUrl')
.once()
.withExactArgs(CLIENT_URL).returns(CLIENT_URL);
transformer.scriptParsed(<any>{ body: { scriptUrl: TARGET_URL } });
return transformer.setBreakpoints(<any>SET_BP_ARGS).then(() => {
utilsMock.verify();
assert.deepEqual(SET_BP_ARGS, EXPECTED_SET_BP_ARGS);
});
});
test(`doesn't resolve until it can map the client script to the target script`, () => {
utilsMock.expects('canonicalizeUrl')
.twice()
.withExactArgs(CLIENT_URL).returns(CLIENT_URL);
const setBreakpointsP = transformer.setBreakpoints(<any>SET_BP_ARGS).then(() => {
// If this assert doesn't fail, we know that it resolved at the right time because otherwise it would have no
// way to produce args with the right url
utilsMock.verify();
assert.deepEqual(SET_BP_ARGS, EXPECTED_SET_BP_ARGS);
});
transformer.scriptParsed(<any>{ body: { scriptUrl: TARGET_URL } });
return setBreakpointsP;
});
});
suite('scriptParsed', () => {
test('Modifies args.source.path of the script parsed event', () => {
const scriptParsedArgs = <any>{ body: { scriptUrl: TARGET_URL } };
const expectedScriptParsedArgs = <any>{ body: { scriptUrl: CLIENT_URL } };
transformer.scriptParsed(scriptParsedArgs);
assert.deepEqual(scriptParsedArgs, expectedScriptParsedArgs);
});
});
});

Просмотреть файл

@ -2,6 +2,8 @@
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
import * as sinon from 'sinon';
export function setupUnhandledRejectionListener(): void {
process.addListener('unhandledRejection', unhandledRejectionListener);
}
@ -16,7 +18,8 @@ function unhandledRejectionListener(reason, p) {
console.log('***');
console.log('****');
console.log('*****');
console.log(`ERROR!! Unhandled promise rejection: ${reason}. A previous test may have failed but reported success.`);
console.log(`ERROR!! Unhandled promise rejection, a previous test may have failed but reported success.`);
console.log(reason.toString());
console.log('*****');
console.log('****');
console.log('***');
@ -30,3 +33,32 @@ export class MockEvent implements DebugProtocol.Event {
constructor(public event: string, public body?: any) { }
}
/**
* Calls sinon.mock and patches its 'expects' method to not expect that the mock base object
* already has an implementation of the expected method.
*/
export function getSinonMock(mockBase: any): Sinon.SinonMock {
const m = sinon.mock(mockBase);
// Add a default implementation of every expected method so sinon doesn't complain if it doesn't exist.
const originalMExpects = m.expects.bind(m);
m.expects = methodName => {
if (!mockBase[methodName]) {
mockBase[methodName] = () => Promise.resolve();
}
return originalMExpects(methodName);
};
return m;
}
/**
* Return a base Utilities mock that has Logger.log stubbed out
*/
export function getDefaultUtilitiesMock(): any {
return {
Logger: { log: () => { } }
};
}

Просмотреть файл

@ -160,7 +160,7 @@ suite('Utilities', () => {
.then(
() => assert.fail('This promise should fail'),
e => {
assert.equal(e.message, 'Error: fail');
assert.equal(e.message, 'fail');
});
});
});

Просмотреть файл

@ -2,7 +2,6 @@
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
import * as sinon from 'sinon';
import * as mockery from 'mockery';
import {EventEmitter} from 'events';
import * as assert from 'assert';
@ -312,25 +311,12 @@ class DefaultMockWebKitConnection {
/**
* Creates an instance of the default mock WKC, registers it with mockery.
* Then creates a sinon mock against it, and customizes sinon's 'expects'
* Then creates a sinon mock against it.
*/
function registerMockWebKitConnection(): Sinon.SinonMock {
const mockInstance = new DefaultMockWebKitConnection();
mockery.registerMock('./webKitConnection', { WebKitConnection: () => mockInstance });
const m = sinon.mock(mockInstance);
// Prevent sinon from complaining that the mocked object doesn't have an implementation of
// the expected method.
const originalMExpects = m.expects.bind(m);
m.expects = methodName => {
if (!mockInstance[methodName]) {
mockInstance[methodName] = () => Promise.resolve();
}
return originalMExpects(methodName);
};
return m;
return testUtils.getSinonMock(mockInstance);
}
function instantiateWKDA(): _WebKitDebugAdapter {