|
| 1 | +import { TestItem, env } from 'vscode'; |
| 2 | +import { traceLog } from '../logging'; |
| 3 | + |
| 4 | +export async function writeTestIdToClipboard(testItem: TestItem): Promise<void> { |
| 5 | + if (testItem && typeof testItem.id === 'string') { |
| 6 | + if (testItem.id.includes('\\') && testItem.id.indexOf('::') === -1) { |
| 7 | + // Convert the id to a module.class.method format as this is a unittest |
| 8 | + const moduleClassMethod = idToModuleClassMethod(testItem.id); |
| 9 | + if (moduleClassMethod) { |
| 10 | + await env.clipboard.writeText(moduleClassMethod); |
| 11 | + traceLog('Testing: Copied test id to clipboard, id: ' + moduleClassMethod); |
| 12 | + return; |
| 13 | + } |
| 14 | + } |
| 15 | + // Otherwise use the id as is for pytest |
| 16 | + await clipboardWriteText(testItem.id); |
| 17 | + traceLog('Testing: Copied test id to clipboard, id: ' + testItem.id); |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +export function idToModuleClassMethod(id: string): string | undefined { |
| 22 | + // Split by backslash |
| 23 | + const parts = id.split('\\'); |
| 24 | + if (parts.length === 1) { |
| 25 | + // Only one part, likely a parent folder or file |
| 26 | + return parts[0]; |
| 27 | + } |
| 28 | + if (parts.length === 2) { |
| 29 | + // Two parts: filePath and className |
| 30 | + const [filePath, className] = parts.slice(-2); |
| 31 | + const fileName = filePath.split(/[\\/]/).pop(); |
| 32 | + if (!fileName) { |
| 33 | + return undefined; |
| 34 | + } |
| 35 | + const module = fileName.replace(/\.py$/, ''); |
| 36 | + return `${module}.${className}`; |
| 37 | + } |
| 38 | + // Three or more parts: filePath, className, methodName |
| 39 | + const [filePath, className, methodName] = parts.slice(-3); |
| 40 | + const fileName = filePath.split(/[\\/]/).pop(); |
| 41 | + if (!fileName) { |
| 42 | + return undefined; |
| 43 | + } |
| 44 | + const module = fileName.replace(/\.py$/, ''); |
| 45 | + return `${module}.${className}.${methodName}`; |
| 46 | +} |
| 47 | +export function clipboardWriteText(text: string): Thenable<void> { |
| 48 | + return env.clipboard.writeText(text); |
| 49 | +} |
0 commit comments