Skip to content
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
18 changes: 18 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@
"category": "Python",
"command": "python.createNewFile"
},
{
"category": "Python",
"command": "python.copyTestId",
"title": "%python.command.python.testing.copyTestId.title%"
},
{
"category": "Python",
"command": "python.analysis.restartLanguageServer",
Expand Down Expand Up @@ -1231,6 +1236,13 @@
"command": "python.reportIssue"
}
],
"testing/item/context": [
{
"command": "python.copyTestId",
"group": "navigation",
"when": "controllerId == 'python-tests'"
}
],
"commandPalette": [
{
"category": "Python",
Expand Down Expand Up @@ -1306,6 +1318,12 @@
"title": "%python.command.python.execSelectionInTerminal.title%",
"when": "!virtualWorkspace && shellExecutionSupported && editorLangId == python"
},
{
"category": "Python",
"command": "python.copyTestId",
"title": "%python.command.python.testing.copyTestId.title%",
"when": "false"
},
{
"category": "Python",
"command": "python.execInREPL",
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"python.command.python.analysis.restartLanguageServer.title": "Restart Language Server",
"python.command.python.launchTensorBoard.title": "Launch TensorBoard",
"python.command.python.refreshTensorBoard.title": "Refresh TensorBoard",
"python.command.python.testing.copyTestId.title": "Copy Test Id",
"python.createEnvironment.contentButton.description": "Show or hide Create Environment button in the editor for `requirements.txt` or other dependency files.",
"python.createEnvironment.trigger.description": "Detect if environment creation is required for the current project",
"python.menu.createNewFile.title": "Python File",
Expand Down
3 changes: 2 additions & 1 deletion src/client/common/application/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

'use strict';

import { CancellationToken, Position, TextDocument, Uri } from 'vscode';
import { CancellationToken, Position, TestItem, TextDocument, Uri } from 'vscode';
import { Commands as LSCommands } from '../../activation/commands';
import { Channel, Commands, CommandSource } from '../constants';
import { CreateEnvironmentOptions } from '../../pythonEnvironments/creation/proposed.createEnvApis';
Expand Down Expand Up @@ -50,6 +50,7 @@ export type AllCommands = keyof ICommandNameArgumentTypeMapping;
* Used to provide strong typing for command & args.
*/
export interface ICommandNameArgumentTypeMapping extends ICommandNameWithoutArgumentTypeMapping {
[Commands.CopyTestId]: [TestItem];
[Commands.Create_Environment]: [CreateEnvironmentOptions];
['vscode.openWith']: [Uri, string];
['workbench.action.quickOpen']: [string];
Expand Down
1 change: 1 addition & 0 deletions src/client/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export namespace Commands {
export const CreateNewFile = 'python.createNewFile';
export const ClearWorkspaceInterpreter = 'python.clearWorkspaceInterpreter';
export const Create_Environment = 'python.createEnvironment';
export const CopyTestId = 'python.copyTestId';
export const Create_Environment_Button = 'python.createEnvironment-button';
export const Create_Environment_Check = 'python.createEnvironmentCheck';
export const Create_Terminal = 'python.createTerminal';
Expand Down
16 changes: 14 additions & 2 deletions src/client/testing/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
'use strict';

import { inject, injectable } from 'inversify';
import { ConfigurationChangeEvent, Disposable, Uri, tests, TestResultState, WorkspaceFolder, Command } from 'vscode';
import {
ConfigurationChangeEvent,
Disposable,
Uri,
tests,
TestResultState,
WorkspaceFolder,
Command,
TestItem,
} from 'vscode';
import { IApplicationShell, ICommandManager, IContextKeyManager, IWorkspaceService } from '../common/application/types';
import * as constants from '../common/constants';
import '../common/extensions';
Expand All @@ -21,6 +30,7 @@ import { ExtensionContextKey } from '../common/application/contextKeys';
import { checkForFailedTests, updateTestResultMap } from './testController/common/testItemUtilities';
import { Testing } from '../common/utils/localize';
import { traceVerbose } from '../logging';
import { writeTestIdToClipboard } from './utils';

@injectable()
export class TestingService implements ITestingService {
Expand Down Expand Up @@ -158,7 +168,6 @@ export class UnitTestManagementService implements IExtensionActivationService {

private registerCommands(): void {
const commandManager = this.serviceContainer.get<ICommandManager>(ICommandManager);

this.disposableRegistry.push(
commandManager.registerCommand(
constants.Commands.Tests_Configure,
Expand Down Expand Up @@ -195,6 +204,9 @@ export class UnitTestManagementService implements IExtensionActivationService {
},
};
}),
commandManager.registerCommand(constants.Commands.CopyTestId, async (testItem: TestItem) => {
writeTestIdToClipboard(testItem);
}),
);
}

Expand Down
49 changes: 49 additions & 0 deletions src/client/testing/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { TestItem, env } from 'vscode';
import { traceLog } from '../logging';

export async function writeTestIdToClipboard(testItem: TestItem): Promise<void> {
if (testItem && typeof testItem.id === 'string') {
if (testItem.id.includes('\\') && testItem.id.indexOf('::') === -1) {
// Convert the id to a module.class.method format as this is a unittest
const moduleClassMethod = idToModuleClassMethod(testItem.id);
if (moduleClassMethod) {
await env.clipboard.writeText(moduleClassMethod);
traceLog('Testing: Copied test id to clipboard, id: ' + moduleClassMethod);
return;
}
}
// Otherwise use the id as is for pytest
await clipboardWriteText(testItem.id);
traceLog('Testing: Copied test id to clipboard, id: ' + testItem.id);
}
}

export function idToModuleClassMethod(id: string): string | undefined {
// Split by backslash
const parts = id.split('\\');
if (parts.length === 1) {
// Only one part, likely a parent folder or file
return parts[0];
}
if (parts.length === 2) {
// Two parts: filePath and className
const [filePath, className] = parts.slice(-2);
const fileName = filePath.split(/[\\/]/).pop();
if (!fileName) {
return undefined;
}
const module = fileName.replace(/\.py$/, '');
return `${module}.${className}`;
}
// Three or more parts: filePath, className, methodName
const [filePath, className, methodName] = parts.slice(-3);
const fileName = filePath.split(/[\\/]/).pop();
if (!fileName) {
return undefined;
}
const module = fileName.replace(/\.py$/, '');
return `${module}.${className}.${methodName}`;
}
export function clipboardWriteText(text: string): Thenable<void> {
return env.clipboard.writeText(text);
}
51 changes: 51 additions & 0 deletions src/test/testing/utils.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { expect, use } from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import * as utils from '../../client/testing/utils';
import sinon from 'sinon';
use(chaiAsPromised.default);

function test_idToModuleClassMethod() {
try {
expect(utils.idToModuleClassMethod('foo')).to.equal('foo');
expect(utils.idToModuleClassMethod('a/b/c.pyMyClass')).to.equal('c.MyClass');
expect(utils.idToModuleClassMethod('a/b/c.pyMyClassmy_method')).to.equal('c.MyClass.my_method');
expect(utils.idToModuleClassMethod('\\MyClass')).to.be.undefined;
console.log('test_idToModuleClassMethod passed');
} catch (e) {
console.error('test_idToModuleClassMethod failed:', e);
}
}

async function test_writeTestIdToClipboard() {
let clipboardStub = sinon.stub(utils, 'clipboardWriteText').resolves();
const { writeTestIdToClipboard } = utils;
try {
// unittest id
const testItem = { id: 'a/b/c.pyMyClass\\my_method' };
await writeTestIdToClipboard(testItem as any);
sinon.assert.calledOnceWithExactly(clipboardStub, 'c.MyClass.my_method');
clipboardStub.resetHistory();

// pytest id
const testItem2 = { id: 'tests/test_foo.py::TestClass::test_method' };
await writeTestIdToClipboard(testItem2 as any);
sinon.assert.calledOnceWithExactly(clipboardStub, 'tests/test_foo.py::TestClass::test_method');
clipboardStub.resetHistory();

// undefined
await writeTestIdToClipboard(undefined as any);
sinon.assert.notCalled(clipboardStub);

console.log('test_writeTestIdToClipboard passed');
} catch (e) {
console.error('test_writeTestIdToClipboard failed:', e);
} finally {
sinon.restore();
}
}

// Run tests
(async () => {
test_idToModuleClassMethod();
await test_writeTestIdToClipboard();
})();