Skip to content

Commit faaf5e2

Browse files
committed
add copy test id menu item for pytest
1 parent a57b431 commit faaf5e2

File tree

5 files changed

+40
-4
lines changed

5 files changed

+40
-4
lines changed

package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,11 @@
272272
"category": "Python",
273273
"command": "python.createNewFile"
274274
},
275+
{
276+
"category": "Python",
277+
"command": "python.copyTestId",
278+
"title": "%python.command.python.testing.copyTestId.title%"
279+
},
275280
{
276281
"category": "Python",
277282
"command": "python.analysis.restartLanguageServer",
@@ -1231,6 +1236,13 @@
12311236
"command": "python.reportIssue"
12321237
}
12331238
],
1239+
"testing/item/context": [
1240+
{
1241+
"command": "python.copyTestId",
1242+
"group": "navigation",
1243+
"when": "resourceLangId == 'python'"
1244+
}
1245+
],
12341246
"commandPalette": [
12351247
{
12361248
"category": "Python",
@@ -1306,6 +1318,12 @@
13061318
"title": "%python.command.python.execSelectionInTerminal.title%",
13071319
"when": "!virtualWorkspace && shellExecutionSupported && editorLangId == python"
13081320
},
1321+
{
1322+
"category": "Python",
1323+
"command": "python.copyTestId",
1324+
"title": "%python.command.python.testing.copyTestId.title%",
1325+
"when": "false"
1326+
},
13091327
{
13101328
"category": "Python",
13111329
"command": "python.execInREPL",

package.nls.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"python.command.python.analysis.restartLanguageServer.title": "Restart Language Server",
2828
"python.command.python.launchTensorBoard.title": "Launch TensorBoard",
2929
"python.command.python.refreshTensorBoard.title": "Refresh TensorBoard",
30+
"python.command.python.testing.copyTestId.title": "Copy Test Id",
3031
"python.createEnvironment.contentButton.description": "Show or hide Create Environment button in the editor for `requirements.txt` or other dependency files.",
3132
"python.createEnvironment.trigger.description": "Detect if environment creation is required for the current project",
3233
"python.menu.createNewFile.title": "Python File",

src/client/common/application/commands.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
'use strict';
55

6-
import { CancellationToken, Position, TextDocument, Uri } from 'vscode';
6+
import { CancellationToken, Position, TestItem, TextDocument, Uri } from 'vscode';
77
import { Commands as LSCommands } from '../../activation/commands';
88
import { Channel, Commands, CommandSource } from '../constants';
99
import { CreateEnvironmentOptions } from '../../pythonEnvironments/creation/proposed.createEnvApis';
@@ -50,6 +50,7 @@ export type AllCommands = keyof ICommandNameArgumentTypeMapping;
5050
* Used to provide strong typing for command & args.
5151
*/
5252
export interface ICommandNameArgumentTypeMapping extends ICommandNameWithoutArgumentTypeMapping {
53+
[Commands.CopyTestId]: [TestItem];
5354
[Commands.Create_Environment]: [CreateEnvironmentOptions];
5455
['vscode.openWith']: [Uri, string];
5556
['workbench.action.quickOpen']: [string];

src/client/common/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export namespace Commands {
3939
export const CreateNewFile = 'python.createNewFile';
4040
export const ClearWorkspaceInterpreter = 'python.clearWorkspaceInterpreter';
4141
export const Create_Environment = 'python.createEnvironment';
42+
export const CopyTestId = 'python.copyTestId';
4243
export const Create_Environment_Button = 'python.createEnvironment-button';
4344
export const Create_Environment_Check = 'python.createEnvironmentCheck';
4445
export const Create_Terminal = 'python.createTerminal';

src/client/testing/main.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
'use strict';
22

33
import { inject, injectable } from 'inversify';
4-
import { ConfigurationChangeEvent, Disposable, Uri, tests, TestResultState, WorkspaceFolder, Command } from 'vscode';
4+
import {
5+
ConfigurationChangeEvent,
6+
Disposable,
7+
Uri,
8+
tests,
9+
TestResultState,
10+
WorkspaceFolder,
11+
Command,
12+
TestItem,
13+
} from 'vscode';
14+
import { env } from 'vscode';
515
import { IApplicationShell, ICommandManager, IContextKeyManager, IWorkspaceService } from '../common/application/types';
616
import * as constants from '../common/constants';
717
import '../common/extensions';
@@ -20,7 +30,7 @@ import { DelayedTrigger, IDelayedTrigger } from '../common/utils/delayTrigger';
2030
import { ExtensionContextKey } from '../common/application/contextKeys';
2131
import { checkForFailedTests, updateTestResultMap } from './testController/common/testItemUtilities';
2232
import { Testing } from '../common/utils/localize';
23-
import { traceVerbose } from '../logging';
33+
import { traceLog, traceVerbose } from '../logging';
2434

2535
@injectable()
2636
export class TestingService implements ITestingService {
@@ -158,7 +168,6 @@ export class UnitTestManagementService implements IExtensionActivationService {
158168

159169
private registerCommands(): void {
160170
const commandManager = this.serviceContainer.get<ICommandManager>(ICommandManager);
161-
162171
this.disposableRegistry.push(
163172
commandManager.registerCommand(
164173
constants.Commands.Tests_Configure,
@@ -195,6 +204,12 @@ export class UnitTestManagementService implements IExtensionActivationService {
195204
},
196205
};
197206
}),
207+
commandManager.registerCommand(constants.Commands.CopyTestId, async (testItem: TestItem) => {
208+
if (testItem && typeof testItem.id === 'string') {
209+
await env.clipboard.writeText(testItem.id);
210+
traceLog('Testing: Copied test id to clipboard, id: ' + testItem.id);
211+
}
212+
}),
198213
);
199214
}
200215

0 commit comments

Comments
 (0)