PY-35341 Enable pytest output only when Debug console was enabled

GitOrigin-RevId: f30a4921bc9c332860f6ba4daa4a29f5c6433fdd
This commit is contained in:
Elizaveta Shashkova
2019-04-29 13:31:41 +03:00
committed by intellij-monorepo-bot
parent e9edb7cf19
commit d36d61805c
6 changed files with 114 additions and 2 deletions
@@ -13,6 +13,7 @@ from _pydev_bundle.pydev_code_executor import BaseCodeExecutor
from _pydev_bundle.pydev_console_types import CodeFragment, Command
from _pydev_bundle.pydev_imports import Exec
from _pydevd_bundle import pydevd_vars, pydevd_save_locals
from _pydevd_bundle.pydevd_console_pytest import enable_pytest_output
try:
import __builtin__
@@ -41,6 +42,8 @@ if 'IPYTHONENABLE' in os.environ:
else:
IPYTHON = True
PYTEST_RUN_CONFIG = 'PYTEST_RUN_CONFIG' in os.environ
try:
try:
exitfunc = sys.exitfunc
@@ -176,6 +179,9 @@ def console_exec(thread_id, frame_id, expression, dbg):
updated_globals.update(frame.f_globals)
updated_globals.update(frame.f_locals) # locals later because it has precedence over the actual globals
if PYTEST_RUN_CONFIG:
enable_pytest_output()
if IPYTHON:
need_more = exec_code(CodeFragment(expression), updated_globals, frame.f_locals, dbg)
if not need_more:
@@ -0,0 +1,25 @@
from _pydev_bundle import pydev_log
def enable_pytest_output():
try:
import _pytest
if hasattr(_pytest, "debugging"): # pytest >= 3
from _pytest.debugging import pytestPDB as _pytestDebug
else:
raise ValueError("Failed to find debugger in _pytest")
plugin_manager = _pytestDebug._pluginmanager
if plugin_manager is not None:
capman = plugin_manager.getplugin("capturemanager")
if hasattr(capman, "suspend"): # pytest 4
capman.suspend(in_=True)
elif hasattr(capman, "suspend_global_capture"): # pytest 3
capman.suspend_global_capture(in_=True)
else:
raise ValueError("Failed to find suspend method")
except Exception:
pydev_log.debug("Failed to enable pytest output: %s" % traceback.format_exc())
@@ -40,6 +40,11 @@ class PyTestSettingsEditor(configuration: PyAbstractTestConfiguration) :
class PyPyTestExecutionEnvironment(configuration: PyTestConfiguration, environment: ExecutionEnvironment) :
PyTestExecutionEnvironment<PyTestConfiguration>(configuration, environment) {
override fun getRunner(): PythonHelper = PythonHelper.PYTEST
override fun customizeEnvironmentVars(envs: MutableMap<String, String>, passParentEnvs: Boolean) {
super.customizeEnvironmentVars(envs, passParentEnvs)
envs[PYTEST_RUN_CONFIG] = "True"
}
}
@@ -82,4 +87,6 @@ object PyTestFactory : PyAbstractTestFactory<PyTestConfiguration>() {
override fun getName(): String = PyTestFrameworkService.getSdkReadableNameByFramework(PyNames.PY_TEST)
override fun getId() = "py.test" //Do not rename: used as ID for run configurations
}
}
private const val PYTEST_RUN_CONFIG: String = "PYTEST_RUN_CONFIG"
@@ -0,0 +1,10 @@
def test_func1():
a = 1
assert a == 1 # breakpoint
def test_func2():
b = 2
assert b == 2
@@ -1977,4 +1977,33 @@ public class PythonDebuggerTest extends PyEnvTestCase {
}
});
}
@Test
public void testDebugConsolePytest() {
runPythonTest(new PyDebuggerTask("/debug", "test_debug_console_pytest.py") {
@Override
public void before() {
toggleBreakpoint(getFilePath(getScriptName()), 4);
}
@Override
protected boolean usePytestRunner() {
return true;
}
@Override
public void testing() throws Exception {
waitForPause();
eval("a").hasValue("1");
consoleExec("print('a = %s' % a)");
waitForOutput("a = 1");
}
@NotNull
@Override
public Set<String> getTags() {
return Sets.newHashSet("pytest");
}
});
}
}
@@ -4,6 +4,8 @@ package com.jetbrains.env.python.debug;
import com.google.common.collect.Sets;
import com.intellij.execution.*;
import com.intellij.execution.configurations.ConfigurationFactory;
import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.execution.configurations.ParamsGroup;
import com.intellij.execution.configurations.RunProfile;
import com.intellij.execution.executors.DefaultDebugExecutor;
import com.intellij.execution.process.KillableColoredProcessHandler;
@@ -17,9 +19,11 @@ import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.util.Key;
import com.intellij.xdebugger.*;
import com.jetbrains.env.python.PythonDebuggerTest;
import com.jetbrains.python.PythonHelper;
import com.jetbrains.python.debugger.PyDebugProcess;
import com.jetbrains.python.debugger.PyDebugRunner;
import com.jetbrains.python.debugger.PyDebugValueExecutionService;
import com.jetbrains.python.run.CommandLinePatcher;
import com.jetbrains.python.run.PythonCommandLineState;
import com.jetbrains.python.run.PythonConfigurationType;
import com.jetbrains.python.run.PythonRunConfiguration;
@@ -29,6 +33,8 @@ import org.junit.Assert;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Set;
import java.util.concurrent.Semaphore;
@@ -121,7 +127,7 @@ public class PyDebuggerTask extends PyBaseDebuggerTask {
WriteAction.computeAndWait(() -> {
myExecutionResult =
pyState.execute(executor, runner.createCommandLinePatchers(myFixture.getProject(), pyState, profile, serverLocalPort));
pyState.execute(executor, createCommandLinePatchers(runner, pyState, profile, serverLocalPort));
mySession = XDebuggerManager.getInstance(getProject()).
startSession(env, new XDebugProcessStarter() {
@@ -182,6 +188,35 @@ public class PyDebuggerTask extends PyBaseDebuggerTask {
doTest(myOutputPrinter);
}
protected boolean usePytestRunner() {
return false;
}
protected CommandLinePatcher[] createCommandLinePatchers(PyDebugRunner runner, PythonCommandLineState pyState, RunProfile profile,
int serverLocalPort) {
final CommandLinePatcher[] debugPatchers = runner.createCommandLinePatchers(myFixture.getProject(), pyState, profile, serverLocalPort);
if (!usePytestRunner()) {
return debugPatchers;
}
ArrayList<CommandLinePatcher> result = new ArrayList<>();
result.add(pytestPatcher());
result.addAll(Arrays.asList(debugPatchers));
return result.toArray(new CommandLinePatcher[0]);
}
private static CommandLinePatcher pytestPatcher() {
return new CommandLinePatcher() {
@Override
public void patchCommandLine(GeneralCommandLine commandLine) {
final ParamsGroup scriptGroup = commandLine.getParametersList().getParamsGroup(PythonCommandLineState.GROUP_SCRIPT);
scriptGroup.addParameterAt(0, "--path");
scriptGroup.addParameterAt(0, PythonHelper.PYTEST.asParamString());
commandLine.getEnvironment().put("PYTEST_RUN_CONFIG", "True");
}
};
}
protected String getExecutorId() {
return DefaultDebugExecutor.EXECUTOR_ID;
}