Skip to content

Fixing test related issues #432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 120 additions & 9 deletions patches/8470-draft.diff
Original file line number Diff line number Diff line change
@@ -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<ActionProvider, String> 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<Void> nbLaunch(FileObject toRun, boolean preferProjActions, @NullAllowed File nativeImageFile,
@NullAllowed String method, @NullAllowed String nestedClassName, Map<String, Object> launchArguments, DebugAdapterContext context,
- boolean debug, boolean testRun, Consumer<NbProcessConsole.ConsoleMessage> consoleMessages,
+ boolean debug, LaunchType launchType, Consumer<NbProcessConsole.ConsoleMessage> consoleMessages,
boolean testInParallel) {
CompletableFuture<Void> launchFuture = new CompletableFuture<>();
NbProcessConsole ioContext = new NbProcessConsole(consoleMessages);
@@ -200,7 +200,7 @@ public void close() throws IOException {
}
}
W writer = new W();
- CompletableFuture<Pair<ActionProvider, String>> commandFuture = findTargetWithPossibleRebuild(prj, preferProjActions, toRun, singleMethod, nestedClass, debug, testRun, ioContext, testInParallel, projectFilter);
+ CompletableFuture<Pair<ActionProvider, String>> 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<String> argsToStringList(Object o) {
}
}

- private static CompletableFuture<Pair<ActionProvider, String>> findTargetWithPossibleRebuild(Project proj, boolean preferProjActions, FileObject toRun, SingleMethod singleMethod, NestedClass nestedClass, boolean debug, boolean testRun, NbProcessConsole ioContext, boolean testInParallel, ContainedProjectFilter projectFilter) throws IllegalArgumentException {
- Pair<ActionProvider, String> providerAndCommand = findTarget(proj, preferProjActions, toRun, singleMethod, nestedClass, debug, testRun, testInParallel, projectFilter);
+ private static CompletableFuture<Pair<ActionProvider, String>> findTargetWithPossibleRebuild(Project proj, boolean preferProjActions, FileObject toRun, SingleMethod singleMethod, NestedClass nestedClass, boolean debug, LaunchType launchType, NbProcessConsole ioContext, boolean testInParallel, ContainedProjectFilter projectFilter) throws IllegalArgumentException {
+ Pair<ActionProvider, String> 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<ActionProvider, String> providerAndCommand = findTarget(proj, preferProjActions, toRun, singleMethod, nestedClass, debug, testRun, testInParallel, projectFilter);
+ Pair<ActionProvider, String> 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<ActionProvider, String> findTarget(Project prj, boolean preferProjActions, FileObject toRun, SingleMethod singleMethod, NestedClass nestedClass, boolean debug, boolean testRun, boolean testInParallel, ContainedProjectFilter projectFilter) {
+ protected static @CheckForNull Pair<ActionProvider, String> 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<ActionProvider> actionProviders = findActionProviders(prj);
@@ -723,4 +725,21 @@ private static Collection<ActionProvider> findNestedActionProviders(Project prj,
}
return actionProviders;
}
+
+ public enum LaunchType {
+ AUTODETECT,
+ RUN_MAIN,
+ RUN_TEST;
+
+ static LaunchType from(Map<String, Object> 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<Void> launch(Map<String, Object> 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<Void> launch(Map<String, Object> 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<Void> launch(Map<String, Object> 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 -> {
41 changes: 2 additions & 39 deletions vscode/src/commands/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -71,15 +70,14 @@ 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();
}
const debugOptions : vscode.DebugSessionOptions = {
noDebug: noDebug,
}


const ret = await vscode.debug.startDebugging(workspaceFolder, debugConfig, debugOptions);
return ret ? new Promise((resolve) => {
const listener = vscode.debug.onDidTerminateDebugSession(() => {
Expand All @@ -90,41 +88,6 @@ const runDebug = async (noDebug: boolean, testRun: boolean, uri: any, methodName
}
};


async function findRunConfiguration(uri : vscode.Uri) : Promise<vscode.DebugConfiguration|undefined> {
// 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<vscode.DebugConfiguration> {
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,
Expand Down