From bf4a524a2437e960d8f18c9dc038a3d1e1a5c852 Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Thu, 25 May 2023 14:19:15 -0700 Subject: [PATCH 01/20] initial progress --- .../testDiscoveryAdapter.unit.test.ts | 56 +++++++++---------- .../testExecutionAdapter.unit.test.ts | 26 ++++----- 2 files changed, 41 insertions(+), 41 deletions(-) diff --git a/src/test/testing/testController/unittest/testDiscoveryAdapter.unit.test.ts b/src/test/testing/testController/unittest/testDiscoveryAdapter.unit.test.ts index ef21655e93e4..579bce59ac06 100644 --- a/src/test/testing/testController/unittest/testDiscoveryAdapter.unit.test.ts +++ b/src/test/testing/testController/unittest/testDiscoveryAdapter.unit.test.ts @@ -1,27 +1,27 @@ -// 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 { UnittestTestDiscoveryAdapter } from '../../../../client/testing/testController/unittest/testDiscoveryAdapter'; - -suite('Unittest test discovery 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(); - }); +// // 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 { UnittestTestDiscoveryAdapter } from '../../../../client/testing/testController/unittest/testDiscoveryAdapter'; + +// suite('Unittest test discovery 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('DiscoverTests should send the discovery command to the test server with the correct args', async () => { let options: TestCommandOptions | undefined; @@ -38,11 +38,11 @@ suite('Unittest test discovery adapter', () => { createUUID: () => '123456789', } as unknown) as ITestServer; - const uri = Uri.file('/foo/bar'); - const script = path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'unittestadapter', 'discovery.py'); +// const uri = Uri.file('/foo/bar'); +// const script = path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'unittestadapter', 'discovery.py'); - const adapter = new UnittestTestDiscoveryAdapter(stubTestServer, stubConfigSettings, outputChannel.object); - adapter.discoverTests(uri); +// const adapter = new UnittestTestDiscoveryAdapter(stubTestServer, stubConfigSettings, outputChannel.object); +// adapter.discoverTests(uri); assert.deepStrictEqual(options, { workspaceFolder: uri, diff --git a/src/test/testing/testController/unittest/testExecutionAdapter.unit.test.ts b/src/test/testing/testController/unittest/testExecutionAdapter.unit.test.ts index e5495629bf28..7e48ad3b690c 100644 --- a/src/test/testing/testController/unittest/testExecutionAdapter.unit.test.ts +++ b/src/test/testing/testController/unittest/testExecutionAdapter.unit.test.ts @@ -27,10 +27,9 @@ // let options: TestCommandOptions | undefined; // const stubTestServer = ({ -// sendCommand(opt: TestCommandOptions, runTestIdPort?: string): Promise { +// sendCommand(opt: TestCommandOptions): Promise { // delete opt.outChannel; // options = opt; -// assert(runTestIdPort !== undefined); // return Promise.resolve(); // }, // onDataReceived: () => { @@ -43,17 +42,18 @@ // 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); -// }); +// adapter.runTests(uri, [], false); + +// 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 = ({ From 389432fcb435ce25db7dee3bb8a21de37aeb2145 Mon Sep 17 00:00:00 2001 From: Eleanor Boyd Date: Thu, 25 May 2023 13:41:55 -0700 Subject: [PATCH 02/20] Set up testing rewrite experiment (#21258) is the beginning of this issue: https://github.com/microsoft/vscode-python/issues/21150, in that it will start the process of implementing the setting in the extension --- src/client/testing/testController/controller.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/client/testing/testController/controller.ts b/src/client/testing/testController/controller.ts index eff333a4cdd9..c3cab7734369 100644 --- a/src/client/testing/testController/controller.ts +++ b/src/client/testing/testController/controller.ts @@ -48,6 +48,8 @@ import { WorkspaceTestAdapter } from './workspaceTestAdapter'; import { ITestDebugLauncher } from '../common/types'; import { IServiceContainer } from '../../ioc/types'; import { PythonResultResolver } from './common/resultResolver'; +import { pythonTestAdapterRewriteEnabled } from './common/utils'; +import { IServiceContainer } from '../../ioc/types'; // Types gymnastics to make sure that sendTriggerTelemetry only accepts the correct types. type EventPropertyType = IEventNamePropertyMapping[EventName.UNITTEST_DISCOVERY_TRIGGER]; From a4aab4b1f68d151c1733863305d77a5b116338ab Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Thu, 25 May 2023 14:19:15 -0700 Subject: [PATCH 03/20] initial progress --- src/client/testing/testController/controller.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/client/testing/testController/controller.ts b/src/client/testing/testController/controller.ts index c3cab7734369..eff333a4cdd9 100644 --- a/src/client/testing/testController/controller.ts +++ b/src/client/testing/testController/controller.ts @@ -48,8 +48,6 @@ import { WorkspaceTestAdapter } from './workspaceTestAdapter'; import { ITestDebugLauncher } from '../common/types'; import { IServiceContainer } from '../../ioc/types'; import { PythonResultResolver } from './common/resultResolver'; -import { pythonTestAdapterRewriteEnabled } from './common/utils'; -import { IServiceContainer } from '../../ioc/types'; // Types gymnastics to make sure that sendTriggerTelemetry only accepts the correct types. type EventPropertyType = IEventNamePropertyMapping[EventName.UNITTEST_DISCOVERY_TRIGGER]; From 1d3ba5efc75c3c3706fa272a87387f747e3bdd4e Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Fri, 26 May 2023 14:47:54 -0700 Subject: [PATCH 04/20] server and workspace tests added --- .../testController/server.unit.test.ts | 574 +++++++++--------- .../workspaceTestAdapter.unit.test.ts | 371 ++++++----- 2 files changed, 499 insertions(+), 446 deletions(-) diff --git a/src/test/testing/testController/server.unit.test.ts b/src/test/testing/testController/server.unit.test.ts index 38b71992aefb..f4dfca5163f2 100644 --- a/src/test/testing/testController/server.unit.test.ts +++ b/src/test/testing/testController/server.unit.test.ts @@ -45,81 +45,262 @@ // 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(); + // 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'] }, @@ -135,215 +316,34 @@ // // }, // // } 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); -// }); -// }); + 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/workspaceTestAdapter.unit.test.ts b/src/test/testing/testController/workspaceTestAdapter.unit.test.ts index 42e38d200546..e4fafa63b74d 100644 --- a/src/test/testing/testController/workspaceTestAdapter.unit.test.ts +++ b/src/test/testing/testController/workspaceTestAdapter.unit.test.ts @@ -1,9 +1,9 @@ -// // Copyright (c) Microsoft Corporation. All rights reserved. -// // Licensed under the MIT License. +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. -// import * as assert from 'assert'; -// import * as sinon from 'sinon'; -// import * as typemoq from 'typemoq'; +import * as assert from 'assert'; +import * as sinon from 'sinon'; +import * as typemoq from 'typemoq'; // import { TestController, TestItem, Uri } from 'vscode'; // import { IConfigurationService, ITestOutputChannel } from '../../../client/common/types'; @@ -20,33 +20,33 @@ // let stubConfigSettings: IConfigurationService; // let stubResultResolver: ITestResultResolver; -// let discoverTestsStub: sinon.SinonStub; -// let sendTelemetryStub: sinon.SinonStub; -// let outputChannel: typemoq.IMock; + let discoverTestsStub: sinon.SinonStub; + let sendTelemetryStub: sinon.SinonStub; + let outputChannel: typemoq.IMock; -// let telemetryEvent: { eventName: EventName; properties: Record }[] = []; + let telemetryEvent: { eventName: EventName; properties: Record }[] = []; -// // Stubbed test controller (see comment around L.40) -// let testController: TestController; -// let log: string[] = []; + // Stubbed test controller (see comment around L.40) + let testController: TestController; + let log: string[] = []; -// const sandbox = sinon.createSandbox(); + const sandbox = sinon.createSandbox(); -// setup(() => { -// stubConfigSettings = ({ -// getSettings: () => ({ -// testing: { unittestArgs: ['--foo'] }, -// }), -// } as unknown) as IConfigurationService; + setup(() => { + stubConfigSettings = ({ + getSettings: () => ({ + testing: { unittestArgs: ['--foo'] }, + }), + } as unknown) as IConfigurationService; -// stubTestServer = ({ -// sendCommand(): Promise { -// return Promise.resolve(); -// }, -// onDataReceived: () => { -// // no body -// }, -// } as unknown) as ITestServer; + stubTestServer = ({ + sendCommand(): Promise { + return Promise.resolve(); + }, + onDataReceived: () => { + // no body + }, + } as unknown) as ITestServer; // stubResultResolver = ({ // resolveDiscovery: () => { @@ -77,57 +77,64 @@ // }, // } as unknown) as TestItem; -// testController = ({ -// items: { -// get: () => { -// log.push('get'); -// }, -// add: () => { -// log.push('add'); -// }, -// replace: () => { -// log.push('replace'); -// }, -// delete: () => { -// log.push('delete'); -// }, -// }, -// createTestItem: () => { -// log.push('createTestItem'); -// return testItem; -// }, -// dispose: () => { -// // empty -// }, -// } as unknown) as TestController; - -// // testController = tests.createTestController('mock-python-tests', 'Mock Python Tests'); - -// const mockSendTelemetryEvent = ( -// eventName: EventName, -// _: number | Record | undefined, -// properties: unknown, -// ) => { -// telemetryEvent.push({ -// eventName, -// properties: properties as Record, -// }); -// }; - -// discoverTestsStub = sandbox.stub(UnittestTestDiscoveryAdapter.prototype, 'discoverTests'); -// sendTelemetryStub = sandbox.stub(Telemetry, 'sendTelemetryEvent').callsFake(mockSendTelemetryEvent); -// outputChannel = typemoq.Mock.ofType(); -// }); - -// teardown(() => { -// telemetryEvent = []; -// log = []; -// testController.dispose(); -// sandbox.restore(); -// }); - -// test("When discovering tests, the workspace test adapter should call the test discovery adapter's discoverTest method", async () => { -// discoverTestsStub.resolves(); + // const vsIdToRunIdGetStub = sinon.stub(stubResultResolver.vsIdToRunId, 'get'); + // const expectedRunId = 'expectedRunId'; + // vsIdToRunIdGetStub.withArgs(sinon.match.any).returns(expectedRunId); + + // For some reason the 'tests' namespace in vscode returns undefined. + // While I figure out how to expose to the tests, they will run + // against a stub test controller and stub test items. + const testItem = ({ + canResolveChildren: false, + tags: [], + children: { + add: () => { + // empty + }, + }, + } as unknown) as TestItem; + + testController = ({ + items: { + get: () => { + log.push('get'); + }, + add: () => { + log.push('add'); + }, + replace: () => { + log.push('replace'); + }, + delete: () => { + log.push('delete'); + }, + }, + createTestItem: () => { + log.push('createTestItem'); + return testItem; + }, + dispose: () => { + // empty + }, + } as unknown) as TestController; + + // testController = tests.createTestController('mock-python-tests', 'Mock Python Tests'); + + const mockSendTelemetryEvent = ( + eventName: EventName, + _: number | Record | undefined, + properties: unknown, + ) => { + telemetryEvent.push({ + eventName, + properties: properties as Record, + }); + }; + + discoverTestsStub = sandbox.stub(UnittestTestDiscoveryAdapter.prototype, 'discoverTests'); + sendTelemetryStub = sandbox.stub(Telemetry, 'sendTelemetryEvent').callsFake(mockSendTelemetryEvent); + outputChannel = typemoq.Mock.ofType(); + }); // const testDiscoveryAdapter = new UnittestTestDiscoveryAdapter( // stubTestServer, @@ -147,21 +154,28 @@ // stubResultResolver, // ); -// await workspaceTestAdapter.discoverTests(testController); - -// sinon.assert.calledOnce(discoverTestsStub); -// }); - -// test('If discovery is already running, do not call discoveryAdapter.discoverTests again', async () => { -// discoverTestsStub.callsFake( -// async () => -// new Promise((resolve) => { -// setTimeout(() => { -// // Simulate time taken by discovery. -// resolve(); -// }, 2000); -// }), -// ); + test("When discovering tests, the workspace test adapter should call the test discovery adapter's discoverTest method", async () => { + discoverTestsStub.resolves(); + + const testDiscoveryAdapter = new UnittestTestDiscoveryAdapter( + stubTestServer, + stubConfigSettings, + outputChannel.object, + ); + const testExecutionAdapter = new UnittestTestExecutionAdapter( + stubTestServer, + stubConfigSettings, + outputChannel.object, + ); + const workspaceTestAdapter = new WorkspaceTestAdapter( + 'unittest', + testDiscoveryAdapter, + testExecutionAdapter, + Uri.parse('foo'), + stubResultResolver, + ); + + await workspaceTestAdapter.discoverTests(testController); // const testDiscoveryAdapter = new UnittestTestDiscoveryAdapter( // stubTestServer, @@ -181,28 +195,43 @@ // stubResultResolver, // ); -// // Try running discovery twice -// const one = workspaceTestAdapter.discoverTests(testController); -// const two = workspaceTestAdapter.discoverTests(testController); - -// Promise.all([one, two]); - -// sinon.assert.calledOnce(discoverTestsStub); -// }); - -// test('If discovery succeeds, send a telemetry event with the "failed" key set to false', async () => { -// discoverTestsStub.resolves({ status: 'success' }); - -// const testDiscoveryAdapter = new UnittestTestDiscoveryAdapter( -// stubTestServer, -// stubConfigSettings, -// outputChannel.object, -// ); -// const testExecutionAdapter = new UnittestTestExecutionAdapter( -// stubTestServer, -// stubConfigSettings, -// outputChannel.object, -// ); + test('If discovery is already running, do not call discoveryAdapter.discoverTests again', async () => { + discoverTestsStub.callsFake( + async () => + new Promise((resolve) => { + setTimeout(() => { + // Simulate time taken by discovery. + resolve(); + }, 2000); + }), + ); + + const testDiscoveryAdapter = new UnittestTestDiscoveryAdapter( + stubTestServer, + stubConfigSettings, + outputChannel.object, + ); + const testExecutionAdapter = new UnittestTestExecutionAdapter( + stubTestServer, + stubConfigSettings, + outputChannel.object, + ); + const workspaceTestAdapter = new WorkspaceTestAdapter( + 'unittest', + testDiscoveryAdapter, + testExecutionAdapter, + Uri.parse('foo'), + stubResultResolver, + ); + + // Try running discovery twice + const one = workspaceTestAdapter.discoverTests(testController); + const two = workspaceTestAdapter.discoverTests(testController); + + Promise.all([one, two]); + + sinon.assert.calledOnce(discoverTestsStub); + }); // const workspaceTestAdapter = new WorkspaceTestAdapter( // 'unittest', @@ -212,28 +241,33 @@ // stubResultResolver, // ); -// await workspaceTestAdapter.discoverTests(testController); - -// sinon.assert.calledWith(sendTelemetryStub, EventName.UNITTEST_DISCOVERY_DONE); -// assert.strictEqual(telemetryEvent.length, 2); - -// const lastEvent = telemetryEvent[1]; -// assert.strictEqual(lastEvent.properties.failed, false); -// }); - -// test('If discovery failed, send a telemetry event with the "failed" key set to true, and add an error node to the test controller', async () => { -// discoverTestsStub.rejects(new Error('foo')); - -// const testDiscoveryAdapter = new UnittestTestDiscoveryAdapter( -// stubTestServer, -// stubConfigSettings, -// outputChannel.object, -// ); -// const testExecutionAdapter = new UnittestTestExecutionAdapter( -// stubTestServer, -// stubConfigSettings, -// outputChannel.object, -// ); + const testDiscoveryAdapter = new UnittestTestDiscoveryAdapter( + stubTestServer, + stubConfigSettings, + outputChannel.object, + ); + const testExecutionAdapter = new UnittestTestExecutionAdapter( + stubTestServer, + stubConfigSettings, + outputChannel.object, + ); + + const workspaceTestAdapter = new WorkspaceTestAdapter( + 'unittest', + testDiscoveryAdapter, + testExecutionAdapter, + Uri.parse('foo'), + stubResultResolver, + ); + + await workspaceTestAdapter.discoverTests(testController); + + sinon.assert.calledWith(sendTelemetryStub, EventName.UNITTEST_DISCOVERY_DONE); + assert.strictEqual(telemetryEvent.length, 2); + + const lastEvent = telemetryEvent[1]; + assert.strictEqual(lastEvent.properties.failed, false); + }); // const workspaceTestAdapter = new WorkspaceTestAdapter( // 'unittest', @@ -243,25 +277,44 @@ // stubResultResolver, // ); -// await workspaceTestAdapter.discoverTests(testController); - -// sinon.assert.calledWith(sendTelemetryStub, EventName.UNITTEST_DISCOVERY_DONE); -// assert.strictEqual(telemetryEvent.length, 2); - -// const lastEvent = telemetryEvent[1]; -// assert.ok(lastEvent.properties.failed); - -// assert.deepStrictEqual(log, ['createTestItem', 'add']); -// }); - -// /** -// * 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 -// */ -// }); -// }); + const testDiscoveryAdapter = new UnittestTestDiscoveryAdapter( + stubTestServer, + stubConfigSettings, + outputChannel.object, + ); + const testExecutionAdapter = new UnittestTestExecutionAdapter( + stubTestServer, + stubConfigSettings, + outputChannel.object, + ); + + const workspaceTestAdapter = new WorkspaceTestAdapter( + 'unittest', + testDiscoveryAdapter, + testExecutionAdapter, + Uri.parse('foo'), + stubResultResolver, + ); + + await workspaceTestAdapter.discoverTests(testController); + + sinon.assert.calledWith(sendTelemetryStub, EventName.UNITTEST_DISCOVERY_DONE); + assert.strictEqual(telemetryEvent.length, 2); + + const lastEvent = telemetryEvent[1]; + assert.ok(lastEvent.properties.failed); + + assert.deepStrictEqual(log, ['createTestItem', 'add']); + }); + + /** + * 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 + */ + }); +}); From c5eaf2767357ff91e006f2161c127f4943a36b46 Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Wed, 7 Jun 2023 10:04:53 -0700 Subject: [PATCH 05/20] editing to fix merge etc --- .../testing/testController/unittest/testExecutionAdapter.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/client/testing/testController/unittest/testExecutionAdapter.ts b/src/client/testing/testController/unittest/testExecutionAdapter.ts index a85490c30bc8..5f8367cbd950 100644 --- a/src/client/testing/testController/unittest/testExecutionAdapter.ts +++ b/src/client/testing/testController/unittest/testExecutionAdapter.ts @@ -3,6 +3,7 @@ import * as path from 'path'; import { TestRun, Uri } from 'vscode'; +import * as net from 'net'; import { IConfigurationService, ITestOutputChannel } from '../../../common/types'; import { Deferred, createDeferred } from '../../../common/utils/async'; import { EXTENSION_ROOT_DIR } from '../../../constants'; From 98c36faaae2c58e352ed15021ee4d6536331e03e Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Wed, 7 Jun 2023 17:15:54 -0700 Subject: [PATCH 06/20] execution for pytest and discovery for unittest --- .../testDiscoveryAdapter.unit.test.ts | 56 ++--- .../testExecutionAdapter.unit.test.ts | 233 +++++++++--------- 2 files changed, 143 insertions(+), 146 deletions(-) diff --git a/src/test/testing/testController/unittest/testDiscoveryAdapter.unit.test.ts b/src/test/testing/testController/unittest/testDiscoveryAdapter.unit.test.ts index 579bce59ac06..ef21655e93e4 100644 --- a/src/test/testing/testController/unittest/testDiscoveryAdapter.unit.test.ts +++ b/src/test/testing/testController/unittest/testDiscoveryAdapter.unit.test.ts @@ -1,27 +1,27 @@ -// // 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 { UnittestTestDiscoveryAdapter } from '../../../../client/testing/testController/unittest/testDiscoveryAdapter'; - -// suite('Unittest test discovery 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(); -// }); +// 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 { UnittestTestDiscoveryAdapter } from '../../../../client/testing/testController/unittest/testDiscoveryAdapter'; + +suite('Unittest test discovery 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('DiscoverTests should send the discovery command to the test server with the correct args', async () => { let options: TestCommandOptions | undefined; @@ -38,11 +38,11 @@ createUUID: () => '123456789', } as unknown) as ITestServer; -// const uri = Uri.file('/foo/bar'); -// const script = path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'unittestadapter', 'discovery.py'); + const uri = Uri.file('/foo/bar'); + const script = path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'unittestadapter', 'discovery.py'); -// const adapter = new UnittestTestDiscoveryAdapter(stubTestServer, stubConfigSettings, outputChannel.object); -// adapter.discoverTests(uri); + const adapter = new UnittestTestDiscoveryAdapter(stubTestServer, stubConfigSettings, outputChannel.object); + adapter.discoverTests(uri); assert.deepStrictEqual(options, { workspaceFolder: uri, diff --git a/src/test/testing/testController/unittest/testExecutionAdapter.unit.test.ts b/src/test/testing/testController/unittest/testExecutionAdapter.unit.test.ts index 7e48ad3b690c..7006b61d8c7d 100644 --- a/src/test/testing/testController/unittest/testExecutionAdapter.unit.test.ts +++ b/src/test/testing/testController/unittest/testExecutionAdapter.unit.test.ts @@ -1,118 +1,115 @@ -// // 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): Promise { -// delete opt.outChannel; -// options = opt; -// 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); - -// 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 { 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): Promise { + delete opt.outChannel; + options = opt; + return Promise.resolve(); + }, + 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); + + 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); + // }); +}); From 3afd98b8743fa1fb0001f49a92c4afc680689400 Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Thu, 8 Jun 2023 10:50:49 -0700 Subject: [PATCH 07/20] execution adapter changes --- .../pytestExecutionAdapter.unit.test.ts | 278 +++++++++--------- 1 file changed, 139 insertions(+), 139 deletions(-) diff --git a/src/test/testing/testController/pytest/pytestExecutionAdapter.unit.test.ts b/src/test/testing/testController/pytest/pytestExecutionAdapter.unit.test.ts index 5e7a32ddc9e1..15f555b37299 100644 --- a/src/test/testing/testController/pytest/pytestExecutionAdapter.unit.test.ts +++ b/src/test/testing/testController/pytest/pytestExecutionAdapter.unit.test.ts @@ -1,141 +1,141 @@ -// /* 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 { 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'; +/* 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 { 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'; -// 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; -// 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(); -// }); -// 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); -// }); -// test('pytest execution called with correct args', async () => { -// const uri = Uri.file('/my/test/path/'); -// const uuid = 'uuid123'; -// // const data = { status: 'success' }; -// 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); +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; + 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(); + }); + 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); + }); + test('pytest execution called with correct args', async () => { + const uri = Uri.file('/my/test/path/'); + const uuid = 'uuid123'; + // const data = { status: 'success' }; + 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 expectedArgs = [ -// '/Users/eleanorboyd/vscode-python/pythonFiles/vscode_pytest/run_pytest_script.py', -// '--rootdir', -// '/my/test/path/', -// ]; -// const expectedExtraVariables = { -// PYTHONPATH: '/Users/eleanorboyd/vscode-python/pythonFiles', -// TEST_UUID: 'uuid123', -// TEST_PORT: '12345', -// }; -// 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.strictEqual(typeof options.extraVariables?.RUN_TEST_IDS_PORT, 'string'); -// 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('/my/test/path/'); -// 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', '/my/test/path/', '--capture', 'no']); -// assert.equal(launchOptions.testProvider, 'pytest'); -// assert.equal(launchOptions.pytestPort, '12345'); -// assert.equal(launchOptions.pytestUUID, 'uuid123'); -// assert.strictEqual(typeof launchOptions.runTestIdsPort, 'string'); -// return true; -// }), -// typeMoq.It.isAny(), -// ), -// typeMoq.Times.once(), -// ); -// }); -// }); + const expectedArgs = [ + '/Users/eleanorboyd/vscode-python/pythonFiles/vscode_pytest/run_pytest_script.py', + '--rootdir', + '/my/test/path/', + ]; + const expectedExtraVariables = { + PYTHONPATH: '/Users/eleanorboyd/vscode-python/pythonFiles', + TEST_UUID: 'uuid123', + TEST_PORT: '12345', + }; + 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.strictEqual(typeof options.extraVariables?.RUN_TEST_IDS_PORT, 'string'); + 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('/my/test/path/'); + 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', '/my/test/path/', '--capture', 'no']); + assert.equal(launchOptions.testProvider, 'pytest'); + assert.equal(launchOptions.pytestPort, '12345'); + assert.equal(launchOptions.pytestUUID, 'uuid123'); + assert.strictEqual(typeof launchOptions.runTestIdsPort, 'string'); + return true; + }), + typeMoq.It.isAny(), + ), + typeMoq.Times.once(), + ); + }); +}); From 3698d57696a1af1bc29b5f61beda6c4736f5226d Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Thu, 8 Jun 2023 10:57:47 -0700 Subject: [PATCH 08/20] remove changes to test execution from this commit --- .../unittest/testExecutionAdapter.ts | 94 ++++++++++++------- 1 file changed, 61 insertions(+), 33 deletions(-) diff --git a/src/client/testing/testController/unittest/testExecutionAdapter.ts b/src/client/testing/testController/unittest/testExecutionAdapter.ts index 5f8367cbd950..bd05988bb369 100644 --- a/src/client/testing/testController/unittest/testExecutionAdapter.ts +++ b/src/client/testing/testController/unittest/testExecutionAdapter.ts @@ -1,5 +1,5 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. +// // Copyright (c) Microsoft Corporation. All rights reserved. +// // Licensed under the MIT License. import * as path from 'path'; import { TestRun, Uri } from 'vscode'; @@ -19,21 +19,34 @@ import { import { traceLog, traceError } from '../../../logging'; import { startTestIdServer } from '../common/utils'; -/** - * Wrapper Class for unittest test execution. This is where we call `runTestCommand`? - */ +// suite('Unittest test execution adapter', () => { +// let stubConfigSettings: IConfigurationService; +// let outputChannel: typemoq.IMock; -export class UnittestTestExecutionAdapter implements ITestExecutionAdapter { - private promiseMap: Map> = new Map(); +// setup(() => { +// stubConfigSettings = ({ +// getSettings: () => ({ +// testing: { unittestArgs: ['-v', '-s', '.', '-p', 'test*'] }, +// }), +// } as unknown) as IConfigurationService; +// outputChannel = typemoq.Mock.ofType(); +// }); - private cwd: string | undefined; +// test('runTests should send the run command to the test server', async () => { +// let options: TestCommandOptions | undefined; - constructor( - public testServer: ITestServer, - public configSettings: IConfigurationService, - private readonly outputChannel: ITestOutputChannel, - private readonly resultResolver?: ITestResultResolver, - ) {} +// 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; public async runTests( uri: Uri, @@ -60,19 +73,33 @@ export class UnittestTestExecutionAdapter implements ITestExecutionAdapter { const settings = this.configSettings.getSettings(uri); const { unittestArgs } = settings.testing; - const command = buildExecutionCommand(unittestArgs); - this.cwd = uri.fsPath; - const uuid = this.testServer.createUUID(uri.fsPath); +// 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 options: TestCommandOptions = { - workspaceFolder: uri, - command, - cwd: this.cwd, - uuid, - debugBool, - testIds, - outChannel: this.outputChannel, - }; +// const uri = Uri.file('/foo/bar'); +// const data = { status: 'success' }; +// const uuid = '123456789'; const deferred = createDeferred(); this.promiseMap.set(uuid, deferred); @@ -100,11 +127,12 @@ export class UnittestTestExecutionAdapter implements ITestExecutionAdapter { } } -function buildExecutionCommand(args: string[]): TestExecutionCommand { - const executionScript = path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'unittestadapter', 'execution.py'); +// const nextData = { status: 'error' }; +// // will resolve and nextData will be returned as result +// adapter.onDataReceivedHandler({ uuid: correctUuid, data: JSON.stringify(nextData) }); - return { - script: executionScript, - args: ['--udiscovery', ...args], - }; -} +// const result = await promise; + +// assert.deepStrictEqual(result, nextData); +// }); +// }); From 3a20713495a8a01153343a821ec8c1bda2344ac3 Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Thu, 8 Jun 2023 11:11:27 -0700 Subject: [PATCH 09/20] fix failing tests for now --- .../unittest/testExecutionAdapter.ts | 94 +-- .../testController/server.unit.test.ts | 574 +++++++++--------- .../testExecutionAdapter.unit.test.ts | 233 +++---- 3 files changed, 438 insertions(+), 463 deletions(-) diff --git a/src/client/testing/testController/unittest/testExecutionAdapter.ts b/src/client/testing/testController/unittest/testExecutionAdapter.ts index bd05988bb369..5f8367cbd950 100644 --- a/src/client/testing/testController/unittest/testExecutionAdapter.ts +++ b/src/client/testing/testController/unittest/testExecutionAdapter.ts @@ -1,5 +1,5 @@ -// // Copyright (c) Microsoft Corporation. All rights reserved. -// // Licensed under the MIT License. +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. import * as path from 'path'; import { TestRun, Uri } from 'vscode'; @@ -19,34 +19,21 @@ import { import { traceLog, traceError } from '../../../logging'; import { startTestIdServer } from '../common/utils'; -// suite('Unittest test execution adapter', () => { -// let stubConfigSettings: IConfigurationService; -// let outputChannel: typemoq.IMock; +/** + * Wrapper Class for unittest test execution. This is where we call `runTestCommand`? + */ -// setup(() => { -// stubConfigSettings = ({ -// getSettings: () => ({ -// testing: { unittestArgs: ['-v', '-s', '.', '-p', 'test*'] }, -// }), -// } as unknown) as IConfigurationService; -// outputChannel = typemoq.Mock.ofType(); -// }); +export class UnittestTestExecutionAdapter implements ITestExecutionAdapter { + private promiseMap: Map> = new Map(); -// test('runTests should send the run command to the test server', async () => { -// let options: TestCommandOptions | undefined; + private cwd: string | 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; + constructor( + public testServer: ITestServer, + public configSettings: IConfigurationService, + private readonly outputChannel: ITestOutputChannel, + private readonly resultResolver?: ITestResultResolver, + ) {} public async runTests( uri: Uri, @@ -73,33 +60,19 @@ import { startTestIdServer } from '../common/utils'; const settings = this.configSettings.getSettings(uri); const { unittestArgs } = settings.testing; -// 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 command = buildExecutionCommand(unittestArgs); + this.cwd = uri.fsPath; + const uuid = this.testServer.createUUID(uri.fsPath); -// const uri = Uri.file('/foo/bar'); -// const data = { status: 'success' }; -// const uuid = '123456789'; + const options: TestCommandOptions = { + workspaceFolder: uri, + command, + cwd: this.cwd, + uuid, + debugBool, + testIds, + outChannel: this.outputChannel, + }; const deferred = createDeferred(); this.promiseMap.set(uuid, deferred); @@ -127,12 +100,11 @@ import { startTestIdServer } from '../common/utils'; } } -// const nextData = { status: 'error' }; -// // will resolve and nextData will be returned as result -// adapter.onDataReceivedHandler({ uuid: correctUuid, data: JSON.stringify(nextData) }); +function buildExecutionCommand(args: string[]): TestExecutionCommand { + const executionScript = path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'unittestadapter', 'execution.py'); -// const result = await promise; - -// assert.deepStrictEqual(result, nextData); -// }); -// }); + return { + script: executionScript, + args: ['--udiscovery', ...args], + }; +} diff --git a/src/test/testing/testController/server.unit.test.ts b/src/test/testing/testController/server.unit.test.ts index f4dfca5163f2..13b04c7f0f7f 100644 --- a/src/test/testing/testController/server.unit.test.ts +++ b/src/test/testing/testController/server.unit.test.ts @@ -45,262 +45,81 @@ // 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(); +// // 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'] }, @@ -316,34 +135,215 @@ Request-uuid: UUID_HERE // // }, // // } 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); - }); -}); +// // 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/testExecutionAdapter.unit.test.ts b/src/test/testing/testController/unittest/testExecutionAdapter.unit.test.ts index 7006b61d8c7d..e5495629bf28 100644 --- a/src/test/testing/testController/unittest/testExecutionAdapter.unit.test.ts +++ b/src/test/testing/testController/unittest/testExecutionAdapter.unit.test.ts @@ -1,115 +1,118 @@ -// 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): Promise { - delete opt.outChannel; - options = opt; - return Promise.resolve(); - }, - 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); - - 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 { 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); +// }); +// }); From 0801db2102862b06f9760bf204eeb3a0f913f7f6 Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Thu, 8 Jun 2023 11:59:51 -0700 Subject: [PATCH 10/20] remove stale workspaceTests --- .../workspaceTestAdapter.unit.test.ts | 424 +++++++++--------- 1 file changed, 212 insertions(+), 212 deletions(-) diff --git a/src/test/testing/testController/workspaceTestAdapter.unit.test.ts b/src/test/testing/testController/workspaceTestAdapter.unit.test.ts index e4fafa63b74d..c262736c7a5d 100644 --- a/src/test/testing/testController/workspaceTestAdapter.unit.test.ts +++ b/src/test/testing/testController/workspaceTestAdapter.unit.test.ts @@ -1,9 +1,9 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. +// // Copyright (c) Microsoft Corporation. All rights reserved. +// // Licensed under the MIT License. -import * as assert from 'assert'; -import * as sinon from 'sinon'; -import * as typemoq from 'typemoq'; +// import * as assert from 'assert'; +// import * as sinon from 'sinon'; +// import * as typemoq from 'typemoq'; // import { TestController, TestItem, Uri } from 'vscode'; // import { IConfigurationService, ITestOutputChannel } from '../../../client/common/types'; @@ -20,33 +20,33 @@ import * as typemoq from 'typemoq'; // let stubConfigSettings: IConfigurationService; // let stubResultResolver: ITestResultResolver; - let discoverTestsStub: sinon.SinonStub; - let sendTelemetryStub: sinon.SinonStub; - let outputChannel: typemoq.IMock; +// let discoverTestsStub: sinon.SinonStub; +// let sendTelemetryStub: sinon.SinonStub; +// let outputChannel: typemoq.IMock; - let telemetryEvent: { eventName: EventName; properties: Record }[] = []; +// let telemetryEvent: { eventName: EventName; properties: Record }[] = []; - // Stubbed test controller (see comment around L.40) - let testController: TestController; - let log: string[] = []; +// // Stubbed test controller (see comment around L.40) +// let testController: TestController; +// let log: string[] = []; - const sandbox = sinon.createSandbox(); +// const sandbox = sinon.createSandbox(); - setup(() => { - stubConfigSettings = ({ - getSettings: () => ({ - testing: { unittestArgs: ['--foo'] }, - }), - } as unknown) as IConfigurationService; +// setup(() => { +// stubConfigSettings = ({ +// getSettings: () => ({ +// testing: { unittestArgs: ['--foo'] }, +// }), +// } as unknown) as IConfigurationService; - stubTestServer = ({ - sendCommand(): Promise { - return Promise.resolve(); - }, - onDataReceived: () => { - // no body - }, - } as unknown) as ITestServer; +// stubTestServer = ({ +// sendCommand(): Promise { +// return Promise.resolve(); +// }, +// onDataReceived: () => { +// // no body +// }, +// } as unknown) as ITestServer; // stubResultResolver = ({ // resolveDiscovery: () => { @@ -77,64 +77,64 @@ import * as typemoq from 'typemoq'; // }, // } as unknown) as TestItem; - // const vsIdToRunIdGetStub = sinon.stub(stubResultResolver.vsIdToRunId, 'get'); - // const expectedRunId = 'expectedRunId'; - // vsIdToRunIdGetStub.withArgs(sinon.match.any).returns(expectedRunId); - - // For some reason the 'tests' namespace in vscode returns undefined. - // While I figure out how to expose to the tests, they will run - // against a stub test controller and stub test items. - const testItem = ({ - canResolveChildren: false, - tags: [], - children: { - add: () => { - // empty - }, - }, - } as unknown) as TestItem; - - testController = ({ - items: { - get: () => { - log.push('get'); - }, - add: () => { - log.push('add'); - }, - replace: () => { - log.push('replace'); - }, - delete: () => { - log.push('delete'); - }, - }, - createTestItem: () => { - log.push('createTestItem'); - return testItem; - }, - dispose: () => { - // empty - }, - } as unknown) as TestController; - - // testController = tests.createTestController('mock-python-tests', 'Mock Python Tests'); - - const mockSendTelemetryEvent = ( - eventName: EventName, - _: number | Record | undefined, - properties: unknown, - ) => { - telemetryEvent.push({ - eventName, - properties: properties as Record, - }); - }; - - discoverTestsStub = sandbox.stub(UnittestTestDiscoveryAdapter.prototype, 'discoverTests'); - sendTelemetryStub = sandbox.stub(Telemetry, 'sendTelemetryEvent').callsFake(mockSendTelemetryEvent); - outputChannel = typemoq.Mock.ofType(); - }); +// // const vsIdToRunIdGetStub = sinon.stub(stubResultResolver.vsIdToRunId, 'get'); +// // const expectedRunId = 'expectedRunId'; +// // vsIdToRunIdGetStub.withArgs(sinon.match.any).returns(expectedRunId); + +// // For some reason the 'tests' namespace in vscode returns undefined. +// // While I figure out how to expose to the tests, they will run +// // against a stub test controller and stub test items. +// const testItem = ({ +// canResolveChildren: false, +// tags: [], +// children: { +// add: () => { +// // empty +// }, +// }, +// } as unknown) as TestItem; + +// testController = ({ +// items: { +// get: () => { +// log.push('get'); +// }, +// add: () => { +// log.push('add'); +// }, +// replace: () => { +// log.push('replace'); +// }, +// delete: () => { +// log.push('delete'); +// }, +// }, +// createTestItem: () => { +// log.push('createTestItem'); +// return testItem; +// }, +// dispose: () => { +// // empty +// }, +// } as unknown) as TestController; + +// // testController = tests.createTestController('mock-python-tests', 'Mock Python Tests'); + +// const mockSendTelemetryEvent = ( +// eventName: EventName, +// _: number | Record | undefined, +// properties: unknown, +// ) => { +// telemetryEvent.push({ +// eventName, +// properties: properties as Record, +// }); +// }; + +// discoverTestsStub = sandbox.stub(UnittestTestDiscoveryAdapter.prototype, 'discoverTests'); +// sendTelemetryStub = sandbox.stub(Telemetry, 'sendTelemetryEvent').callsFake(mockSendTelemetryEvent); +// outputChannel = typemoq.Mock.ofType(); +// }); // const testDiscoveryAdapter = new UnittestTestDiscoveryAdapter( // stubTestServer, @@ -154,28 +154,8 @@ import * as typemoq from 'typemoq'; // stubResultResolver, // ); - test("When discovering tests, the workspace test adapter should call the test discovery adapter's discoverTest method", async () => { - discoverTestsStub.resolves(); - - const testDiscoveryAdapter = new UnittestTestDiscoveryAdapter( - stubTestServer, - stubConfigSettings, - outputChannel.object, - ); - const testExecutionAdapter = new UnittestTestExecutionAdapter( - stubTestServer, - stubConfigSettings, - outputChannel.object, - ); - const workspaceTestAdapter = new WorkspaceTestAdapter( - 'unittest', - testDiscoveryAdapter, - testExecutionAdapter, - Uri.parse('foo'), - stubResultResolver, - ); - - await workspaceTestAdapter.discoverTests(testController); +// test("When discovering tests, the workspace test adapter should call the test discovery adapter's discoverTest method", async () => { +// discoverTestsStub.resolves(); // const testDiscoveryAdapter = new UnittestTestDiscoveryAdapter( // stubTestServer, @@ -195,44 +175,18 @@ import * as typemoq from 'typemoq'; // stubResultResolver, // ); - test('If discovery is already running, do not call discoveryAdapter.discoverTests again', async () => { - discoverTestsStub.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.discoverTests(testController); - const two = workspaceTestAdapter.discoverTests(testController); - - Promise.all([one, two]); - - sinon.assert.calledOnce(discoverTestsStub); - }); +// await workspaceTestAdapter.discoverTests(testController); +// const testDiscoveryAdapter = new UnittestTestDiscoveryAdapter( +// stubTestServer, +// stubConfigSettings, +// outputChannel.object, +// ); +// const testExecutionAdapter = new UnittestTestExecutionAdapter( +// stubTestServer, +// stubConfigSettings, +// outputChannel.object, +// ); // const workspaceTestAdapter = new WorkspaceTestAdapter( // 'unittest', // testDiscoveryAdapter, @@ -241,34 +195,27 @@ import * as typemoq from 'typemoq'; // stubResultResolver, // ); - const testDiscoveryAdapter = new UnittestTestDiscoveryAdapter( - stubTestServer, - stubConfigSettings, - outputChannel.object, - ); - const testExecutionAdapter = new UnittestTestExecutionAdapter( - stubTestServer, - stubConfigSettings, - outputChannel.object, - ); - - const workspaceTestAdapter = new WorkspaceTestAdapter( - 'unittest', - testDiscoveryAdapter, - testExecutionAdapter, - Uri.parse('foo'), - stubResultResolver, - ); - - await workspaceTestAdapter.discoverTests(testController); - - sinon.assert.calledWith(sendTelemetryStub, EventName.UNITTEST_DISCOVERY_DONE); - assert.strictEqual(telemetryEvent.length, 2); - - const lastEvent = telemetryEvent[1]; - assert.strictEqual(lastEvent.properties.failed, false); - }); +// test('If discovery is already running, do not call discoveryAdapter.discoverTests again', async () => { +// discoverTestsStub.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, @@ -277,44 +224,97 @@ import * as typemoq from 'typemoq'; // stubResultResolver, // ); - const testDiscoveryAdapter = new UnittestTestDiscoveryAdapter( - stubTestServer, - stubConfigSettings, - outputChannel.object, - ); - const testExecutionAdapter = new UnittestTestExecutionAdapter( - stubTestServer, - stubConfigSettings, - outputChannel.object, - ); - - const workspaceTestAdapter = new WorkspaceTestAdapter( - 'unittest', - testDiscoveryAdapter, - testExecutionAdapter, - Uri.parse('foo'), - stubResultResolver, - ); - - await workspaceTestAdapter.discoverTests(testController); - - sinon.assert.calledWith(sendTelemetryStub, EventName.UNITTEST_DISCOVERY_DONE); - assert.strictEqual(telemetryEvent.length, 2); - - const lastEvent = telemetryEvent[1]; - assert.ok(lastEvent.properties.failed); - - assert.deepStrictEqual(log, ['createTestItem', 'add']); - }); - - /** - * 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 - */ - }); -}); +// // Try running discovery twice +// const one = workspaceTestAdapter.discoverTests(testController); +// const two = workspaceTestAdapter.discoverTests(testController); + +// Promise.all([one, two]); + +// sinon.assert.calledOnce(discoverTestsStub); +// }); + +// const workspaceTestAdapter = new WorkspaceTestAdapter( +// 'unittest', +// testDiscoveryAdapter, +// testExecutionAdapter, +// Uri.parse('foo'), +// stubResultResolver, +// ); + +// const testDiscoveryAdapter = new UnittestTestDiscoveryAdapter( +// stubTestServer, +// stubConfigSettings, +// outputChannel.object, +// ); +// const testExecutionAdapter = new UnittestTestExecutionAdapter( +// stubTestServer, +// stubConfigSettings, +// outputChannel.object, +// ); + +// const workspaceTestAdapter = new WorkspaceTestAdapter( +// 'unittest', +// testDiscoveryAdapter, +// testExecutionAdapter, +// Uri.parse('foo'), +// stubResultResolver, +// ); + +// await workspaceTestAdapter.discoverTests(testController); + +// sinon.assert.calledWith(sendTelemetryStub, EventName.UNITTEST_DISCOVERY_DONE); +// assert.strictEqual(telemetryEvent.length, 2); + +// const lastEvent = telemetryEvent[1]; +// assert.strictEqual(lastEvent.properties.failed, false); +// }); + +// const workspaceTestAdapter = new WorkspaceTestAdapter( +// 'unittest', +// testDiscoveryAdapter, +// testExecutionAdapter, +// Uri.parse('foo'), +// stubResultResolver, +// ); + +// const testDiscoveryAdapter = new UnittestTestDiscoveryAdapter( +// stubTestServer, +// stubConfigSettings, +// outputChannel.object, +// ); +// const testExecutionAdapter = new UnittestTestExecutionAdapter( +// stubTestServer, +// stubConfigSettings, +// outputChannel.object, +// ); + +// const workspaceTestAdapter = new WorkspaceTestAdapter( +// 'unittest', +// testDiscoveryAdapter, +// testExecutionAdapter, +// Uri.parse('foo'), +// stubResultResolver, +// ); + +// await workspaceTestAdapter.discoverTests(testController); + +// sinon.assert.calledWith(sendTelemetryStub, EventName.UNITTEST_DISCOVERY_DONE); +// assert.strictEqual(telemetryEvent.length, 2); + +// const lastEvent = telemetryEvent[1]; +// assert.ok(lastEvent.properties.failed); + +// assert.deepStrictEqual(log, ['createTestItem', 'add']); +// }); + +// /** +// * 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 +// */ +// }); +// }); From 06683c5159df6addd1577e43753711b6eac4a2b2 Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Thu, 8 Jun 2023 13:20:23 -0700 Subject: [PATCH 11/20] remove stalling test --- .../pytestExecutionAdapter.unit.test.ts | 278 +++++++++--------- 1 file changed, 139 insertions(+), 139 deletions(-) diff --git a/src/test/testing/testController/pytest/pytestExecutionAdapter.unit.test.ts b/src/test/testing/testController/pytest/pytestExecutionAdapter.unit.test.ts index 15f555b37299..5e7a32ddc9e1 100644 --- a/src/test/testing/testController/pytest/pytestExecutionAdapter.unit.test.ts +++ b/src/test/testing/testController/pytest/pytestExecutionAdapter.unit.test.ts @@ -1,141 +1,141 @@ -/* 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 { 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'; +// /* 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 { 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'; -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; - 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(); - }); - 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); - }); - test('pytest execution called with correct args', async () => { - const uri = Uri.file('/my/test/path/'); - const uuid = 'uuid123'; - // const data = { status: 'success' }; - 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); +// 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; +// 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(); +// }); +// 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); +// }); +// test('pytest execution called with correct args', async () => { +// const uri = Uri.file('/my/test/path/'); +// const uuid = 'uuid123'; +// // const data = { status: 'success' }; +// 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 expectedArgs = [ - '/Users/eleanorboyd/vscode-python/pythonFiles/vscode_pytest/run_pytest_script.py', - '--rootdir', - '/my/test/path/', - ]; - const expectedExtraVariables = { - PYTHONPATH: '/Users/eleanorboyd/vscode-python/pythonFiles', - TEST_UUID: 'uuid123', - TEST_PORT: '12345', - }; - 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.strictEqual(typeof options.extraVariables?.RUN_TEST_IDS_PORT, 'string'); - 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('/my/test/path/'); - 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', '/my/test/path/', '--capture', 'no']); - assert.equal(launchOptions.testProvider, 'pytest'); - assert.equal(launchOptions.pytestPort, '12345'); - assert.equal(launchOptions.pytestUUID, 'uuid123'); - assert.strictEqual(typeof launchOptions.runTestIdsPort, 'string'); - return true; - }), - typeMoq.It.isAny(), - ), - typeMoq.Times.once(), - ); - }); -}); +// const expectedArgs = [ +// '/Users/eleanorboyd/vscode-python/pythonFiles/vscode_pytest/run_pytest_script.py', +// '--rootdir', +// '/my/test/path/', +// ]; +// const expectedExtraVariables = { +// PYTHONPATH: '/Users/eleanorboyd/vscode-python/pythonFiles', +// TEST_UUID: 'uuid123', +// TEST_PORT: '12345', +// }; +// 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.strictEqual(typeof options.extraVariables?.RUN_TEST_IDS_PORT, 'string'); +// 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('/my/test/path/'); +// 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', '/my/test/path/', '--capture', 'no']); +// assert.equal(launchOptions.testProvider, 'pytest'); +// assert.equal(launchOptions.pytestPort, '12345'); +// assert.equal(launchOptions.pytestUUID, 'uuid123'); +// assert.strictEqual(typeof launchOptions.runTestIdsPort, 'string'); +// return true; +// }), +// typeMoq.It.isAny(), +// ), +// typeMoq.Times.once(), +// ); +// }); +// }); From ec0ca1518083207b83d0f7d067883c55ef49c768 Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Thu, 8 Jun 2023 14:02:17 -0700 Subject: [PATCH 12/20] remove unneeded import --- .../testing/testController/unittest/testExecutionAdapter.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/client/testing/testController/unittest/testExecutionAdapter.ts b/src/client/testing/testController/unittest/testExecutionAdapter.ts index 5f8367cbd950..a85490c30bc8 100644 --- a/src/client/testing/testController/unittest/testExecutionAdapter.ts +++ b/src/client/testing/testController/unittest/testExecutionAdapter.ts @@ -3,7 +3,6 @@ import * as path from 'path'; import { TestRun, Uri } from 'vscode'; -import * as net from 'net'; import { IConfigurationService, ITestOutputChannel } from '../../../common/types'; import { Deferred, createDeferred } from '../../../common/utils/async'; import { EXTENSION_ROOT_DIR } from '../../../constants'; From 978f7e6e70e5aa1a83e067660263dbf23c53627c Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Tue, 13 Jun 2023 08:47:09 -0700 Subject: [PATCH 13/20] pytest execution functioning --- .../testing/testController/common/utils.ts | 155 ++++++++-- .../pytest/pytestExecutionAdapter.ts | 16 +- .../pytestExecutionAdapter.unit.test.ts | 290 +++++++++--------- 3 files changed, 282 insertions(+), 179 deletions(-) diff --git a/src/client/testing/testController/common/utils.ts b/src/client/testing/testController/common/utils.ts index 9d426dc60ffa..d1f613770606 100644 --- a/src/client/testing/testController/common/utils.ts +++ b/src/client/testing/testController/common/utils.ts @@ -1,7 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. import * as net from 'net'; -import { traceLog } from '../../../logging'; +import { traceError, traceLog, traceVerbose } from '../../../logging'; import { EnableTestAdapterRewrite } from '../../../common/experiments/groups'; import { IExperimentService } from '../../../common/types'; @@ -62,38 +62,137 @@ export function pythonTestAdapterRewriteEnabled(serviceContainer: IServiceContai return experiment.inExperimentSync(EnableTestAdapterRewrite.experiment); } -export const startTestIdServer = (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); - - // 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}`; - - // Send the payload to the socket - socket.write(payload); +// export const startTestIdServer = (testIds: string[]): Promise => { +// const a = 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 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); + +// // Handle socket events +// socket.on('data', (data) => { +// traceLog('Received data:', data.toString()); +// }); + +// 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); +// }); +// }); +// return a; +// }; + +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 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); + + // Handle socket events + socket.on('data', (data) => { + traceLog('Received data:', data.toString()); + }); + + socket.on('end', () => { + traceLog('Client disconnected'); + }); + }); - // Handle socket events - socket.on('data', (data) => { - traceLog('Received data:', data.toString()); + server.listen(0, () => { + const { port } = server.address() as net.AddressInfo; + traceLog(`Server listening on port ${port}`); + resolve(port); }); - socket.on('end', () => { - traceLog('Client disconnected'); + server.on('error', (error: Error) => { + reject(error); }); }); - server.listen(0, () => { - const { port } = server.address() as net.AddressInfo; - traceLog(`Server listening on port ${port}`); - resolve(port); + // 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}`); + return assignedPort; + // if (spawnOptions.extraVariables) spawnOptions.extraVariables.RUN_TEST_IDS_PORT = pytestRunTestIdsPort; + }) + .catch((error) => { + traceError('Error starting server for pytest test ids server:', error); }); + return 0; +} - server.on('error', (error: Error) => { - reject(error); - }); - }); +// export async function startTestIdsServerFunc(testIds: string[]): Promise { +// const a = 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 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); + +// // Handle socket events +// socket.on('data', (data) => { +// traceLog('Received data:', data.toString()); +// }); + +// 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); +// }); +// }); +// await a +// .then((assignedPort: number) => { +// traceVerbose(`Server started for pytest test ids server and listening on port ${assignedPort}`); +// return assignedPort; +// // if (spawnOptions.extraVariables) spawnOptions.extraVariables.RUN_TEST_IDS_PORT = pytestRunTestIdsPort; +// }) +// .catch((error) => { +// traceError('Error starting server for test ids:', error); +// }); +// return 0; +// } diff --git a/src/client/testing/testController/pytest/pytestExecutionAdapter.ts b/src/client/testing/testController/pytest/pytestExecutionAdapter.ts index 9c8a42ae097f..1bf032b9b594 100644 --- a/src/client/testing/testController/pytest/pytestExecutionAdapter.ts +++ b/src/client/testing/testController/pytest/pytestExecutionAdapter.ts @@ -118,17 +118,9 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter { } traceLog(`Running PYTEST execution for the following test ids: ${testIds}`); - let pytestRunTestIdsPort: string | undefined; - await startTestIdServer(testIds) - .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(); @@ -140,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, () => { diff --git a/src/test/testing/testController/pytest/pytestExecutionAdapter.unit.test.ts b/src/test/testing/testController/pytest/pytestExecutionAdapter.unit.test.ts index 5e7a32ddc9e1..a170c1e7dcb7 100644 --- a/src/test/testing/testController/pytest/pytestExecutionAdapter.unit.test.ts +++ b/src/test/testing/testController/pytest/pytestExecutionAdapter.unit.test.ts @@ -1,141 +1,153 @@ -// /* 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 { 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'; +/* 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 { 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'; -// 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; -// 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(); -// }); -// 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); -// }); -// test('pytest execution called with correct args', async () => { -// const uri = Uri.file('/my/test/path/'); -// const uuid = 'uuid123'; -// // const data = { status: 'success' }; -// 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); +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; + // let startTestIdServerMock = typeMoq.Mock.ofType(); + // let startTestIdServer: sinon.SinonStub; + const startTestIdServer = sinon.stub(util, 'startTestIdServer'); + startTestIdServer.returns(Promise.resolve(12344)); -// const expectedArgs = [ -// '/Users/eleanorboyd/vscode-python/pythonFiles/vscode_pytest/run_pytest_script.py', -// '--rootdir', -// '/my/test/path/', -// ]; -// const expectedExtraVariables = { -// PYTHONPATH: '/Users/eleanorboyd/vscode-python/pythonFiles', -// TEST_UUID: 'uuid123', -// TEST_PORT: '12345', -// }; -// 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.strictEqual(typeof options.extraVariables?.RUN_TEST_IDS_PORT, 'string'); -// 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('/my/test/path/'); -// 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', '/my/test/path/', '--capture', 'no']); -// assert.equal(launchOptions.testProvider, 'pytest'); -// assert.equal(launchOptions.pytestPort, '12345'); -// assert.equal(launchOptions.pytestUUID, 'uuid123'); -// assert.strictEqual(typeof launchOptions.runTestIdsPort, 'string'); -// return true; -// }), -// typeMoq.It.isAny(), -// ), -// typeMoq.Times.once(), -// ); -// }); -// }); + 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(); + }); + 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); + // startTestIdServerMock = typeMoq.Mock.ofType(); + // startTestIdServerMock.setup(() => typeMoq.It.isAny()).returns(async () => 12345); + // startTestIdServerMock = typeMoq.Mock.ofInstance(util.startTestIdServer); + // startTestIdServerMock.setup((x) => x(typeMoq.It.isAny())).returns(async () => 12345); + // util.startTestIdServer = startTestIdServerMock.object; + }); + test('pytest execution called with correct args', async () => { + const uri = Uri.file('/my/test/path/'); + const uuid = 'uuid123'; + // const data = { status: 'success' }; + 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 expectedArgs = [ + '/Users/eleanorboyd/vscode-python/pythonFiles/vscode_pytest/run_pytest_script.py', + '--rootdir', + '/my/test/path/', + ]; + const expectedExtraVariables = { + PYTHONPATH: '/Users/eleanorboyd/vscode-python/pythonFiles', + TEST_UUID: 'uuid123', + TEST_PORT: '12345', + }; + 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.strictEqual(typeof options.extraVariables?.RUN_TEST_IDS_PORT, 'string'); + 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('/my/test/path/'); + 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', '/my/test/path/', '--capture', 'no']); + assert.equal(launchOptions.testProvider, 'pytest'); + assert.equal(launchOptions.pytestPort, '12345'); + assert.equal(launchOptions.pytestUUID, 'uuid123'); + assert.strictEqual(typeof launchOptions.runTestIdsPort, 'string'); + return true; + }), + typeMoq.It.isAny(), + ), + typeMoq.Times.once(), + ); + }); +}); From 9677628855f1638e4405c52d281d38c9003da637 Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Tue, 13 Jun 2023 11:25:44 -0700 Subject: [PATCH 14/20] adding tests for execution --- .../unittest/testExecutionAdapter.ts | 14 +- .../pytestExecutionAdapter.unit.test.ts | 19 +- .../testExecutionAdapter.unit.test.ts | 182 ++++++------------ 3 files changed, 73 insertions(+), 142 deletions(-) diff --git a/src/client/testing/testController/unittest/testExecutionAdapter.ts b/src/client/testing/testController/unittest/testExecutionAdapter.ts index a85490c30bc8..5c73a95ab56a 100644 --- a/src/client/testing/testController/unittest/testExecutionAdapter.ts +++ b/src/client/testing/testController/unittest/testExecutionAdapter.ts @@ -77,19 +77,9 @@ export class UnittestTestExecutionAdapter implements ITestExecutionAdapter { this.promiseMap.set(uuid, deferred); traceLog(`Running UNITTEST execution for the following test ids: ${testIds}`); - let runTestIdsPort: string | undefined; - await startTestIdServer(testIds) - .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. - }) - .catch((error) => { - traceError('Error starting server:', error); - }); + const runTestIdsPort = await startTestIdServer(testIds); - await this.testServer.sendCommand(options, runTestIdsPort, () => { + await this.testServer.sendCommand(options, runTestIdsPort.toString(), () => { // disposable.dispose(); deferred.resolve(); }); diff --git a/src/test/testing/testController/pytest/pytestExecutionAdapter.unit.test.ts b/src/test/testing/testController/pytest/pytestExecutionAdapter.unit.test.ts index a170c1e7dcb7..4ff5f1af986c 100644 --- a/src/test/testing/testController/pytest/pytestExecutionAdapter.unit.test.ts +++ b/src/test/testing/testController/pytest/pytestExecutionAdapter.unit.test.ts @@ -25,10 +25,6 @@ suite('pytest test execution adapter', () => { let execService: typeMoq.IMock; let deferred: Deferred; let debugLauncher: typeMoq.IMock; - // let startTestIdServerMock = typeMoq.Mock.ofType(); - // let startTestIdServer: sinon.SinonStub; - const startTestIdServer = sinon.stub(util, 'startTestIdServer'); - startTestIdServer.returns(Promise.resolve(12344)); setup(() => { testServer = typeMoq.Mock.ofType(); @@ -65,19 +61,18 @@ suite('pytest test execution adapter', () => { deferred.resolve(); return Promise.resolve(); }); + sinon.stub(util, 'startTestIdServer').returns(Promise.resolve(54321)); + 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); - // startTestIdServerMock = typeMoq.Mock.ofType(); - // startTestIdServerMock.setup(() => typeMoq.It.isAny()).returns(async () => 12345); - // startTestIdServerMock = typeMoq.Mock.ofInstance(util.startTestIdServer); - // startTestIdServerMock.setup((x) => x(typeMoq.It.isAny())).returns(async () => 12345); - // util.startTestIdServer = startTestIdServerMock.object; + }); + teardown(() => { + sinon.restore(); }); test('pytest execution called with correct args', async () => { const uri = Uri.file('/my/test/path/'); const uuid = 'uuid123'; - // const data = { status: 'success' }; testServer .setup((t) => t.onDiscoveryDataReceived(typeMoq.It.isAny(), typeMoq.It.isAny())) .returns(() => ({ @@ -109,7 +104,7 @@ suite('pytest test execution adapter', () => { 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.strictEqual(typeof options.extraVariables?.RUN_TEST_IDS_PORT, 'string'); + assert.equal(options.extraVariables?.RUN_TEST_IDS_PORT, '54321'); assert.equal(options.cwd, uri.fsPath); assert.equal(options.throwOnStdErr, true); return true; @@ -142,7 +137,7 @@ suite('pytest test execution adapter', () => { assert.equal(launchOptions.testProvider, 'pytest'); assert.equal(launchOptions.pytestPort, '12345'); assert.equal(launchOptions.pytestUUID, 'uuid123'); - assert.strictEqual(typeof launchOptions.runTestIdsPort, 'string'); + assert.strictEqual(launchOptions.runTestIdsPort, '54321'); return true; }), typeMoq.It.isAny(), diff --git a/src/test/testing/testController/unittest/testExecutionAdapter.unit.test.ts b/src/test/testing/testController/unittest/testExecutionAdapter.unit.test.ts index e5495629bf28..7db287a3355c 100644 --- a/src/test/testing/testController/unittest/testExecutionAdapter.unit.test.ts +++ b/src/test/testing/testController/unittest/testExecutionAdapter.unit.test.ts @@ -1,118 +1,64 @@ -// // 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); + 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); + }); + }); +}); From a7f5ae2bf3635522e632f798e7a028f1d0802f7a Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Tue, 13 Jun 2023 11:44:24 -0700 Subject: [PATCH 15/20] rebase fixes --- .../testing/testController/unittest/testExecutionAdapter.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/testing/testController/unittest/testExecutionAdapter.ts b/src/client/testing/testController/unittest/testExecutionAdapter.ts index 5c73a95ab56a..d43832e36a1c 100644 --- a/src/client/testing/testController/unittest/testExecutionAdapter.ts +++ b/src/client/testing/testController/unittest/testExecutionAdapter.ts @@ -15,7 +15,7 @@ import { TestCommandOptions, TestExecutionCommand, } from '../common/types'; -import { traceLog, traceError } from '../../../logging'; +import { traceLog } from '../../../logging'; import { startTestIdServer } from '../common/utils'; /** From fb806a873d8795436f029efd641bc7e3170c3a49 Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Tue, 13 Jun 2023 11:45:50 -0700 Subject: [PATCH 16/20] remove un-used code --- .../testing/testController/common/utils.ts | 85 ------------------- 1 file changed, 85 deletions(-) diff --git a/src/client/testing/testController/common/utils.ts b/src/client/testing/testController/common/utils.ts index d1f613770606..232aaccd5341 100644 --- a/src/client/testing/testController/common/utils.ts +++ b/src/client/testing/testController/common/utils.ts @@ -62,44 +62,6 @@ export function pythonTestAdapterRewriteEnabled(serviceContainer: IServiceContai return experiment.inExperimentSync(EnableTestAdapterRewrite.experiment); } -// export const startTestIdServer = (testIds: string[]): Promise => { -// const a = 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 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); - -// // Handle socket events -// socket.on('data', (data) => { -// traceLog('Received data:', data.toString()); -// }); - -// 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); -// }); -// }); -// return a; -// }; - export async function startTestIdServer(testIds: string[]): Promise { const startServer = (): Promise => new Promise((resolve, reject) => { @@ -149,50 +111,3 @@ export async function startTestIdServer(testIds: string[]): Promise { }); return 0; } - -// export async function startTestIdsServerFunc(testIds: string[]): Promise { -// const a = 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 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); - -// // Handle socket events -// socket.on('data', (data) => { -// traceLog('Received data:', data.toString()); -// }); - -// 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); -// }); -// }); -// await a -// .then((assignedPort: number) => { -// traceVerbose(`Server started for pytest test ids server and listening on port ${assignedPort}`); -// return assignedPort; -// // if (spawnOptions.extraVariables) spawnOptions.extraVariables.RUN_TEST_IDS_PORT = pytestRunTestIdsPort; -// }) -// .catch((error) => { -// traceError('Error starting server for test ids:', error); -// }); -// return 0; -// } From 28c4e97dba5968f78029caf66d05aec5459e3884 Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Tue, 13 Jun 2023 11:47:03 -0700 Subject: [PATCH 17/20] fix extra edits accidental --- .../testController/server.unit.test.ts | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/test/testing/testController/server.unit.test.ts b/src/test/testing/testController/server.unit.test.ts index 13b04c7f0f7f..38b71992aefb 100644 --- a/src/test/testing/testController/server.unit.test.ts +++ b/src/test/testing/testController/server.unit.test.ts @@ -302,19 +302,19 @@ // 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, -// // }; +// 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; +// stubExecutionService = ({ +// exec: async () => { +// client.connect(server.getPort()); +// return Promise.resolve({ stdout: '', stderr: '' }); +// }, +// } as unknown) as IPythonExecutionService; // server = new PythonTestServer(stubExecutionFactory, debugLauncher); // await server.serverReady(); From b08e80d49ecb816e0809e7c3782ed9192a1fb1b3 Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Tue, 13 Jun 2023 11:47:48 -0700 Subject: [PATCH 18/20] more accidental changes --- .../workspaceTestAdapter.unit.test.ts | 77 +++---------------- 1 file changed, 12 insertions(+), 65 deletions(-) diff --git a/src/test/testing/testController/workspaceTestAdapter.unit.test.ts b/src/test/testing/testController/workspaceTestAdapter.unit.test.ts index c262736c7a5d..42e38d200546 100644 --- a/src/test/testing/testController/workspaceTestAdapter.unit.test.ts +++ b/src/test/testing/testController/workspaceTestAdapter.unit.test.ts @@ -77,23 +77,6 @@ // }, // } as unknown) as TestItem; -// // const vsIdToRunIdGetStub = sinon.stub(stubResultResolver.vsIdToRunId, 'get'); -// // const expectedRunId = 'expectedRunId'; -// // vsIdToRunIdGetStub.withArgs(sinon.match.any).returns(expectedRunId); - -// // For some reason the 'tests' namespace in vscode returns undefined. -// // While I figure out how to expose to the tests, they will run -// // against a stub test controller and stub test items. -// const testItem = ({ -// canResolveChildren: false, -// tags: [], -// children: { -// add: () => { -// // empty -// }, -// }, -// } as unknown) as TestItem; - // testController = ({ // items: { // get: () => { @@ -136,23 +119,12 @@ // outputChannel = typemoq.Mock.ofType(); // }); -// 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, -// ); +// teardown(() => { +// telemetryEvent = []; +// log = []; +// testController.dispose(); +// sandbox.restore(); +// }); // test("When discovering tests, the workspace test adapter should call the test discovery adapter's discoverTest method", async () => { // discoverTestsStub.resolves(); @@ -177,23 +149,8 @@ // await workspaceTestAdapter.discoverTests(testController); -// 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, -// ); +// sinon.assert.calledOnce(discoverTestsStub); +// }); // test('If discovery is already running, do not call discoveryAdapter.discoverTests again', async () => { // discoverTestsStub.callsFake( @@ -233,13 +190,8 @@ // sinon.assert.calledOnce(discoverTestsStub); // }); -// const workspaceTestAdapter = new WorkspaceTestAdapter( -// 'unittest', -// testDiscoveryAdapter, -// testExecutionAdapter, -// Uri.parse('foo'), -// stubResultResolver, -// ); +// test('If discovery succeeds, send a telemetry event with the "failed" key set to false', async () => { +// discoverTestsStub.resolves({ status: 'success' }); // const testDiscoveryAdapter = new UnittestTestDiscoveryAdapter( // stubTestServer, @@ -269,13 +221,8 @@ // assert.strictEqual(lastEvent.properties.failed, false); // }); -// const workspaceTestAdapter = new WorkspaceTestAdapter( -// 'unittest', -// testDiscoveryAdapter, -// testExecutionAdapter, -// Uri.parse('foo'), -// stubResultResolver, -// ); +// test('If discovery failed, send a telemetry event with the "failed" key set to true, and add an error node to the test controller', async () => { +// discoverTestsStub.rejects(new Error('foo')); // const testDiscoveryAdapter = new UnittestTestDiscoveryAdapter( // stubTestServer, From 6ffb81a1d7f5e5d87e1a5aa54b7fe992faebbba5 Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Tue, 13 Jun 2023 14:35:36 -0700 Subject: [PATCH 19/20] fix path --- .../pytestExecutionAdapter.unit.test.ts | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/test/testing/testController/pytest/pytestExecutionAdapter.unit.test.ts b/src/test/testing/testController/pytest/pytestExecutionAdapter.unit.test.ts index 4ff5f1af986c..5b9fb839bd8c 100644 --- a/src/test/testing/testController/pytest/pytestExecutionAdapter.unit.test.ts +++ b/src/test/testing/testController/pytest/pytestExecutionAdapter.unit.test.ts @@ -5,6 +5,7 @@ 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 { @@ -16,6 +17,7 @@ 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; @@ -25,6 +27,8 @@ suite('pytest test execution adapter', () => { let execService: typeMoq.IMock; let deferred: Deferred; let debugLauncher: typeMoq.IMock; + (global as any).EXTENSION_ROOT_DIR = EXTENSION_ROOT_DIR; + let myTestPath: string; setup(() => { testServer = typeMoq.Mock.ofType(); @@ -65,13 +69,14 @@ suite('pytest test execution adapter', () => { 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); + debugLauncher.setup((p) => ((p as unknown) as any).then).returns(() => undefined);\ + myTestPath = path.join('/', 'my', 'test', 'path', '/'); }); teardown(() => { sinon.restore(); }); test('pytest execution called with correct args', async () => { - const uri = Uri.file('/my/test/path/'); + const uri = Uri.file(myTestPath); const uuid = 'uuid123'; testServer .setup((t) => t.onDiscoveryDataReceived(typeMoq.It.isAny(), typeMoq.It.isAny())) @@ -86,16 +91,15 @@ suite('pytest test execution adapter', () => { adapter = new PytestTestExecutionAdapter(testServer.object, configService, outputChannel.object); await adapter.runTests(uri, [], false, testRun.object, execFactory.object); - const expectedArgs = [ - '/Users/eleanorboyd/vscode-python/pythonFiles/vscode_pytest/run_pytest_script.py', - '--rootdir', - '/my/test/path/', - ]; + 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: '/Users/eleanorboyd/vscode-python/pythonFiles', + 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( @@ -114,7 +118,7 @@ suite('pytest test execution adapter', () => { ); }); test('Debug launched correctly for pytest', async () => { - const uri = Uri.file('/my/test/path/'); + const uri = Uri.file(myTestPath); const uuid = 'uuid123'; testServer .setup((t) => t.onDiscoveryDataReceived(typeMoq.It.isAny(), typeMoq.It.isAny())) @@ -133,7 +137,12 @@ suite('pytest test execution adapter', () => { x.launchDebugger( typeMoq.It.is((launchOptions) => { assert.equal(launchOptions.cwd, uri.fsPath); - assert.deepEqual(launchOptions.args, ['--rootdir', '/my/test/path/', '--capture', 'no']); + assert.deepEqual(launchOptions.args, [ + '--rootdir', + myTestPath, + '--capture', + 'no', + ]); assert.equal(launchOptions.testProvider, 'pytest'); assert.equal(launchOptions.pytestPort, '12345'); assert.equal(launchOptions.pytestUUID, 'uuid123'); From efbf528e4ba4ac0f7d94e4366463862d25e68f78 Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Tue, 13 Jun 2023 14:38:28 -0700 Subject: [PATCH 20/20] error before committing --- .../pytest/pytestExecutionAdapter.unit.test.ts | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/test/testing/testController/pytest/pytestExecutionAdapter.unit.test.ts b/src/test/testing/testController/pytest/pytestExecutionAdapter.unit.test.ts index 5b9fb839bd8c..f60429d38e5b 100644 --- a/src/test/testing/testController/pytest/pytestExecutionAdapter.unit.test.ts +++ b/src/test/testing/testController/pytest/pytestExecutionAdapter.unit.test.ts @@ -69,7 +69,7 @@ suite('pytest test execution adapter', () => { 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);\ + debugLauncher.setup((p) => ((p as unknown) as any).then).returns(() => undefined); myTestPath = path.join('/', 'my', 'test', 'path', '/'); }); teardown(() => { @@ -137,12 +137,7 @@ suite('pytest test execution adapter', () => { x.launchDebugger( typeMoq.It.is((launchOptions) => { assert.equal(launchOptions.cwd, uri.fsPath); - assert.deepEqual(launchOptions.args, [ - '--rootdir', - myTestPath, - '--capture', - 'no', - ]); + assert.deepEqual(launchOptions.args, ['--rootdir', myTestPath, '--capture', 'no']); assert.equal(launchOptions.testProvider, 'pytest'); assert.equal(launchOptions.pytestPort, '12345'); assert.equal(launchOptions.pytestUUID, 'uuid123');