From bdf10674abe3f8c8b3e69dd6a1403c8d2fe3f5f5 Mon Sep 17 00:00:00 2001 From: Subhash Arabhi Date: Thu, 12 Jun 2025 14:17:05 +0530 Subject: [PATCH] Fixing test related issues Signed-off-by: Subhash Arabhi --- patches/8470-draft.diff | 129 ++++++++++++++++++++++++++++++++--- vscode/src/commands/debug.ts | 41 +---------- 2 files changed, 122 insertions(+), 48 deletions(-) diff --git a/patches/8470-draft.diff b/patches/8470-draft.diff index b03ca27..e0b0ae7 100644 --- a/patches/8470-draft.diff +++ b/patches/8470-draft.diff @@ -1,18 +1,129 @@ diff --git a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/debugging/launch/NbLaunchDelegate.java b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/debugging/launch/NbLaunchDelegate.java -index 01d92d5c82a3..49676ecae0a7 100644 +index a4c951ae8d8f..a8ca4f2143de 100644 --- a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/debugging/launch/NbLaunchDelegate.java +++ b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/debugging/launch/NbLaunchDelegate.java -@@ -563,12 +563,7 @@ public void finished(boolean success) { - protected static @CheckForNull Pair findTarget(Project prj, boolean preferProjActions, FileObject toRun, SingleMethod singleMethod, NestedClass nestedClass, boolean debug, boolean testRun, boolean testInParallel, ContainedProjectFilter projectFilter) { +@@ -129,7 +129,7 @@ protected void notifyFinished(DebugAdapterContext ctx, boolean success) { + + public final CompletableFuture nbLaunch(FileObject toRun, boolean preferProjActions, @NullAllowed File nativeImageFile, + @NullAllowed String method, @NullAllowed String nestedClassName, Map launchArguments, DebugAdapterContext context, +- boolean debug, boolean testRun, Consumer consoleMessages, ++ boolean debug, LaunchType launchType, Consumer consoleMessages, + boolean testInParallel) { + CompletableFuture launchFuture = new CompletableFuture<>(); + NbProcessConsole ioContext = new NbProcessConsole(consoleMessages); +@@ -200,7 +200,7 @@ public void close() throws IOException { + } + } + W writer = new W(); +- CompletableFuture> commandFuture = findTargetWithPossibleRebuild(prj, preferProjActions, toRun, singleMethod, nestedClass, debug, testRun, ioContext, testInParallel, projectFilter); ++ CompletableFuture> commandFuture = findTargetWithPossibleRebuild(prj, preferProjActions, toRun, singleMethod, nestedClass, debug, launchType, ioContext, testInParallel, projectFilter); + commandFuture.thenAccept((providerAndCommand) -> { + ExplicitProcessParameters params = createExplicitProcessParameters(launchArguments); + OperationContext ctx = OperationContext.find(Lookup.getDefault()); +@@ -512,8 +512,8 @@ static List argsToStringList(Object o) { + } + } + +- private static CompletableFuture> findTargetWithPossibleRebuild(Project proj, boolean preferProjActions, FileObject toRun, SingleMethod singleMethod, NestedClass nestedClass, boolean debug, boolean testRun, NbProcessConsole ioContext, boolean testInParallel, ContainedProjectFilter projectFilter) throws IllegalArgumentException { +- Pair providerAndCommand = findTarget(proj, preferProjActions, toRun, singleMethod, nestedClass, debug, testRun, testInParallel, projectFilter); ++ private static CompletableFuture> findTargetWithPossibleRebuild(Project proj, boolean preferProjActions, FileObject toRun, SingleMethod singleMethod, NestedClass nestedClass, boolean debug, LaunchType launchType, NbProcessConsole ioContext, boolean testInParallel, ContainedProjectFilter projectFilter) throws IllegalArgumentException { ++ Pair providerAndCommand = findTarget(proj, preferProjActions, toRun, singleMethod, nestedClass, debug, launchType, testInParallel, projectFilter); + if (providerAndCommand != null) { + return CompletableFuture.completedFuture(providerAndCommand); + } +@@ -529,7 +529,7 @@ protected void started() { + @Override + public void finished(boolean success) { + if (success) { +- Pair providerAndCommand = findTarget(proj, preferProjActions, toRun, singleMethod, nestedClass, debug, testRun, testInParallel, projectFilter); ++ Pair providerAndCommand = findTarget(proj, preferProjActions, toRun, singleMethod, nestedClass, debug, launchType, testInParallel, projectFilter); + if (providerAndCommand != null) { + afterBuild.complete(providerAndCommand); + return; +@@ -562,14 +562,16 @@ public void finished(boolean success) { + return afterBuild; + } + +- protected static @CheckForNull Pair findTarget(Project prj, boolean preferProjActions, FileObject toRun, SingleMethod singleMethod, NestedClass nestedClass, boolean debug, boolean testRun, boolean testInParallel, ContainedProjectFilter projectFilter) { ++ protected static @CheckForNull Pair findTarget(Project prj, boolean preferProjActions, FileObject toRun, SingleMethod singleMethod, NestedClass nestedClass, boolean debug, LaunchType launchType, boolean testInParallel, ContainedProjectFilter projectFilter) { ClassPath sourceCP = ClassPath.getClassPath(toRun, ClassPath.SOURCE); - FileObject fileRoot = sourceCP != null ? sourceCP.findOwnerRoot(toRun) : null; -- boolean mainSource; +- FileObject fileRoot = sourceCP != null ? sourceCP.findOwnerRoot(toRun) : null; + boolean mainSource; - if (fileRoot != null) { - mainSource = UnitTestForSourceQuery.findUnitTests(fileRoot).length > 0; -- } else { ++ if (launchType == LaunchType.RUN_MAIN) { ++ mainSource = true; ++ } else if (launchType == LaunchType.RUN_TEST) { ++ mainSource = false; + } else { - mainSource = !testRun; -- } -+ boolean mainSource = !testRun; ++ FileObject fileRoot = sourceCP != null ? sourceCP.findOwnerRoot(toRun) : null; ++ mainSource = fileRoot != null && UnitTestForSourceQuery.findUnitTests(fileRoot).length > 0; + } ActionProvider provider = null; String command = null; - Collection actionProviders = findActionProviders(prj); +@@ -723,4 +725,21 @@ private static Collection findNestedActionProviders(Project prj, + } + return actionProviders; + } ++ ++ public enum LaunchType { ++ AUTODETECT, ++ RUN_MAIN, ++ RUN_TEST; ++ ++ static LaunchType from(Map launchArguments) { ++ Object testRunValue = launchArguments.get("testRun"); ++ ++ if (testRunValue instanceof Boolean) { ++ Boolean testRunSetting = (Boolean) testRunValue; ++ return testRunSetting ? RUN_TEST : RUN_MAIN; ++ } else { ++ return AUTODETECT; ++ } ++ } ++ } + } +diff --git a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/debugging/launch/NbLaunchRequestHandler.java b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/debugging/launch/NbLaunchRequestHandler.java +index ea6138764b46..b1a472ce99a4 100644 +--- a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/debugging/launch/NbLaunchRequestHandler.java ++++ b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/debugging/launch/NbLaunchRequestHandler.java +@@ -52,6 +52,7 @@ + import org.netbeans.modules.java.lsp.server.LspServerState; + import org.netbeans.modules.java.lsp.server.debugging.DebugAdapterContext; + import org.netbeans.modules.java.lsp.server.debugging.NbSourceProvider; ++import org.netbeans.modules.java.lsp.server.debugging.launch.NbLaunchDelegate.LaunchType; + import org.netbeans.modules.java.lsp.server.debugging.utils.ErrorUtilities; + import org.netbeans.spi.java.classpath.support.ClassPathSupport; + import org.openide.DialogDescriptor; +@@ -88,10 +89,10 @@ public CompletableFuture launch(Map launchArguments, Debug + String filePath = (String)launchArguments.get("file"); + String projectFilePath = (String)launchArguments.get("projectFile"); + String mainFilePath = (String)launchArguments.get("mainClass"); +- boolean testRun = (Boolean) launchArguments.getOrDefault("testRun", Boolean.FALSE); ++ LaunchType launchType = LaunchType.from(launchArguments); + + if (!isNative && (StringUtils.isBlank(mainFilePath) && StringUtils.isBlank(filePath) && StringUtils.isBlank(projectFilePath) +- || modulePaths.isEmpty() && classPaths.isEmpty()) && !testRun) { ++ || modulePaths.isEmpty() && classPaths.isEmpty()) && launchType != LaunchType.RUN_TEST) { + if (modulePaths.isEmpty() && classPaths.isEmpty()) { + ErrorUtilities.completeExceptionally(resultFuture, + "Failed to launch debuggee VM. Missing modulePaths/classPaths options in launch configuration.", +@@ -207,6 +208,8 @@ public CompletableFuture launch(Map launchArguments, Debug + filePath = projectFilePath; + } + boolean preferProjActions = true; // True when we prefer project actions to the current (main) file actions. ++ Object preferProj = launchArguments.get("project"); ++ if(preferProj instanceof Boolean) preferProjActions = (Boolean) preferProj; + FileObject file = null; + File nativeImageFile = null; + if (!isNative) { +@@ -293,7 +296,7 @@ public CompletableFuture launch(Map launchArguments, Debug + String singleMethod = (String)launchArguments.get("methodName"); + String nestedClass = (String)launchArguments.get("nestedClass"); + boolean testInParallel = (Boolean) launchArguments.getOrDefault("testInParallel", Boolean.FALSE); +- activeLaunchHandler.nbLaunch(file, preferProjActions, nativeImageFile, singleMethod, nestedClass, launchArguments, context, !noDebug, testRun, new OutputListener(context), testInParallel).thenRun(() -> { ++ activeLaunchHandler.nbLaunch(file, preferProjActions, nativeImageFile, singleMethod, nestedClass, launchArguments, context, !noDebug, launchType, new OutputListener(context), testInParallel).thenRun(() -> { + activeLaunchHandler.postLaunch(launchArguments, context); + resultFuture.complete(null); + }).exceptionally(e -> { diff --git a/vscode/src/commands/debug.ts b/vscode/src/commands/debug.ts index dbed23c..9a19806 100644 --- a/vscode/src/commands/debug.ts +++ b/vscode/src/commands/debug.ts @@ -51,8 +51,7 @@ const packageTest = async (uri: any, launchConfiguration? : string) => { const runDebug = async (noDebug: boolean, testRun: boolean, uri: any, methodName?: string, launchConfiguration?: string, project : boolean = false, ) => { const docUri = getContextUri(uri); if (docUri) { - // attempt to find the active configuration in the vsode launch settings; undefined if no config is there. - let debugConfig : vscode.DebugConfiguration = await findRunConfiguration(docUri) || { + let debugConfig : vscode.DebugConfiguration = { type: extConstants.COMMAND_PREFIX, name: "Java Single Debug", request: "launch" @@ -71,7 +70,7 @@ const runDebug = async (noDebug: boolean, testRun: boolean, uri: any, methodName const workspaceFolder = vscode.workspace.getWorkspaceFolder(docUri); if (project || testRun) { debugConfig['projectFile'] = docUri.toString(); - debugConfig['project'] = true; + debugConfig['project'] = project; } else { debugConfig['mainClass'] = docUri.toString(); } @@ -79,7 +78,6 @@ const runDebug = async (noDebug: boolean, testRun: boolean, uri: any, methodName noDebug: noDebug, } - const ret = await vscode.debug.startDebugging(workspaceFolder, debugConfig, debugOptions); return ret ? new Promise((resolve) => { const listener = vscode.debug.onDidTerminateDebugSession(() => { @@ -90,41 +88,6 @@ const runDebug = async (noDebug: boolean, testRun: boolean, uri: any, methodName } }; - -async function findRunConfiguration(uri : vscode.Uri) : Promise { - // do not invoke debug start with no (jdk) configurations, as it would probably create an user prompt - let cfg = vscode.workspace.getConfiguration("launch"); - let c = cfg.get('configurations'); - if (!Array.isArray(c)) { - return undefined; - } - let f = c.filter((v) => v['type'] === extConstants.COMMAND_PREFIX); - if (!f.length) { - return undefined; - } - class P implements vscode.DebugConfigurationProvider { - config : vscode.DebugConfiguration | undefined; - - resolveDebugConfigurationWithSubstitutedVariables(folder: vscode.WorkspaceFolder | undefined, debugConfiguration: vscode.DebugConfiguration, token?: vscode.CancellationToken): vscode.ProviderResult { - this.config = debugConfiguration; - return undefined; - } - } - let provider = new P(); - let d = vscode.debug.registerDebugConfigurationProvider(extConstants.COMMAND_PREFIX, provider); - // let vscode to select a debug config - return await vscode.commands.executeCommand(builtInCommands.startDebug, { config: { - type: extConstants.COMMAND_PREFIX, - mainClass: uri.toString() - }, noDebug: true}).then((v) => { - d.dispose(); - return provider.config; - }, (err) => { - d.dispose(); - return undefined; - }); -} - export const registerDebugCommands: ICommand[] = [ { command: extCommands.runTest,