Skip to content

Commit 51edb8f

Browse files
committed
refactor a bit
1 parent a240707 commit 51edb8f

File tree

5 files changed

+169
-93
lines changed

5 files changed

+169
-93
lines changed

editors/code/src/test_explorer/RunnableFacde.ts

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import { TargetKind, NodeKind, type TestLikeNodeKind, type TestLocation } from "
66

77
/**
88
* A wrapper of `ra.Runnable` to provide typed/cached information rather than string.
9+
*
10+
* An important asumption is the format of `label` is
11+
* - if test, "test test::path". Because test always has name, so the later part could not be empty.
12+
* - if test module, "test-mod test::path". Attention, the later part could be empty if it's the root module of a target.
913
*/
1014
export class RunnableFacde {
1115
public readonly origin: ra.Runnable;
@@ -30,16 +34,20 @@ export class RunnableFacde {
3034

3135
get workspaceRoot(): string {
3236
if (this._workspaceRoot) return this._workspaceRoot;
37+
3338
const workspaceRoot = this.origin.args.workspaceRoot;
39+
3440
assert(!!workspaceRoot);
41+
3542
return this._workspaceRoot = workspaceRoot;
3643
}
3744

3845
private _testKind?: TestLikeNodeKind;
3946

4047
get testKind(): TestLikeNodeKind {
41-
if (this._testKind) { return this._testKind; }
48+
if (this._testKind) return this._testKind;
4249
const testKindString = this.origin.label.split(' ')[0];
50+
4351
switch (testKindString) {
4452
case 'test':
4553
return this._testKind = NodeKind.Test;
@@ -81,7 +89,8 @@ export class RunnableFacde {
8189
private _targetKind?: TargetKind;
8290

8391
get targetKind(): TargetKind {
84-
if (this._targetKind) { return this._targetKind; }
92+
if (this._targetKind) return this._targetKind;
93+
8594
switch (true) {
8695
case this.origin.args.cargoArgs.includes("--lib"):
8796
return this._targetKind = TargetKind.Library;
@@ -97,11 +106,15 @@ export class RunnableFacde {
97106
private _packageName?: string;
98107

99108
get packageName(): string {
100-
if (this._packageName) { return this._packageName; }
109+
if (this._packageName) return this._packageName;
110+
101111
const packageQualifiedNameIndex = this.origin.args.cargoArgs.findIndex(arg => arg === "--package") + 1;
112+
102113
// The format of `packageQualifiedName` is `name:version`, like `hello:1.2.3`
103114
const packageQualifiedName = this.origin.args.cargoArgs[packageQualifiedNameIndex];
115+
104116
assert(!!packageQualifiedName, "There should be a value for '--package' in runnable");
117+
105118
return this._packageName = packageQualifiedName.split(':')[0]!;
106119
}
107120

@@ -111,14 +124,18 @@ export class RunnableFacde {
111124
* Only have value if `targetKind` is `TargetKind.IntegrationTest`
112125
*/
113126
get integrationTestFileName(): string | null {
114-
if (this._integrationTestFileName !== undefined) { return this._integrationTestFileName; }
127+
128+
if (this._integrationTestFileName !== undefined) return this._integrationTestFileName;
129+
115130
const integrationTestFileNameIndex = this.origin.args.cargoArgs.findIndex(arg => arg === "--test") + 1;
131+
116132
if (integrationTestFileNameIndex === 0) {
117133
this._integrationTestFileName = null;
118134
} else {
119135
this._integrationTestFileName = this.origin.args.cargoArgs[integrationTestFileNameIndex];
120136
assert(typeof this._integrationTestFileName === "string","There should be a value for '--test' in runnable");
121137
}
138+
122139
return this._integrationTestFileName;
123140
}
124141

@@ -128,22 +145,27 @@ export class RunnableFacde {
128145
* Only have value if `targetKind` is `TargetKind.Binary`
129146
*/
130147
get binaryTestFileName(): string | null {
131-
if (this._binaryTestFileName !== undefined) { return this._binaryTestFileName; }
148+
if (this._binaryTestFileName !== undefined) return this._binaryTestFileName;
149+
132150
const integrationTestFileNameIndex = this.origin.args.cargoArgs.findIndex(arg => arg === "--bin") + 1;
151+
133152
if (integrationTestFileNameIndex === 0) {
134153
this._binaryTestFileName = null;
135154
} else {
136155
this._binaryTestFileName = this.origin.args.cargoArgs[integrationTestFileNameIndex];
137156
assert(typeof this._binaryTestFileName === "string","There should be a value for '--bin' in runnable");
138157
}
158+
139159
return this._binaryTestFileName;
140160
}
141161

142162
private _uri?: vscode.Uri;
143163

144164
get uri(): vscode.Uri {
145-
if (this._uri) { return this._uri; }
165+
if (this._uri) return this._uri;
166+
146167
assert(!!this.origin.location?.targetUri, "Need to investigate why targetUri is undefined");
168+
147169
return this._uri = vscode.Uri.parse(this.origin.location.targetUri);
148170
}
149171

@@ -156,6 +178,7 @@ export class RunnableFacde {
156178
*/
157179
get isTestModuleDeclarationRunnable() {
158180
assert(this.testKind === NodeKind.TestModule, "Only compare definition for test module.");
181+
159182
return !this.isTestModuleFileDefinitionRunnable
160183
// filter out module with items
161184
// Not accurate. But who will write `mode xxx { ... }` in one line?
@@ -167,6 +190,7 @@ export class RunnableFacde {
167190
*/
168191
get isTestModuleWithItemsRunnable() {
169192
assert(this.testKind === NodeKind.TestModule, "Only compare definition for test module.");
193+
170194
return !this.isTestModuleFileDefinitionRunnable
171195
&& !this.isTestModuleDeclarationRunnable;
172196
}
@@ -176,8 +200,11 @@ export class RunnableFacde {
176200
*/
177201
get isTestModuleFileDefinitionRunnable() {
178202
const runnable = this.origin;
203+
179204
assert(this.testKind === NodeKind.TestModule, "Only compare definition for test module.");
205+
180206
assert(!!runnable.location, "Should always have location");
207+
181208
return isRangeValueEqual(
182209
runnable.location.targetRange,
183210
runnable.location.targetSelectionRange,

editors/code/src/test_explorer/RustcOutputAnalyzer.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import * as vscode from "vscode";
22
import { assert, assertNever } from "../util";
33
import { getTestItemByTestLikeNode, getTestModelByTestItem } from "./discover_and_update";
4-
import { CargoPackageNode, NodeKind, Nodes, TargetNode, TestModuleNode, TestNode, getPackageNodeOfTestModelNode, getWorkspaceNodeOfTestModelNode, testModelTree } from "./test_model_tree";
4+
import {
5+
type CargoPackageNode,
6+
DummyRootNode,
7+
NodeKind,
8+
type TargetNode,
9+
type TestModuleNode,
10+
type TestNode,
11+
getPackageNodeOfTestModelNode,
12+
} from "./test_model_tree";
513
import { sep } from 'node:path';
614

715
const targetPatternNamedCaptureGroup = {
@@ -93,23 +101,28 @@ abstract class RustcOutputAnalyzer {
93101
this._failureContextAnalyticsFlag = true;
94102
return;
95103
}
104+
96105
if (this._failureContextAnalyticsFlag && stacktraceEndPattern.test(line)) {
97106
this._failureContextAnalyticsFlag = false;
98107
this.flushStackTraceAnalytics();
99108
return;
100109
}
110+
101111
if (this._failureContextAnalyticsFlag) {
102112
const match = stacktraceTestCasePattern.exec(line);
103113
const rustcCasePath = match?.[1];
114+
104115
if (rustcCasePath) {
105116
const testItem = this._testItemLocator.findTestItemByRustcOutputCasePath(
106117
this._currentNormalizedTargetName!,
107118
this._currentTargetRelativePath!,
108119
rustcCasePath);
120+
109121
if (!testItem) { assert(false, "Should never happened. Could not bear this error."); }
110122
this.flushStackTraceAnalytics();
111123
this._currentFailedRustcOutputTest = testItem;
112124
}
125+
113126
this._currentFailedCaseOutputWithStackTrace.push(line);
114127
}
115128
}
@@ -154,12 +167,14 @@ class TestItemLocator {
154167
// We only allow one test case to be runned
155168
constructor(chosenRunnedTestItem: vscode.TestItem) {
156169
const node = getTestModelByTestItem(chosenRunnedTestItem);
170+
157171
assert(node.kind === NodeKind.Test
158172
|| node.kind === NodeKind.TestModule
159173
|| node.kind === NodeKind.Target
160174
|| node.kind === NodeKind.CargoPackage,
161175
"does not support workspace level, until we allow try to guess the target"
162176
);
177+
163178
this._testModel = node;
164179
}
165180

@@ -169,6 +184,7 @@ class TestItemLocator {
169184
findTestItemByRustcOutputCasePath(packageNormalizedName: string, targetRelativePath: string, path: string): vscode.TestItem | undefined {
170185
// const workspaceRootNode = getWorkspaceNodeOfTestModelNode(this._testModel);
171186
let targetNode = tryGetTargetNodeOfTestModelNode(this._testModel);
187+
172188
if (!targetNode) {
173189
const packageNode = getPackageNodeOfTestModelNode(this._testModel);
174190

@@ -183,10 +199,10 @@ class TestItemLocator {
183199

184200
assert(targetCandidates.length === 1, "should find one and only one target node, but they might have same name and relative path, although it should be really rare");
185201
// REVIEW: What should we do if we found 2 or more candidates?
186-
targetNode = targetCandidates[0];
202+
targetNode = targetCandidates[0]!; // safe, we have checked the length
187203
}
188204

189-
const testNode = testModelTree.findTestLikeNodeUnderTarget(
205+
const testNode = DummyRootNode.instance.findTestLikeNodeUnderTarget(
190206
targetNode,
191207
NodeKind.Test,
192208
path.split('::')

editors/code/src/test_explorer/TestItemControllerHelper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as vscode from "vscode";
1+
import type * as vscode from "vscode";
22
import { testController } from ".";
33

44
/**

0 commit comments

Comments
 (0)