Skip to content

Add Pytest Logging On Workspace Level #21133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 26, 2023
Merged
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
4 changes: 4 additions & 0 deletions src/client/common/utils/localize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,13 @@ export namespace Testing {
export const testNotConfigured = l10n.t('No test framework configured.');
export const cancelUnittestDiscovery = l10n.t('Canceled unittest test discovery');
export const errorUnittestDiscovery = l10n.t('Unittest test discovery error');
export const cancelPytestDiscovery = l10n.t('Canceled pytest test discovery');
export const errorPytestDiscovery = l10n.t('pytest test discovery error');
export const seePythonOutput = l10n.t('(see Output > Python)');
export const cancelUnittestExecution = l10n.t('Canceled unittest test execution');
export const errorUnittestExecution = l10n.t('Unittest test execution error');
export const cancelPytestExecution = l10n.t('Canceled pytest test execution');
export const errorPytestExecution = l10n.t('Pytest test execution error');
}

export namespace OutdatedDebugger {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,11 @@ export class PytestTestDiscoveryAdapter implements ITestDiscoveryAdapter {
resource: uri,
};
const execService = await executionFactory.createActivatedEnvironment(creationOptions);

try {
execService.exec(
['-m', 'pytest', '-p', 'vscode_pytest', '--collect-only'].concat(pytestArgs),
spawnOptions,
);
} catch (ex) {
console.error(ex);
}
execService
.exec(['-m', 'pytest', '-p', 'vscode_pytest', '--collect-only'].concat(pytestArgs), spawnOptions)
.catch((ex) => {
deferred.reject(ex as Error);
});
return deferred.promise;
}
}
27 changes: 18 additions & 9 deletions src/client/testing/testController/workspaceTestAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,17 @@ export class WorkspaceTestAdapter {
// handle token and telemetry here
sendTelemetryEvent(EventName.UNITTEST_RUN_ALL_FAILED, undefined);

const cancel = token?.isCancellationRequested
let cancel = token?.isCancellationRequested
? Testing.cancelUnittestExecution
: Testing.errorUnittestExecution;
if (this.testProvider === 'pytest') {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking for a suggestion for the best way to do this, would it work better as a double ternary statement?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double ternary statements are not allowed by eslint I think, this looks fine.

cancel = token?.isCancellationRequested ? Testing.cancelPytestExecution : Testing.errorPytestExecution;
}
traceError(`${cancel}\r\n`, ex);

// Also report on the test view
const message = util.format(`${cancel} ${Testing.seePythonOutput}\r\n`, ex);
const options = buildErrorNodeOptions(this.workspaceUri, message);
const options = buildErrorNodeOptions(this.workspaceUri, message, this.testProvider);
const errorNode = createErrorTestItem(testController, options);
testController.items.add(errorNode);

Expand Down Expand Up @@ -310,15 +313,18 @@ export class WorkspaceTestAdapter {
} catch (ex) {
sendTelemetryEvent(EventName.UNITTEST_DISCOVERY_DONE, undefined, { tool: this.testProvider, failed: true });

const cancel = token?.isCancellationRequested
let cancel = token?.isCancellationRequested
? Testing.cancelUnittestDiscovery
: Testing.errorUnittestDiscovery;
if (this.testProvider === 'pytest') {
cancel = token?.isCancellationRequested ? Testing.cancelPytestDiscovery : Testing.errorPytestDiscovery;
}

traceError(`${cancel}\r\n`, ex);

// Report also on the test view.
const message = util.format(`${cancel} ${Testing.seePythonOutput}\r\n`, ex);
const options = buildErrorNodeOptions(this.workspaceUri, message);
const options = buildErrorNodeOptions(this.workspaceUri, message, this.testProvider);
const errorNode = createErrorTestItem(testController, options);
testController.items.add(errorNode);

Expand All @@ -336,17 +342,19 @@ export class WorkspaceTestAdapter {

// Check if there were any errors in the discovery process.
if (rawTestData.status === 'error') {
const testingErrorConst =
this.testProvider === 'pytest' ? Testing.errorPytestDiscovery : Testing.errorUnittestDiscovery;
const { errors } = rawTestData;
traceError(Testing.errorUnittestDiscovery, '\r\n', errors!.join('\r\n\r\n'));
traceError(testingErrorConst, '\r\n', errors!.join('\r\n\r\n'));

let errorNode = testController.items.get(`DiscoveryError:${workspacePath}`);
const message = util.format(
`${Testing.errorUnittestDiscovery} ${Testing.seePythonOutput}\r\n`,
`${testingErrorConst} ${Testing.seePythonOutput}\r\n`,
errors!.join('\r\n\r\n'),
);

if (errorNode === undefined) {
const options = buildErrorNodeOptions(this.workspaceUri, message);
const options = buildErrorNodeOptions(this.workspaceUri, message, this.testProvider);
errorNode = createErrorTestItem(testController, options);
testController.items.add(errorNode);
}
Expand Down Expand Up @@ -462,10 +470,11 @@ function populateTestTree(
});
}

function buildErrorNodeOptions(uri: Uri, message: string): ErrorTestItemOptions {
function buildErrorNodeOptions(uri: Uri, message: string, testType: string): ErrorTestItemOptions {
const labelText = testType === 'pytest' ? 'Pytest Discovery Error' : 'Unittest Discovery Error';
return {
id: `DiscoveryError:${uri.fsPath}`,
label: `Unittest Discovery Error [${path.basename(uri.fsPath)}]`,
label: `${labelText} [${path.basename(uri.fsPath)}]`,
error: message,
};
}