Skip to content

Commit

Permalink
Remote resolver (microsoft#21332)
Browse files Browse the repository at this point in the history
This is branch will serve as a feature branch for all changes related to
switching to the remote resolver.
This will include
- switching from using the testAdapter to parse the return data to now
using this new class resultResolver
- adding tests for all testAdapters, fixing for server and adding for
resultResolver
- moving sendCommand to a new file, out of the server, and getting
pytest to adopt it
- moving the server which send the test IDs to a new file and adopt it
for both pytest and unittest
- write tests for these two new files.
  • Loading branch information
eleanorjboyd committed Jun 21, 2023
1 parent bfdb6ac commit 1fa950d
Showing 1 changed file with 164 additions and 117 deletions.
281 changes: 164 additions & 117 deletions src/test/testing/testController/workspaceTestAdapter.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// // Copyright (c) Microsoft Corporation. All rights reserved.
// // Licensed under the MIT License.

import * as assert from 'assert';
import * as sinon from 'sinon';
import * as typemoq from 'typemoq';
// import * as assert from 'assert';
// import * as sinon from 'sinon';
// import * as typemoq from 'typemoq';

import { TestController, TestItem, TestItemCollection, TestRun, Uri } from 'vscode';
import { IConfigurationService, ITestOutputChannel } from '../../../client/common/types';
Expand All @@ -23,15 +23,15 @@ suite('Workspace test adapter', () => {
let stubConfigSettings: IConfigurationService;
let stubResultResolver: ITestResultResolver;

let discoverTestsStub: sinon.SinonStub;
let sendTelemetryStub: sinon.SinonStub;
let outputChannel: typemoq.IMock<ITestOutputChannel>;
// let discoverTestsStub: sinon.SinonStub;
// let sendTelemetryStub: sinon.SinonStub;
// let outputChannel: typemoq.IMock<ITestOutputChannel>;

let telemetryEvent: { eventName: EventName; properties: Record<string, unknown> }[] = [];
// let telemetryEvent: { eventName: EventName; properties: Record<string, unknown> }[] = [];

// Stubbed test controller (see comment around L.40)
let testController: TestController;
let log: string[] = [];
// // Stubbed test controller (see comment around L.40)
// let testController: TestController;
// let log: string[] = [];

setup(() => {
stubConfigSettings = ({
Expand All @@ -40,14 +40,14 @@ suite('Workspace test adapter', () => {
}),
} as unknown) as IConfigurationService;

stubTestServer = ({
sendCommand(): Promise<void> {
return Promise.resolve();
},
onDataReceived: () => {
// no body
},
} as unknown) as ITestServer;
// stubTestServer = ({
// sendCommand(): Promise<void> {
// return Promise.resolve();
// },
// onDataReceived: () => {
// // no body
// },
// } as unknown) as ITestServer;

stubResultResolver = ({
resolveDiscovery: () => {
Expand Down Expand Up @@ -75,42 +75,46 @@ suite('Workspace test adapter', () => {
},
} as unknown) as TestItem;

testController = ({
items: {
get: () => {
log.push('get');
},
add: () => {
log.push('add');
},
replace: () => {
log.push('replace');
},
delete: () => {
log.push('delete');
},
},
createTestItem: () => {
log.push('createTestItem');
return testItem;
},
dispose: () => {
// empty
},
} as unknown) as TestController;

// testController = tests.createTestController('mock-python-tests', 'Mock Python Tests');

const mockSendTelemetryEvent = (
eventName: EventName,
_: number | Record<string, number> | undefined,
properties: unknown,
) => {
telemetryEvent.push({
eventName,
properties: properties as Record<string, unknown>,
});
};
// // const vsIdToRunIdGetStub = sinon.stub(stubResultResolver.vsIdToRunId, 'get');
// // const expectedRunId = 'expectedRunId';
// // vsIdToRunIdGetStub.withArgs(sinon.match.any).returns(expectedRunId);

// // For some reason the 'tests' namespace in vscode returns undefined.
// // While I figure out how to expose to the tests, they will run
// // against a stub test controller and stub test items.
// const testItem = ({
// canResolveChildren: false,
// tags: [],
// children: {
// add: () => {
// // empty
// },
// },
// } as unknown) as TestItem;

// testController = ({
// items: {
// get: () => {
// log.push('get');
// },
// add: () => {
// log.push('add');
// },
// replace: () => {
// log.push('replace');
// },
// delete: () => {
// log.push('delete');
// },
// },
// createTestItem: () => {
// log.push('createTestItem');
// return testItem;
// },
// dispose: () => {
// // empty
// },
// } as unknown) as TestController;

discoverTestsStub = sinon.stub(UnittestTestDiscoveryAdapter.prototype, 'discoverTests');
sendTelemetryStub = sinon.stub(Telemetry, 'sendTelemetryEvent').callsFake(mockSendTelemetryEvent);
Expand Down Expand Up @@ -171,8 +175,10 @@ suite('Workspace test adapter', () => {
sinon.assert.calledWithMatch(buildErrorNodeOptionsStub, Uri.parse('foo'), sinon.match.any, testProvider);
});

test("When discovering tests, the workspace test adapter should call the test discovery adapter's discoverTest method", async () => {
discoverTestsStub.resolves();
// discoverTestsStub = sandbox.stub(UnittestTestDiscoveryAdapter.prototype, 'discoverTests');
// sendTelemetryStub = sandbox.stub(Telemetry, 'sendTelemetryEvent').callsFake(mockSendTelemetryEvent);
// outputChannel = typemoq.Mock.ofType<ITestOutputChannel>();
// });

const testDiscoveryAdapter = new UnittestTestDiscoveryAdapter(
stubTestServer,
Expand All @@ -192,21 +198,28 @@ suite('Workspace test adapter', () => {
stubResultResolver,
);

await workspaceTestAdapter.discoverTests(testController);

sinon.assert.calledOnce(discoverTestsStub);
});

test('If discovery is already running, do not call discoveryAdapter.discoverTests again', async () => {
discoverTestsStub.callsFake(
async () =>
new Promise<void>((resolve) => {
setTimeout(() => {
// Simulate time taken by discovery.
resolve();
}, 2000);
}),
);
// test("When discovering tests, the workspace test adapter should call the test discovery adapter's discoverTest method", async () => {
// discoverTestsStub.resolves();

// const testDiscoveryAdapter = new UnittestTestDiscoveryAdapter(
// stubTestServer,
// stubConfigSettings,
// outputChannel.object,
// );
// const testExecutionAdapter = new UnittestTestExecutionAdapter(
// stubTestServer,
// stubConfigSettings,
// outputChannel.object,
// );
// const workspaceTestAdapter = new WorkspaceTestAdapter(
// 'unittest',
// testDiscoveryAdapter,
// testExecutionAdapter,
// Uri.parse('foo'),
// stubResultResolver,
// );

// await workspaceTestAdapter.discoverTests(testController);

const testDiscoveryAdapter = new UnittestTestDiscoveryAdapter(
stubTestServer,
Expand All @@ -226,28 +239,43 @@ suite('Workspace test adapter', () => {
stubResultResolver,
);

// Try running discovery twice
const one = workspaceTestAdapter.discoverTests(testController);
const two = workspaceTestAdapter.discoverTests(testController);

Promise.all([one, two]);

sinon.assert.calledOnce(discoverTestsStub);
});

test('If discovery succeeds, send a telemetry event with the "failed" key set to false', async () => {
discoverTestsStub.resolves({ status: 'success' });

const testDiscoveryAdapter = new UnittestTestDiscoveryAdapter(
stubTestServer,
stubConfigSettings,
outputChannel.object,
);
const testExecutionAdapter = new UnittestTestExecutionAdapter(
stubTestServer,
stubConfigSettings,
outputChannel.object,
);
// test('If discovery is already running, do not call discoveryAdapter.discoverTests again', async () => {
// discoverTestsStub.callsFake(
// async () =>
// new Promise<void>((resolve) => {
// setTimeout(() => {
// // Simulate time taken by discovery.
// resolve();
// }, 2000);
// }),
// );

// const testDiscoveryAdapter = new UnittestTestDiscoveryAdapter(
// stubTestServer,
// stubConfigSettings,
// outputChannel.object,
// );
// const testExecutionAdapter = new UnittestTestExecutionAdapter(
// stubTestServer,
// stubConfigSettings,
// outputChannel.object,
// );
// const workspaceTestAdapter = new WorkspaceTestAdapter(
// 'unittest',
// testDiscoveryAdapter,
// testExecutionAdapter,
// Uri.parse('foo'),
// stubResultResolver,
// );

// // Try running discovery twice
// const one = workspaceTestAdapter.discoverTests(testController);
// const two = workspaceTestAdapter.discoverTests(testController);

// Promise.all([one, two]);

// sinon.assert.calledOnce(discoverTestsStub);
// });

const workspaceTestAdapter = new WorkspaceTestAdapter(
'unittest',
Expand All @@ -257,28 +285,33 @@ suite('Workspace test adapter', () => {
stubResultResolver,
);

await workspaceTestAdapter.discoverTests(testController);
// const testDiscoveryAdapter = new UnittestTestDiscoveryAdapter(
// stubTestServer,
// stubConfigSettings,
// outputChannel.object,
// );
// const testExecutionAdapter = new UnittestTestExecutionAdapter(
// stubTestServer,
// stubConfigSettings,
// outputChannel.object,
// );

sinon.assert.calledWith(sendTelemetryStub, EventName.UNITTEST_DISCOVERY_DONE);
assert.strictEqual(telemetryEvent.length, 2);
// const workspaceTestAdapter = new WorkspaceTestAdapter(
// 'unittest',
// testDiscoveryAdapter,
// testExecutionAdapter,
// Uri.parse('foo'),
// stubResultResolver,
// );

const lastEvent = telemetryEvent[1];
assert.strictEqual(lastEvent.properties.failed, false);
});
// await workspaceTestAdapter.discoverTests(testController);

test('If discovery failed, send a telemetry event with the "failed" key set to true, and add an error node to the test controller', async () => {
discoverTestsStub.rejects(new Error('foo'));
// sinon.assert.calledWith(sendTelemetryStub, EventName.UNITTEST_DISCOVERY_DONE);
// assert.strictEqual(telemetryEvent.length, 2);

const testDiscoveryAdapter = new UnittestTestDiscoveryAdapter(
stubTestServer,
stubConfigSettings,
outputChannel.object,
);
const testExecutionAdapter = new UnittestTestExecutionAdapter(
stubTestServer,
stubConfigSettings,
outputChannel.object,
);
// const lastEvent = telemetryEvent[1];
// assert.strictEqual(lastEvent.properties.failed, false);
// });

const workspaceTestAdapter = new WorkspaceTestAdapter(
'unittest',
Expand All @@ -288,10 +321,24 @@ suite('Workspace test adapter', () => {
stubResultResolver,
);

await workspaceTestAdapter.discoverTests(testController);

sinon.assert.calledWith(sendTelemetryStub, EventName.UNITTEST_DISCOVERY_DONE);
assert.strictEqual(telemetryEvent.length, 2);
// const testDiscoveryAdapter = new UnittestTestDiscoveryAdapter(
// stubTestServer,
// stubConfigSettings,
// outputChannel.object,
// );
// const testExecutionAdapter = new UnittestTestExecutionAdapter(
// stubTestServer,
// stubConfigSettings,
// outputChannel.object,
// );

// const workspaceTestAdapter = new WorkspaceTestAdapter(
// 'unittest',
// testDiscoveryAdapter,
// testExecutionAdapter,
// Uri.parse('foo'),
// stubResultResolver,
// );

const lastEvent = telemetryEvent[1];
assert.ok(lastEvent.properties.failed);
Expand Down

0 comments on commit 1fa950d

Please sign in to comment.