Use PythonHelpersLocator enum in test runner.

This commit is contained in:
Dmitry Trofimov
2015-08-04 17:53:45 +02:00
parent 3ad2e2ec81
commit 8f0a956ec6
9 changed files with 71 additions and 33 deletions
@@ -27,6 +27,8 @@ import java.util.Map;
public interface PythonHelper {
void addToPythonPath(@NotNull Map<String, String> environment);
String getPythonPath();
void addToGroup(@NotNull ParamsGroup group, @NotNull GeneralCommandLine cmd);
String asParamString();
@@ -29,10 +29,25 @@ import java.io.File;
import java.util.Map;
public enum PythonHelpersLocator implements PythonHelper {
COVERAGEPY("", "coveragepy"), COVERAGE("run_coverage", "coverage"),
DEBUGGER("pydevd", "pydev"),
CONSOLE("pydevconsole", "pydev"),
RUN_IN_CONSOLE("pydev_run_in_console", "pydev");
COVERAGEPY("coveragepy", ""), COVERAGE("coverage", "run_coverage"),
DEBUGGER("pydev", "pydevd"),
CONSOLE("pydev", "pydevconsole"),
RUN_IN_CONSOLE("pydev", "pydev_run_in_console"),
UT("pycharm", "utrunner"),
SETUPPY("pycharm", "pycharm_setup_runner"),
NOSE("pycharm", "noserunner"),
PYTEST("pycharm", "pytestrunner"),
ATTEST("pycharm", "attestrunner"),
DOCSTRING("pycharm", "docrunner"),
BEHAVE("pycharm", "behave_runner"),
LETTUCE("pycharm", "lettuce_runner"),
DJANGO_TEST_MANAGE("pycharm", "django_test_manage"),
;
@NotNull
private PathPythonHelper findModule(String moduleEntryPoint, String path) {
@@ -44,7 +59,7 @@ public enum PythonHelpersLocator implements PythonHelper {
return new ModulePythonHelper(moduleEntryPoint, path);
}
return new ScriptPythonHelper(path + ".py");
return new ScriptPythonHelper(moduleEntryPoint, path);
}
private static final Logger LOG = Logger.getInstance("#com.jetbrains.python.PythonHelpersLocator");
@@ -52,10 +67,14 @@ public enum PythonHelpersLocator implements PythonHelper {
private PathPythonHelper myModule;
PythonHelpersLocator(String moduleName, String pythonPath) {
PythonHelpersLocator(String pythonPath, String moduleName) {
myModule = findModule(moduleName, pythonPath);
}
public String getPythonPath() {
return myModule.getPythonPath();
}
/**
* @return the base directory under which various scripts, etc are stored.
@@ -145,6 +164,11 @@ public enum PythonHelpersLocator implements PythonHelper {
public String asParamString() {
return "-m" + myModuleName;
}
@Override
public String getPythonPath() {
return FileUtil.toSystemDependentName(myPath.getAbsolutePath());
}
}
/**
@@ -153,15 +177,25 @@ public enum PythonHelpersLocator implements PythonHelper {
* with .pyc files
*/
public static class ScriptPythonHelper extends PathPythonHelper {
public ScriptPythonHelper(String relativePath) {
super(relativePath);
private String myPythonPath;
public ScriptPythonHelper(String module, String pythonPath) {
super(new File(pythonPath, module.replace(".", File.separator)).getPath());
myPythonPath = pythonPath;
}
@Override
public void addToPythonPath(@NotNull Map<String, String> environment) {
PythonEnvUtil.setPythonDontWriteBytecode(environment);
PythonEnvUtil.addToPythonPath(environment, myPath.getParent());
PythonEnvUtil.addToPythonPath(environment, myPythonPath);
}
@Override
public String getPythonPath() {
return myPythonPath;
}
}
@@ -30,6 +30,7 @@ import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.ComponentContainer;
import com.intellij.psi.PsiElement;
import com.intellij.psi.search.GlobalSearchScope;
import com.jetbrains.python.PythonHelpersLocator;
import com.jetbrains.python.run.AbstractPythonRunConfiguration;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -99,7 +100,7 @@ public class PyRerunFailedTestsAction extends AbstractRerunFailedTestsAction {
}
@Override
protected String getRunner() {
protected PythonHelpersLocator getRunner() {
return myState.getRunner();
}
@@ -158,11 +158,11 @@ public abstract class PythonTestCommandLineStateBase extends PythonCommandLineSt
protected void addAfterParameters(GeneralCommandLine cmd) {}
protected void addTestRunnerParameters(GeneralCommandLine cmd) {
ParamsGroup script_params = cmd.getParametersList().getParamsGroup(GROUP_SCRIPT);
assert script_params != null;
script_params.addParameter(new File(PythonHelpersLocator.getHelpersRoot(), getRunner()).getAbsolutePath());
ParamsGroup scriptParams = cmd.getParametersList().getParamsGroup(GROUP_SCRIPT);
assert scriptParams != null;
getRunner().addToGroup(scriptParams, cmd);
addBeforeParameters(cmd);
script_params.addParameters(getTestSpecs());
scriptParams.addParameters(getTestSpecs());
addAfterParameters(cmd);
}
@@ -172,6 +172,6 @@ public abstract class PythonTestCommandLineStateBase extends PythonCommandLineSt
envs.put("PYCHARM_HELPERS_DIR", PythonHelpersLocator.getHelperPath("pycharm"));
}
protected abstract String getRunner();
protected abstract PythonHelpersLocator getRunner();
protected abstract List<String> getTestSpecs();
}
@@ -17,6 +17,7 @@ package com.jetbrains.python.testing.attest;
import com.intellij.execution.runners.ExecutionEnvironment;
import com.intellij.openapi.util.io.FileUtil;
import com.jetbrains.python.PythonHelpersLocator;
import com.jetbrains.python.testing.PythonTestCommandLineStateBase;
import java.util.ArrayList;
@@ -27,7 +28,7 @@ import java.util.List;
*/
public class PythonAtTestCommandLineState extends PythonTestCommandLineStateBase {
private final PythonAtTestRunConfiguration myConfig;
private static final String UTRUNNER_PY = "pycharm/attestrunner.py";
public PythonAtTestCommandLineState(PythonAtTestRunConfiguration runConfiguration, ExecutionEnvironment env) {
super(runConfiguration, env);
@@ -35,8 +36,8 @@ public class PythonAtTestCommandLineState extends PythonTestCommandLineStateBase
}
@Override
protected String getRunner() {
return UTRUNNER_PY;
protected PythonHelpersLocator getRunner() {
return PythonHelpersLocator.ATTEST;
}
protected List<String> getTestSpecs() {
@@ -16,6 +16,7 @@
package com.jetbrains.python.testing.doctest;
import com.intellij.execution.runners.ExecutionEnvironment;
import com.jetbrains.python.PythonHelpersLocator;
import com.jetbrains.python.testing.PythonTestCommandLineStateBase;
import java.util.ArrayList;
@@ -26,7 +27,7 @@ import java.util.List;
*/
public class PythonDocTestCommandLineState extends PythonTestCommandLineStateBase {
private final PythonDocTestRunConfiguration myConfig;
private static final String UTRUNNER_PY = "pycharm/docrunner.py";
public PythonDocTestCommandLineState(PythonDocTestRunConfiguration runConfiguration, ExecutionEnvironment env) {
super(runConfiguration, env);
@@ -34,8 +35,8 @@ public class PythonDocTestCommandLineState extends PythonTestCommandLineStateBas
}
@Override
protected String getRunner() {
return UTRUNNER_PY;
protected PythonHelpersLocator getRunner() {
return PythonHelpersLocator.DOCSTRING;
}
protected List<String> getTestSpecs() {
@@ -15,12 +15,12 @@
*/
package com.jetbrains.python.testing.nosetest;
import com.intellij.execution.ExecutionException;
import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.execution.configurations.ParamsGroup;
import com.intellij.execution.runners.ExecutionEnvironment;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.jetbrains.python.PythonHelpersLocator;
import com.jetbrains.python.testing.PythonTestCommandLineStateBase;
import java.util.ArrayList;
@@ -31,7 +31,6 @@ import java.util.List;
*/
public class PythonNoseTestCommandLineState extends PythonTestCommandLineStateBase {
private final PythonNoseTestRunConfiguration myConfig;
private static final String NOSERUNNER_PY = "pycharm/noserunner.py";
public PythonNoseTestCommandLineState(PythonNoseTestRunConfiguration runConfiguration, ExecutionEnvironment env) {
super(runConfiguration, env);
@@ -39,8 +38,8 @@ public class PythonNoseTestCommandLineState extends PythonTestCommandLineStateBa
}
@Override
protected String getRunner() {
return NOSERUNNER_PY;
protected PythonHelpersLocator getRunner() {
return PythonHelpersLocator.NOSE;
}
protected List<String> getTestSpecs() {
@@ -24,6 +24,7 @@ import com.intellij.execution.runners.ExecutionEnvironment;
import com.intellij.execution.ui.ConsoleView;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.jetbrains.python.PythonHelpersLocator;
import com.jetbrains.python.testing.PythonTestCommandLineStateBase;
import org.jetbrains.annotations.NotNull;
@@ -35,7 +36,6 @@ import java.util.List;
*/
public class PyTestCommandLineState extends PythonTestCommandLineStateBase {
private final PyTestRunConfiguration myConfiguration;
private static final String PYTESTRUNNER_PY = "pycharm/pytestrunner.py";
public PyTestCommandLineState(PyTestRunConfiguration configuration, ExecutionEnvironment env) {
super(configuration, env);
@@ -50,8 +50,8 @@ public class PyTestCommandLineState extends PythonTestCommandLineStateBase {
}
@Override
protected String getRunner() {
return PYTESTRUNNER_PY;
protected PythonHelpersLocator getRunner() {
return PythonHelpersLocator.PYTEST;
}
@Override
@@ -22,6 +22,7 @@ import com.intellij.execution.runners.ExecutionEnvironment;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.jetbrains.python.PyNames;
import com.jetbrains.python.PythonHelpersLocator;
import com.jetbrains.python.testing.AbstractPythonTestRunConfiguration;
import com.jetbrains.python.testing.PythonTestCommandLineStateBase;
@@ -34,8 +35,7 @@ import java.util.List;
public class PythonUnitTestCommandLineState extends
PythonTestCommandLineStateBase {
private final PythonUnitTestRunConfiguration myConfig;
private static final String UTRUNNER_PY = "pycharm/utrunner.py";
private static final String SETUP_PY_TESTRUNNER = "pycharm/pycharm_setup_runner.py";
public PythonUnitTestCommandLineState(PythonUnitTestRunConfiguration runConfiguration, ExecutionEnvironment env) {
super(runConfiguration, env);
@@ -43,11 +43,11 @@ public class PythonUnitTestCommandLineState extends
}
@Override
protected String getRunner() {
protected PythonHelpersLocator getRunner() {
if (myConfig.getTestType() == AbstractPythonTestRunConfiguration.TestType.TEST_SCRIPT &&
myConfig.getScriptName().endsWith(PyNames.SETUP_DOT_PY))
return SETUP_PY_TESTRUNNER;
return UTRUNNER_PY;
return PythonHelpersLocator.SETUPPY;
return PythonHelpersLocator.UT;
}
protected List<String> getTestSpecs() {