Skip to content
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

Fix debugger not starting for junit tests #515

Merged
merged 1 commit into from
Apr 8, 2020
Merged
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
43 changes: 20 additions & 23 deletions src/com/twitter/intellij/pants/service/task/PantsTaskManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.Optional;

import static com.intellij.openapi.externalSystem.rt.execution.ForkedDebuggerHelper.DEBUG_FORK_SOCKET_PARAM;
import static com.intellij.openapi.externalSystem.rt.execution.ForkedDebuggerHelper.JVM_DEBUG_SETUP_PREFIX;
import static com.intellij.openapi.externalSystem.service.execution.ExternalSystemRunnableState.BUILD_PROCESS_DEBUGGER_PORT_KEY;

public class PantsTaskManager implements ExternalSystemTaskManager<PantsExecutionSettings> {

Expand All @@ -58,9 +58,7 @@ public void executeTasks(
return;
}

final GeneralCommandLine commandLine =
constructCommandLine(
taskNames, projectPath, settings, Lists.newArrayList(settings.getVmOptions()), settings.getArguments(), jvmAgentSetup);
GeneralCommandLine commandLine = constructCommandLine(taskNames, projectPath, settings);
if (commandLine == null) {
return;
}
Expand Down Expand Up @@ -104,44 +102,43 @@ public void onTextAvailable(ProcessEvent event, Key outputType) {
}
}

/**
* Eliminate `DEBUG_FORK_SOCKET_PARAM`(-forkSocket) from debug setup parameters
*/
@VisibleForTesting
protected static String getCleanedDebugSetup(String debugSetup) {
ArrayList<String> params = Lists.newArrayList(debugSetup.split(" "));
return params.stream().filter(s -> !s.contains(DEBUG_FORK_SOCKET_PARAM)).collect(Collectors.joining(" "));
protected static void setupDebuggerSettings(PantsExecutionSettings settings) {
Optional.ofNullable(settings.getUserData(BUILD_PROCESS_DEBUGGER_PORT_KEY))
.filter(port -> port > 0)
.map(port -> JVM_DEBUG_SETUP_PREFIX + port)
.ifPresent(settings::withVmOption);
}

@Nullable
public GeneralCommandLine constructCommandLine(
@NotNull List<String> taskNames,
@NotNull String projectPath,
@Nullable PantsExecutionSettings settings,
@NotNull List<String> vmOptions,
@NotNull List<String> scriptParameters,
@Nullable String debuggerSetup
@Nullable PantsExecutionSettings settings
) {
if (settings == null) {
return null;
}
projectPath = PantsTargetAddress.extractPath(projectPath).get();
final GeneralCommandLine commandLine = PantsUtil.defaultCommandLine(projectPath);

setupDebuggerSettings(settings);
boolean debugEnabled = settings.getJvmArguments().stream()
.anyMatch(jvmOpt -> jvmOpt.startsWith(JVM_DEBUG_SETUP_PREFIX));

/**
* Global options section.
*/
if (debuggerSetup != null) {
if (debugEnabled) {
if (taskNames.size() > 1) {
throw new ExternalSystemException(PantsBundle.message("pants.error.multiple.tasks.for.debugging"));
}
commandLine.addParameter(PantsConstants.PANTS_CLI_OPTION_NO_TEST_JUNIT_TIMEOUTS);
final String goal = taskNames.iterator().next();
final String jvmOptionsFlag = goal2JvmOptionsFlag.get(goal);
if (jvmOptionsFlag == null) {
if (!goal2JvmOptionsFlag.containsKey(goal)) {
throw new ExternalSystemException(PantsBundle.message("pants.error.cannot.debug.task", goal));
}
commandLine.addParameter(jvmOptionsFlag + "=" + getCleanedDebugSetup(debuggerSetup));

commandLine.addParameter(PantsConstants.PANTS_CLI_OPTION_NO_TEST_JUNIT_TIMEOUTS);
}
if (settings.isUseIdeaProjectJdk()) {
try {
Expand All @@ -161,14 +158,14 @@ public GeneralCommandLine constructCommandLine(
if (jvmOptionsFlag == null) {
continue;
}
for (String vmOption : vmOptions) {
for (String vmOption : settings.getJvmArguments()) {
commandLine.addParameter(jvmOptionsFlag + "=" + vmOption);
}
}
/**
* Script parameters section including targets and options.
*/
commandLine.addParameters(scriptParameters);
commandLine.addParameters(settings.getArguments());
return commandLine;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
import com.intellij.psi.PsiPackage;
import com.intellij.testFramework.MapDataContext;
import com.twitter.intellij.pants.PantsManager;
import com.twitter.intellij.pants.util.ProjectTestJvms;
import com.twitter.intellij.pants.service.task.PantsTaskManager;
import com.twitter.intellij.pants.settings.PantsExecutionSettings;
import com.twitter.intellij.pants.testFramework.OSSPantsIntegrationTest;
import com.twitter.intellij.pants.util.PantsUtil;
import com.twitter.intellij.pants.util.ProjectTestJvms;
import org.jetbrains.annotations.NotNull;
import org.junit.Assert;

Expand All @@ -36,10 +36,14 @@
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.intellij.openapi.externalSystem.rt.execution.ForkedDebuggerHelper.JVM_DEBUG_SETUP_PREFIX;
import static com.intellij.openapi.externalSystem.service.execution.ExternalSystemRunnableState.BUILD_PROCESS_DEBUGGER_PORT_KEY;


public class OSSPantsJvmRunConfigurationIntegrationTest extends OSSPantsIntegrationTest {
public void testClassRunConfiguration() throws Throwable {
Expand Down Expand Up @@ -67,38 +71,43 @@ public void testClassRunConfiguration() throws Throwable {
ExternalSystemManager.EP_NAME.findExtension(PantsManager.class).getTaskManagerClass();
assertEquals(PantsTaskManager.class, taskManagerClass);

String debuggerSetup = "dummy_debugger_setup";

List<String> debugParameters = Arrays.asList("--no-test-junit-timeouts", "--jvm-test-junit-options=" + debuggerSetup, "test");
int debugPort = 5005;
List<String> debugParameters = Arrays.asList(
"--no-test-junit-timeouts",
"test",
"--jvm-test-junit-options=" + JVM_DEBUG_SETUP_PREFIX + debugPort
);
List<String> expectedDebugParameters = Stream.of(debugParameters, configScriptParameters)
.flatMap(Collection::stream)
.collect(Collectors.toList());

GeneralCommandLine finalDebugCommandline = getFinalCommandline(esc, debuggerSetup, taskManagerClass);
GeneralCommandLine finalDebugCommandline = getFinalCommandline(esc, taskManagerClass, OptionalInt.of(debugPort));
assertEquals(expectedDebugParameters, finalDebugCommandline.getParametersList().getParameters());

List<String> runParameters = Collections.singletonList("test");
List<String> expectedRunParameters = Stream.of(runParameters, configScriptParameters)
.flatMap(Collection::stream)
.collect(Collectors.toList());

GeneralCommandLine finalRunCommandline = getFinalCommandline(esc, null, taskManagerClass);
GeneralCommandLine finalRunCommandline = getFinalCommandline(esc, taskManagerClass, OptionalInt.empty());
assertEquals(expectedRunParameters, finalRunCommandline.getParametersList().getParameters());
}

@NotNull
private GeneralCommandLine getFinalCommandline(
ExternalSystemRunConfiguration esc,
String debuggerSetup,
Class<? extends ExternalSystemTaskManager<PantsExecutionSettings>> taskManagerClass
Class<? extends ExternalSystemTaskManager<PantsExecutionSettings>> taskManagerClass,
OptionalInt debugPort
) throws InstantiationException, IllegalAccessException {
PantsExecutionSettings settings = PantsExecutionSettings.createDefault();
settings.withVmOptions(PantsUtil.parseCmdParameters(Optional.ofNullable(esc.getSettings().getVmOptions())));
settings.withArguments(PantsUtil.parseCmdParameters(Optional.ofNullable(esc.getSettings().getScriptParameters())));
debugPort.ifPresent(port -> settings.putUserData(BUILD_PROCESS_DEBUGGER_PORT_KEY, port));

GeneralCommandLine commandLine = ((PantsTaskManager) taskManagerClass.newInstance()).constructCommandLine(
esc.getSettings().getTaskNames(),
esc.getSettings().getExternalProjectPath(),
PantsExecutionSettings.createDefault(),
PantsUtil.parseCmdParameters(Optional.ofNullable(esc.getSettings().getVmOptions())),
PantsUtil.parseCmdParameters(Optional.ofNullable(esc.getSettings().getScriptParameters())),
debuggerSetup
settings
);
assertNotNull(commandLine);
return commandLine;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@

package com.twitter.intellij.pants.service.task;

import com.twitter.intellij.pants.settings.PantsExecutionSettings;
import junit.framework.TestCase;

import java.util.Collections;

import static com.intellij.openapi.externalSystem.service.execution.ExternalSystemRunnableState.BUILD_PROCESS_DEBUGGER_PORT_KEY;

public class PantsTaskManagerTest extends TestCase {

public void testGetCleanedDebugSetup() {
String cleanedSetup =
PantsTaskManager.getCleanedDebugSetup("-agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=63212 -forkSocket63213");
assertEquals("-agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=63212", cleanedSetup);
PantsExecutionSettings settings = new PantsExecutionSettings(Collections.emptyList(), false, false, false, false, false);
settings.putUserData(BUILD_PROCESS_DEBUGGER_PORT_KEY, 63212);

PantsTaskManager.setupDebuggerSettings(settings);
assertEquals(1, settings.getJvmArguments().size());
assertEquals("-agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=63212", settings.getJvmArguments().get(0));
}
}