Skip to content

Commit eace651

Browse files
committed
More test fixes
1 parent 665a17d commit eace651

File tree

4 files changed

+102
-12
lines changed

4 files changed

+102
-12
lines changed

src/client/testing/testController/pytest/pytestDiscoveryAdapter.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ export class PytestTestDiscoveryAdapter implements ITestDiscoveryAdapter {
104104
if (useEnvExtension()) {
105105
const pythonEnv = await getEnvironment(uri);
106106
if (pythonEnv) {
107+
const deferredTillExecClose: Deferred<void> = createTestingDeferred();
108+
107109
const proc = await runInBackground(pythonEnv, {
108110
cwd,
109111
args: execArgs,
@@ -126,7 +128,9 @@ export class PytestTestDiscoveryAdapter implements ITestDiscoveryAdapter {
126128
`Subprocess exited unsuccessfully with exit code ${code} and signal ${signal} on workspace ${uri.fsPath}`,
127129
);
128130
}
131+
deferredTillExecClose.resolve();
129132
});
133+
await deferredTillExecClose.promise;
130134
} else {
131135
traceError(`Python Environment not found for: ${uri.fsPath}`);
132136
}

src/client/testing/testController/pytest/pytestExecutionAdapter.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter {
181181
} else if (useEnvExtension()) {
182182
const pythonEnv = await getEnvironment(uri);
183183
if (pythonEnv) {
184+
const deferredTillExecClose: Deferred<void> = utils.createTestingDeferred();
185+
184186
const scriptPath = path.join(fullPluginPath, 'vscode_pytest', 'run_pytest_script.py');
185187
const runArgs = [scriptPath, ...testArgs];
186188
traceInfo(`Running pytest with arguments: ${runArgs.join(' ')} for workspace ${uri.fsPath} \r\n`);
@@ -193,6 +195,8 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter {
193195
runInstance?.token.onCancellationRequested(() => {
194196
traceInfo(`Test run cancelled, killing pytest subprocess for workspace ${uri.fsPath}`);
195197
proc.kill();
198+
deferredTillExecClose.resolve();
199+
serverCancel.cancel();
196200
});
197201
proc.stdout.on('data', (data) => {
198202
const out = utils.fixLogLinesNoTrailing(data.toString());
@@ -211,7 +215,10 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter {
211215
`Subprocess exited unsuccessfully with exit code ${code} and signal ${signal} on workspace ${uri.fsPath}`,
212216
);
213217
}
218+
deferredTillExecClose.resolve();
219+
serverCancel.cancel();
214220
});
221+
await deferredTillExecClose.promise;
215222
} else {
216223
traceError(`Python Environment not found for: ${uri.fsPath}`);
217224
}

src/client/testing/testController/unittest/testDiscoveryAdapter.ts

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
startDiscoveryNamedPipe,
2828
} from '../common/utils';
2929
import { traceError, traceInfo, traceLog } from '../../../logging';
30+
import { getEnvironment, runInBackground, useEnvExtension } from '../../../envExt/api.internal';
3031

3132
/**
3233
* Wrapper class for unittest test discovery. This is where we call `runTestCommand`.
@@ -86,6 +87,47 @@ export class UnittestTestDiscoveryAdapter implements ITestDiscoveryAdapter {
8687
...(await this.envVarsService?.getEnvironmentVariables(uri)),
8788
};
8889
mutableEnv.TEST_RUN_PIPE = testRunPipeName;
90+
const args = [options.command.script].concat(options.command.args);
91+
92+
if (options.outChannel) {
93+
options.outChannel.appendLine(`python ${args.join(' ')}`);
94+
}
95+
96+
if (useEnvExtension()) {
97+
const pythonEnv = await getEnvironment(uri);
98+
if (pythonEnv) {
99+
const deferredTillExecClose = createDeferred();
100+
101+
const proc = await runInBackground(pythonEnv, {
102+
cwd,
103+
args,
104+
env: (mutableEnv as unknown) as { [key: string]: string },
105+
});
106+
proc.stdout.on('data', (data) => {
107+
const out = fixLogLinesNoTrailing(data.toString());
108+
traceInfo(out);
109+
this.outputChannel?.append(out);
110+
});
111+
proc.stderr.on('data', (data) => {
112+
const out = fixLogLinesNoTrailing(data.toString());
113+
traceError(out);
114+
this.outputChannel?.append(out);
115+
});
116+
proc.onExit((code, signal) => {
117+
this.outputChannel?.append(MESSAGE_ON_TESTING_OUTPUT_MOVE);
118+
if (code !== 0) {
119+
traceError(
120+
`Subprocess exited unsuccessfully with exit code ${code} and signal ${signal} on workspace ${uri.fsPath}`,
121+
);
122+
}
123+
deferredTillExecClose.resolve();
124+
});
125+
await deferredTillExecClose.promise;
126+
} else {
127+
traceError(`Python Environment not found for: ${uri.fsPath}`);
128+
}
129+
return;
130+
}
89131

90132
const spawnOptions: SpawnOptions = {
91133
token: options.token,
@@ -94,23 +136,18 @@ export class UnittestTestDiscoveryAdapter implements ITestDiscoveryAdapter {
94136
outputChannel: options.outChannel,
95137
env: mutableEnv,
96138
};
97-
// Create the Python environment in which to execute the command.
98-
const creationOptions: ExecutionFactoryCreateWithEnvironmentOptions = {
99-
allowEnvironmentFetchExceptions: false,
100-
resource: options.workspaceFolder,
101-
};
102-
const execService = await executionFactory?.createActivatedEnvironment(creationOptions);
103-
104-
const args = [options.command.script].concat(options.command.args);
105-
106-
if (options.outChannel) {
107-
options.outChannel.appendLine(`python ${args.join(' ')}`);
108-
}
109139

110140
try {
111141
traceLog(`Discovering unittest tests for workspace ${options.cwd} with arguments: ${args}\r\n`);
112142
const deferredTillExecClose = createDeferred<ExecutionResult<string>>();
113143

144+
// Create the Python environment in which to execute the command.
145+
const creationOptions: ExecutionFactoryCreateWithEnvironmentOptions = {
146+
allowEnvironmentFetchExceptions: false,
147+
resource: options.workspaceFolder,
148+
};
149+
const execService = await executionFactory?.createActivatedEnvironment(creationOptions);
150+
114151
const result = execService?.execObservable(args, spawnOptions);
115152

116153
// Displays output to user and ensure the subprocess doesn't run into buffer overflow.

src/client/testing/testController/unittest/testExecutionAdapter.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
import { ITestDebugLauncher, LaunchOptions } from '../../common/types';
2727
import { UNITTEST_PROVIDER } from '../../common/constants';
2828
import * as utils from '../common/utils';
29+
import { getEnvironment, runInBackground, useEnvExtension } from '../../../envExt/api.internal';
2930

3031
/**
3132
* Wrapper Class for unittest test execution. This is where we call `runTestCommand`?
@@ -182,6 +183,47 @@ export class UnittestTestExecutionAdapter implements ITestExecutionAdapter {
182183
},
183184
sessionOptions,
184185
);
186+
} else if (useEnvExtension()) {
187+
const pythonEnv = await getEnvironment(uri);
188+
if (pythonEnv) {
189+
traceInfo(`Running unittest with arguments: ${args.join(' ')} for workspace ${uri.fsPath} \r\n`);
190+
const deferredTillExecClose = createDeferred();
191+
192+
const proc = await runInBackground(pythonEnv, {
193+
cwd,
194+
args,
195+
env: (mutableEnv as unknown) as { [key: string]: string },
196+
});
197+
runInstance?.token.onCancellationRequested(() => {
198+
traceInfo(`Test run cancelled, killing unittest subprocess for workspace ${uri.fsPath}`);
199+
proc.kill();
200+
deferredTillExecClose.resolve();
201+
serverCancel.cancel();
202+
});
203+
proc.stdout.on('data', (data) => {
204+
const out = utils.fixLogLinesNoTrailing(data.toString());
205+
runInstance?.appendOutput(out);
206+
this.outputChannel?.append(out);
207+
});
208+
proc.stderr.on('data', (data) => {
209+
const out = utils.fixLogLinesNoTrailing(data.toString());
210+
runInstance?.appendOutput(out);
211+
this.outputChannel?.append(out);
212+
});
213+
proc.onExit((code, signal) => {
214+
this.outputChannel?.append(utils.MESSAGE_ON_TESTING_OUTPUT_MOVE);
215+
if (code !== 0) {
216+
traceError(
217+
`Subprocess exited unsuccessfully with exit code ${code} and signal ${signal} on workspace ${uri.fsPath}`,
218+
);
219+
}
220+
deferredTillExecClose.resolve();
221+
serverCancel.cancel();
222+
});
223+
await deferredTillExecClose.promise;
224+
} else {
225+
traceError(`Python Environment not found for: ${uri.fsPath}`);
226+
}
185227
} else {
186228
// This means it is running the test
187229
traceInfo(`Running unittests for workspace ${cwd} with arguments: ${args}\r\n`);

0 commit comments

Comments
 (0)