Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/client/testing/testController/pytest/pytestExecutionAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,20 +233,29 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter {
traceInfo(`Running pytest with arguments: ${runArgs.join(' ')} for workspace ${uri.fsPath} \r\n`);

let resultProc: ChildProcess | undefined;
let processKilled = false;
const killResultProcess = () => {
if (resultProc && !processKilled) {
processKilled = true;
resultProc.kill();
return true;
}
return false;
};

runInstance.token.onCancellationRequested(() => {
traceInfo(`Test run cancelled, killing pytest subprocess for workspace ${uri.fsPath}`);
// if the resultProc exists just call kill on it which will handle resolving the ExecClose deferred, otherwise resolve the deferred here.
if (resultProc) {
resultProc?.kill();
} else {
if (!killResultProcess()) {
deferredTillExecClose.resolve();
serverCancel.cancel();
}
});

const result = execService?.execObservable(runArgs, spawnOptions);
resultProc = result?.proc;
if (runInstance.token.isCancellationRequested) {
killResultProcess();
}

Comment thread
eleanorjboyd marked this conversation as resolved.
// Take all output from the subprocess and add it to the test output channel. This will be the pytest output.
// Displays output to user and ensure the subprocess doesn't run into buffer overflow.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ suite('pytest test execution adapter', () => {
let mockProc: MockChildProcess;
let utilsWriteTestIdsFileStub: sinon.SinonStub;
let utilsStartRunResultNamedPipeStub: sinon.SinonStub;
let onExecObservable: (() => void) | undefined;

setup(() => {
onExecObservable = undefined;
useEnvExtensionStub = sinon.stub(extapi, 'useEnvExtension');
useEnvExtensionStub.returns(false);
configService = ({
Expand All @@ -59,6 +61,7 @@ suite('pytest test execution adapter', () => {
execService
.setup((x) => x.execObservable(typeMoq.It.isAny(), typeMoq.It.isAny()))
.returns(() => {
onExecObservable?.();
deferred4.resolve();
return {
proc: mockProc as any,
Expand Down Expand Up @@ -204,6 +207,25 @@ suite('pytest test execution adapter', () => {
await execution;
cancellationToken.dispose();
});
test('cancelling before the pytest subprocess is captured kills it once available', async () => {
utilsWriteTestIdsFileStub.resolves('testIdPipe-mockName');
utilsStartRunResultNamedPipeStub.callsFake((_callback, deferredTillServerClose, token) => {
token?.onCancellationRequested(() => deferredTillServerClose.resolve());
return Promise.resolve('runResultPipe-mockName');
});
const cancellationToken = new CancellationTokenSource();
const testRun = typeMoq.Mock.ofType<TestRun>();
testRun.setup((t) => t.token).returns(() => cancellationToken.token);
const killStub = sinon.stub(mockProc, 'kill');
onExecObservable = () => cancellationToken.cancel();
const uri = Uri.file(myTestPath);
adapter = new PytestTestExecutionAdapter(configService);

await adapter.runTests(uri, [], TestRunProfileKind.Run, testRun.object, execFactory.object);

sinon.assert.calledOnce(killStub);
cancellationToken.dispose();
});
test('pytest execution respects settings.testing.cwd when present', async () => {
const deferred2 = createDeferred();
const deferred3 = createDeferred();
Expand Down