|
| 1 | +import * as vscode from 'vscode' |
| 2 | +import * as path from 'path' |
| 3 | + |
| 4 | +import {DocumentTestItem, LanguageTools, TestFileContents} from './manager' |
| 5 | +import {TestFinish} from '../bsp/bsp' |
| 6 | +import {SourceFileTestCaseInfo, TestCaseInfo} from '../test-info/test-info' |
| 7 | +import {BaseLanguageTools} from './base' |
| 8 | +import {JUnitStyleTestCaseData, TestFinishDataKind} from '../bsp/bsp-ext' |
| 9 | +import * as bsp from '../bsp/bsp' |
| 10 | + |
| 11 | +const TEST_FILE_REGEX = /^.+\.(test|spec)\.ts$/ |
| 12 | + |
| 13 | +export class TypeScriptLanguageTools |
| 14 | + extends BaseLanguageTools |
| 15 | + implements LanguageTools |
| 16 | +{ |
| 17 | + static inferSourcesFromJestTarget( |
| 18 | + targetUri: string, |
| 19 | + baseDirectory: string | undefined |
| 20 | + ): bsp.SourcesResult | undefined { |
| 21 | + if (!targetUri.endsWith('_jest') || !baseDirectory) { |
| 22 | + return undefined |
| 23 | + } |
| 24 | + |
| 25 | + const colonIndex = targetUri.lastIndexOf(':') |
| 26 | + if (colonIndex === -1) { |
| 27 | + return undefined |
| 28 | + } |
| 29 | + |
| 30 | + const targetName = targetUri.slice(colonIndex + 1, -5) |
| 31 | + const isTestFile = targetName.includes('_test_') |
| 32 | + const baseName = targetName |
| 33 | + .replace(/_spec_ts_library$/, '') |
| 34 | + .replace(/_test_ts_library$/, '') |
| 35 | + .replace(/_ts_library$/, '') |
| 36 | + .replace(/_spec$/, '') |
| 37 | + .replace(/_test$/, '') |
| 38 | + const extension = isTestFile ? '.test.ts' : '.spec.ts' |
| 39 | + const fileName = baseName.replace(/_/g, '-') + extension |
| 40 | + const fileUri = `${baseDirectory}/${fileName}` |
| 41 | + |
| 42 | + return { |
| 43 | + items: [ |
| 44 | + { |
| 45 | + target: {uri: targetUri}, |
| 46 | + sources: [ |
| 47 | + { |
| 48 | + uri: fileUri, |
| 49 | + kind: bsp.SourceItemKind.File, |
| 50 | + generated: false, |
| 51 | + }, |
| 52 | + ], |
| 53 | + roots: [], |
| 54 | + }, |
| 55 | + ], |
| 56 | + } |
| 57 | + } |
| 58 | + mapTestFinishDataToLookupKey(testFinishData: TestFinish): string | undefined { |
| 59 | + if ( |
| 60 | + testFinishData.dataKind === TestFinishDataKind.JUnitStyleTestCaseData && |
| 61 | + testFinishData.data |
| 62 | + ) { |
| 63 | + const testCaseData = testFinishData.data as JUnitStyleTestCaseData |
| 64 | + return testCaseData.className || testFinishData.displayName |
| 65 | + } |
| 66 | + return undefined |
| 67 | + } |
| 68 | + |
| 69 | + mapTestCaseInfoToLookupKey(testCaseInfo: TestCaseInfo): string | undefined { |
| 70 | + if (!(testCaseInfo instanceof SourceFileTestCaseInfo)) { |
| 71 | + return undefined |
| 72 | + } |
| 73 | + |
| 74 | + const data = testCaseInfo.getDocumentTestItem() |
| 75 | + return data?.lookupKey |
| 76 | + } |
| 77 | + |
| 78 | + async getDocumentTestCases( |
| 79 | + document: vscode.Uri, |
| 80 | + workspaceRoot: string |
| 81 | + ): Promise<TestFileContents> { |
| 82 | + if (!TEST_FILE_REGEX.test(path.basename(document.fsPath))) { |
| 83 | + return { |
| 84 | + isTestFile: false, |
| 85 | + testCases: [], |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + const fileContents = await vscode.workspace.fs.readFile(document) |
| 90 | + const text = fileContents.toString() |
| 91 | + const lines = text.split('\n') |
| 92 | + |
| 93 | + const testCases: DocumentTestItem[] = [] |
| 94 | + const documentTest: DocumentTestItem = { |
| 95 | + name: path.basename(document.fsPath), |
| 96 | + range: new vscode.Range(0, 0, 0, 0), |
| 97 | + uri: document, |
| 98 | + testFilter: '', |
| 99 | + } |
| 100 | + |
| 101 | + const describeStack: DocumentTestItem[] = [] |
| 102 | + const indentStack: number[] = [] |
| 103 | + |
| 104 | + for (let lineNum = 0; lineNum < lines.length; lineNum++) { |
| 105 | + const line = lines[lineNum] |
| 106 | + const indent = line.search(/\S/) |
| 107 | + |
| 108 | + if (indent === -1) continue |
| 109 | + |
| 110 | + while ( |
| 111 | + indentStack.length > 0 && |
| 112 | + indent <= indentStack[indentStack.length - 1] |
| 113 | + ) { |
| 114 | + describeStack.pop() |
| 115 | + indentStack.pop() |
| 116 | + } |
| 117 | + |
| 118 | + const describeMatch = line.match(/describe\s*\(\s*['"`]([^'"`]+)['"`]/) |
| 119 | + if (describeMatch) { |
| 120 | + const position = new vscode.Position(lineNum, 0) |
| 121 | + const describeName = describeMatch[1] |
| 122 | + const parent = |
| 123 | + describeStack.length > 0 |
| 124 | + ? describeStack[describeStack.length - 1] |
| 125 | + : undefined |
| 126 | + |
| 127 | + const lookupKey = parent?.lookupKey |
| 128 | + ? `${parent.lookupKey} ${describeName}` |
| 129 | + : describeName |
| 130 | + |
| 131 | + const describeItem: DocumentTestItem = { |
| 132 | + name: describeName, |
| 133 | + range: new vscode.Range(position, position), |
| 134 | + uri: document, |
| 135 | + testFilter: describeName, |
| 136 | + parent: parent, |
| 137 | + lookupKey: lookupKey, |
| 138 | + } |
| 139 | + testCases.push(describeItem) |
| 140 | + describeStack.push(describeItem) |
| 141 | + indentStack.push(indent) |
| 142 | + continue |
| 143 | + } |
| 144 | + |
| 145 | + const testMatch = line.match(/(?:it|test)\s*\(\s*['"`]([^'"`]+)['"`]/) |
| 146 | + if (testMatch) { |
| 147 | + const position = new vscode.Position(lineNum, 0) |
| 148 | + const testName = testMatch[1] |
| 149 | + const parent = |
| 150 | + describeStack.length > 0 |
| 151 | + ? describeStack[describeStack.length - 1] |
| 152 | + : undefined |
| 153 | + |
| 154 | + const lookupKey = parent?.lookupKey |
| 155 | + ? `${parent.lookupKey} ${testName}` |
| 156 | + : testName |
| 157 | + |
| 158 | + const testItem: DocumentTestItem = { |
| 159 | + name: testName, |
| 160 | + range: new vscode.Range(position, position), |
| 161 | + uri: document, |
| 162 | + testFilter: testName, |
| 163 | + parent: parent, |
| 164 | + lookupKey: lookupKey, |
| 165 | + } |
| 166 | + testCases.push(testItem) |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + return { |
| 171 | + isTestFile: true, |
| 172 | + testCases: testCases, |
| 173 | + documentTest: documentTest, |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments