forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.ts
More file actions
49 lines (47 loc) · 1.88 KB
/
utils.ts
File metadata and controls
49 lines (47 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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);
}