summaryrefslogtreecommitdiff
path: root/python/testSrc/com/jetbrains/env/python/debug
diff options
context:
space:
mode:
Diffstat (limited to 'python/testSrc/com/jetbrains/env/python/debug')
-rw-r--r--python/testSrc/com/jetbrains/env/python/debug/PyBaseDebuggerTask.java361
-rw-r--r--python/testSrc/com/jetbrains/env/python/debug/PyDebuggerTask.java211
2 files changed, 572 insertions, 0 deletions
diff --git a/python/testSrc/com/jetbrains/env/python/debug/PyBaseDebuggerTask.java b/python/testSrc/com/jetbrains/env/python/debug/PyBaseDebuggerTask.java
new file mode 100644
index 000000000000..38eed736c7ef
--- /dev/null
+++ b/python/testSrc/com/jetbrains/env/python/debug/PyBaseDebuggerTask.java
@@ -0,0 +1,361 @@
+package com.jetbrains.env.python.debug;
+
+import com.google.common.collect.Sets;
+import com.intellij.openapi.application.Result;
+import com.intellij.openapi.application.WriteAction;
+import com.intellij.openapi.project.Project;
+import com.intellij.openapi.util.Pair;
+import com.intellij.openapi.util.text.StringUtil;
+import com.intellij.openapi.vfs.JarFileSystem;
+import com.intellij.openapi.vfs.LocalFileSystem;
+import com.intellij.openapi.vfs.VirtualFile;
+import com.intellij.util.ui.UIUtil;
+import com.intellij.xdebugger.*;
+import com.intellij.xdebugger.frame.XValue;
+import com.jetbrains.env.PyExecutionFixtureTestTask;
+import com.jetbrains.python.console.PythonDebugLanguageConsoleView;
+import com.jetbrains.python.debugger.PyDebugProcess;
+import com.jetbrains.python.debugger.PyDebugValue;
+import com.jetbrains.python.debugger.PyDebuggerException;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.junit.Assert;
+
+import java.io.PrintWriter;
+import java.lang.reflect.InvocationTargetException;
+import java.util.Set;
+import java.util.concurrent.Semaphore;
+
+/**
+ * @author traff
+ */
+public abstract class PyBaseDebuggerTask extends PyExecutionFixtureTestTask {
+ private Set<Pair<String, Integer>> myBreakpoints = Sets.newHashSet();
+ protected PyDebugProcess myDebugProcess;
+ protected XDebugSession mySession;
+ protected Semaphore myPausedSemaphore;
+ protected Semaphore myTerminateSemaphore;
+ protected boolean shouldPrintOutput = false;
+ protected boolean myProcessCanTerminate;
+
+ protected void waitForPause() throws InterruptedException, InvocationTargetException {
+ Assert.assertTrue("Debugger didn't stopped within timeout\nOutput:" + output(), waitFor(myPausedSemaphore));
+
+ XDebuggerTestUtil.waitForSwing();
+ }
+
+ protected void waitForTerminate() throws InterruptedException, InvocationTargetException {
+ setProcessCanTerminate(true);
+
+ Assert.assertTrue("Debugger didn't terminated within timeout\nOutput:" + output(), waitFor(myTerminateSemaphore));
+ XDebuggerTestUtil.waitForSwing();
+ }
+
+ protected void runToLine(int line) throws InvocationTargetException, InterruptedException {
+ XDebugSession currentSession = XDebuggerManager.getInstance(getProject()).getCurrentSession();
+ XSourcePosition position = currentSession.getCurrentPosition();
+
+
+ currentSession.runToPosition(XDebuggerUtil.getInstance().createPosition(position.getFile(), line), false);
+
+ waitForPause();
+ }
+
+ protected void resume() {
+ XDebugSession currentSession = XDebuggerManager.getInstance(getProject()).getCurrentSession();
+
+ Assert.assertTrue(currentSession.isSuspended());
+ Assert.assertEquals(0, myPausedSemaphore.availablePermits());
+
+ currentSession.resume();
+ }
+
+ protected void stepOver() {
+ XDebugSession currentSession = XDebuggerManager.getInstance(getProject()).getCurrentSession();
+
+ Assert.assertTrue(currentSession.isSuspended());
+ Assert.assertEquals(0, myPausedSemaphore.availablePermits());
+
+ currentSession.stepOver(false);
+ }
+
+ protected void stepInto() {
+ XDebugSession currentSession = XDebuggerManager.getInstance(getProject()).getCurrentSession();
+
+ Assert.assertTrue(currentSession.isSuspended());
+ Assert.assertEquals(0, myPausedSemaphore.availablePermits());
+
+ currentSession.stepInto();
+ }
+
+ protected void smartStepInto(String funcName) {
+ XDebugSession currentSession = XDebuggerManager.getInstance(getProject()).getCurrentSession();
+
+ Assert.assertTrue(currentSession.isSuspended());
+ Assert.assertEquals(0, myPausedSemaphore.availablePermits());
+
+ myDebugProcess.startSmartStepInto(funcName);
+ }
+
+ @NotNull
+ protected String output() {
+ if (mySession != null && mySession.getConsoleView() != null) {
+ PythonDebugLanguageConsoleView pydevConsoleView = (PythonDebugLanguageConsoleView)mySession.getConsoleView();
+ if (pydevConsoleView != null) {
+ return XDebuggerTestUtil.getConsoleText(pydevConsoleView.getTextConsole());
+ }
+ }
+ return "Console output not available.";
+ }
+
+ protected void input(String text) {
+ PrintWriter pw = new PrintWriter(myDebugProcess.getProcessHandler().getProcessInput());
+ pw.println(text);
+ pw.flush();
+ }
+
+ private void outputContains(String substring) {
+ Assert.assertTrue(output().contains(substring));
+ }
+
+ public void setProcessCanTerminate(boolean processCanTerminate) {
+ myProcessCanTerminate = processCanTerminate;
+ }
+
+ protected void clearAllBreakpoints() {
+
+ UIUtil.invokeAndWaitIfNeeded(new Runnable() {
+ @Override
+ public void run() {
+ XDebuggerTestUtil.removeAllBreakpoints(getProject());
+ }
+ });
+ }
+
+ /**
+ * Toggles breakpoint
+ *
+ * @param file getScriptPath() or path to script
+ * @param line starting with 0
+ */
+ protected void toggleBreakpoint(final String file, final int line) {
+ UIUtil.invokeAndWaitIfNeeded(new Runnable() {
+ @Override
+ public void run() {
+ doToggleBreakpoint(file, line);
+ }
+ });
+
+ addOrRemoveBreakpoint(file, line);
+ }
+
+ private void addOrRemoveBreakpoint(String file, int line) {
+ if (myBreakpoints.contains(Pair.create(file, line))) {
+ myBreakpoints.remove(Pair.create(file, line));
+ }
+ else {
+ myBreakpoints.add(Pair.create(file, line));
+ }
+ }
+
+ protected void toggleBreakpointInEgg(final String file, final String innerPath, final int line) {
+ UIUtil.invokeAndWaitIfNeeded(new Runnable() {
+ @Override
+ public void run() {
+ VirtualFile f = LocalFileSystem.getInstance().findFileByPath(file);
+ Assert.assertNotNull(f);
+ final VirtualFile jarRoot = JarFileSystem.getInstance().getJarRootForLocalFile(f);
+ Assert.assertNotNull(jarRoot);
+ VirtualFile innerFile = jarRoot.findFileByRelativePath(innerPath);
+ Assert.assertNotNull(innerFile);
+ XDebuggerTestUtil.toggleBreakpoint(getProject(), innerFile, line);
+ }
+ });
+
+ addOrRemoveBreakpoint(file, line);
+ }
+
+ public boolean canPutBreakpointAt(Project project, String file, int line) {
+ VirtualFile vFile = LocalFileSystem.getInstance().findFileByPath(file);
+ Assert.assertNotNull(vFile);
+ return XDebuggerUtil.getInstance().canPutBreakpointAt(project, vFile, line);
+ }
+
+ private void doToggleBreakpoint(String file, int line) {
+ Assert.assertTrue(canPutBreakpointAt(getProject(), file, line));
+ XDebuggerTestUtil.toggleBreakpoint(getProject(), LocalFileSystem.getInstance().findFileByPath(file), line);
+ }
+
+ protected Variable eval(String name) throws InterruptedException {
+ Assert.assertTrue("Eval works only while suspended", mySession.isSuspended());
+ XValue var = XDebuggerTestUtil.evaluate(mySession, name).first;
+ Assert.assertNotNull("There is no variable named " + name, var);
+ return new Variable(var);
+ }
+
+ protected void setVal(String name, String value) throws InterruptedException, PyDebuggerException {
+ XValue var = XDebuggerTestUtil.evaluate(mySession, name).first;
+ myDebugProcess.changeVariable((PyDebugValue)var, value);
+ }
+
+ public void waitForOutput(String ... string) throws InterruptedException {
+ long started = System.currentTimeMillis();
+
+ while (!containsOneOf(output(), string)) {
+ if (System.currentTimeMillis() - started > myTimeout) {
+ Assert.fail("None of '" + StringUtil.join(string, ", ") + "'" + " is not present in output.\n" + output());
+ }
+ Thread.sleep(2000);
+ }
+ }
+
+ protected boolean containsOneOf(String output, String[] strings) {
+ for (String s: strings) {
+ if (output.contains(s)) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+
+ public void setShouldPrintOutput(boolean shouldPrintOutput) {
+ this.shouldPrintOutput = shouldPrintOutput;
+ }
+
+ @Override
+ public void setUp(final String testName) throws Exception {
+ UIUtil.invokeAndWaitIfNeeded(new Runnable() {
+ @Override
+ public void run() {
+ try {
+ if (myFixture == null) {
+ PyBaseDebuggerTask.super.setUp(testName);
+ }
+ }
+ catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+ });
+ }
+
+ @Override
+ public void tearDown() throws Exception {
+ UIUtil.invokeAndWaitIfNeeded(new Runnable() {
+ public void run() {
+ try {
+ if (mySession != null) {
+ finishSession();
+ }
+ PyBaseDebuggerTask.super.tearDown();
+ }
+ catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+ });
+ }
+
+ protected void finishSession() throws InterruptedException {
+ disposeDebugProcess();
+
+ if (mySession != null) {
+ new WriteAction() {
+ protected void run(Result result) throws Throwable {
+ mySession.stop();
+ }
+ }.execute();
+
+ waitFor(mySession.getDebugProcess().getProcessHandler()); //wait for process termination after session.stop() which is async
+
+ XDebuggerTestUtil.disposeDebugSession(mySession);
+ mySession = null;
+ myDebugProcess = null;
+ myPausedSemaphore = null;
+ }
+ }
+
+ protected abstract void disposeDebugProcess() throws InterruptedException;
+
+ protected void doTest(@Nullable OutputPrinter myOutputPrinter) throws InterruptedException {
+ try {
+ testing();
+ after();
+ }
+ catch (Throwable e) {
+ throw new RuntimeException(output(), e);
+ }
+ finally {
+ clearAllBreakpoints();
+
+ setProcessCanTerminate(true);
+
+ if (myOutputPrinter != null) {
+ myOutputPrinter.stop();
+ }
+
+ finishSession();
+ }
+ }
+
+ protected static class Variable {
+ private final XTestValueNode myValueNode;
+
+ public Variable(XValue value) throws InterruptedException {
+ myValueNode = XDebuggerTestUtil.computePresentation(value);
+ }
+
+ public Variable hasValue(String value) {
+ Assert.assertEquals(value, myValueNode.myValue);
+ return this;
+ }
+
+ public Variable hasType(String type) {
+ Assert.assertEquals(type, myValueNode.myType);
+ return this;
+ }
+
+ public Variable hasName(String name) {
+ Assert.assertEquals(name, myValueNode.myName);
+ return this;
+ }
+ }
+
+ public class OutputPrinter {
+ private Thread myThread;
+
+ public void start() {
+ myThread = new Thread(new Runnable() {
+ @Override
+ public void run() {
+ doJob();
+ }
+ });
+ myThread.setDaemon(true);
+ myThread.start();
+ }
+
+ private void doJob() {
+ int len = 0;
+ try {
+ while (true) {
+ String s = output();
+ if (s.length() > len) {
+ System.out.print(s.substring(len));
+ }
+ len = s.length();
+
+ Thread.sleep(500);
+ }
+ }
+ catch (Exception e) {
+ }
+ }
+
+ public void stop() {
+ myThread.interrupt();
+ }
+ }
+}
diff --git a/python/testSrc/com/jetbrains/env/python/debug/PyDebuggerTask.java b/python/testSrc/com/jetbrains/env/python/debug/PyDebuggerTask.java
new file mode 100644
index 000000000000..7de13d1ebed6
--- /dev/null
+++ b/python/testSrc/com/jetbrains/env/python/debug/PyDebuggerTask.java
@@ -0,0 +1,211 @@
+package com.jetbrains.env.python.debug;
+
+import com.intellij.execution.*;
+import com.intellij.execution.configurations.ConfigurationFactory;
+import com.intellij.execution.configurations.RunProfile;
+import com.intellij.execution.executors.DefaultDebugExecutor;
+import com.intellij.execution.process.KillableColoredProcessHandler;
+import com.intellij.execution.process.ProcessAdapter;
+import com.intellij.execution.process.ProcessEvent;
+import com.intellij.execution.process.ProcessHandler;
+import com.intellij.execution.runners.ExecutionEnvironment;
+import com.intellij.openapi.application.Result;
+import com.intellij.openapi.application.WriteAction;
+import com.intellij.openapi.project.Project;
+import com.intellij.openapi.util.Key;
+import com.intellij.xdebugger.*;
+import com.jetbrains.python.debugger.PyDebugProcess;
+import com.jetbrains.python.debugger.PyDebugRunner;
+import com.jetbrains.python.run.PythonCommandLineState;
+import com.jetbrains.python.run.PythonConfigurationType;
+import com.jetbrains.python.run.PythonRunConfiguration;
+import org.jetbrains.annotations.NotNull;
+import org.junit.Assert;
+
+import java.io.IOException;
+import java.net.ServerSocket;
+import java.util.concurrent.Semaphore;
+
+/**
+ * @author traff
+ */
+public class PyDebuggerTask extends PyBaseDebuggerTask {
+
+ private boolean myMultiprocessDebug = false;
+ private PythonRunConfiguration myRunConfiguration;
+
+ public PyDebuggerTask() {
+ init();
+ }
+
+ public PyDebuggerTask(String workingFolder, String scriptName, String scriptParameters) {
+ setWorkingFolder(getTestDataPath() + workingFolder);
+ setScriptName(scriptName);
+ setScriptParameters(scriptParameters);
+ init();
+ }
+
+ public PyDebuggerTask(String workingFolder, String scriptName) {
+ this(workingFolder, scriptName, null);
+ }
+
+ protected void init() {
+
+ }
+
+ public void runTestOn(String sdkHome) throws Exception {
+ final Project project = getProject();
+
+ final ConfigurationFactory factory = PythonConfigurationType.getInstance().getConfigurationFactories()[0];
+
+
+ final RunnerAndConfigurationSettings settings =
+ RunManager.getInstance(project).createRunConfiguration("test", factory);
+
+ myRunConfiguration = (PythonRunConfiguration)settings.getConfiguration();
+
+ myRunConfiguration.setSdkHome(sdkHome);
+ myRunConfiguration.setScriptName(getScriptPath());
+ myRunConfiguration.setWorkingDirectory(getWorkingFolder());
+ myRunConfiguration.setScriptParameters(getScriptParameters());
+
+ new WriteAction() {
+ @Override
+ protected void run(Result result) throws Throwable {
+ RunManagerEx.getInstanceEx(project).addConfiguration(settings, false);
+ RunManagerEx.getInstanceEx(project).setSelectedConfiguration(settings);
+ Assert.assertSame(settings, RunManagerEx.getInstanceEx(project).getSelectedConfiguration());
+ }
+ }.execute();
+
+ final PyDebugRunner runner = (PyDebugRunner)ProgramRunnerUtil.getRunner(DefaultDebugExecutor.EXECUTOR_ID, settings);
+ Assert.assertTrue(runner.canRun(DefaultDebugExecutor.EXECUTOR_ID, myRunConfiguration));
+
+ final Executor executor = DefaultDebugExecutor.getDebugExecutorInstance();
+ final ExecutionEnvironment env = new ExecutionEnvironment(executor, runner, settings, project);
+
+ final PythonCommandLineState pyState = (PythonCommandLineState)myRunConfiguration.getState(executor, env);
+
+ assert pyState != null;
+ pyState.setMultiprocessDebug(isMultiprocessDebug());
+
+ final ServerSocket serverSocket;
+ try {
+ //noinspection SocketOpenedButNotSafelyClosed
+ serverSocket = new ServerSocket(0);
+ }
+ catch (IOException e) {
+ throw new ExecutionException("Failed to find free socket port", e);
+ }
+
+
+ final int serverLocalPort = serverSocket.getLocalPort();
+ final RunProfile profile = env.getRunProfile();
+
+ before();
+
+ setProcessCanTerminate(false);
+
+ myTerminateSemaphore = new Semaphore(0);
+
+ new WriteAction<ExecutionResult>() {
+ @Override
+ protected void run(@NotNull Result<ExecutionResult> result) throws Throwable {
+ final ExecutionResult res =
+ pyState.execute(executor, PyDebugRunner.createCommandLinePatchers(myFixture.getProject(), pyState, profile, serverLocalPort));
+
+ mySession = XDebuggerManager.getInstance(getProject()).
+ startSession(runner, env, env.getContentToReuse(), new XDebugProcessStarter() {
+ @NotNull
+ public XDebugProcess start(@NotNull final XDebugSession session) {
+ myDebugProcess =
+ new PyDebugProcess(session, serverSocket, res.getExecutionConsole(), res.getProcessHandler(), isMultiprocessDebug());
+
+ myDebugProcess.getProcessHandler().addProcessListener(new ProcessAdapter() {
+
+ @Override
+ public void onTextAvailable(ProcessEvent event, Key outputType) {
+ }
+
+ @Override
+ public void processTerminated(ProcessEvent event) {
+ myTerminateSemaphore.release();
+ if (event.getExitCode() != 0 && !myProcessCanTerminate) {
+ Assert.fail("Process terminated unexpectedly\n" + output());
+ }
+ }
+ });
+
+
+ myDebugProcess.getProcessHandler().startNotify();
+
+ return myDebugProcess;
+ }
+ });
+ result.setResult(res);
+ }
+ }.execute().getResultObject();
+
+ OutputPrinter myOutputPrinter = null;
+ if (shouldPrintOutput) {
+ myOutputPrinter = new OutputPrinter();
+ myOutputPrinter.start();
+ }
+
+
+ myPausedSemaphore = new Semaphore(0);
+
+
+ mySession.addSessionListener(new XDebugSessionAdapter() {
+ @Override
+ public void sessionPaused() {
+ if (myPausedSemaphore != null) {
+ myPausedSemaphore.release();
+ }
+ }
+ });
+
+ doTest(myOutputPrinter);
+ }
+
+ public PythonRunConfiguration getRunConfiguration() {
+ return myRunConfiguration;
+ }
+
+ private boolean isMultiprocessDebug() {
+ return myMultiprocessDebug;
+ }
+
+ public void setMultiprocessDebug(boolean multiprocessDebug) {
+ myMultiprocessDebug = multiprocessDebug;
+ }
+
+ @Override
+ protected void disposeDebugProcess() throws InterruptedException {
+ if (myDebugProcess != null) {
+ ProcessHandler processHandler = myDebugProcess.getProcessHandler();
+
+ myDebugProcess.stop();
+
+ waitFor(processHandler);
+
+ if (!processHandler.isProcessTerminated()) {
+ killDebugProcess();
+ if (!waitFor(processHandler)) {
+ new Throwable("Cannot stop debugger process").printStackTrace();
+ }
+ }
+ }
+ }
+
+ private void killDebugProcess() {
+ if (myDebugProcess.getProcessHandler() instanceof KillableColoredProcessHandler) {
+ KillableColoredProcessHandler h = (KillableColoredProcessHandler)myDebugProcess.getProcessHandler();
+
+ h.killProcess();
+ }
+ else {
+ myDebugProcess.getProcessHandler().destroyProcess();
+ }
+ }
+}