|
2 | 2 | // Licensed under the MIT License. |
3 | 3 |
|
4 | 4 | import { Uri } from 'vscode'; |
| 5 | +import path from 'path'; |
5 | 6 | import { IConfigurationService, ITestOutputChannel } from '../../../common/types'; |
6 | 7 | import { createDeferred, Deferred } from '../../../common/utils/async'; |
7 | 8 | import { traceVerbose } from '../../../logging'; |
8 | 9 | import { DataReceivedEvent, ExecutionTestPayload, ITestExecutionAdapter, ITestServer } from '../common/types'; |
| 10 | +import { |
| 11 | + ExecutionFactoryCreateWithEnvironmentOptions, |
| 12 | + IPythonExecutionFactory, |
| 13 | + SpawnOptions, |
| 14 | +} from '../../../common/process/types'; |
| 15 | +import { EXTENSION_ROOT_DIR } from '../../../constants'; |
9 | 16 |
|
10 | 17 | /** |
11 | 18 | * Wrapper Class for pytest test execution. This is where we call `runTestCommand`? |
@@ -33,69 +40,68 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter { |
33 | 40 | } |
34 | 41 |
|
35 | 42 | // ** Old version of discover tests. |
36 | | - async runTests(uri: Uri, testIds: string[], debugBool?: boolean): Promise<ExecutionTestPayload> { |
| 43 | + async runTests( |
| 44 | + uri: Uri, |
| 45 | + testIds: string[], |
| 46 | + debugBool?: boolean, |
| 47 | + executionFactory?: IPythonExecutionFactory, |
| 48 | + ): Promise<ExecutionTestPayload> { |
37 | 49 | traceVerbose(uri, testIds, debugBool); |
| 50 | + if (executionFactory !== undefined) { |
| 51 | + return this.runTestsNew(uri, testIds, debugBool, executionFactory); |
| 52 | + } |
38 | 53 | // TODO:Remove this line after enabling runs |
| 54 | + // if executionFactory is undefined, we are using the old version of discover tests. |
39 | 55 | this.outputChannel.appendLine('Running tests.'); |
40 | 56 | this.deferred = createDeferred<ExecutionTestPayload>(); |
41 | 57 | return this.deferred.promise; |
42 | 58 | } |
43 | | -} |
44 | | - |
45 | | -// public async runTests( |
46 | | -// uri: Uri, |
47 | | -// testIds: string[], |
48 | | -// debugBool?: boolean, |
49 | | -// executionFactory?: IPythonExecutionFactory, |
50 | | -// ): Promise<ExecutionTestPayload> { |
51 | | -// const deferred = createDeferred<ExecutionTestPayload>(); |
52 | | -// const relativePathToPytest = 'pythonFiles'; |
53 | | -// const fullPluginPath = path.join(EXTENSION_ROOT_DIR, relativePathToPytest); |
54 | | -// this.configSettings.isTestExecution(); |
55 | | -// const uuid = this.testServer.createUUID(uri.fsPath); |
56 | | -// this.promiseMap.set(uuid, deferred); |
57 | | -// const settings = this.configSettings.getSettings(uri); |
58 | | -// const { pytestArgs } = settings.testing; |
59 | | - |
60 | | -// const pythonPathParts: string[] = process.env.PYTHONPATH?.split(path.delimiter) ?? []; |
61 | | -// const pythonPathCommand = [fullPluginPath, ...pythonPathParts].join(path.delimiter); |
62 | 59 |
|
63 | | -// const spawnOptions: SpawnOptions = { |
64 | | -// cwd: uri.fsPath, |
65 | | -// throwOnStdErr: true, |
66 | | -// extraVariables: { |
67 | | -// PYTHONPATH: pythonPathCommand, |
68 | | -// TEST_UUID: uuid.toString(), |
69 | | -// TEST_PORT: this.testServer.getPort().toString(), |
70 | | -// }, |
71 | | -// outputChannel: this.outputChannel, |
72 | | -// }; |
| 60 | + private async runTestsNew( |
| 61 | + uri: Uri, |
| 62 | + testIds: string[], |
| 63 | + debugBool?: boolean, |
| 64 | + executionFactory?: IPythonExecutionFactory, |
| 65 | + ): Promise<ExecutionTestPayload> { |
| 66 | + const deferred = createDeferred<ExecutionTestPayload>(); |
| 67 | + const relativePathToPytest = 'pythonFiles'; |
| 68 | + const fullPluginPath = path.join(EXTENSION_ROOT_DIR, relativePathToPytest); |
| 69 | + this.configSettings.isTestExecution(); |
| 70 | + const uuid = this.testServer.createUUID(uri.fsPath); |
| 71 | + this.promiseMap.set(uuid, deferred); |
| 72 | + const settings = this.configSettings.getSettings(uri); |
| 73 | + const { pytestArgs } = settings.testing; |
73 | 74 |
|
74 | | -// // Create the Python environment in which to execute the command. |
75 | | -// const creationOptions: ExecutionFactoryCreateWithEnvironmentOptions = { |
76 | | -// allowEnvironmentFetchExceptions: false, |
77 | | -// resource: uri, |
78 | | -// }; |
79 | | -// // need to check what will happen in the exec service is NOT defined and is null |
80 | | -// const execService = await executionFactory?.createActivatedEnvironment(creationOptions); |
| 75 | + const pythonPathParts: string[] = process.env.PYTHONPATH?.split(path.delimiter) ?? []; |
| 76 | + const pythonPathCommand = [fullPluginPath, ...pythonPathParts].join(path.delimiter); |
81 | 77 |
|
82 | | -// const testIdsString = testIds.join(' '); |
83 | | -// console.debug('what to do with debug bool?', debugBool); |
84 | | -// try { |
85 | | -// execService?.exec(['-m', 'pytest', '-p', 'vscode_pytest', testIdsString].concat(pytestArgs), spawnOptions); |
86 | | -// } catch (ex) { |
87 | | -// console.error(ex); |
88 | | -// } |
| 78 | + const spawnOptions: SpawnOptions = { |
| 79 | + cwd: uri.fsPath, |
| 80 | + throwOnStdErr: true, |
| 81 | + extraVariables: { |
| 82 | + PYTHONPATH: pythonPathCommand, |
| 83 | + TEST_UUID: uuid.toString(), |
| 84 | + TEST_PORT: this.testServer.getPort().toString(), |
| 85 | + }, |
| 86 | + outputChannel: this.outputChannel, |
| 87 | + }; |
89 | 88 |
|
90 | | -// return deferred.promise; |
91 | | -// } |
92 | | -// } |
| 89 | + // Create the Python environment in which to execute the command. |
| 90 | + const creationOptions: ExecutionFactoryCreateWithEnvironmentOptions = { |
| 91 | + allowEnvironmentFetchExceptions: false, |
| 92 | + resource: uri, |
| 93 | + }; |
| 94 | + // need to check what will happen in the exec service is NOT defined and is null |
| 95 | + const execService = await executionFactory?.createActivatedEnvironment(creationOptions); |
93 | 96 |
|
94 | | -// function buildExecutionCommand(args: string[]): TestExecutionCommand { |
95 | | -// const executionScript = path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'unittestadapter', 'execution.py'); |
| 97 | + const testIdsString = testIds.join(' '); |
| 98 | + console.debug('what to do with debug bool?', debugBool); |
| 99 | + try { |
| 100 | + execService?.exec(['-m', 'pytest', '-p', 'vscode_pytest', testIdsString].concat(pytestArgs), spawnOptions); |
| 101 | + } catch (ex) { |
| 102 | + console.error(ex); |
| 103 | + } |
96 | 104 |
|
97 | | -// return { |
98 | | -// script: executionScript, |
99 | | -// args: ['--udiscovery', ...args], |
100 | | -// }; |
101 | | -// } |
| 105 | + return deferred.promise; |
| 106 | + } |
| 107 | +} |
0 commit comments