Skip to content

Commit 24862fb

Browse files
committed
Adopt new api changes
1 parent 97e65fe commit 24862fb

File tree

6 files changed

+116
-77
lines changed

6 files changed

+116
-77
lines changed

extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildClient.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@ public void onBuildTaskStart(TaskStartParams params) {
150150
// ignore the suite start message as the display name.
151151
displayName = null;
152152
}
153-
String testIdentifier = getTestIdentifier(testStartEx.getTestName());
153+
List<String> testParts = getTestParts(testStartEx.getTestName());
154154
lsClient.sendNotification(new ExecuteCommandParams("java.gradle.buildServer.onDidChangeTestItemStatus",
155-
Arrays.asList(testIdentifier, 2/*Running status*/, displayName)));
155+
Arrays.asList(testParts, 2/*Running status*/, displayName)));
156156
} else {
157157
Either<String, Integer> id = Either.forLeft(params.getTaskId().getId());
158158
lsClient.createProgress(new WorkDoneProgressCreateParams(id));
@@ -187,7 +187,7 @@ public void onBuildTaskFinish(TaskFinishParams params) {
187187
}
188188
} else if (Objects.equals(params.getDataKind(), TaskDataKind.TEST_FINISH)) {
189189
TestFinishEx testFinishEx = JSONUtility.toModel(params.getData(), TestFinishEx.class);
190-
String testIdentifier = getTestIdentifier(testFinishEx.getTestName());
190+
List<String> testParts = getTestParts(testFinishEx.getTestName());
191191
JavaTestStatus testStatus = switch (testFinishEx.getStatus()) {
192192
case PASSED -> JavaTestStatus.Passed;
193193
case FAILED -> JavaTestStatus.Failed;
@@ -198,7 +198,7 @@ public void onBuildTaskFinish(TaskFinishParams params) {
198198
throw new IllegalArgumentException("Unsupported test status: " + testFinishEx.getStatus());
199199
}
200200
lsClient.sendNotification(new ExecuteCommandParams("java.gradle.buildServer.onDidChangeTestItemStatus",
201-
Arrays.asList(testIdentifier, testStatus.getValue(), null, testFinishEx.getStackTrace()))); // TODO: test duration is missing
201+
Arrays.asList(testParts, testStatus.getValue(), null, testFinishEx.getStackTrace()))); // TODO: test duration is missing
202202
} else if (Objects.equals(params.getDataKind(), TaskDataKind.TEST_REPORT)) {
203203
lsClient.sendNotification(new ExecuteCommandParams("java.gradle.buildServer.onDidFinishTestRun",
204204
Arrays.asList(params.getTaskId().getId(), params.getMessage())));
@@ -214,9 +214,9 @@ public void onBuildTaskFinish(TaskFinishParams params) {
214214
/**
215215
* Currently, the test name returned from gradle build server is started from the class name,
216216
* then follows the method or invocation name.
217-
* @return The test identifier
217+
* @return The test identifier parts
218218
*/
219-
private String getTestIdentifier(TestName testName) {
219+
private List<String> getTestParts(TestName testName) {
220220
List<String> testNames = new LinkedList<>();
221221
while (testName != null) {
222222
if (testName.getSuiteName() != null) {
@@ -241,7 +241,7 @@ private String getTestIdentifier(TestName testName) {
241241
}
242242
}
243243

244-
return String.join("#", testNames.subList(i, testNames.size()));
244+
return testNames.subList(i, testNames.size());
245245
}
246246

247247
private class LruCache<T> extends LinkedHashSet<T> {

extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/handler/GradleDelegateCommandHandler.java

+2-25
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
import java.util.List;
44
import java.util.Map;
55
import java.util.stream.Collectors;
6-
import java.util.ArrayList;
76
import java.util.Collections;
8-
import java.util.HashMap;
97
import java.util.LinkedList;
108

119
import org.eclipse.core.resources.IProject;
@@ -33,7 +31,7 @@ public Object executeCommand(String commandId, List<Object> arguments, IProgress
3331
switch (commandId) {
3432
case "java.gradle.delegateTest":
3533
String projectName = (String) arguments.get(0);
36-
List<String> tests = (ArrayList<String>) arguments.get(1);
34+
Map<String, List<String>> tests = JSONUtility.toModel(arguments.get(1), Map.class);
3735
IProject project = ProjectUtils.getProject(projectName);
3836
if (project == null) {
3937
throw new IllegalArgumentException("Project not found: " + projectName);
@@ -56,9 +54,8 @@ public Object executeCommand(String commandId, List<Object> arguments, IProgress
5654
TestParams testParams = new TestParams(btIds);
5755
testParams.setDataKind("scala-test-suites-selection");
5856
testParams.setArguments(getArguments(arguments));
59-
Map<String, List<String>> groupedTests = groupTests(tests);
6057
List<ScalaTestSuiteSelection> testSelections = new LinkedList<>();
61-
for (Map.Entry<String, List<String>> entry : groupedTests.entrySet()) {
58+
for (Map.Entry<String, List<String>> entry : tests.entrySet()) {
6259
ScalaTestSuiteSelection testSelection = new ScalaTestSuiteSelection(
6360
entry.getKey(),
6461
entry.getValue()
@@ -79,26 +76,6 @@ public Object executeCommand(String commandId, List<Object> arguments, IProgress
7976
throw new UnsupportedOperationException("The command: " + commandId + "is not supported.");
8077
}
8178

82-
private Map<String, List<String>> groupTests(List<String> tests) {
83-
Map<String, List<String>> groupedTests = new HashMap<>();
84-
85-
for (String test : tests) {
86-
String[] parts = test.split("#");
87-
String className = parts[0];
88-
String methodName = parts.length > 1 ? parts[1] : null;
89-
90-
groupedTests.computeIfAbsent(className, k -> new ArrayList<>());
91-
if (methodName != null) {
92-
if (methodName.contains("(")) {
93-
methodName = methodName.substring(0, methodName.indexOf('(')); // gradle test task doesn't support method with parameters
94-
}
95-
groupedTests.get(className).add(methodName);
96-
}
97-
}
98-
99-
return groupedTests;
100-
}
101-
10279
private List<String> getArguments(List<Object> arguments) {
10380
if (arguments.size() < 3) {
10481
return Collections.emptyList();

extension/src/Extension.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
import { instrumentOperation, sendInfo } from "vscode-extension-telemetry-wrapper";
3636
import { GradleBuildContentProvider } from "./client/GradleBuildContentProvider";
3737
import { BuildServerController } from "./bs/BuildServerController";
38+
import { GradleTestRunner } from "./bs/GradleTestRunner";
3839

3940
export class Extension {
4041
private readonly client: GradleClient;
@@ -230,11 +231,10 @@ export class Extension {
230231
const testExtension = vscode.extensions.getExtension("vscjava.vscode-java-test");
231232
if (testExtension) {
232233
testExtension.activate().then((api: any) => {
233-
api.registerTestProfile(
234-
"Delegate Test to Gradle",
235-
vscode.TestRunProfileKind.Run,
236-
this.buildServerController.getGradleTestRunner()
237-
);
234+
if (api) {
235+
const testRunner: GradleTestRunner = this.buildServerController.getGradleTestRunner(api);
236+
api.registerTestProfile("Delegate Test to Gradle", vscode.TestRunProfileKind.Run, testRunner);
237+
}
238238
});
239239
}
240240
const activated = !!(await this.rootProjectsStore.getProjectRoots()).length;

extension/src/bs/BuildServerController.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ export class BuildServerController implements Disposable {
2626
private disposable: Disposable;
2727
private buildOutputChannel: OutputChannel;
2828
private logOutputChannel: OutputChannel;
29-
private gradleTestRunner: GradleTestRunner;
29+
private gradleTestRunner: GradleTestRunner | undefined;
3030

3131
public constructor(readonly context: ExtensionContext) {
32-
this.gradleTestRunner = new GradleTestRunner();
3332
this.buildOutputChannel = window.createOutputChannel("Build Server for Gradle (Build)", "gradle-build");
3433
this.logOutputChannel = window.createOutputChannel("Build Server for Gradle (Log)");
3534
this.disposable = Disposable.from(
@@ -89,13 +88,13 @@ export class BuildServerController implements Disposable {
8988
commands.registerCommand(
9089
"java.gradle.buildServer.onDidFinishTestRun",
9190
(status: number, message?: string) => {
92-
this.gradleTestRunner.finishTestRun(status, message);
91+
this.gradleTestRunner?.finishTestRun(status, message);
9392
}
9493
),
9594
commands.registerCommand(
9695
"java.gradle.buildServer.onDidChangeTestItemStatus",
97-
(test: string, state: number, displayName?: string, message?: string, duration?: number) => {
98-
this.gradleTestRunner.updateTestItem(test, state, displayName, message, duration);
96+
(testParts: string[], state: number, displayName?: string, message?: string, duration?: number) => {
97+
this.gradleTestRunner?.updateTestItem(testParts, state, displayName, message, duration);
9998
}
10099
),
101100
workspace.onDidChangeConfiguration((e: ConfigurationChangeEvent) => {
@@ -125,7 +124,10 @@ export class BuildServerController implements Disposable {
125124
this.checkMachineStatus();
126125
}
127126

128-
public getGradleTestRunner(): GradleTestRunner {
127+
public getGradleTestRunner(testRunnerApi: any): GradleTestRunner {
128+
if (!this.gradleTestRunner) {
129+
this.gradleTestRunner = new GradleTestRunner(testRunnerApi);
130+
}
129131
return this.gradleTestRunner;
130132
}
131133

extension/src/bs/GradleTestRunner.ts

+38-10
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,43 @@
11
import * as vscode from "vscode";
2-
import { TestRunner, TestItemStatusChangeEvent, TestFinishEvent, IRunTestContext } from "../java-test-runner.api";
2+
import {
3+
TestRunner,
4+
TestItemStatusChangeEvent,
5+
TestFinishEvent,
6+
IRunTestContext,
7+
TestIdParts,
8+
} from "../java-test-runner.api";
39

410
export class GradleTestRunner implements TestRunner {
511
private readonly _onDidChangeTestItemStatus = new vscode.EventEmitter<TestItemStatusChangeEvent>();
612
private readonly _onDidFinishTestRun = new vscode.EventEmitter<TestFinishEvent>();
13+
private context: IRunTestContext;
14+
private testRunnerApi: any;
715

816
public onDidChangeTestItemStatus: vscode.Event<TestItemStatusChangeEvent> = this._onDidChangeTestItemStatus.event;
917
public onDidFinishTestRun: vscode.Event<TestFinishEvent> = this._onDidFinishTestRun.event;
1018

19+
constructor(testRunnerApi: any) {
20+
this.testRunnerApi = testRunnerApi;
21+
}
22+
1123
public launch(context: IRunTestContext): void {
12-
const tests: string[] = context.testItems.map((testItem) => {
24+
this.context = context;
25+
const tests: Map<string, string[]> = new Map();
26+
context.testItems.forEach((testItem) => {
1327
const id = testItem.id;
14-
if (id.includes("@")) {
15-
return id.slice(id.indexOf("@") + 1);
28+
const parts: TestIdParts = this.testRunnerApi.parsePartsFromTestId(id);
29+
if (!parts.class) {
30+
return;
1631
}
17-
return id;
32+
const testMethods = tests.get(parts.class) || [];
33+
if (parts.invocations?.length) {
34+
let methodId = parts.invocations[0];
35+
if (methodId.includes("(")) {
36+
methodId = methodId.slice(0, methodId.indexOf("(")); // gradle test task doesn't support method with parameters
37+
}
38+
testMethods.push(methodId);
39+
}
40+
tests.set(parts.class, testMethods);
1841
});
1942

2043
const agrs = context.testConfig?.args;
@@ -24,15 +47,15 @@ export class GradleTestRunner implements TestRunner {
2447
"java.execute.workspaceCommand",
2548
"java.gradle.delegateTest",
2649
context.projectName,
27-
tests,
50+
JSON.stringify([...tests]),
2851
agrs,
2952
vmArgs,
3053
env
3154
);
3255
}
3356

3457
public updateTestItem(
35-
test: string,
58+
testParts: string[],
3659
state: number,
3760
displayName?: string,
3861
message?: string,
@@ -41,18 +64,23 @@ export class GradleTestRunner implements TestRunner {
4164
if (message) {
4265
message = this.filterStackTrace(message);
4366
}
67+
const testId = this.testRunnerApi.parseTestIdFromParts({
68+
project: this.context.projectName,
69+
class: testParts[0],
70+
invocations: testParts.slice(1),
71+
});
4472
this._onDidChangeTestItemStatus.fire({
45-
test,
73+
testId,
4674
state,
4775
displayName,
4876
message,
4977
duration,
5078
});
5179
}
5280

53-
public finishTestRun(status: number, message?: string): void {
81+
public finishTestRun(statusCode: number, message?: string): void {
5482
this._onDidFinishTestRun.fire({
55-
status,
83+
statusCode,
5684
message,
5785
});
5886
}

0 commit comments

Comments
 (0)