From 0f232386375b8012cd2dd838183e0e82e4803076 Mon Sep 17 00:00:00 2001 From: Eleanor Boyd Date: Wed, 21 Jun 2023 10:28:34 -0700 Subject: [PATCH] Result resolver feature branch (#21457) fixes https://github.com/microsoft/vscode-python/issues/21394 --- pythonFiles/unittestadapter/execution.py | 2 - src/client/testing/common/socketServer.ts | 2 +- .../testController/common/resultResolver.ts | 233 ++++++++ .../testing/testController/common/server.ts | 43 +- .../testing/testController/common/types.ts | 12 +- .../testing/testController/common/utils.ts | 141 ++++- .../testing/testController/controller.ts | 16 +- .../pytest/pytestDiscoveryAdapter.ts | 73 +-- .../pytest/pytestExecutionAdapter.ts | 99 ++-- .../unittest/testDiscoveryAdapter.ts | 36 +- .../unittest/testExecutionAdapter.ts | 94 ++-- .../testController/workspaceTestAdapter.ts | 306 +---------- src/test/mocks/vsc/index.ts | 112 ++++ .../pytestDiscoveryAdapter.unit.test.ts | 123 +++-- .../pytestExecutionAdapter.unit.test.ts | 257 ++++++--- .../resultResolver.unit.test.ts | 368 +++++++++++++ .../testController/server.unit.test.ts | 507 ++++++++++++------ .../testDiscoveryAdapter.unit.test.ts | 62 +-- .../testExecutionAdapter.unit.test.ts | 183 +++---- .../workspaceTestAdapter.unit.test.ts | 404 +++++++++++++- src/test/vscode-mock.ts | 2 + 21 files changed, 2091 insertions(+), 984 deletions(-) create mode 100644 src/client/testing/testController/common/resultResolver.ts create mode 100644 src/test/testing/testController/resultResolver.unit.test.ts diff --git a/pythonFiles/unittestadapter/execution.py b/pythonFiles/unittestadapter/execution.py index 4695064396cc..17c125e5843a 100644 --- a/pythonFiles/unittestadapter/execution.py +++ b/pythonFiles/unittestadapter/execution.py @@ -242,7 +242,6 @@ def run_tests( try: client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client_socket.connect(("localhost", run_test_ids_port_int)) - print(f"CLIENT: Server listening on port {run_test_ids_port_int}...") buffer = b"" while True: @@ -263,7 +262,6 @@ def run_tests( buffer = b"" # Process the JSON data - print(f"Received JSON data: {test_ids_from_buffer}") break except json.JSONDecodeError: # JSON decoding error, the complete JSON object is not yet received diff --git a/src/client/testing/common/socketServer.ts b/src/client/testing/common/socketServer.ts index 554d8c8a0c76..c27bf5a1606c 100644 --- a/src/client/testing/common/socketServer.ts +++ b/src/client/testing/common/socketServer.ts @@ -123,7 +123,7 @@ export class UnitTestSocketServer extends EventEmitter implements IUnitTestSocke if ((socket as any).id) { destroyedSocketId = (socket as any).id; } - this.log('socket disconnected', destroyedSocketId.toString()); + this.log('socket disconnected', destroyedSocketId?.toString()); if (socket && socket.destroy) { socket.destroy(); } diff --git a/src/client/testing/testController/common/resultResolver.ts b/src/client/testing/testController/common/resultResolver.ts new file mode 100644 index 000000000000..49243390ad0f --- /dev/null +++ b/src/client/testing/testController/common/resultResolver.ts @@ -0,0 +1,233 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +import { CancellationToken, TestController, TestItem, Uri, TestMessage, Location, TestRun } from 'vscode'; +import * as util from 'util'; +import { DiscoveredTestPayload, ExecutionTestPayload, ITestResultResolver } from './types'; +import { TestProvider } from '../../types'; +import { traceError, traceLog } from '../../../logging'; +import { Testing } from '../../../common/utils/localize'; +import { clearAllChildren, createErrorTestItem, getTestCaseNodes } from './testItemUtilities'; +import { sendTelemetryEvent } from '../../../telemetry'; +import { EventName } from '../../../telemetry/constants'; +import { splitLines } from '../../../common/stringUtils'; +import { buildErrorNodeOptions, fixLogLines, populateTestTree } from './utils'; + +export class PythonResultResolver implements ITestResultResolver { + testController: TestController; + + testProvider: TestProvider; + + public runIdToTestItem: Map; + + public runIdToVSid: Map; + + public vsIdToRunId: Map; + + constructor(testController: TestController, testProvider: TestProvider, private workspaceUri: Uri) { + this.testController = testController; + this.testProvider = testProvider; + + this.runIdToTestItem = new Map(); + this.runIdToVSid = new Map(); + this.vsIdToRunId = new Map(); + } + + public resolveDiscovery(payload: DiscoveredTestPayload, token?: CancellationToken): Promise { + const workspacePath = this.workspaceUri.fsPath; + traceLog('Using result resolver for discovery'); + + const rawTestData = payload; + if (!rawTestData) { + // No test data is available + return Promise.resolve(); + } + + // Check if there were any errors in the discovery process. + if (rawTestData.status === 'error') { + const testingErrorConst = + this.testProvider === 'pytest' ? Testing.errorPytestDiscovery : Testing.errorUnittestDiscovery; + const { errors } = rawTestData; + traceError(testingErrorConst, '\r\n', errors!.join('\r\n\r\n')); + + let errorNode = this.testController.items.get(`DiscoveryError:${workspacePath}`); + const message = util.format( + `${testingErrorConst} ${Testing.seePythonOutput}\r\n`, + errors!.join('\r\n\r\n'), + ); + + if (errorNode === undefined) { + const options = buildErrorNodeOptions(this.workspaceUri, message, this.testProvider); + errorNode = createErrorTestItem(this.testController, options); + this.testController.items.add(errorNode); + } + errorNode.error = message; + } else { + // Remove the error node if necessary, + // then parse and insert test data. + this.testController.items.delete(`DiscoveryError:${workspacePath}`); + + if (rawTestData.tests) { + // If the test root for this folder exists: Workspace refresh, update its children. + // Otherwise, it is a freshly discovered workspace, and we need to create a new test root and populate the test tree. + populateTestTree(this.testController, rawTestData.tests, undefined, this, token); + } else { + // Delete everything from the test controller. + this.testController.items.replace([]); + } + } + + sendTelemetryEvent(EventName.UNITTEST_DISCOVERY_DONE, undefined, { + tool: this.testProvider, + failed: false, + }); + return Promise.resolve(); + } + + public resolveExecution(payload: ExecutionTestPayload, runInstance: TestRun): Promise { + const rawTestExecData = payload; + if (rawTestExecData !== undefined && rawTestExecData.result !== undefined) { + // Map which holds the subtest information for each test item. + const subTestStats: Map = new Map(); + + // iterate through payload and update the UI accordingly. + for (const keyTemp of Object.keys(rawTestExecData.result)) { + const testCases: TestItem[] = []; + + // grab leaf level test items + this.testController.items.forEach((i) => { + const tempArr: TestItem[] = getTestCaseNodes(i); + testCases.push(...tempArr); + }); + + if ( + rawTestExecData.result[keyTemp].outcome === 'failure' || + rawTestExecData.result[keyTemp].outcome === 'passed-unexpected' + ) { + const rawTraceback = rawTestExecData.result[keyTemp].traceback ?? ''; + const traceback = splitLines(rawTraceback, { + trim: false, + removeEmptyEntries: true, + }).join('\r\n'); + + const text = `${rawTestExecData.result[keyTemp].test} failed: ${ + rawTestExecData.result[keyTemp].message ?? rawTestExecData.result[keyTemp].outcome + }\r\n${traceback}\r\n`; + const message = new TestMessage(text); + + // note that keyTemp is a runId for unittest library... + const grabVSid = this.runIdToVSid.get(keyTemp); + // search through freshly built array of testItem to find the failed test and update UI. + testCases.forEach((indiItem) => { + if (indiItem.id === grabVSid) { + if (indiItem.uri && indiItem.range) { + message.location = new Location(indiItem.uri, indiItem.range); + runInstance.failed(indiItem, message); + runInstance.appendOutput(fixLogLines(text)); + } + } + }); + } else if ( + rawTestExecData.result[keyTemp].outcome === 'success' || + rawTestExecData.result[keyTemp].outcome === 'expected-failure' + ) { + const grabTestItem = this.runIdToTestItem.get(keyTemp); + const grabVSid = this.runIdToVSid.get(keyTemp); + if (grabTestItem !== undefined) { + testCases.forEach((indiItem) => { + if (indiItem.id === grabVSid) { + if (indiItem.uri && indiItem.range) { + runInstance.passed(grabTestItem); + runInstance.appendOutput('Passed here'); + } + } + }); + } + } else if (rawTestExecData.result[keyTemp].outcome === 'skipped') { + const grabTestItem = this.runIdToTestItem.get(keyTemp); + const grabVSid = this.runIdToVSid.get(keyTemp); + if (grabTestItem !== undefined) { + testCases.forEach((indiItem) => { + if (indiItem.id === grabVSid) { + if (indiItem.uri && indiItem.range) { + runInstance.skipped(grabTestItem); + runInstance.appendOutput('Skipped here'); + } + } + }); + } + } else if (rawTestExecData.result[keyTemp].outcome === 'subtest-failure') { + // split on " " since the subtest ID has the parent test ID in the first part of the ID. + const parentTestCaseId = keyTemp.split(' ')[0]; + const parentTestItem = this.runIdToTestItem.get(parentTestCaseId); + const data = rawTestExecData.result[keyTemp]; + // find the subtest's parent test item + if (parentTestItem) { + const subtestStats = subTestStats.get(parentTestCaseId); + if (subtestStats) { + subtestStats.failed += 1; + } else { + subTestStats.set(parentTestCaseId, { failed: 1, passed: 0 }); + runInstance.appendOutput(fixLogLines(`${parentTestCaseId} [subtests]:\r\n`)); + // clear since subtest items don't persist between runs + clearAllChildren(parentTestItem); + } + const subtestId = keyTemp; + const subTestItem = this.testController?.createTestItem(subtestId, subtestId); + runInstance.appendOutput(fixLogLines(`${subtestId} Failed\r\n`)); + // create a new test item for the subtest + if (subTestItem) { + const traceback = data.traceback ?? ''; + const text = `${data.subtest} Failed: ${data.message ?? data.outcome}\r\n${traceback}\r\n`; + runInstance.appendOutput(fixLogLines(text)); + parentTestItem.children.add(subTestItem); + runInstance.started(subTestItem); + const message = new TestMessage(rawTestExecData?.result[keyTemp].message ?? ''); + if (parentTestItem.uri && parentTestItem.range) { + message.location = new Location(parentTestItem.uri, parentTestItem.range); + } + runInstance.failed(subTestItem, message); + } else { + throw new Error('Unable to create new child node for subtest'); + } + } else { + throw new Error('Parent test item not found'); + } + } else if (rawTestExecData.result[keyTemp].outcome === 'subtest-success') { + // split on " " since the subtest ID has the parent test ID in the first part of the ID. + const parentTestCaseId = keyTemp.split(' ')[0]; + const parentTestItem = this.runIdToTestItem.get(parentTestCaseId); + + // find the subtest's parent test item + if (parentTestItem) { + const subtestStats = subTestStats.get(parentTestCaseId); + if (subtestStats) { + subtestStats.passed += 1; + } else { + subTestStats.set(parentTestCaseId, { failed: 0, passed: 1 }); + runInstance.appendOutput(fixLogLines(`${parentTestCaseId} [subtests]:\r\n`)); + // clear since subtest items don't persist between runs + clearAllChildren(parentTestItem); + } + const subtestId = keyTemp; + const subTestItem = this.testController?.createTestItem(subtestId, subtestId); + // create a new test item for the subtest + if (subTestItem) { + parentTestItem.children.add(subTestItem); + runInstance.started(subTestItem); + runInstance.passed(subTestItem); + runInstance.appendOutput(fixLogLines(`${subtestId} Passed\r\n`)); + } else { + throw new Error('Unable to create new child node for subtest'); + } + } else { + throw new Error('Parent test item not found'); + } + } + } + } + return Promise.resolve(); + } +} + +// had to switch the order of the original parameter since required param cannot follow optional. diff --git a/src/client/testing/testController/common/server.ts b/src/client/testing/testController/common/server.ts index 6bd9bf348e20..32829e355ccb 100644 --- a/src/client/testing/testController/common/server.ts +++ b/src/client/testing/testController/common/server.ts @@ -9,7 +9,7 @@ import { IPythonExecutionFactory, SpawnOptions, } from '../../../common/process/types'; -import { traceError, traceInfo, traceLog, traceVerbose } from '../../../logging'; +import { traceError, traceInfo, traceLog } from '../../../logging'; import { DataReceivedEvent, ITestServer, TestCommandOptions } from './types'; import { ITestDebugLauncher, LaunchOptions } from '../../common/types'; import { UNITTEST_PROVIDER } from '../../common/constants'; @@ -24,6 +24,10 @@ export class PythonTestServer implements ITestServer, Disposable { private ready: Promise; + private _onRunDataReceived: EventEmitter = new EventEmitter(); + + private _onDiscoveryDataReceived: EventEmitter = new EventEmitter(); + constructor(private executionFactory: IPythonExecutionFactory, private debugLauncher: ITestDebugLauncher) { this.server = net.createServer((socket: net.Socket) => { let buffer: Buffer = Buffer.alloc(0); // Buffer to accumulate received data @@ -48,11 +52,28 @@ export class PythonTestServer implements ITestServer, Disposable { rawData = rpcHeaders.remainingRawData; const rpcContent = jsonRPCContent(rpcHeaders.headers, rawData); const extractedData = rpcContent.extractedJSON; + // do not send until we have the full content if (extractedData.length === Number(totalContentLength)) { - // do not send until we have the full content - traceVerbose(`Received data from test server: ${extractedData}`); - this._onDataReceived.fire({ uuid, data: extractedData }); - this.uuids = this.uuids.filter((u) => u !== uuid); + // if the rawData includes tests then this is a discovery request + if (rawData.includes(`"tests":`)) { + this._onDiscoveryDataReceived.fire({ + uuid, + data: rpcContent.extractedJSON, + }); + // if the rawData includes result then this is a run request + } else if (rawData.includes(`"result":`)) { + this._onRunDataReceived.fire({ + uuid, + data: rpcContent.extractedJSON, + }); + } else { + traceLog( + `Error processing test server request: request is not recognized as discovery or run.`, + ); + this._onDataReceived.fire({ uuid: '', data: '' }); + return; + } + // this.uuids = this.uuids.filter((u) => u !== uuid); WHERE DOES THIS GO?? buffer = Buffer.alloc(0); } else { break; @@ -97,6 +118,18 @@ export class PythonTestServer implements ITestServer, Disposable { return uuid; } + public deleteUUID(uuid: string): void { + this.uuids = this.uuids.filter((u) => u !== uuid); + } + + public get onRunDataReceived(): Event { + return this._onRunDataReceived.event; + } + + public get onDiscoveryDataReceived(): Event { + return this._onDiscoveryDataReceived.event; + } + public dispose(): void { this.server.close(); this._onDataReceived.dispose(); diff --git a/src/client/testing/testController/common/types.ts b/src/client/testing/testController/common/types.ts index 4307d7a3913f..cb7fda797c4a 100644 --- a/src/client/testing/testController/common/types.ts +++ b/src/client/testing/testController/common/types.ts @@ -172,12 +172,21 @@ export type TestCommandOptionsPytest = { */ export interface ITestServer { readonly onDataReceived: Event; + readonly onRunDataReceived: Event; + readonly onDiscoveryDataReceived: Event; sendCommand(options: TestCommandOptions, runTestIdsPort?: string, callback?: () => void): Promise; serverReady(): Promise; getPort(): number; createUUID(cwd: string): string; + deleteUUID(uuid: string): void; +} +export interface ITestResultResolver { + runIdToVSid: Map; + runIdToTestItem: Map; + vsIdToRunId: Map; + resolveDiscovery(payload: DiscoveredTestPayload, token?: CancellationToken): Promise; + resolveExecution(payload: ExecutionTestPayload, runInstance: TestRun): Promise; } - export interface ITestDiscoveryAdapter { // ** first line old method signature, second line new method signature discoverTests(uri: Uri): Promise; @@ -192,6 +201,7 @@ export interface ITestExecutionAdapter { uri: Uri, testIds: string[], debugBool?: boolean, + runInstance?: TestRun, executionFactory?: IPythonExecutionFactory, debugLauncher?: ITestDebugLauncher, ): Promise; diff --git a/src/client/testing/testController/common/utils.ts b/src/client/testing/testController/common/utils.ts index 1bf31e80e11a..f98550d3e72b 100644 --- a/src/client/testing/testController/common/utils.ts +++ b/src/client/testing/testController/common/utils.ts @@ -1,11 +1,15 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. import * as net from 'net'; -import { traceLog } from '../../../logging'; +import * as path from 'path'; +import { CancellationToken, Position, TestController, TestItem, Uri, Range } from 'vscode'; +import { traceError, traceLog, traceVerbose } from '../../../logging'; import { EnableTestAdapterRewrite } from '../../../common/experiments/groups'; import { IExperimentService } from '../../../common/types'; import { IServiceContainer } from '../../../ioc/types'; +import { DebugTestTag, ErrorTestItemOptions, RunTestTag } from './testItemUtilities'; +import { DiscoveredTestItem, DiscoveredTestNode, ITestResultResolver } from './types'; export function fixLogLines(content: string): string { const lines = content.split(/\r?\n/g); @@ -62,38 +66,125 @@ export function pythonTestAdapterRewriteEnabled(serviceContainer: IServiceContai return experiment.inExperimentSync(EnableTestAdapterRewrite.experiment); } -export const startServer = (testIds: string): Promise => - new Promise((resolve, reject) => { - const server = net.createServer((socket: net.Socket) => { - // Convert the test_ids array to JSON - const testData = JSON.stringify(testIds); +export async function startTestIdServer(testIds: string[]): Promise { + const startServer = (): Promise => + new Promise((resolve, reject) => { + const server = net.createServer((socket: net.Socket) => { + // Convert the test_ids array to JSON + const testData = JSON.stringify(testIds); - // Create the headers - const headers = [`Content-Length: ${Buffer.byteLength(testData)}`, 'Content-Type: application/json']; + // Create the headers + const headers = [`Content-Length: ${Buffer.byteLength(testData)}`, 'Content-Type: application/json']; - // Create the payload by concatenating the headers and the test data - const payload = `${headers.join('\r\n')}\r\n\r\n${testData}`; + // Create the payload by concatenating the headers and the test data + const payload = `${headers.join('\r\n')}\r\n\r\n${testData}`; - // Send the payload to the socket - socket.write(payload); + // Send the payload to the socket + socket.write(payload); - // Handle socket events - socket.on('data', (data) => { - traceLog('Received data:', data.toString()); + // Handle socket events + socket.on('data', (data) => { + traceLog('Received data:', data.toString()); + }); + + socket.on('end', () => { + traceLog('Client disconnected'); + }); }); - socket.on('end', () => { - traceLog('Client disconnected'); + server.listen(0, () => { + const { port } = server.address() as net.AddressInfo; + traceLog(`Server listening on port ${port}`); + resolve(port); }); - }); - server.listen(0, () => { - const { port } = server.address() as net.AddressInfo; - traceLog(`Server listening on port ${port}`); - resolve(port); + server.on('error', (error: Error) => { + reject(error); + }); }); - server.on('error', (error: Error) => { - reject(error); - }); + // Start the server and wait until it is listening + let returnPort = 0; + try { + await startServer() + .then((assignedPort) => { + traceVerbose(`Server started for pytest test ids server and listening on port ${assignedPort}`); + returnPort = assignedPort; + }) + .catch((error) => { + traceError('Error starting server for pytest test ids server:', error); + return 0; + }) + .finally(() => returnPort); + return returnPort; + } catch { + traceError('Error starting server for pytest test ids server, cannot get port.'); + return returnPort; + } +} + +export function buildErrorNodeOptions(uri: Uri, message: string, testType: string): ErrorTestItemOptions { + const labelText = testType === 'pytest' ? 'Pytest Discovery Error' : 'Unittest Discovery Error'; + return { + id: `DiscoveryError:${uri.fsPath}`, + label: `${labelText} [${path.basename(uri.fsPath)}]`, + error: message, + }; +} + +export function populateTestTree( + testController: TestController, + testTreeData: DiscoveredTestNode, + testRoot: TestItem | undefined, + resultResolver: ITestResultResolver, + token?: CancellationToken, +): void { + // If testRoot is undefined, use the info of the root item of testTreeData to create a test item, and append it to the test controller. + if (!testRoot) { + testRoot = testController.createTestItem(testTreeData.path, testTreeData.name, Uri.file(testTreeData.path)); + + testRoot.canResolveChildren = true; + testRoot.tags = [RunTestTag, DebugTestTag]; + + testController.items.add(testRoot); + } + + // Recursively populate the tree with test data. + testTreeData.children.forEach((child) => { + if (!token?.isCancellationRequested) { + if (isTestItem(child)) { + const testItem = testController.createTestItem(child.id_, child.name, Uri.file(child.path)); + testItem.tags = [RunTestTag, DebugTestTag]; + + const range = new Range( + new Position(Number(child.lineno) - 1, 0), + new Position(Number(child.lineno), 0), + ); + testItem.canResolveChildren = false; + testItem.range = range; + testItem.tags = [RunTestTag, DebugTestTag]; + + testRoot!.children.add(testItem); + // add to our map + resultResolver.runIdToTestItem.set(child.runID, testItem); + resultResolver.runIdToVSid.set(child.runID, child.id_); + resultResolver.vsIdToRunId.set(child.id_, child.runID); + } else { + let node = testController.items.get(child.path); + + if (!node) { + node = testController.createTestItem(child.id_, child.name, Uri.file(child.path)); + + node.canResolveChildren = true; + node.tags = [RunTestTag, DebugTestTag]; + testRoot!.children.add(node); + } + populateTestTree(testController, child, node, resultResolver, token); + } + } }); +} + +function isTestItem(test: DiscoveredTestNode | DiscoveredTestItem): test is DiscoveredTestItem { + return test.type_ === 'test'; +} diff --git a/src/client/testing/testController/controller.ts b/src/client/testing/testController/controller.ts index 0d3487855380..eff333a4cdd9 100644 --- a/src/client/testing/testController/controller.ts +++ b/src/client/testing/testController/controller.ts @@ -31,12 +31,14 @@ import { PYTEST_PROVIDER, UNITTEST_PROVIDER } from '../common/constants'; import { TestProvider } from '../types'; import { PythonTestServer } from './common/server'; import { DebugTestTag, getNodeByUri, RunTestTag } from './common/testItemUtilities'; +import { pythonTestAdapterRewriteEnabled } from './common/utils'; import { ITestController, ITestDiscoveryAdapter, ITestFrameworkController, TestRefreshOptions, ITestExecutionAdapter, + ITestResultResolver, } from './common/types'; import { UnittestTestDiscoveryAdapter } from './unittest/testDiscoveryAdapter'; import { UnittestTestExecutionAdapter } from './unittest/testExecutionAdapter'; @@ -44,8 +46,8 @@ import { PytestTestDiscoveryAdapter } from './pytest/pytestDiscoveryAdapter'; import { PytestTestExecutionAdapter } from './pytest/pytestExecutionAdapter'; import { WorkspaceTestAdapter } from './workspaceTestAdapter'; import { ITestDebugLauncher } from '../common/types'; -import { pythonTestAdapterRewriteEnabled } from './common/utils'; import { IServiceContainer } from '../../ioc/types'; +import { PythonResultResolver } from './common/resultResolver'; // Types gymnastics to make sure that sendTriggerTelemetry only accepts the correct types. type EventPropertyType = IEventNamePropertyMapping[EventName.UNITTEST_DISCOVERY_TRIGGER]; @@ -161,30 +163,37 @@ export class PythonTestController implements ITestController, IExtensionSingleAc let discoveryAdapter: ITestDiscoveryAdapter; let executionAdapter: ITestExecutionAdapter; let testProvider: TestProvider; + let resultResolver: ITestResultResolver; if (settings.testing.unittestEnabled) { + testProvider = UNITTEST_PROVIDER; + resultResolver = new PythonResultResolver(this.testController, testProvider, workspace.uri); discoveryAdapter = new UnittestTestDiscoveryAdapter( this.pythonTestServer, this.configSettings, this.testOutputChannel, + resultResolver, ); executionAdapter = new UnittestTestExecutionAdapter( this.pythonTestServer, this.configSettings, this.testOutputChannel, + resultResolver, ); - testProvider = UNITTEST_PROVIDER; } else { + testProvider = PYTEST_PROVIDER; + resultResolver = new PythonResultResolver(this.testController, testProvider, workspace.uri); discoveryAdapter = new PytestTestDiscoveryAdapter( this.pythonTestServer, this.configSettings, this.testOutputChannel, + resultResolver, ); executionAdapter = new PytestTestExecutionAdapter( this.pythonTestServer, this.configSettings, this.testOutputChannel, + resultResolver, ); - testProvider = PYTEST_PROVIDER; } const workspaceTestAdapter = new WorkspaceTestAdapter( @@ -192,6 +201,7 @@ export class PythonTestController implements ITestController, IExtensionSingleAc discoveryAdapter, executionAdapter, workspace.uri, + resultResolver, ); this.testAdapters.set(workspace.uri, workspaceTestAdapter); diff --git a/src/client/testing/testController/pytest/pytestDiscoveryAdapter.ts b/src/client/testing/testController/pytest/pytestDiscoveryAdapter.ts index aeb920407cd2..4378c68b534c 100644 --- a/src/client/testing/testController/pytest/pytestDiscoveryAdapter.ts +++ b/src/client/testing/testController/pytest/pytestDiscoveryAdapter.ts @@ -10,8 +10,14 @@ import { import { IConfigurationService, ITestOutputChannel } from '../../../common/types'; import { createDeferred, Deferred } from '../../../common/utils/async'; import { EXTENSION_ROOT_DIR } from '../../../constants'; -import { traceError, traceLog, traceVerbose } from '../../../logging'; -import { DataReceivedEvent, DiscoveredTestPayload, ITestDiscoveryAdapter, ITestServer } from '../common/types'; +import { traceError, traceVerbose } from '../../../logging'; +import { + DataReceivedEvent, + DiscoveredTestPayload, + ITestDiscoveryAdapter, + ITestResultResolver, + ITestServer, +} from '../common/types'; /** * Wrapper class for unittest test discovery. This is where we call `runTestCommand`. #this seems incorrectly copied @@ -19,39 +25,32 @@ import { DataReceivedEvent, DiscoveredTestPayload, ITestDiscoveryAdapter, ITestS export class PytestTestDiscoveryAdapter implements ITestDiscoveryAdapter { private promiseMap: Map> = new Map(); - private deferred: Deferred | undefined; - constructor( public testServer: ITestServer, public configSettings: IConfigurationService, private readonly outputChannel: ITestOutputChannel, - ) { - testServer.onDataReceived(this.onDataReceivedHandler, this); - } - - public onDataReceivedHandler({ uuid, data }: DataReceivedEvent): void { - const deferred = this.promiseMap.get(uuid); - if (deferred) { - deferred.resolve(JSON.parse(data)); - this.promiseMap.delete(uuid); - } - } + private readonly resultResolver?: ITestResultResolver, + ) {} - discoverTests(uri: Uri, executionFactory?: IPythonExecutionFactory): Promise { - if (executionFactory !== undefined) { - // ** new version of discover tests. - const settings = this.configSettings.getSettings(uri); - const { pytestArgs } = settings.testing; - traceVerbose(pytestArgs); - return this.runPytestDiscovery(uri, executionFactory); + async discoverTests(uri: Uri, executionFactory?: IPythonExecutionFactory): Promise { + const settings = this.configSettings.getSettings(uri); + const { pytestArgs } = settings.testing; + traceVerbose(pytestArgs); + const disposable = this.testServer.onDiscoveryDataReceived((e: DataReceivedEvent) => { + // cancelation token ? + this.resultResolver?.resolveDiscovery(JSON.parse(e.data)); + }); + try { + await this.runPytestDiscovery(uri, executionFactory); + } finally { + disposable.dispose(); } - // if executionFactory is undefined, we are using the old method signature of discover tests. - traceVerbose(uri); - this.deferred = createDeferred(); - return this.deferred.promise; + // this is only a placeholder to handle function overloading until rewrite is finished + const discoveryPayload: DiscoveredTestPayload = { cwd: uri.fsPath, status: 'success' }; + return discoveryPayload; } - async runPytestDiscovery(uri: Uri, executionFactory: IPythonExecutionFactory): Promise { + async runPytestDiscovery(uri: Uri, executionFactory?: IPythonExecutionFactory): Promise { const deferred = createDeferred(); const relativePathToPytest = 'pythonFiles'; const fullPluginPath = path.join(EXTENSION_ROOT_DIR, relativePathToPytest); @@ -79,13 +78,19 @@ export class PytestTestDiscoveryAdapter implements ITestDiscoveryAdapter { allowEnvironmentFetchExceptions: false, resource: uri, }; - const execService = await executionFactory.createActivatedEnvironment(creationOptions); - const discoveryArgs = ['-m', 'pytest', '-p', 'vscode_pytest', '--collect-only'].concat(pytestArgs); - traceLog(`Discovering pytest tests with arguments: ${discoveryArgs.join(' ')}`); - execService.exec(discoveryArgs, spawnOptions).catch((ex) => { - traceError(`Error occurred while discovering tests: ${ex}`); - deferred.reject(ex as Error); - }); + const execService = await executionFactory?.createActivatedEnvironment(creationOptions); + // delete UUID following entire discovery finishing. + execService + ?.exec(['-m', 'pytest', '-p', 'vscode_pytest', '--collect-only'].concat(pytestArgs), spawnOptions) + .then(() => { + this.testServer.deleteUUID(uuid); + return deferred.resolve(); + }) + .catch((err) => { + traceError(`Error while trying to run pytest discovery, \n${err}\r\n\r\n`); + this.testServer.deleteUUID(uuid); + return deferred.reject(err); + }); return deferred.promise; } } diff --git a/src/client/testing/testController/pytest/pytestExecutionAdapter.ts b/src/client/testing/testController/pytest/pytestExecutionAdapter.ts index 90704b5d67f4..1bf032b9b594 100644 --- a/src/client/testing/testController/pytest/pytestExecutionAdapter.ts +++ b/src/client/testing/testController/pytest/pytestExecutionAdapter.ts @@ -1,13 +1,18 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -import { Uri } from 'vscode'; +import { TestRun, Uri } from 'vscode'; import * as path from 'path'; -import * as net from 'net'; import { IConfigurationService, ITestOutputChannel } from '../../../common/types'; import { createDeferred, Deferred } from '../../../common/utils/async'; import { traceError, traceInfo, traceLog, traceVerbose } from '../../../logging'; -import { DataReceivedEvent, ExecutionTestPayload, ITestExecutionAdapter, ITestServer } from '../common/types'; +import { + DataReceivedEvent, + ExecutionTestPayload, + ITestExecutionAdapter, + ITestResultResolver, + ITestServer, +} from '../common/types'; import { ExecutionFactoryCreateWithEnvironmentOptions, IPythonExecutionFactory, @@ -17,6 +22,7 @@ import { removePositionalFoldersAndFiles } from './arguments'; import { ITestDebugLauncher, LaunchOptions } from '../../common/types'; import { PYTEST_PROVIDER } from '../../common/constants'; import { EXTENSION_ROOT_DIR } from '../../../common/constants'; +import { startTestIdServer } from '../common/utils'; // eslint-disable-next-line @typescript-eslint/no-explicit-any // (global as any).EXTENSION_ROOT_DIR = EXTENSION_ROOT_DIR; @@ -27,39 +33,35 @@ import { EXTENSION_ROOT_DIR } from '../../../common/constants'; export class PytestTestExecutionAdapter implements ITestExecutionAdapter { private promiseMap: Map> = new Map(); - private deferred: Deferred | undefined; - constructor( public testServer: ITestServer, public configSettings: IConfigurationService, private readonly outputChannel: ITestOutputChannel, - ) { - testServer.onDataReceived(this.onDataReceivedHandler, this); - } - - public onDataReceivedHandler({ uuid, data }: DataReceivedEvent): void { - const deferred = this.promiseMap.get(uuid); - if (deferred) { - deferred.resolve(JSON.parse(data)); - this.promiseMap.delete(uuid); - } - } + private readonly resultResolver?: ITestResultResolver, + ) {} async runTests( uri: Uri, testIds: string[], debugBool?: boolean, + runInstance?: TestRun, executionFactory?: IPythonExecutionFactory, debugLauncher?: ITestDebugLauncher, ): Promise { - if (executionFactory !== undefined) { - // ** new version of run tests. - return this.runTestsNew(uri, testIds, debugBool, executionFactory, debugLauncher); + traceVerbose(uri, testIds, debugBool); + const disposable = this.testServer.onRunDataReceived((e: DataReceivedEvent) => { + if (runInstance) { + this.resultResolver?.resolveExecution(JSON.parse(e.data), runInstance); + } + }); + try { + await this.runTestsNew(uri, testIds, debugBool, executionFactory, debugLauncher); + } finally { + disposable.dispose(); + // confirm with testing that this gets called (it must clean this up) } - // if executionFactory is undefined, we are using the old method signature of run tests. - this.outputChannel.appendLine('Running tests.'); - this.deferred = createDeferred(); - return this.deferred.promise; + const executionPayload: ExecutionTestPayload = { cwd: uri.fsPath, status: 'success', error: '' }; + return executionPayload; } private async runTestsNew( @@ -114,49 +116,11 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter { if (debugBool && !testArgs.some((a) => a.startsWith('--capture') || a === '-s')) { testArgs.push('--capture', 'no'); } + traceLog(`Running PYTEST execution for the following test ids: ${testIds}`); - // create payload with testIds to send to run pytest script - const testData = JSON.stringify(testIds); - const headers = [`Content-Length: ${Buffer.byteLength(testData)}`, 'Content-Type: application/json']; - const payload = `${headers.join('\r\n')}\r\n\r\n${testData}`; - traceLog(`Running pytest execution for the following test ids: ${testIds}`); - - let pytestRunTestIdsPort: string | undefined; - const startServer = (): Promise => - new Promise((resolve, reject) => { - const server = net.createServer((socket: net.Socket) => { - socket.on('end', () => { - traceVerbose('Client disconnected for pytest test ids server'); - }); - }); - - server.listen(0, () => { - const { port } = server.address() as net.AddressInfo; - traceVerbose(`Server listening on port ${port} for pytest test ids server`); - resolve(port); - }); - - server.on('error', (error: Error) => { - traceError('Error starting server for pytest test ids server:', error); - reject(error); - }); - server.on('connection', (socket: net.Socket) => { - socket.write(payload); - traceVerbose('payload sent for pytest execution', payload); - }); - }); - - // Start the server and wait until it is listening - await startServer() - .then((assignedPort) => { - traceVerbose(`Server started for pytest test ids server and listening on port ${assignedPort}`); - pytestRunTestIdsPort = assignedPort.toString(); - if (spawnOptions.extraVariables) - spawnOptions.extraVariables.RUN_TEST_IDS_PORT = pytestRunTestIdsPort; - }) - .catch((error) => { - traceError('Error starting server for pytest test ids server:', error); - }); + const pytestRunTestIdsPort = await startTestIdServer(testIds); + if (spawnOptions.extraVariables) + spawnOptions.extraVariables.RUN_TEST_IDS_PORT = pytestRunTestIdsPort.toString(); if (debugBool) { const pytestPort = this.testServer.getPort().toString(); @@ -168,7 +132,7 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter { testProvider: PYTEST_PROVIDER, pytestPort, pytestUUID, - runTestIdsPort: pytestRunTestIdsPort, + runTestIdsPort: pytestRunTestIdsPort.toString(), }; traceInfo(`Running DEBUG pytest with arguments: ${testArgs.join(' ')}\r\n`); await debugLauncher!.launchDebugger(launchOptions, () => { @@ -187,6 +151,7 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter { return Promise.reject(ex); } - return deferred.promise; + const executionPayload: ExecutionTestPayload = { cwd: uri.fsPath, status: 'success', error: '' }; + return executionPayload; } } diff --git a/src/client/testing/testController/unittest/testDiscoveryAdapter.ts b/src/client/testing/testController/unittest/testDiscoveryAdapter.ts index 9c565af78c08..8d393a8da18d 100644 --- a/src/client/testing/testController/unittest/testDiscoveryAdapter.ts +++ b/src/client/testing/testController/unittest/testDiscoveryAdapter.ts @@ -10,11 +10,11 @@ import { DataReceivedEvent, DiscoveredTestPayload, ITestDiscoveryAdapter, + ITestResultResolver, ITestServer, TestCommandOptions, TestDiscoveryCommand, } from '../common/types'; -import { traceInfo } from '../../../logging'; /** * Wrapper class for unittest test discovery. This is where we call `runTestCommand`. @@ -28,17 +28,8 @@ export class UnittestTestDiscoveryAdapter implements ITestDiscoveryAdapter { public testServer: ITestServer, public configSettings: IConfigurationService, private readonly outputChannel: ITestOutputChannel, - ) { - testServer.onDataReceived(this.onDataReceivedHandler, this); - } - - public onDataReceivedHandler({ uuid, data }: DataReceivedEvent): void { - const deferred = this.promiseMap.get(uuid); - if (deferred) { - deferred.resolve(JSON.parse(data)); - this.promiseMap.delete(uuid); - } - } + private readonly resultResolver?: ITestResultResolver, + ) {} public async discoverTests(uri: Uri): Promise { const deferred = createDeferred(); @@ -60,12 +51,23 @@ export class UnittestTestDiscoveryAdapter implements ITestDiscoveryAdapter { this.promiseMap.set(uuid, deferred); - // Send the test command to the server. - // The server will fire an onDataReceived event once it gets a response. - traceInfo(`Sending discover unittest script to server.`); - this.testServer.sendCommand(options); + const disposable = this.testServer.onDiscoveryDataReceived((e: DataReceivedEvent) => { + this.resultResolver?.resolveDiscovery(JSON.parse(e.data)); + }); + try { + await this.callSendCommand(options); + } finally { + disposable.dispose(); + // confirm with testing that this gets called (it must clean this up) + } + const discoveryPayload: DiscoveredTestPayload = { cwd: uri.fsPath, status: 'success' }; + return discoveryPayload; + } - return deferred.promise; + private async callSendCommand(options: TestCommandOptions): Promise { + await this.testServer.sendCommand(options); + const discoveryPayload: DiscoveredTestPayload = { cwd: '', status: 'success' }; + return discoveryPayload; } } diff --git a/src/client/testing/testController/unittest/testExecutionAdapter.ts b/src/client/testing/testController/unittest/testExecutionAdapter.ts index b671a64138cb..ca88d3871706 100644 --- a/src/client/testing/testController/unittest/testExecutionAdapter.ts +++ b/src/client/testing/testController/unittest/testExecutionAdapter.ts @@ -2,20 +2,21 @@ // Licensed under the MIT License. import * as path from 'path'; -import { Uri } from 'vscode'; -import * as net from 'net'; +import { TestRun, Uri } from 'vscode'; import { IConfigurationService, ITestOutputChannel } from '../../../common/types'; -import { createDeferred, Deferred } from '../../../common/utils/async'; +import { Deferred, createDeferred } from '../../../common/utils/async'; import { EXTENSION_ROOT_DIR } from '../../../constants'; import { DataReceivedEvent, ExecutionTestPayload, ITestExecutionAdapter, + ITestResultResolver, ITestServer, TestCommandOptions, TestExecutionCommand, } from '../common/types'; -import { traceLog, traceError } from '../../../logging'; +import { traceLog } from '../../../logging'; +import { startTestIdServer } from '../common/utils'; /** * Wrapper Class for unittest test execution. This is where we call `runTestCommand`? @@ -30,19 +31,31 @@ export class UnittestTestExecutionAdapter implements ITestExecutionAdapter { public testServer: ITestServer, public configSettings: IConfigurationService, private readonly outputChannel: ITestOutputChannel, - ) { - testServer.onDataReceived(this.onDataReceivedHandler, this); - } - - public onDataReceivedHandler({ uuid, data }: DataReceivedEvent): void { - const deferred = this.promiseMap.get(uuid); - if (deferred) { - deferred.resolve(JSON.parse(data)); - this.promiseMap.delete(uuid); + private readonly resultResolver?: ITestResultResolver, + ) {} + + public async runTests( + uri: Uri, + testIds: string[], + debugBool?: boolean, + runInstance?: TestRun, + ): Promise { + const disposable = this.testServer.onRunDataReceived((e: DataReceivedEvent) => { + if (runInstance) { + this.resultResolver?.resolveExecution(JSON.parse(e.data), runInstance); + } + }); + try { + await this.runTestsNew(uri, testIds, debugBool); + } finally { + disposable.dispose(); + // confirm with testing that this gets called (it must clean this up) } + const executionPayload: ExecutionTestPayload = { cwd: uri.fsPath, status: 'success', error: '' }; + return executionPayload; } - public async runTests(uri: Uri, testIds: string[], debugBool?: boolean): Promise { + private async runTestsNew(uri: Uri, testIds: string[], debugBool?: boolean): Promise { const settings = this.configSettings.getSettings(uri); const { cwd, unittestArgs } = settings.testing; @@ -62,52 +75,17 @@ export class UnittestTestExecutionAdapter implements ITestExecutionAdapter { const deferred = createDeferred(); this.promiseMap.set(uuid, deferred); + traceLog(`Running UNITTEST execution for the following test ids: ${testIds}`); - // create payload with testIds to send to run pytest script - const testData = JSON.stringify(testIds); - const headers = [`Content-Length: ${Buffer.byteLength(testData)}`, 'Content-Type: application/json']; - const payload = `${headers.join('\r\n')}\r\n\r\n${testData}`; - - let runTestIdsPort: string | undefined; - const startServer = (): Promise => - new Promise((resolve, reject) => { - const server = net.createServer((socket: net.Socket) => { - socket.on('end', () => { - traceLog('Client disconnected'); - }); - }); - - server.listen(0, () => { - const { port } = server.address() as net.AddressInfo; - traceLog(`Server listening on port ${port}`); - resolve(port); - }); - - server.on('error', (error: Error) => { - reject(error); - }); - server.on('connection', (socket: net.Socket) => { - socket.write(payload); - traceLog('payload sent', payload); - }); - }); - - // Start the server and wait until it is listening - await startServer() - .then((assignedPort) => { - traceLog(`Server started and listening on port ${assignedPort}`); - runTestIdsPort = assignedPort.toString(); - // Send test command to server. - // Server fire onDataReceived event once it gets response. - this.testServer.sendCommand(options, runTestIdsPort, () => { - deferred.resolve(); - }); - }) - .catch((error) => { - traceError('Error starting server:', error); - }); + const runTestIdsPort = await startTestIdServer(testIds); - return deferred.promise; + await this.testServer.sendCommand(options, runTestIdsPort.toString(), () => { + // disposable.dispose(); + deferred.resolve(); + }); + // return deferred.promise; + const executionPayload: ExecutionTestPayload = { cwd: uri.fsPath, status: 'success', error: '' }; + return executionPayload; } } diff --git a/src/client/testing/testController/workspaceTestAdapter.ts b/src/client/testing/testController/workspaceTestAdapter.ts index 5cba6c193d3c..f3ea0b9f6193 100644 --- a/src/client/testing/testController/workspaceTestAdapter.ts +++ b/src/client/testing/testController/workspaceTestAdapter.ts @@ -1,38 +1,19 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -import * as path from 'path'; import * as util from 'util'; -import { - CancellationToken, - Position, - Range, - TestController, - TestItem, - TestMessage, - TestRun, - Uri, - Location, -} from 'vscode'; -import { splitLines } from '../../common/stringUtils'; +import { CancellationToken, TestController, TestItem, TestRun, Uri } from 'vscode'; import { createDeferred, Deferred } from '../../common/utils/async'; import { Testing } from '../../common/utils/localize'; -import { traceError, traceVerbose } from '../../logging'; +import { traceError } from '../../logging'; import { sendTelemetryEvent } from '../../telemetry'; import { EventName } from '../../telemetry/constants'; import { TestProvider } from '../types'; -import { - clearAllChildren, - createErrorTestItem, - DebugTestTag, - ErrorTestItemOptions, - getTestCaseNodes, - RunTestTag, -} from './common/testItemUtilities'; -import { DiscoveredTestItem, DiscoveredTestNode, ITestDiscoveryAdapter, ITestExecutionAdapter } from './common/types'; -import { fixLogLines } from './common/utils'; +import { createErrorTestItem, getTestCaseNodes } from './common/testItemUtilities'; +import { ITestDiscoveryAdapter, ITestExecutionAdapter, ITestResultResolver } from './common/types'; import { IPythonExecutionFactory } from '../../common/process/types'; import { ITestDebugLauncher } from '../common/types'; +import { buildErrorNodeOptions } from './common/utils'; /** * This class exposes a test-provider-agnostic way of discovering tests. @@ -48,22 +29,13 @@ export class WorkspaceTestAdapter { private executing: Deferred | undefined; - runIdToTestItem: Map; - - runIdToVSid: Map; - - vsIdToRunId: Map; - constructor( private testProvider: TestProvider, private discoveryAdapter: ITestDiscoveryAdapter, private executionAdapter: ITestExecutionAdapter, private workspaceUri: Uri, - ) { - this.runIdToTestItem = new Map(); - this.runIdToVSid = new Map(); - this.vsIdToRunId = new Map(); - } + private resultResolver: ITestResultResolver, + ) {} public async executeTests( testController: TestController, @@ -81,7 +53,6 @@ export class WorkspaceTestAdapter { const deferred = createDeferred(); this.executing = deferred; - let rawTestExecData; const testCaseNodes: TestItem[] = []; const testCaseIdsSet = new Set(); try { @@ -93,7 +64,7 @@ export class WorkspaceTestAdapter { // iterate through testItems nodes and fetch their unittest runID to pass in as argument testCaseNodes.forEach((node) => { runInstance.started(node); // do the vscode ui test item start here before runtest - const runId = this.vsIdToRunId.get(node.id); + const runId = this.resultResolver.vsIdToRunId.get(node.id); if (runId) { testCaseIdsSet.add(runId); } @@ -101,16 +72,16 @@ export class WorkspaceTestAdapter { const testCaseIds = Array.from(testCaseIdsSet); // ** execution factory only defined for new rewrite way if (executionFactory !== undefined) { - traceVerbose('executionFactory defined'); - rawTestExecData = await this.executionAdapter.runTests( + await this.executionAdapter.runTests( this.workspaceUri, testCaseIds, debugBool, + runInstance, executionFactory, debugLauncher, ); } else { - rawTestExecData = await this.executionAdapter.runTests(this.workspaceUri, testCaseIds, debugBool); + await this.executionAdapter.runTests(this.workspaceUri, testCaseIds, debugBool); } deferred.resolve(); } catch (ex) { @@ -136,146 +107,6 @@ export class WorkspaceTestAdapter { this.executing = undefined; } - if (rawTestExecData !== undefined && rawTestExecData.result !== undefined) { - // Map which holds the subtest information for each test item. - const subTestStats: Map = new Map(); - - // iterate through payload and update the UI accordingly. - for (const keyTemp of Object.keys(rawTestExecData.result)) { - const testCases: TestItem[] = []; - - // grab leaf level test items - testController.items.forEach((i) => { - const tempArr: TestItem[] = getTestCaseNodes(i); - testCases.push(...tempArr); - }); - - if ( - rawTestExecData.result[keyTemp].outcome === 'failure' || - rawTestExecData.result[keyTemp].outcome === 'passed-unexpected' - ) { - const rawTraceback = rawTestExecData.result[keyTemp].traceback ?? ''; - const traceback = splitLines(rawTraceback, { - trim: false, - removeEmptyEntries: true, - }).join('\r\n'); - - const text = `${rawTestExecData.result[keyTemp].test} failed: ${ - rawTestExecData.result[keyTemp].message ?? rawTestExecData.result[keyTemp].outcome - }\r\n${traceback}\r\n`; - const message = new TestMessage(text); - - // note that keyTemp is a runId for unittest library... - const grabVSid = this.runIdToVSid.get(keyTemp); - // search through freshly built array of testItem to find the failed test and update UI. - testCases.forEach((indiItem) => { - if (indiItem.id === grabVSid) { - if (indiItem.uri && indiItem.range) { - message.location = new Location(indiItem.uri, indiItem.range); - runInstance.failed(indiItem, message); - runInstance.appendOutput(fixLogLines(text)); - } - } - }); - } else if ( - rawTestExecData.result[keyTemp].outcome === 'success' || - rawTestExecData.result[keyTemp].outcome === 'expected-failure' - ) { - const grabTestItem = this.runIdToTestItem.get(keyTemp); - const grabVSid = this.runIdToVSid.get(keyTemp); - if (grabTestItem !== undefined) { - testCases.forEach((indiItem) => { - if (indiItem.id === grabVSid) { - if (indiItem.uri && indiItem.range) { - runInstance.passed(grabTestItem); - runInstance.appendOutput('Passed here'); - } - } - }); - } - } else if (rawTestExecData.result[keyTemp].outcome === 'skipped') { - const grabTestItem = this.runIdToTestItem.get(keyTemp); - const grabVSid = this.runIdToVSid.get(keyTemp); - if (grabTestItem !== undefined) { - testCases.forEach((indiItem) => { - if (indiItem.id === grabVSid) { - if (indiItem.uri && indiItem.range) { - runInstance.skipped(grabTestItem); - runInstance.appendOutput('Skipped here'); - } - } - }); - } - } else if (rawTestExecData.result[keyTemp].outcome === 'subtest-failure') { - // split on " " since the subtest ID has the parent test ID in the first part of the ID. - const parentTestCaseId = keyTemp.split(' ')[0]; - const parentTestItem = this.runIdToTestItem.get(parentTestCaseId); - const data = rawTestExecData.result[keyTemp]; - // find the subtest's parent test item - if (parentTestItem) { - const subtestStats = subTestStats.get(parentTestCaseId); - if (subtestStats) { - subtestStats.failed += 1; - } else { - subTestStats.set(parentTestCaseId, { failed: 1, passed: 0 }); - runInstance.appendOutput(fixLogLines(`${parentTestCaseId} [subtests]:\r\n`)); - // clear since subtest items don't persist between runs - clearAllChildren(parentTestItem); - } - const subtestId = keyTemp; - const subTestItem = testController?.createTestItem(subtestId, subtestId); - runInstance.appendOutput(fixLogLines(`${subtestId} Failed\r\n`)); - // create a new test item for the subtest - if (subTestItem) { - const traceback = data.traceback ?? ''; - const text = `${data.subtest} Failed: ${data.message ?? data.outcome}\r\n${traceback}\r\n`; - runInstance.appendOutput(fixLogLines(text)); - parentTestItem.children.add(subTestItem); - runInstance.started(subTestItem); - const message = new TestMessage(rawTestExecData?.result[keyTemp].message ?? ''); - if (parentTestItem.uri && parentTestItem.range) { - message.location = new Location(parentTestItem.uri, parentTestItem.range); - } - runInstance.failed(subTestItem, message); - } else { - throw new Error('Unable to create new child node for subtest'); - } - } else { - throw new Error('Parent test item not found'); - } - } else if (rawTestExecData.result[keyTemp].outcome === 'subtest-success') { - // split on " " since the subtest ID has the parent test ID in the first part of the ID. - const parentTestCaseId = keyTemp.split(' ')[0]; - const parentTestItem = this.runIdToTestItem.get(parentTestCaseId); - - // find the subtest's parent test item - if (parentTestItem) { - const subtestStats = subTestStats.get(parentTestCaseId); - if (subtestStats) { - subtestStats.passed += 1; - } else { - subTestStats.set(parentTestCaseId, { failed: 0, passed: 1 }); - runInstance.appendOutput(fixLogLines(`${parentTestCaseId} [subtests]:\r\n`)); - // clear since subtest items don't persist between runs - clearAllChildren(parentTestItem); - } - const subtestId = keyTemp; - const subTestItem = testController?.createTestItem(subtestId, subtestId); - // create a new test item for the subtest - if (subTestItem) { - parentTestItem.children.add(subTestItem); - runInstance.started(subTestItem); - runInstance.passed(subTestItem); - runInstance.appendOutput(fixLogLines(`${subtestId} Passed\r\n`)); - } else { - throw new Error('Unable to create new child node for subtest'); - } - } else { - throw new Error('Parent test item not found'); - } - } - } - } return Promise.resolve(); } @@ -286,8 +117,6 @@ export class WorkspaceTestAdapter { ): Promise { sendTelemetryEvent(EventName.UNITTEST_DISCOVERING, undefined, { tool: this.testProvider }); - const workspacePath = this.workspaceUri.fsPath; - // Discovery is expensive. If it is already running, use the existing promise. if (this.discovering) { return this.discovering.promise; @@ -296,14 +125,12 @@ export class WorkspaceTestAdapter { const deferred = createDeferred(); this.discovering = deferred; - let rawTestData; try { // ** execution factory only defined for new rewrite way if (executionFactory !== undefined) { - traceVerbose('executionFactory defined'); - rawTestData = await this.discoveryAdapter.discoverTests(this.workspaceUri, executionFactory); + await this.discoveryAdapter.discoverTests(this.workspaceUri, executionFactory); } else { - rawTestData = await this.discoveryAdapter.discoverTests(this.workspaceUri); + await this.discoveryAdapter.discoverTests(this.workspaceUri); } deferred.resolve(); } catch (ex) { @@ -324,119 +151,14 @@ export class WorkspaceTestAdapter { const errorNode = createErrorTestItem(testController, options); testController.items.add(errorNode); - deferred.reject(ex as Error); + return deferred.reject(ex as Error); } finally { // Discovery has finished running, we have the data, // we don't need the deferred promise anymore. this.discovering = undefined; } - if (!rawTestData) { - // No test data is available - return Promise.resolve(); - } - - // Check if there were any errors in the discovery process. - if (rawTestData.status === 'error') { - const testingErrorConst = - this.testProvider === 'pytest' ? Testing.errorPytestDiscovery : Testing.errorUnittestDiscovery; - const { errors } = rawTestData; - traceError(testingErrorConst, '\r\n', errors?.join('\r\n\r\n')); - let errorNode = testController.items.get(`DiscoveryError:${workspacePath}`); - const message = util.format( - `${testingErrorConst} ${Testing.seePythonOutput}\r\n`, - errors?.join('\r\n\r\n'), - ); - - if (errorNode === undefined) { - const options = buildErrorNodeOptions(this.workspaceUri, message, this.testProvider); - errorNode = createErrorTestItem(testController, options); - testController.items.add(errorNode); - } - errorNode.error = message; - } else { - // Remove the error node if necessary, - // then parse and insert test data. - testController.items.delete(`DiscoveryError:${workspacePath}`); - - if (rawTestData.tests) { - // If the test root for this folder exists: Workspace refresh, update its children. - // Otherwise, it is a freshly discovered workspace, and we need to create a new test root and populate the test tree. - populateTestTree(testController, rawTestData.tests, undefined, this, token); - } else { - // Delete everything from the test controller. - testController.items.replace([]); - } - } - sendTelemetryEvent(EventName.UNITTEST_DISCOVERY_DONE, undefined, { tool: this.testProvider, failed: false }); return Promise.resolve(); } } - -function isTestItem(test: DiscoveredTestNode | DiscoveredTestItem): test is DiscoveredTestItem { - return test.type_ === 'test'; -} - -// had to switch the order of the original parameter since required param cannot follow optional. -function populateTestTree( - testController: TestController, - testTreeData: DiscoveredTestNode, - testRoot: TestItem | undefined, - wstAdapter: WorkspaceTestAdapter, - token?: CancellationToken, -): void { - // If testRoot is undefined, use the info of the root item of testTreeData to create a test item, and append it to the test controller. - if (!testRoot) { - testRoot = testController.createTestItem(testTreeData.path, testTreeData.name, Uri.file(testTreeData.path)); - - testRoot.canResolveChildren = true; - testRoot.tags = [RunTestTag, DebugTestTag]; - - testController.items.add(testRoot); - } - - // Recursively populate the tree with test data. - testTreeData.children.forEach((child) => { - if (!token?.isCancellationRequested) { - if (isTestItem(child)) { - const testItem = testController.createTestItem(child.id_, child.name, Uri.file(child.path)); - testItem.tags = [RunTestTag, DebugTestTag]; - - const range = new Range( - new Position(Number(child.lineno) - 1, 0), - new Position(Number(child.lineno), 0), - ); - testItem.canResolveChildren = false; - testItem.range = range; - testItem.tags = [RunTestTag, DebugTestTag]; - - testRoot!.children.add(testItem); - // add to our map - wstAdapter.runIdToTestItem.set(child.runID, testItem); - wstAdapter.runIdToVSid.set(child.runID, child.id_); - wstAdapter.vsIdToRunId.set(child.id_, child.runID); - } else { - let node = testController.items.get(child.path); - - if (!node) { - node = testController.createTestItem(child.id_, child.name, Uri.file(child.path)); - - node.canResolveChildren = true; - node.tags = [RunTestTag, DebugTestTag]; - testRoot!.children.add(node); - } - populateTestTree(testController, child, node, wstAdapter, token); - } - } - }); -} - -function buildErrorNodeOptions(uri: Uri, message: string, testType: string): ErrorTestItemOptions { - const labelText = testType === 'pytest' ? 'Pytest Discovery Error' : 'Unittest Discovery Error'; - return { - id: `DiscoveryError:${uri.fsPath}`, - label: `${labelText} [${path.basename(uri.fsPath)}]`, - error: message, - }; -} diff --git a/src/test/mocks/vsc/index.ts b/src/test/mocks/vsc/index.ts index 89f4ab1a2d07..092fc67da6c6 100644 --- a/src/test/mocks/vsc/index.ts +++ b/src/test/mocks/vsc/index.ts @@ -6,6 +6,7 @@ import { EventEmitter as NodeEventEmitter } from 'events'; import * as vscode from 'vscode'; + // export * from './range'; // export * from './position'; // export * from './selection'; @@ -443,3 +444,114 @@ export enum LogLevel { */ Error = 5, } + +export class TestMessage { + /** + * Human-readable message text to display. + */ + message: string | MarkdownString; + + /** + * Expected test output. If given with {@link TestMessage.actualOutput actualOutput }, a diff view will be shown. + */ + expectedOutput?: string; + + /** + * Actual test output. If given with {@link TestMessage.expectedOutput expectedOutput }, a diff view will be shown. + */ + actualOutput?: string; + + /** + * Associated file location. + */ + location?: vscode.Location; + + /** + * Creates a new TestMessage that will present as a diff in the editor. + * @param message Message to display to the user. + * @param expected Expected output. + * @param actual Actual output. + */ + static diff(message: string | MarkdownString, expected: string, actual: string): TestMessage { + const testMessage = new TestMessage(message); + testMessage.expectedOutput = expected; + testMessage.actualOutput = actual; + return testMessage; + } + + /** + * Creates a new TestMessage instance. + * @param message The message to show to the user. + */ + constructor(message: string | MarkdownString) { + this.message = message; + } +} + +export interface TestItemCollection extends Iterable<[string, vscode.TestItem]> { + /** + * Gets the number of items in the collection. + */ + readonly size: number; + + /** + * Replaces the items stored by the collection. + * @param items Items to store. + */ + replace(items: readonly vscode.TestItem[]): void; + + /** + * Iterate over each entry in this collection. + * + * @param callback Function to execute for each entry. + * @param thisArg The `this` context used when invoking the handler function. + */ + forEach(callback: (item: vscode.TestItem, collection: TestItemCollection) => unknown, thisArg?: unknown): void; + + /** + * Adds the test item to the children. If an item with the same ID already + * exists, it'll be replaced. + * @param item Item to add. + */ + add(item: vscode.TestItem): void; + + /** + * Removes a single test item from the collection. + * @param itemId Item ID to delete. + */ + delete(itemId: string): void; + + /** + * Efficiently gets a test item by ID, if it exists, in the children. + * @param itemId Item ID to get. + * @returns The found item or undefined if it does not exist. + */ + get(itemId: string): vscode.TestItem | undefined; +} + +/** + * Represents a location inside a resource, such as a line + * inside a text file. + */ +export class Location { + /** + * The resource identifier of this location. + */ + uri: vscode.Uri; + + /** + * The document range of this location. + */ + range: vscode.Range; + + /** + * Creates a new location object. + * + * @param uri The resource identifier. + * @param rangeOrPosition The range or position. Positions will be converted to an empty range. + */ + constructor(uri: vscode.Uri, rangeOrPosition: vscode.Range) { + this.uri = uri; + this.range = rangeOrPosition; + } +} diff --git a/src/test/testing/testController/pytest/pytestDiscoveryAdapter.unit.test.ts b/src/test/testing/testController/pytest/pytestDiscoveryAdapter.unit.test.ts index 12c79a23c7fd..0286235be1bf 100644 --- a/src/test/testing/testController/pytest/pytestDiscoveryAdapter.unit.test.ts +++ b/src/test/testing/testController/pytest/pytestDiscoveryAdapter.unit.test.ts @@ -4,11 +4,17 @@ import * as assert from 'assert'; import { Uri } from 'vscode'; import * as typeMoq from 'typemoq'; +import * as path from 'path'; import { IConfigurationService, ITestOutputChannel } from '../../../../client/common/types'; import { PytestTestDiscoveryAdapter } from '../../../../client/testing/testController/pytest/pytestDiscoveryAdapter'; -import { DataReceivedEvent, ITestServer } from '../../../../client/testing/testController/common/types'; -import { IPythonExecutionFactory, IPythonExecutionService } from '../../../../client/common/process/types'; +import { ITestServer } from '../../../../client/testing/testController/common/types'; +import { + IPythonExecutionFactory, + IPythonExecutionService, + SpawnOptions, +} from '../../../../client/common/process/types'; import { createDeferred, Deferred } from '../../../../client/common/utils/async'; +import { EXTENSION_ROOT_DIR } from '../../../../client/constants'; suite('pytest test discovery adapter', () => { let testServer: typeMoq.IMock; @@ -18,27 +24,56 @@ suite('pytest test discovery adapter', () => { let execService: typeMoq.IMock; let deferred: Deferred; let outputChannel: typeMoq.IMock; + let portNum: number; + let uuid: string; + let expectedPath: string; + let uri: Uri; + let expectedExtraVariables: Record; setup(() => { + const mockExtensionRootDir = typeMoq.Mock.ofType(); + mockExtensionRootDir.setup((m) => m.toString()).returns(() => '/mocked/extension/root/dir'); + + // constants + portNum = 12345; + uuid = 'uuid123'; + expectedPath = path.join('/', 'my', 'test', 'path'); + uri = Uri.file(expectedPath); + const relativePathToPytest = 'pythonFiles'; + const fullPluginPath = path.join(EXTENSION_ROOT_DIR, relativePathToPytest); + expectedExtraVariables = { + PYTHONPATH: fullPluginPath, + TEST_UUID: uuid, + TEST_PORT: portNum.toString(), + }; + + // set up test server testServer = typeMoq.Mock.ofType(); - testServer.setup((t) => t.getPort()).returns(() => 12345); + testServer.setup((t) => t.getPort()).returns(() => portNum); + testServer.setup((t) => t.createUUID(typeMoq.It.isAny())).returns(() => uuid); testServer - .setup((t) => t.onDataReceived(typeMoq.It.isAny(), typeMoq.It.isAny())) + .setup((t) => t.onDiscoveryDataReceived(typeMoq.It.isAny(), typeMoq.It.isAny())) .returns(() => ({ dispose: () => { /* no-body */ }, })); + + // set up config service configService = ({ getSettings: () => ({ testing: { pytestArgs: ['.'] }, }), } as unknown) as IConfigurationService; + + // set up exec factory execFactory = typeMoq.Mock.ofType(); - execService = typeMoq.Mock.ofType(); execFactory .setup((x) => x.createActivatedEnvironment(typeMoq.It.isAny())) .returns(() => Promise.resolve(execService.object)); + + // set up exec service + execService = typeMoq.Mock.ofType(); deferred = createDeferred(); execService .setup((x) => x.exec(typeMoq.It.isAny(), typeMoq.It.isAny())) @@ -46,49 +81,51 @@ suite('pytest test discovery adapter', () => { deferred.resolve(); return Promise.resolve({ stdout: '{}' }); }); - execFactory.setup((p) => ((p as unknown) as any).then).returns(() => undefined); execService.setup((p) => ((p as unknown) as any).then).returns(() => undefined); outputChannel = typeMoq.Mock.ofType(); }); - test('onDataReceivedHandler should parse only if known UUID', async () => { - const uri = Uri.file('/my/test/path/'); - const uuid = 'uuid123'; - const data = { status: 'success' }; - testServer.setup((t) => t.createUUID(typeMoq.It.isAny())).returns(() => uuid); - const eventData: DataReceivedEvent = { - uuid, - data: JSON.stringify(data), - }; - + test('Discovery should call exec with correct basic args', async () => { adapter = new PytestTestDiscoveryAdapter(testServer.object, configService, outputChannel.object); - const promise = adapter.discoverTests(uri, execFactory.object); - // const promise = adapter.discoverTests(uri); - await deferred.promise; - adapter.onDataReceivedHandler(eventData); - const result = await promise; - assert.deepStrictEqual(result, data); + await adapter.discoverTests(uri, execFactory.object); + const expectedArgs = ['-m', 'pytest', '-p', 'vscode_pytest', '--collect-only', '.']; + + execService.verify( + (x) => + x.exec( + expectedArgs, + typeMoq.It.is((options) => { + assert.deepEqual(options.extraVariables, expectedExtraVariables); + assert.equal(options.cwd, expectedPath); + assert.equal(options.throwOnStdErr, true); + return true; + }), + ), + typeMoq.Times.once(), + ); }); - test('onDataReceivedHandler should not parse if it is unknown UUID', async () => { - const uri = Uri.file('/my/test/path/'); - const uuid = 'uuid456'; - let data = { status: 'error' }; - testServer.setup((t) => t.createUUID(typeMoq.It.isAny())).returns(() => uuid); - const wrongUriEventData: DataReceivedEvent = { - uuid: 'incorrect-uuid456', - data: JSON.stringify(data), - }; - adapter = new PytestTestDiscoveryAdapter(testServer.object, configService, outputChannel.object); - const promise = adapter.discoverTests(uri, execFactory.object); - // const promise = adapter.discoverTests(uri); - adapter.onDataReceivedHandler(wrongUriEventData); + test('Test discovery correctly pulls pytest args from config service settings', async () => { + // set up a config service with different pytest args + const configServiceNew: IConfigurationService = ({ + getSettings: () => ({ + testing: { pytestArgs: ['.', 'abc', 'xyz'] }, + }), + } as unknown) as IConfigurationService; - data = { status: 'success' }; - const correctUriEventData: DataReceivedEvent = { - uuid, - data: JSON.stringify(data), - }; - adapter.onDataReceivedHandler(correctUriEventData); - const result = await promise; - assert.deepStrictEqual(result, data); + adapter = new PytestTestDiscoveryAdapter(testServer.object, configServiceNew, outputChannel.object); + await adapter.discoverTests(uri, execFactory.object); + const expectedArgs = ['-m', 'pytest', '-p', 'vscode_pytest', '--collect-only', '.', 'abc', 'xyz']; + execService.verify( + (x) => + x.exec( + expectedArgs, + typeMoq.It.is((options) => { + assert.deepEqual(options.extraVariables, expectedExtraVariables); + assert.equal(options.cwd, expectedPath); + assert.equal(options.throwOnStdErr, true); + return true; + }), + ), + typeMoq.Times.once(), + ); }); }); diff --git a/src/test/testing/testController/pytest/pytestExecutionAdapter.unit.test.ts b/src/test/testing/testController/pytest/pytestExecutionAdapter.unit.test.ts index ac6c6bd274a4..9f4c41ba8309 100644 --- a/src/test/testing/testController/pytest/pytestExecutionAdapter.unit.test.ts +++ b/src/test/testing/testController/pytest/pytestExecutionAdapter.unit.test.ts @@ -1,90 +1,173 @@ -// /* eslint-disable @typescript-eslint/no-explicit-any */ -// // Copyright (c) Microsoft Corporation. All rights reserved. -// // Licensed under the MIT License. -// import * as assert from 'assert'; -// import { Uri } from 'vscode'; -// import * as typeMoq from 'typemoq'; -// import { IConfigurationService } from '../../../../client/common/types'; -// import { DataReceivedEvent, ITestServer } from '../../../../client/testing/testController/common/types'; -// import { IPythonExecutionFactory, IPythonExecutionService } from '../../../../client/common/process/types'; -// import { createDeferred, Deferred } from '../../../../client/common/utils/async'; -// import { PytestTestExecutionAdapter } from '../../../../client/testing/testController/pytest/pytestExecutionAdapter'; +/* eslint-disable @typescript-eslint/no-explicit-any */ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +import * as assert from 'assert'; +import { TestRun, Uri } from 'vscode'; +import * as typeMoq from 'typemoq'; +import * as sinon from 'sinon'; +import * as path from 'path'; +import { IConfigurationService, ITestOutputChannel } from '../../../../client/common/types'; +import { ITestServer } from '../../../../client/testing/testController/common/types'; +import { + IPythonExecutionFactory, + IPythonExecutionService, + SpawnOptions, +} from '../../../../client/common/process/types'; +import { createDeferred, Deferred } from '../../../../client/common/utils/async'; +import { PytestTestExecutionAdapter } from '../../../../client/testing/testController/pytest/pytestExecutionAdapter'; +import { ITestDebugLauncher, LaunchOptions } from '../../../../client/testing/common/types'; +import * as util from '../../../../client/testing/testController/common/utils'; +import { EXTENSION_ROOT_DIR } from '../../../../client/constants'; -// suite('pytest test execution adapter', () => { -// let testServer: typeMoq.IMock; -// let configService: IConfigurationService; -// let execFactory = typeMoq.Mock.ofType(); -// let adapter: PytestTestExecutionAdapter; -// let execService: typeMoq.IMock; -// let deferred: Deferred; -// setup(() => { -// testServer = typeMoq.Mock.ofType(); -// testServer.setup((t) => t.getPort()).returns(() => 12345); -// testServer -// .setup((t) => t.onDataReceived(typeMoq.It.isAny(), typeMoq.It.isAny())) -// .returns(() => ({ -// dispose: () => { -// /* no-body */ -// }, -// })); -// configService = ({ -// getSettings: () => ({ -// testing: { pytestArgs: ['.'] }, -// }), -// isTestExecution: () => false, -// } as unknown) as IConfigurationService; -// execFactory = typeMoq.Mock.ofType(); -// execService = typeMoq.Mock.ofType(); -// execFactory -// .setup((x) => x.createActivatedEnvironment(typeMoq.It.isAny())) -// .returns(() => Promise.resolve(execService.object)); -// deferred = createDeferred(); -// execService -// .setup((x) => x.exec(typeMoq.It.isAny(), typeMoq.It.isAny())) -// .returns(() => { -// deferred.resolve(); -// return Promise.resolve({ stdout: '{}' }); -// }); -// execFactory.setup((p) => ((p as unknown) as any).then).returns(() => undefined); -// execService.setup((p) => ((p as unknown) as any).then).returns(() => undefined); -// }); -// test('onDataReceivedHandler should parse only if known UUID', async () => { -// const uri = Uri.file('/my/test/path/'); -// const uuid = 'uuid123'; -// const data = { status: 'success' }; -// testServer.setup((t) => t.createUUID(typeMoq.It.isAny())).returns(() => uuid); -// const eventData: DataReceivedEvent = { -// uuid, -// data: JSON.stringify(data), -// }; +suite('pytest test execution adapter', () => { + let testServer: typeMoq.IMock; + let configService: IConfigurationService; + let execFactory = typeMoq.Mock.ofType(); + let adapter: PytestTestExecutionAdapter; + let execService: typeMoq.IMock; + let deferred: Deferred; + let debugLauncher: typeMoq.IMock; + (global as any).EXTENSION_ROOT_DIR = EXTENSION_ROOT_DIR; + let myTestPath: string; + let startTestIdServerStub: sinon.SinonStub; -// adapter = new PytestTestExecutionAdapter(testServer.object, configService); -// const promise = adapter.runTests(uri, [], false); -// await deferred.promise; -// adapter.onDataReceivedHandler(eventData); -// const result = await promise; -// assert.deepStrictEqual(result, data); -// }); -// test('onDataReceivedHandler should not parse if it is unknown UUID', async () => { -// const uri = Uri.file('/my/test/path/'); -// const uuid = 'uuid456'; -// let data = { status: 'error' }; -// testServer.setup((t) => t.createUUID(typeMoq.It.isAny())).returns(() => uuid); -// const wrongUriEventData: DataReceivedEvent = { -// uuid: 'incorrect-uuid456', -// data: JSON.stringify(data), -// }; -// adapter = new PytestTestExecutionAdapter(testServer.object, configService); -// const promise = adapter.runTests(uri, [], false); -// adapter.onDataReceivedHandler(wrongUriEventData); + setup(() => { + testServer = typeMoq.Mock.ofType(); + testServer.setup((t) => t.getPort()).returns(() => 12345); + testServer + .setup((t) => t.onRunDataReceived(typeMoq.It.isAny(), typeMoq.It.isAny())) + .returns(() => ({ + dispose: () => { + /* no-body */ + }, + })); + configService = ({ + getSettings: () => ({ + testing: { pytestArgs: ['.'] }, + }), + isTestExecution: () => false, + } as unknown) as IConfigurationService; + execFactory = typeMoq.Mock.ofType(); + execService = typeMoq.Mock.ofType(); + debugLauncher = typeMoq.Mock.ofType(); + execFactory + .setup((x) => x.createActivatedEnvironment(typeMoq.It.isAny())) + .returns(() => Promise.resolve(execService.object)); + deferred = createDeferred(); + execService + .setup((x) => x.exec(typeMoq.It.isAny(), typeMoq.It.isAny())) + .returns(() => { + deferred.resolve(); + return Promise.resolve({ stdout: '{}' }); + }); + debugLauncher + .setup((d) => d.launchDebugger(typeMoq.It.isAny(), typeMoq.It.isAny())) + .returns(() => { + deferred.resolve(); + return Promise.resolve(); + }); + startTestIdServerStub = sinon.stub(util, 'startTestIdServer').returns(Promise.resolve(54321)); -// data = { status: 'success' }; -// const correctUriEventData: DataReceivedEvent = { -// uuid, -// data: JSON.stringify(data), -// }; -// adapter.onDataReceivedHandler(correctUriEventData); -// const result = await promise; -// assert.deepStrictEqual(result, data); -// }); -// }); + execFactory.setup((p) => ((p as unknown) as any).then).returns(() => undefined); + execService.setup((p) => ((p as unknown) as any).then).returns(() => undefined); + debugLauncher.setup((p) => ((p as unknown) as any).then).returns(() => undefined); + myTestPath = path.join('/', 'my', 'test', 'path', '/'); + }); + teardown(() => { + sinon.restore(); + }); + test('startTestIdServer called with correct testIds', async () => { + const uri = Uri.file(myTestPath); + const uuid = 'uuid123'; + testServer + .setup((t) => t.onDiscoveryDataReceived(typeMoq.It.isAny(), typeMoq.It.isAny())) + .returns(() => ({ + dispose: () => { + /* no-body */ + }, + })); + testServer.setup((t) => t.createUUID(typeMoq.It.isAny())).returns(() => uuid); + const outputChannel = typeMoq.Mock.ofType(); + const testRun = typeMoq.Mock.ofType(); + adapter = new PytestTestExecutionAdapter(testServer.object, configService, outputChannel.object); + + const testIds = ['test1id', 'test2id']; + await adapter.runTests(uri, testIds, false, testRun.object, execFactory.object); + + sinon.assert.calledWithExactly(startTestIdServerStub, testIds); + }); + test('pytest execution called with correct args', async () => { + const uri = Uri.file(myTestPath); + const uuid = 'uuid123'; + testServer + .setup((t) => t.onDiscoveryDataReceived(typeMoq.It.isAny(), typeMoq.It.isAny())) + .returns(() => ({ + dispose: () => { + /* no-body */ + }, + })); + testServer.setup((t) => t.createUUID(typeMoq.It.isAny())).returns(() => uuid); + const outputChannel = typeMoq.Mock.ofType(); + const testRun = typeMoq.Mock.ofType(); + adapter = new PytestTestExecutionAdapter(testServer.object, configService, outputChannel.object); + await adapter.runTests(uri, [], false, testRun.object, execFactory.object); + + const pathToPythonFiles = path.join(EXTENSION_ROOT_DIR, 'pythonFiles'); + const pathToPythonScript = path.join(pathToPythonFiles, 'vscode_pytest', 'run_pytest_script.py'); + const expectedArgs = [pathToPythonScript, '--rootdir', myTestPath]; + const expectedExtraVariables = { + PYTHONPATH: pathToPythonFiles, + TEST_UUID: 'uuid123', + TEST_PORT: '12345', + }; + // execService.verify((x) => x.exec(expectedArgs, typeMoq.It.isAny()), typeMoq.Times.once()); + execService.verify( + (x) => + x.exec( + expectedArgs, + typeMoq.It.is((options) => { + assert.equal(options.extraVariables?.PYTHONPATH, expectedExtraVariables.PYTHONPATH); + assert.equal(options.extraVariables?.TEST_UUID, expectedExtraVariables.TEST_UUID); + assert.equal(options.extraVariables?.TEST_PORT, expectedExtraVariables.TEST_PORT); + assert.equal(options.extraVariables?.RUN_TEST_IDS_PORT, '54321'); + assert.equal(options.cwd, uri.fsPath); + assert.equal(options.throwOnStdErr, true); + return true; + }), + ), + typeMoq.Times.once(), + ); + }); + test('Debug launched correctly for pytest', async () => { + const uri = Uri.file(myTestPath); + const uuid = 'uuid123'; + testServer + .setup((t) => t.onDiscoveryDataReceived(typeMoq.It.isAny(), typeMoq.It.isAny())) + .returns(() => ({ + dispose: () => { + /* no-body */ + }, + })); + testServer.setup((t) => t.createUUID(typeMoq.It.isAny())).returns(() => uuid); + const outputChannel = typeMoq.Mock.ofType(); + const testRun = typeMoq.Mock.ofType(); + adapter = new PytestTestExecutionAdapter(testServer.object, configService, outputChannel.object); + await adapter.runTests(uri, [], true, testRun.object, execFactory.object, debugLauncher.object); + debugLauncher.verify( + (x) => + x.launchDebugger( + typeMoq.It.is((launchOptions) => { + assert.equal(launchOptions.cwd, uri.fsPath); + assert.deepEqual(launchOptions.args, ['--rootdir', myTestPath, '--capture', 'no']); + assert.equal(launchOptions.testProvider, 'pytest'); + assert.equal(launchOptions.pytestPort, '12345'); + assert.equal(launchOptions.pytestUUID, 'uuid123'); + assert.strictEqual(launchOptions.runTestIdsPort, '54321'); + return true; + }), + typeMoq.It.isAny(), + ), + typeMoq.Times.once(), + ); + }); +}); diff --git a/src/test/testing/testController/resultResolver.unit.test.ts b/src/test/testing/testController/resultResolver.unit.test.ts new file mode 100644 index 000000000000..57b321c2e36c --- /dev/null +++ b/src/test/testing/testController/resultResolver.unit.test.ts @@ -0,0 +1,368 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +import { TestController, Uri, TestItem, CancellationToken, TestRun, TestItemCollection, Range } from 'vscode'; +import * as typemoq from 'typemoq'; +import * as sinon from 'sinon'; +import { TestProvider } from '../../../client/testing/types'; +import { + DiscoveredTestNode, + DiscoveredTestPayload, + ExecutionTestPayload, +} from '../../../client/testing/testController/common/types'; +import * as testItemUtilities from '../../../client/testing/testController/common/testItemUtilities'; +import * as ResultResolver from '../../../client/testing/testController/common/resultResolver'; +import * as util from '../../../client/testing/testController/common/utils'; + +suite('Result Resolver tests', () => { + suite('Test discovery', () => { + let resultResolver: ResultResolver.PythonResultResolver; + let testController: TestController; + const log: string[] = []; + let workspaceUri: Uri; + let testProvider: TestProvider; + let defaultErrorMessage: string; + let blankTestItem: TestItem; + let cancelationToken: CancellationToken; + + setup(() => { + testController = ({ + items: { + get: () => { + log.push('get'); + }, + add: () => { + log.push('add'); + }, + replace: () => { + log.push('replace'); + }, + delete: () => { + log.push('delete'); + }, + }, + + dispose: () => { + // empty + }, + } as unknown) as TestController; + defaultErrorMessage = 'pytest test discovery error (see Output > Python)'; + blankTestItem = ({ + canResolveChildren: false, + tags: [], + children: { + add: () => { + // empty + }, + }, + } as unknown) as TestItem; + cancelationToken = ({ + isCancellationRequested: false, + } as unknown) as CancellationToken; + }); + teardown(() => { + sinon.restore(); + }); + + test('resolveDiscovery calls populate test tree correctly', async () => { + // test specific constants used expected values + testProvider = 'pytest'; + workspaceUri = Uri.file('/foo/bar'); + resultResolver = new ResultResolver.PythonResultResolver(testController, testProvider, workspaceUri); + const tests: DiscoveredTestNode = { + path: 'path', + name: 'name', + type_: 'folder', + id_: 'id', + children: [], + }; + const payload: DiscoveredTestPayload = { + cwd: workspaceUri.fsPath, + status: 'success', + tests, + }; + + // stub out functionality of populateTestTreeStub which is called in resolveDiscovery + const populateTestTreeStub = sinon.stub(util, 'populateTestTree').returns(); + + // call resolve discovery + resultResolver.resolveDiscovery(payload, cancelationToken); + + // assert the stub functions were called with the correct parameters + + // header of populateTestTree is (testController: TestController, testTreeData: DiscoveredTestNode, testRoot: TestItem | undefined, resultResolver: ITestResultResolver, token?: CancellationToken) + sinon.assert.calledWithMatch( + populateTestTreeStub, + testController, // testController + tests, // testTreeData + undefined, // testRoot + resultResolver, // resultResolver + cancelationToken, // token + ); + }); + // what about if the error node already exists: this.testController.items.get(`DiscoveryError:${workspacePath}`); + test('resolveDiscovery should create error node on error with correct params', async () => { + // test specific constants used expected values + testProvider = 'pytest'; + workspaceUri = Uri.file('/foo/bar'); + resultResolver = new ResultResolver.PythonResultResolver(testController, testProvider, workspaceUri); + const errorMessage = 'error msg A'; + const expectedErrorMessage = `${defaultErrorMessage}\r\n ${errorMessage}`; + + // stub out return values of functions called in resolveDiscovery + const payload: DiscoveredTestPayload = { + cwd: workspaceUri.fsPath, + status: 'error', + errors: [errorMessage], + }; + const errorTestItemOptions: testItemUtilities.ErrorTestItemOptions = { + id: 'id', + label: 'label', + error: 'error', + }; + + // stub out functionality of buildErrorNodeOptions and createErrorTestItem which are called in resolveDiscovery + const buildErrorNodeOptionsStub = sinon.stub(util, 'buildErrorNodeOptions').returns(errorTestItemOptions); + const createErrorTestItemStub = sinon.stub(testItemUtilities, 'createErrorTestItem').returns(blankTestItem); + + // call resolve discovery + resultResolver.resolveDiscovery(payload); + + // assert the stub functions were called with the correct parameters + + // header of buildErrorNodeOptions is (uri: Uri, message: string, testType: string) + sinon.assert.calledWithMatch(buildErrorNodeOptionsStub, workspaceUri, expectedErrorMessage, testProvider); + // header of createErrorTestItem is (options: ErrorTestItemOptions, testController: TestController, uri: Uri) + sinon.assert.calledWithMatch(createErrorTestItemStub, sinon.match.any, sinon.match.any); + }); + }); + suite('Test execution result resolver', () => { + let resultResolver: ResultResolver.PythonResultResolver; + const log: string[] = []; + let workspaceUri: Uri; + let testProvider: TestProvider; + let cancelationToken: CancellationToken; + let runInstance: typemoq.IMock; + let testControllerMock: typemoq.IMock; + let mockTestItem1: TestItem; + let mockTestItem2: TestItem; + + setup(() => { + // create mock test items + mockTestItem1 = createMockTestItem('mockTestItem1'); + mockTestItem2 = createMockTestItem('mockTestItem2'); + + // create mock testItems to pass into a iterable + const mockTestItems: [string, TestItem][] = [ + ['1', mockTestItem1], + ['2', mockTestItem2], + ]; + const iterableMock = mockTestItems[Symbol.iterator](); + + // create mock testItemCollection + const testItemCollectionMock = typemoq.Mock.ofType(); + testItemCollectionMock + .setup((x) => x.forEach(typemoq.It.isAny())) + .callback((callback) => { + let result = iterableMock.next(); + while (!result.done) { + callback(result.value[1]); + result = iterableMock.next(); + } + }) + .returns(() => mockTestItem1); + + // create mock testController + testControllerMock = typemoq.Mock.ofType(); + testControllerMock.setup((t) => t.items).returns(() => testItemCollectionMock.object); + + cancelationToken = ({ + isCancellationRequested: false, + } as unknown) as CancellationToken; + + // define functions within runInstance + runInstance = typemoq.Mock.ofType(); + runInstance.setup((r) => r.name).returns(() => 'name'); + runInstance.setup((r) => r.token).returns(() => cancelationToken); + runInstance.setup((r) => r.isPersisted).returns(() => true); + runInstance + .setup((r) => r.enqueued(typemoq.It.isAny())) + .returns(() => { + // empty + log.push('enqueue'); + return undefined; + }); + runInstance + .setup((r) => r.started(typemoq.It.isAny())) + .returns(() => { + // empty + log.push('start'); + }); + + // mock getTestCaseNodes to just return the given testNode added + sinon.stub(testItemUtilities, 'getTestCaseNodes').callsFake((testNode: TestItem) => [testNode]); + }); + teardown(() => { + sinon.restore(); + }); + test('resolveExecution handles failed tests correctly', async () => { + // test specific constants used expected values + testProvider = 'pytest'; + workspaceUri = Uri.file('/foo/bar'); + resultResolver = new ResultResolver.PythonResultResolver( + testControllerMock.object, + testProvider, + workspaceUri, + ); + // add a mock test item to the map of known VSCode ids to run ids + resultResolver.runIdToVSid.set('mockTestItem1', 'mockTestItem1'); + resultResolver.runIdToVSid.set('mockTestItem2', 'mockTestItem2'); + + // add this mock test to the map of known test items + resultResolver.runIdToTestItem.set('mockTestItem1', mockTestItem1); + resultResolver.runIdToTestItem.set('mockTestItem2', mockTestItem2); + + // create a successful payload with a single test called mockTestItem1 + const successPayload: ExecutionTestPayload = { + cwd: workspaceUri.fsPath, + status: 'success', + result: { + mockTestItem1: { + test: 'test', + outcome: 'failure', // failure, passed-unexpected, skipped, success, expected-failure, subtest-failure, subtest-succcess + message: 'message', + traceback: 'traceback', + subtest: 'subtest', + }, + }, + error: '', + }; + + // call resolveExecution + resultResolver.resolveExecution(successPayload, runInstance.object); + + // verify that the passed function was called for the single test item + runInstance.verify((r) => r.failed(typemoq.It.isAny(), typemoq.It.isAny()), typemoq.Times.once()); + }); + test('resolveExecution handles skipped correctly', async () => { + // test specific constants used expected values + testProvider = 'pytest'; + workspaceUri = Uri.file('/foo/bar'); + resultResolver = new ResultResolver.PythonResultResolver( + testControllerMock.object, + testProvider, + workspaceUri, + ); + // add a mock test item to the map of known VSCode ids to run ids + resultResolver.runIdToVSid.set('mockTestItem1', 'mockTestItem1'); + resultResolver.runIdToVSid.set('mockTestItem2', 'mockTestItem2'); + + // add this mock test to the map of known test items + resultResolver.runIdToTestItem.set('mockTestItem1', mockTestItem1); + resultResolver.runIdToTestItem.set('mockTestItem2', mockTestItem2); + + // create a successful payload with a single test called mockTestItem1 + const successPayload: ExecutionTestPayload = { + cwd: workspaceUri.fsPath, + status: 'success', + result: { + mockTestItem1: { + test: 'test', + outcome: 'skipped', // failure, passed-unexpected, skipped, success, expected-failure, subtest-failure, subtest-succcess + message: 'message', + traceback: 'traceback', + subtest: 'subtest', + }, + }, + error: '', + }; + + // call resolveExecution + resultResolver.resolveExecution(successPayload, runInstance.object); + + // verify that the passed function was called for the single test item + runInstance.verify((r) => r.skipped(typemoq.It.isAny()), typemoq.Times.once()); + }); + test('resolveExecution handles success correctly', async () => { + // test specific constants used expected values + testProvider = 'pytest'; + workspaceUri = Uri.file('/foo/bar'); + resultResolver = new ResultResolver.PythonResultResolver( + testControllerMock.object, + testProvider, + workspaceUri, + ); + // add a mock test item to the map of known VSCode ids to run ids + resultResolver.runIdToVSid.set('mockTestItem1', 'mockTestItem1'); + resultResolver.runIdToVSid.set('mockTestItem2', 'mockTestItem2'); + + // add this mock test to the map of known test items + resultResolver.runIdToTestItem.set('mockTestItem1', mockTestItem1); + resultResolver.runIdToTestItem.set('mockTestItem2', mockTestItem2); + + // create a successful payload with a single test called mockTestItem1 + const successPayload: ExecutionTestPayload = { + cwd: workspaceUri.fsPath, + status: 'success', + result: { + mockTestItem1: { + test: 'test', + outcome: 'success', // failure, passed-unexpected, skipped, success, expected-failure, subtest-failure, subtest-succcess + message: 'message', + traceback: 'traceback', + subtest: 'subtest', + }, + }, + error: '', + }; + + // call resolveExecution + resultResolver.resolveExecution(successPayload, runInstance.object); + + // verify that the passed function was called for the single test item + runInstance.verify((r) => r.passed(typemoq.It.isAny()), typemoq.Times.once()); + }); + test('resolveExecution handles error correctly', async () => { + // test specific constants used expected values + testProvider = 'pytest'; + workspaceUri = Uri.file('/foo/bar'); + resultResolver = new ResultResolver.PythonResultResolver( + testControllerMock.object, + testProvider, + workspaceUri, + ); + + const errorPayload: ExecutionTestPayload = { + cwd: workspaceUri.fsPath, + status: 'error', + error: 'error', + }; + + resultResolver.resolveExecution(errorPayload, runInstance.object); + + // verify that none of these functions are called + + runInstance.verify((r) => r.passed(typemoq.It.isAny()), typemoq.Times.never()); + runInstance.verify((r) => r.failed(typemoq.It.isAny(), typemoq.It.isAny()), typemoq.Times.never()); + runInstance.verify((r) => r.skipped(typemoq.It.isAny()), typemoq.Times.never()); + }); + }); +}); + +function createMockTestItem(id: string): TestItem { + const range = new Range(0, 0, 0, 0); + const mockTestItem = ({ + id, + canResolveChildren: false, + tags: [], + children: { + add: () => { + // empty + }, + }, + range, + uri: Uri.file('/foo/bar'), + } as unknown) as TestItem; + + return mockTestItem; +} diff --git a/src/test/testing/testController/server.unit.test.ts b/src/test/testing/testController/server.unit.test.ts index d7b3a242ee9a..38b71992aefb 100644 --- a/src/test/testing/testController/server.unit.test.ts +++ b/src/test/testing/testController/server.unit.test.ts @@ -1,158 +1,349 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -import * as assert from 'assert'; -import * as net from 'net'; -import * as sinon from 'sinon'; -import * as crypto from 'crypto'; -import { OutputChannel, Uri } from 'vscode'; -import { IPythonExecutionFactory, IPythonExecutionService } from '../../../client/common/process/types'; -import { PythonTestServer } from '../../../client/testing/testController/common/server'; -import { ITestDebugLauncher } from '../../../client/testing/common/types'; -import { createDeferred } from '../../../client/common/utils/async'; - -suite('Python Test Server', () => { - const fakeUuid = 'fake-uuid'; - - let stubExecutionFactory: IPythonExecutionFactory; - let stubExecutionService: IPythonExecutionService; - let server: PythonTestServer; - let sandbox: sinon.SinonSandbox; - let execArgs: string[]; - let v4Stub: sinon.SinonStub; - let debugLauncher: ITestDebugLauncher; - - setup(() => { - sandbox = sinon.createSandbox(); - v4Stub = sandbox.stub(crypto, 'randomUUID'); - - v4Stub.returns(fakeUuid); - stubExecutionService = ({ - exec: (args: string[]) => { - execArgs = args; - return Promise.resolve({ stdout: '', stderr: '' }); - }, - } as unknown) as IPythonExecutionService; - - stubExecutionFactory = ({ - createActivatedEnvironment: () => Promise.resolve(stubExecutionService), - } as unknown) as IPythonExecutionFactory; - }); - - teardown(() => { - sandbox.restore(); - execArgs = []; - server.dispose(); - }); - - test('sendCommand should add the port to the command being sent', async () => { - const options = { - command: { script: 'myscript', args: ['-foo', 'foo'] }, - workspaceFolder: Uri.file('/foo/bar'), - cwd: '/foo/bar', - uuid: fakeUuid, - }; - - server = new PythonTestServer(stubExecutionFactory, debugLauncher); - await server.serverReady(); - - await server.sendCommand(options); - const port = server.getPort(); - - assert.deepStrictEqual(execArgs, ['myscript', '--port', `${port}`, '--uuid', fakeUuid, '-foo', 'foo']); - }); - - test('sendCommand should write to an output channel if it is provided as an option', async () => { - const output: string[] = []; - const outChannel = { - appendLine: (str: string) => { - output.push(str); - }, - } as OutputChannel; - const options = { - command: { script: 'myscript', args: ['-foo', 'foo'] }, - workspaceFolder: Uri.file('/foo/bar'), - cwd: '/foo/bar', - uuid: fakeUuid, - outChannel, - }; - - server = new PythonTestServer(stubExecutionFactory, debugLauncher); - await server.serverReady(); - - await server.sendCommand(options); - - const port = server.getPort(); - const expected = ['python', 'myscript', '--port', `${port}`, '--uuid', fakeUuid, '-foo', 'foo'].join(' '); - - assert.deepStrictEqual(output, [expected]); - }); - - test('If script execution fails during sendCommand, an onDataReceived event should be fired with the "error" status', async () => { - let eventData: { status: string; errors: string[] }; - stubExecutionService = ({ - exec: () => { - throw new Error('Failed to execute'); - }, - } as unknown) as IPythonExecutionService; - - const options = { - command: { script: 'myscript', args: ['-foo', 'foo'] }, - workspaceFolder: Uri.file('/foo/bar'), - cwd: '/foo/bar', - uuid: fakeUuid, - }; - - server = new PythonTestServer(stubExecutionFactory, debugLauncher); - await server.serverReady(); - - server.onDataReceived(({ data }) => { - eventData = JSON.parse(data); - }); - - await server.sendCommand(options); - - assert.deepStrictEqual(eventData!.status, 'error'); - assert.deepStrictEqual(eventData!.errors, ['Failed to execute']); - }); - - test('If the server receives malformed data, it should display a log message, and not fire an event', async () => { - let eventData: string | undefined; - const client = new net.Socket(); - const deferred = createDeferred(); - - const options = { - command: { script: 'myscript', args: ['-foo', 'foo'] }, - workspaceFolder: Uri.file('/foo/bar'), - cwd: '/foo/bar', - uuid: fakeUuid, - }; - - stubExecutionService = ({ - exec: async () => { - client.connect(server.getPort()); - return Promise.resolve({ stdout: '', stderr: '' }); - }, - } as unknown) as IPythonExecutionService; - - server = new PythonTestServer(stubExecutionFactory, debugLauncher); - await server.serverReady(); - server.onDataReceived(({ data }) => { - eventData = data; - deferred.resolve(); - }); - - client.on('connect', () => { - console.log('Socket connected, local port:', client.localPort); - client.write('malformed data'); - client.end(); - }); - client.on('error', (error) => { - console.log('Socket connection error:', error); - }); - - await server.sendCommand(options); - await deferred.promise; - assert.deepStrictEqual(eventData, ''); - }); -}); +// // Copyright (c) Microsoft Corporation. All rights reserved. +// // Licensed under the MIT License. + +// import * as assert from 'assert'; +// import * as net from 'net'; +// import * as sinon from 'sinon'; +// import * as crypto from 'crypto'; +// import { Uri } from 'vscode'; +// import { IPythonExecutionFactory, IPythonExecutionService } from '../../../client/common/process/types'; +// import { PythonTestServer } from '../../../client/testing/testController/common/server'; +// import { ITestDebugLauncher } from '../../../client/testing/common/types'; +// import { createDeferred } from '../../../client/common/utils/async'; + +// suite('Python Test Server', () => { +// const fakeUuid = 'fake-uuid'; + +// let stubExecutionFactory: IPythonExecutionFactory; +// let stubExecutionService: IPythonExecutionService; +// let server: PythonTestServer; +// let sandbox: sinon.SinonSandbox; +// let execArgs: string[]; +// let v4Stub: sinon.SinonStub; +// let debugLauncher: ITestDebugLauncher; + +// setup(() => { +// sandbox = sinon.createSandbox(); +// v4Stub = sandbox.stub(crypto, 'randomUUID'); + +// v4Stub.returns(fakeUuid); +// stubExecutionService = ({ +// exec: (args: string[]) => { +// execArgs = args; +// return Promise.resolve({ stdout: '', stderr: '' }); +// }, +// } as unknown) as IPythonExecutionService; + +// stubExecutionFactory = ({ +// createActivatedEnvironment: () => Promise.resolve(stubExecutionService), +// } as unknown) as IPythonExecutionFactory; +// }); + +// teardown(() => { +// sandbox.restore(); +// execArgs = []; +// server.dispose(); +// }); + +// // test('sendCommand should add the port to the command being sent', async () => { +// // const options = { +// // command: { script: 'myscript', args: ['-foo', 'foo'] }, +// // workspaceFolder: Uri.file('/foo/bar'), +// // cwd: '/foo/bar', +// // uuid: fakeUuid, +// // }; + +// // server = new PythonTestServer(stubExecutionFactory, debugLauncher); +// // await server.serverReady(); + +// // await server.sendCommand(options); +// // const port = server.getPort(); + +// // assert.deepStrictEqual(execArgs, ['myscript', '--port', `${port}`, '--uuid', fakeUuid, '-foo', 'foo']); +// // }); + +// // test('sendCommand should write to an output channel if it is provided as an option', async () => { +// // const output: string[] = []; +// // const outChannel = { +// // appendLine: (str: string) => { +// // output.push(str); +// // }, +// // } as OutputChannel; +// // const options = { +// // command: { script: 'myscript', args: ['-foo', 'foo'] }, +// // workspaceFolder: Uri.file('/foo/bar'), +// // cwd: '/foo/bar', +// // uuid: fakeUuid, +// // outChannel, +// // }; + +// // server = new PythonTestServer(stubExecutionFactory, debugLauncher); +// // await server.serverReady(); + +// // await server.sendCommand(options); + +// // const port = server.getPort(); +// // const expected = ['python', 'myscript', '--port', `${port}`, '--uuid', fakeUuid, '-foo', 'foo'].join(' '); + +// // assert.deepStrictEqual(output, [expected]); +// // }); + +// // test('If script execution fails during sendCommand, an onDataReceived event should be fired with the "error" status', async () => { +// // let eventData: { status: string; errors: string[] }; +// // stubExecutionService = ({ +// // exec: () => { +// // throw new Error('Failed to execute'); +// // }, +// // } as unknown) as IPythonExecutionService; + +// // const options = { +// // command: { script: 'myscript', args: ['-foo', 'foo'] }, +// // workspaceFolder: Uri.file('/foo/bar'), +// // cwd: '/foo/bar', +// // uuid: fakeUuid, +// // }; + +// // server = new PythonTestServer(stubExecutionFactory, debugLauncher); +// // await server.serverReady(); + +// // server.onDataReceived(({ data }) => { +// // eventData = JSON.parse(data); +// // }); + +// // await server.sendCommand(options); + +// // assert.deepStrictEqual(eventData!.status, 'error'); +// // assert.deepStrictEqual(eventData!.errors, ['Failed to execute']); +// // }); + +// // test('If the server receives malformed data, it should display a log message, and not fire an event', async () => { +// // let eventData: string | undefined; +// // const client = new net.Socket(); +// // const deferred = createDeferred(); + +// // const options = { +// // command: { script: 'myscript', args: ['-foo', 'foo'] }, +// // workspaceFolder: Uri.file('/foo/bar'), +// // cwd: '/foo/bar', +// // uuid: fakeUuid, +// // }; + +// // stubExecutionService = ({ +// // exec: async () => { +// // client.connect(server.getPort()); +// // return Promise.resolve({ stdout: '', stderr: '' }); +// // }, +// // } as unknown) as IPythonExecutionService; + +// // server = new PythonTestServer(stubExecutionFactory, debugLauncher); +// // await server.serverReady(); +// // server.onDataReceived(({ data }) => { +// // eventData = data; +// // deferred.resolve(); +// // }); + +// // client.on('connect', () => { +// // console.log('Socket connected, local port:', client.localPort); +// // client.write('malformed data'); +// // client.end(); +// // }); +// // client.on('error', (error) => { +// // console.log('Socket connection error:', error); +// // }); + +// // await server.sendCommand(options); +// // await deferred.promise; +// // assert.deepStrictEqual(eventData, ''); +// // }); + +// // test('If the server doesnt recognize the UUID it should ignore it', async () => { +// // let eventData: string | undefined; +// // const client = new net.Socket(); +// // const deferred = createDeferred(); + +// // const options = { +// // command: { script: 'myscript', args: ['-foo', 'foo'] }, +// // workspaceFolder: Uri.file('/foo/bar'), +// // cwd: '/foo/bar', +// // uuid: fakeUuid, +// // }; + +// // stubExecutionService = ({ +// // exec: async () => { +// // client.connect(server.getPort()); +// // return Promise.resolve({ stdout: '', stderr: '' }); +// // }, +// // } as unknown) as IPythonExecutionService; + +// // server = new PythonTestServer(stubExecutionFactory, debugLauncher); +// // await server.serverReady(); +// // server.onDataReceived(({ data }) => { +// // eventData = data; +// // deferred.resolve(); +// // }); + +// // client.on('connect', () => { +// // console.log('Socket connected, local port:', client.localPort); +// // client.write('{"Request-uuid": "unknown-uuid"}'); +// // client.end(); +// // }); +// // client.on('error', (error) => { +// // console.log('Socket connection error:', error); +// // }); + +// // await server.sendCommand(options); +// // await deferred.promise; +// // assert.deepStrictEqual(eventData, ''); +// // }); + +// // required to have "tests" or "results" +// // the heading length not being equal and yes being equal +// // multiple payloads +// // test('Error if payload does not have a content length header', async () => { +// // let eventData: string | undefined; +// // const client = new net.Socket(); +// // const deferred = createDeferred(); + +// // const options = { +// // command: { script: 'myscript', args: ['-foo', 'foo'] }, +// // workspaceFolder: Uri.file('/foo/bar'), +// // cwd: '/foo/bar', +// // uuid: fakeUuid, +// // }; + +// // stubExecutionService = ({ +// // exec: async () => { +// // client.connect(server.getPort()); +// // return Promise.resolve({ stdout: '', stderr: '' }); +// // }, +// // } as unknown) as IPythonExecutionService; + +// // server = new PythonTestServer(stubExecutionFactory, debugLauncher); +// // await server.serverReady(); +// // server.onDataReceived(({ data }) => { +// // eventData = data; +// // deferred.resolve(); +// // }); + +// // client.on('connect', () => { +// // console.log('Socket connected, local port:', client.localPort); +// // client.write('{"not content length": "5"}'); +// // client.end(); +// // }); +// // client.on('error', (error) => { +// // console.log('Socket connection error:', error); +// // }); + +// // await server.sendCommand(options); +// // await deferred.promise; +// // assert.deepStrictEqual(eventData, ''); +// // }); + +// const testData = [ +// { +// testName: 'fires discovery correctly on test payload', +// payload: `Content-Length: 52 +// Content-Type: application/json +// Request-uuid: UUID_HERE + +// {"cwd": "path", "status": "success", "tests": "xyz"}`, +// expectedResult: '{"cwd": "path", "status": "success", "tests": "xyz"}', +// }, +// // Add more test data as needed +// ]; + +// testData.forEach(({ testName, payload, expectedResult }) => { +// test(`test: ${testName}`, async () => { +// // Your test logic here +// let eventData: string | undefined; +// const client = new net.Socket(); +// const deferred = createDeferred(); + +// const options = { +// command: { script: 'myscript', args: ['-foo', 'foo'] }, +// workspaceFolder: Uri.file('/foo/bar'), +// cwd: '/foo/bar', +// uuid: fakeUuid, +// }; + +// stubExecutionService = ({ +// exec: async () => { +// client.connect(server.getPort()); +// return Promise.resolve({ stdout: '', stderr: '' }); +// }, +// } as unknown) as IPythonExecutionService; + +// server = new PythonTestServer(stubExecutionFactory, debugLauncher); +// await server.serverReady(); +// const uuid = server.createUUID(); +// payload = payload.replace('UUID_HERE', uuid); +// server.onDiscoveryDataReceived(({ data }) => { +// eventData = data; +// deferred.resolve(); +// }); + +// client.on('connect', () => { +// console.log('Socket connected, local port:', client.localPort); +// client.write(payload); +// client.end(); +// }); +// client.on('error', (error) => { +// console.log('Socket connection error:', error); +// }); + +// await server.sendCommand(options); +// await deferred.promise; +// assert.deepStrictEqual(eventData, expectedResult); +// }); +// }); + +// test('Calls run resolver if the result header is in the payload', async () => { +// let eventData: string | undefined; +// const client = new net.Socket(); +// const deferred = createDeferred(); + +// const options = { +// command: { script: 'myscript', args: ['-foo', 'foo'] }, +// workspaceFolder: Uri.file('/foo/bar'), +// cwd: '/foo/bar', +// uuid: fakeUuid, +// }; + +// stubExecutionService = ({ +// exec: async () => { +// client.connect(server.getPort()); +// return Promise.resolve({ stdout: '', stderr: '' }); +// }, +// } as unknown) as IPythonExecutionService; + +// server = new PythonTestServer(stubExecutionFactory, debugLauncher); +// await server.serverReady(); +// const uuid = server.createUUID(); +// server.onRunDataReceived(({ data }) => { +// eventData = data; +// deferred.resolve(); +// }); + +// const payload = `Content-Length: 87 +// Content-Type: application/json +// Request-uuid: ${uuid} + +// {"cwd": "path", "status": "success", "result": "xyz", "not_found": null, "error": null}`; + +// client.on('connect', () => { +// console.log('Socket connected, local port:', client.localPort); +// client.write(payload); +// client.end(); +// }); +// client.on('error', (error) => { +// console.log('Socket connection error:', error); +// }); + +// await server.sendCommand(options); +// await deferred.promise; +// console.log('event data', eventData); +// const expectedResult = +// '{"cwd": "path", "status": "success", "result": "xyz", "not_found": null, "error": null}'; +// assert.deepStrictEqual(eventData, expectedResult); +// }); +// }); diff --git a/src/test/testing/testController/unittest/testDiscoveryAdapter.unit.test.ts b/src/test/testing/testController/unittest/testDiscoveryAdapter.unit.test.ts index 3d3521291f74..ef21655e93e4 100644 --- a/src/test/testing/testController/unittest/testDiscoveryAdapter.unit.test.ts +++ b/src/test/testing/testController/unittest/testDiscoveryAdapter.unit.test.ts @@ -23,7 +23,7 @@ suite('Unittest test discovery adapter', () => { outputChannel = typemoq.Mock.ofType(); }); - test('discoverTests should send the discovery command to the test server', async () => { + test('DiscoverTests should send the discovery command to the test server with the correct args', async () => { let options: TestCommandOptions | undefined; const stubTestServer = ({ @@ -32,7 +32,7 @@ suite('Unittest test discovery adapter', () => { options = opt; return Promise.resolve(); }, - onDataReceived: () => { + onDiscoveryDataReceived: () => { // no body }, createUUID: () => '123456789', @@ -47,61 +47,11 @@ suite('Unittest test discovery adapter', () => { assert.deepStrictEqual(options, { workspaceFolder: uri, cwd: uri.fsPath, - command: { script, args: ['--udiscovery', '-v', '-s', '.', '-p', 'test*'] }, + command: { + script, + args: ['--udiscovery', '-v', '-s', '.', '-p', 'test*'], + }, uuid: '123456789', }); }); - - test("onDataReceivedHandler should parse the data if the cwd from the payload matches the test adapter's cwd", async () => { - const stubTestServer = ({ - sendCommand(): Promise { - return Promise.resolve(); - }, - onDataReceived: () => { - // no body - }, - createUUID: () => '123456789', - } as unknown) as ITestServer; - - const uri = Uri.file('/foo/bar'); - const data = { status: 'success' }; - const uuid = '123456789'; - const adapter = new UnittestTestDiscoveryAdapter(stubTestServer, stubConfigSettings, outputChannel.object); - const promise = adapter.discoverTests(uri); - - adapter.onDataReceivedHandler({ uuid, data: JSON.stringify(data) }); - - const result = await promise; - - assert.deepStrictEqual(result, data); - }); - - test("onDataReceivedHandler should ignore the data if the cwd from the payload does not match the test adapter's cwd", async () => { - const correctUuid = '123456789'; - const incorrectUuid = '987654321'; - const stubTestServer = ({ - sendCommand(): Promise { - return Promise.resolve(); - }, - onDataReceived: () => { - // no body - }, - createUUID: () => correctUuid, - } as unknown) as ITestServer; - - const uri = Uri.file('/foo/bar'); - - const adapter = new UnittestTestDiscoveryAdapter(stubTestServer, stubConfigSettings, outputChannel.object); - const promise = adapter.discoverTests(uri); - - const data = { status: 'success' }; - adapter.onDataReceivedHandler({ uuid: incorrectUuid, data: JSON.stringify(data) }); - - const nextData = { status: 'error' }; - adapter.onDataReceivedHandler({ uuid: correctUuid, data: JSON.stringify(nextData) }); - - const result = await promise; - - assert.deepStrictEqual(result, nextData); - }); }); diff --git a/src/test/testing/testController/unittest/testExecutionAdapter.unit.test.ts b/src/test/testing/testController/unittest/testExecutionAdapter.unit.test.ts index e5495629bf28..88126225a177 100644 --- a/src/test/testing/testController/unittest/testExecutionAdapter.unit.test.ts +++ b/src/test/testing/testController/unittest/testExecutionAdapter.unit.test.ts @@ -1,118 +1,65 @@ -// // Copyright (c) Microsoft Corporation. All rights reserved. -// // Licensed under the MIT License. - -// import * as assert from 'assert'; -// import * as path from 'path'; -// import * as typemoq from 'typemoq'; -// import { Uri } from 'vscode'; -// import { IConfigurationService, ITestOutputChannel } from '../../../../client/common/types'; -// import { EXTENSION_ROOT_DIR } from '../../../../client/constants'; -// import { ITestServer, TestCommandOptions } from '../../../../client/testing/testController/common/types'; -// import { UnittestTestExecutionAdapter } from '../../../../client/testing/testController/unittest/testExecutionAdapter'; - -// suite('Unittest test execution adapter', () => { -// let stubConfigSettings: IConfigurationService; -// let outputChannel: typemoq.IMock; - -// setup(() => { -// stubConfigSettings = ({ -// getSettings: () => ({ -// testing: { unittestArgs: ['-v', '-s', '.', '-p', 'test*'] }, -// }), -// } as unknown) as IConfigurationService; -// outputChannel = typemoq.Mock.ofType(); -// }); - -// test('runTests should send the run command to the test server', async () => { -// let options: TestCommandOptions | undefined; - -// const stubTestServer = ({ -// sendCommand(opt: TestCommandOptions, runTestIdPort?: string): Promise { -// delete opt.outChannel; -// options = opt; -// assert(runTestIdPort !== undefined); -// return Promise.resolve(); -// }, -// onDataReceived: () => { -// // no body -// }, -// createUUID: () => '123456789', -// } as unknown) as ITestServer; - -// const uri = Uri.file('/foo/bar'); -// const script = path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'unittestadapter', 'execution.py'); - -// const adapter = new UnittestTestExecutionAdapter(stubTestServer, stubConfigSettings, outputChannel.object); -// adapter.runTests(uri, [], false).then(() => { -// const expectedOptions: TestCommandOptions = { -// workspaceFolder: uri, -// command: { script, args: ['--udiscovery', '-v', '-s', '.', '-p', 'test*'] }, -// cwd: uri.fsPath, -// uuid: '123456789', -// debugBool: false, -// testIds: [], -// }; -// assert.deepStrictEqual(options, expectedOptions); -// }); -// }); -// test("onDataReceivedHandler should parse the data if the cwd from the payload matches the test adapter's cwd", async () => { -// const stubTestServer = ({ -// sendCommand(): Promise { -// return Promise.resolve(); -// }, -// onDataReceived: () => { -// // no body -// }, -// createUUID: () => '123456789', -// } as unknown) as ITestServer; - -// const uri = Uri.file('/foo/bar'); -// const data = { status: 'success' }; -// const uuid = '123456789'; - -// const adapter = new UnittestTestExecutionAdapter(stubTestServer, stubConfigSettings, outputChannel.object); - -// // triggers runTests flow which will run onDataReceivedHandler and the -// // promise resolves into the parsed data. -// const promise = adapter.runTests(uri, [], false); - -// adapter.onDataReceivedHandler({ uuid, data: JSON.stringify(data) }); - -// const result = await promise; - -// assert.deepStrictEqual(result, data); -// }); -// test("onDataReceivedHandler should ignore the data if the cwd from the payload does not match the test adapter's cwd", async () => { -// const correctUuid = '123456789'; -// const incorrectUuid = '987654321'; -// const stubTestServer = ({ -// sendCommand(): Promise { -// return Promise.resolve(); -// }, -// onDataReceived: () => { -// // no body -// }, -// createUUID: () => correctUuid, -// } as unknown) as ITestServer; - -// const uri = Uri.file('/foo/bar'); - -// const adapter = new UnittestTestExecutionAdapter(stubTestServer, stubConfigSettings, outputChannel.object); - -// // triggers runTests flow which will run onDataReceivedHandler and the -// // promise resolves into the parsed data. -// const promise = adapter.runTests(uri, [], false); - -// const data = { status: 'success' }; -// // will not resolve due to incorrect UUID -// adapter.onDataReceivedHandler({ uuid: incorrectUuid, data: JSON.stringify(data) }); - -// const nextData = { status: 'error' }; -// // will resolve and nextData will be returned as result -// adapter.onDataReceivedHandler({ uuid: correctUuid, data: JSON.stringify(nextData) }); - -// const result = await promise; - -// assert.deepStrictEqual(result, nextData); -// }); -// }); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +import * as assert from 'assert'; +import * as path from 'path'; +import * as typemoq from 'typemoq'; +import { Uri } from 'vscode'; +import * as sinon from 'sinon'; +import { IConfigurationService, ITestOutputChannel } from '../../../../client/common/types'; +import { EXTENSION_ROOT_DIR } from '../../../../client/constants'; +import { ITestServer, TestCommandOptions } from '../../../../client/testing/testController/common/types'; +import { UnittestTestExecutionAdapter } from '../../../../client/testing/testController/unittest/testExecutionAdapter'; +import * as util from '../../../../client/testing/testController/common/utils'; + +suite('Unittest test execution adapter', () => { + let stubConfigSettings: IConfigurationService; + let outputChannel: typemoq.IMock; + + setup(() => { + stubConfigSettings = ({ + getSettings: () => ({ + testing: { unittestArgs: ['-v', '-s', '.', '-p', 'test*'] }, + }), + } as unknown) as IConfigurationService; + outputChannel = typemoq.Mock.ofType(); + sinon.stub(util, 'startTestIdServer').returns(Promise.resolve(54321)); + }); + teardown(() => { + sinon.restore(); + }); + + test('runTests should send the run command to the test server', async () => { + let options: TestCommandOptions | undefined; + + const stubTestServer = ({ + sendCommand(opt: TestCommandOptions, runTestIdPort?: string): Promise { + delete opt.outChannel; + options = opt; + assert(runTestIdPort !== undefined); + return Promise.resolve(); + }, + onRunDataReceived: () => { + // no body + }, + createUUID: () => '123456789', + } as unknown) as ITestServer; + + const uri = Uri.file('/foo/bar'); + const script = path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'unittestadapter', 'execution.py'); + + const adapter = new UnittestTestExecutionAdapter(stubTestServer, stubConfigSettings, outputChannel.object); + const testIds = ['test1id', 'test2id']; + adapter.runTests(uri, testIds, false).then(() => { + const expectedOptions: TestCommandOptions = { + workspaceFolder: uri, + command: { script, args: ['--udiscovery', '-v', '-s', '.', '-p', 'test*'] }, + cwd: uri.fsPath, + uuid: '123456789', + debugBool: false, + testIds, + }; + assert.deepStrictEqual(options, expectedOptions); + }); + }); +}); diff --git a/src/test/testing/testController/workspaceTestAdapter.unit.test.ts b/src/test/testing/testController/workspaceTestAdapter.unit.test.ts index 539647aece9f..5a2e48130746 100644 --- a/src/test/testing/testController/workspaceTestAdapter.unit.test.ts +++ b/src/test/testing/testController/workspaceTestAdapter.unit.test.ts @@ -5,19 +5,23 @@ import * as assert from 'assert'; import * as sinon from 'sinon'; import * as typemoq from 'typemoq'; -import { TestController, TestItem, Uri } from 'vscode'; +import { TestController, TestItem, TestItemCollection, TestRun, Uri } from 'vscode'; import { IConfigurationService, ITestOutputChannel } from '../../../client/common/types'; import { UnittestTestDiscoveryAdapter } from '../../../client/testing/testController/unittest/testDiscoveryAdapter'; import { UnittestTestExecutionAdapter } from '../../../client/testing/testController/unittest/testExecutionAdapter'; // 7/7 import { WorkspaceTestAdapter } from '../../../client/testing/testController/workspaceTestAdapter'; import * as Telemetry from '../../../client/telemetry'; import { EventName } from '../../../client/telemetry/constants'; -import { ITestServer } from '../../../client/testing/testController/common/types'; +import { ITestResultResolver, ITestServer } from '../../../client/testing/testController/common/types'; +import * as testItemUtilities from '../../../client/testing/testController/common/testItemUtilities'; +import * as util from '../../../client/testing/testController/common/utils'; +import * as ResultResolver from '../../../client/testing/testController/common/resultResolver'; suite('Workspace test adapter', () => { suite('Test discovery', () => { let stubTestServer: ITestServer; let stubConfigSettings: IConfigurationService; + let stubResultResolver: ITestResultResolver; let discoverTestsStub: sinon.SinonStub; let sendTelemetryStub: sinon.SinonStub; @@ -29,8 +33,6 @@ suite('Workspace test adapter', () => { let testController: TestController; let log: string[] = []; - const sandbox = sinon.createSandbox(); - setup(() => { stubConfigSettings = ({ getSettings: () => ({ @@ -47,6 +49,19 @@ suite('Workspace test adapter', () => { }, } as unknown) as ITestServer; + stubResultResolver = ({ + resolveDiscovery: () => { + // no body + }, + resolveExecution: () => { + // no body + }, + } as unknown) as ITestResultResolver; + + // 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. @@ -97,8 +112,8 @@ suite('Workspace test adapter', () => { }); }; - discoverTestsStub = sandbox.stub(UnittestTestDiscoveryAdapter.prototype, 'discoverTests'); - sendTelemetryStub = sandbox.stub(Telemetry, 'sendTelemetryEvent').callsFake(mockSendTelemetryEvent); + discoverTestsStub = sinon.stub(UnittestTestDiscoveryAdapter.prototype, 'discoverTests'); + sendTelemetryStub = sinon.stub(Telemetry, 'sendTelemetryEvent').callsFake(mockSendTelemetryEvent); outputChannel = typemoq.Mock.ofType(); }); @@ -106,7 +121,54 @@ suite('Workspace test adapter', () => { telemetryEvent = []; log = []; testController.dispose(); - sandbox.restore(); + sinon.restore(); + }); + + test('If discovery failed correctly create error node', async () => { + discoverTestsStub.rejects(new Error('foo')); + + 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 blankTestItem = ({ + canResolveChildren: false, + tags: [], + children: { + add: () => { + // empty + }, + }, + } as unknown) as TestItem; + const errorTestItemOptions: testItemUtilities.ErrorTestItemOptions = { + id: 'id', + label: 'label', + error: 'error', + }; + const createErrorTestItemStub = sinon.stub(testItemUtilities, 'createErrorTestItem').returns(blankTestItem); + const buildErrorNodeOptionsStub = sinon.stub(util, 'buildErrorNodeOptions').returns(errorTestItemOptions); + const testProvider = 'unittest'; + + const abc = await workspaceTestAdapter.discoverTests(testController); + console.log(abc); + + sinon.assert.calledWithMatch(createErrorTestItemStub, sinon.match.any, sinon.match.any); + 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 () => { @@ -127,6 +189,7 @@ suite('Workspace test adapter', () => { testDiscoveryAdapter, testExecutionAdapter, Uri.parse('foo'), + stubResultResolver, ); await workspaceTestAdapter.discoverTests(testController); @@ -160,6 +223,7 @@ suite('Workspace test adapter', () => { testDiscoveryAdapter, testExecutionAdapter, Uri.parse('foo'), + stubResultResolver, ); // Try running discovery twice @@ -190,6 +254,7 @@ suite('Workspace test adapter', () => { testDiscoveryAdapter, testExecutionAdapter, Uri.parse('foo'), + stubResultResolver, ); await workspaceTestAdapter.discoverTests(testController); @@ -220,6 +285,7 @@ suite('Workspace test adapter', () => { testDiscoveryAdapter, testExecutionAdapter, Uri.parse('foo'), + stubResultResolver, ); await workspaceTestAdapter.discoverTests(testController); @@ -229,18 +295,322 @@ suite('Workspace test adapter', () => { const lastEvent = telemetryEvent[1]; assert.ok(lastEvent.properties.failed); + }); + }); + suite('Test execution workspace test adapter', () => { + let stubTestServer: ITestServer; + let stubConfigSettings: IConfigurationService; + let stubResultResolver: ITestResultResolver; + let executionTestsStub: sinon.SinonStub; + let sendTelemetryStub: sinon.SinonStub; + let outputChannel: typemoq.IMock; + let runInstance: typemoq.IMock; + let testControllerMock: typemoq.IMock; + let telemetryEvent: { eventName: EventName; properties: Record }[] = []; + let resultResolver: ResultResolver.PythonResultResolver; + + // Stubbed test controller (see comment around L.40) + let testController: TestController; + let log: string[] = []; + + const sandbox = sinon.createSandbox(); + + setup(() => { + stubConfigSettings = ({ + getSettings: () => ({ + testing: { unittestArgs: ['--foo'] }, + }), + } as unknown) as IConfigurationService; + + stubTestServer = ({ + sendCommand(): Promise { + return Promise.resolve(); + }, + onDataReceived: () => { + // no body + }, + } as unknown) as ITestServer; - assert.deepStrictEqual(log, ['createTestItem', 'add']); + stubResultResolver = ({ + resolveDiscovery: () => { + // no body + }, + resolveExecution: () => { + // no body + }, + vsIdToRunId: { + get: sinon.stub().returns('expectedRunId'), + }, + } as unknown) as ITestResultResolver; + 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; + + const mockSendTelemetryEvent = ( + eventName: EventName, + _: number | Record | undefined, + properties: unknown, + ) => { + telemetryEvent.push({ + eventName, + properties: properties as Record, + }); + }; + + executionTestsStub = sandbox.stub(UnittestTestExecutionAdapter.prototype, 'runTests'); + sendTelemetryStub = sandbox.stub(Telemetry, 'sendTelemetryEvent').callsFake(mockSendTelemetryEvent); + outputChannel = typemoq.Mock.ofType(); + runInstance = typemoq.Mock.ofType(); + + const testProvider = 'pytest'; + const workspaceUri = Uri.file('foo'); + resultResolver = new ResultResolver.PythonResultResolver(testController, testProvider, workspaceUri); }); - /** - * TODO To test: - * - successful discovery but no data: delete everything from the test controller - * - successful discovery with error status: add error node to tree - * - single root: populate tree if there's no root node - * - single root: update tree if there's a root node - * - single root: delete tree if there are no tests in the test data - * - multiroot: update the correct folders - */ + teardown(() => { + telemetryEvent = []; + log = []; + testController.dispose(); + sandbox.restore(); + }); + test('When executing tests, the right tests should be sent to be executed', async () => { + 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'), + resultResolver, + ); + resultResolver.runIdToVSid.set('mockTestItem1', 'mockTestItem1'); + + sinon.stub(testItemUtilities, 'getTestCaseNodes').callsFake((testNode: TestItem) => + // Custom implementation logic here based on the provided testNode and collection + + // Example implementation: returning a predefined array of TestItem objects + [testNode], + ); + + const mockTestItem1 = createMockTestItem('mockTestItem1'); + const mockTestItem2 = createMockTestItem('mockTestItem2'); + const mockTestItems: [string, TestItem][] = [ + ['1', mockTestItem1], + ['2', mockTestItem2], + // Add as many mock TestItems as needed + ]; + const iterableMock = mockTestItems[Symbol.iterator](); + + const testItemCollectionMock = typemoq.Mock.ofType(); + + testItemCollectionMock + .setup((x) => x.forEach(typemoq.It.isAny())) + .callback((callback) => { + let result = iterableMock.next(); + while (!result.done) { + callback(result.value[1]); + result = iterableMock.next(); + } + }) + .returns(() => mockTestItem1); + testControllerMock = typemoq.Mock.ofType(); + testControllerMock.setup((t) => t.items).returns(() => testItemCollectionMock.object); + + await workspaceTestAdapter.executeTests(testController, runInstance.object, [mockTestItem1, mockTestItem2]); + + runInstance.verify((r) => r.started(typemoq.It.isAny()), typemoq.Times.exactly(2)); + }); + + test("When executing tests, the workspace test adapter should call the test execute adapter's executionTest method", async () => { + 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.executeTests(testController, runInstance.object, []); + + sinon.assert.calledOnce(executionTestsStub); + }); + + test('If execution is already running, do not call executionAdapter.runTests again', async () => { + executionTestsStub.callsFake( + async () => + new Promise((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.executeTests(testController, runInstance.object, []); + const two = workspaceTestAdapter.executeTests(testController, runInstance.object, []); + + Promise.all([one, two]); + + sinon.assert.calledOnce(executionTestsStub); + }); + + test('If execution failed correctly create error node', async () => { + executionTestsStub.rejects(new Error('foo')); + + 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 blankTestItem = ({ + canResolveChildren: false, + tags: [], + children: { + add: () => { + // empty + }, + }, + } as unknown) as TestItem; + const errorTestItemOptions: testItemUtilities.ErrorTestItemOptions = { + id: 'id', + label: 'label', + error: 'error', + }; + const createErrorTestItemStub = sinon.stub(testItemUtilities, 'createErrorTestItem').returns(blankTestItem); + const buildErrorNodeOptionsStub = sinon.stub(util, 'buildErrorNodeOptions').returns(errorTestItemOptions); + const testProvider = 'unittest'; + + await workspaceTestAdapter.executeTests(testController, runInstance.object, []); + + sinon.assert.calledWithMatch(createErrorTestItemStub, sinon.match.any, sinon.match.any); + sinon.assert.calledWithMatch(buildErrorNodeOptionsStub, Uri.parse('foo'), sinon.match.any, testProvider); + }); + + test('If execution failed, send a telemetry event with the "failed" key set to true, and add an error node to the test controller', async () => { + executionTestsStub.rejects(new Error('foo')); + + 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.executeTests(testController, runInstance.object, []); + + sinon.assert.calledWith(sendTelemetryStub, EventName.UNITTEST_RUN_ALL_FAILED); + assert.strictEqual(telemetryEvent.length, 1); + }); }); }); + +function createMockTestItem(id: string): TestItem { + const range = typemoq.Mock.ofType(); + const mockTestItem = ({ + id, + canResolveChildren: false, + tags: [], + children: { + add: () => { + // empty + }, + }, + range, + uri: Uri.file('/foo/bar'), + } as unknown) as TestItem; + + return mockTestItem; +} diff --git a/src/test/vscode-mock.ts b/src/test/vscode-mock.ts index ebbe7ca59e72..44518e7575a7 100644 --- a/src/test/vscode-mock.ts +++ b/src/test/vscode-mock.ts @@ -70,6 +70,8 @@ mockedVSCode.Hover = vscodeMocks.Hover; mockedVSCode.Disposable = vscodeMocks.Disposable as any; mockedVSCode.ExtensionKind = vscodeMocks.ExtensionKind; mockedVSCode.CodeAction = vscodeMocks.CodeAction; +mockedVSCode.TestMessage = vscodeMocks.TestMessage; +mockedVSCode.Location = vscodeMocks.Location; mockedVSCode.EventEmitter = vscodeMocks.EventEmitter; mockedVSCode.CancellationTokenSource = vscodeMocks.CancellationTokenSource; mockedVSCode.CompletionItemKind = vscodeMocks.CompletionItemKind;