Skip to content

Commit b0e7e6a

Browse files
committed
No longer fake the Local Tests tree
1 parent c082622 commit b0e7e6a

1 file changed

Lines changed: 60 additions & 13 deletions

File tree

src/localTests.ts

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,62 @@ import logger from './logger';
44

55
const isResolvedMap = new WeakMap<vscode.TestItem, boolean>();
66

7-
function resolveItemChildren(item: vscode.TestItem) {
7+
async function resolveItemChildren(item: vscode.TestItem) {
88
if (item) {
99
isResolvedMap.set(item, true);
10-
// Simulation of nested tests
11-
const depth = item.id.split('.').length;
12-
const isLeaf = depth > 4;
13-
const pkgSuffix = 'ABCD'.charAt(depth -1);
14-
for (let index = 1; index < (depth + 1); index++) {
15-
const child = localTestController.createTestItem(`${item.id}.${index}`, `${isLeaf ? 'MockClass' : (depth === 1 ? 'Mock' : '') + 'Pkg' + pkgSuffix}${index}`);
16-
child.canResolveChildren = !isLeaf;
17-
item.children.add(child);
10+
const itemUri = item.uri;
11+
if (itemUri) {
12+
item.busy = true;
13+
try {
14+
const contents = await vscode.workspace.fs.readDirectory(itemUri);
15+
contents.filter((entry) => entry[1] === vscode.FileType.Directory).forEach((entry) => {
16+
const name = entry[0];
17+
const child = localTestController.createTestItem(`${item.id}.${name}`, name, itemUri.with({path: `${itemUri.path}/${name}`}));
18+
child.canResolveChildren = true;
19+
item.children.add(child);
20+
});
21+
contents.filter((entry) => entry[1] === vscode.FileType.File).forEach((entry) => {
22+
const name = entry[0];
23+
if (name.endsWith('.cls')) {
24+
const child = localTestController.createTestItem(`${item.id}.${name}`, name, itemUri.with({path: `${itemUri.path}/${name}`}));
25+
child.canResolveChildren = true;
26+
item.children.add(child);
27+
}
28+
});
29+
} catch (error) {
30+
if (error.code !== vscode.FileSystemError.FileNotADirectory().code) {
31+
throw error;
32+
}
33+
if (itemUri.path.endsWith('.cls')) {
34+
try {
35+
const file = await vscode.workspace.fs.readFile(itemUri);
36+
const lines = file.toString().split('\n');
37+
for (let index = 0; index < lines.length; index++) {
38+
const lineText = lines[index];
39+
if (lineText.startsWith('Class ')) {
40+
if (!lineText.includes('%UnitTest.TestCase')) {
41+
break;
42+
}
43+
item.range = new vscode.Range(new vscode.Position(index, 0), new vscode.Position(index + 1, 0))
44+
}
45+
const match = lineText.match(/^Method Test(.+)\(/);
46+
if (match) {
47+
const testName = match[1];
48+
// const child = localTestController.createTestItem(`${item.id}.${testName}`, testName, itemUri.with({fragment: `L${index + 1}`}));
49+
const child = localTestController.createTestItem(`${item.id}.${testName}`, testName, itemUri);
50+
child.range = new vscode.Range(new vscode.Position(index, 0), new vscode.Position(index + 1, 0))
51+
child.canResolveChildren = false;
52+
item.children.add(child);
53+
}
54+
}
55+
console.log(file);
56+
} catch (error) {
57+
item.error = `${error.name ?? 'Unknown error'} - ${error.message ?? '(no message)'}`;
58+
}
59+
}
60+
} finally {
61+
item.busy = false;
62+
}
1863
}
1964
}
2065
else {
@@ -25,7 +70,7 @@ function resolveItemChildren(item: vscode.TestItem) {
2570
localTestController.createRunProfile('Debug Local Tests', vscode.TestRunProfileKind.Debug, runTestsHandler);
2671
//localTestController.createRunProfile('Test Coverage', vscode.TestRunProfileKind.Coverage, runTestsHandler);
2772
}
28-
}
73+
}
2974
}
3075

3176
export async function setupLocalTestsController() {
@@ -71,8 +116,8 @@ export async function runTestsHandler(request: vscode.TestRunRequest, cancellati
71116

72117
// Return result for leaf items
73118
if (test.children.size === 0) {
74-
const suffix = test.id.split('.').pop()
75-
switch (suffix) {
119+
const outcome = (Math.random() * 5 + 0.5).toFixed(0);
120+
switch (outcome) {
76121
case '1':
77122
run.skipped(test);
78123
break;
@@ -115,7 +160,9 @@ function replaceLocalRootItems(controller: vscode.TestController) {
115160
if (server?.serverName && server.namespace) {
116161
const key = folder.index.toString();
117162
if (!rootMap.has(key)) {
118-
const item = controller.createTestItem(key, folder.name);
163+
const relativeTestRoot = 'internal/testing/unit_tests';
164+
const item = controller.createTestItem(key, folder.name, folder.uri.with({path: `${folder.uri.path}/${relativeTestRoot}`}));
165+
item.description = relativeTestRoot;
119166
item.canResolveChildren = true;
120167
rootMap.set(key, item);
121168
}

0 commit comments

Comments
 (0)