put module content roots in PYTHONPATH

This commit is contained in:
Dmitry Jemerov
2009-06-10 21:25:50 +04:00
parent 581367ff4c
commit f5f9e9f8c4
5 changed files with 48 additions and 13 deletions
@@ -14,8 +14,12 @@ import com.intellij.execution.runners.ExecutionEnvironment;
import com.intellij.execution.runners.ProgramRunner;
import com.intellij.execution.testframework.sm.SMTestRunnerConnectionUtil;
import com.intellij.execution.ui.ConsoleView;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.containers.HashMap;
import com.jetbrains.python.PythonHelpersLocator;
import org.jetbrains.annotations.NotNull;
@@ -71,12 +75,6 @@ public class PythonUnitTestCommandLineState extends CommandLineState {
cmd.setWorkDirectory(myConfig.getWorkingDirectory());
}
cmd.getParametersList().addParametersString(myConfig.getInterpreterOptions());
cmd.addParameter(new File(helpersRoot, UTRUNNER_PY).getAbsolutePath());
for (String testSpec : getTestSpecs()) {
cmd.addParameter(testSpec);
}
Map<String, String> envs = myConfig.getEnvs();
if (envs == null)
envs = new HashMap<String, String>();
@@ -84,8 +82,29 @@ public class PythonUnitTestCommandLineState extends CommandLineState {
envs = new HashMap<String, String>(envs);
envs.put(PYTHONUNBUFFERED, "1");
insertToPythonPath(envs, helpersRoot);
// TODO[yole] use -Dpython.path for jython
List<String> pythonPathList = new ArrayList<String>();
pythonPathList.add(helpersRoot.getPath());
final Module module = myConfig.getModule();
if (module != null) {
final VirtualFile[] contentRoots = ModuleRootManager.getInstance(module).getContentRoots();
for (VirtualFile contentRoot : contentRoots) {
pythonPathList.add(FileUtil.toSystemDependentName(contentRoot.getPath()));
}
}
String pythonPath = StringUtil.join(pythonPathList, File.pathSeparator);
if (new File(myConfig.getInterpreterPath()).getName().toLowerCase().startsWith("jython")) { // HACK rewrite with cleaner API
cmd.getParametersList().add("-Dpython.path=" + pythonPath);
}
else {
insertToPythonPath(envs, pythonPath);
}
cmd.getParametersList().addParametersString(myConfig.getInterpreterOptions());
cmd.addParameter(new File(helpersRoot, UTRUNNER_PY).getAbsolutePath());
for (String testSpec : getTestSpecs()) {
cmd.addParameter(testSpec);
}
cmd.setEnvParams(envs);
cmd.setPassParentEnvs(myConfig.isPassParentEnvs());
@@ -116,11 +135,11 @@ public class PythonUnitTestCommandLineState extends CommandLineState {
return specs;
}
private static void insertToPythonPath(Map<String, String> envs, File path) {
private static void insertToPythonPath(Map<String, String> envs, String path) {
if (envs.containsKey(PYTHONPATH)) {
envs.put(PYTHONPATH, path.getAbsolutePath() + ":" + envs.get(PYTHONPATH));
envs.put(PYTHONPATH, path + File.pathSeparatorChar + envs.get(PYTHONPATH));
} else {
envs.put(PYTHONPATH, path.getAbsolutePath());
envs.put(PYTHONPATH, path);
}
}
@@ -0,0 +1,7 @@
import unittest
from testedCode.my_class import *
class MyClassTest(unittest.TestCase):
def test_foo(self):
c = MyClass()
self.assertEquals("bar", c.foo())
@@ -0,0 +1,4 @@
class MyClass:
def foo(self):
return "bar"
@@ -68,6 +68,12 @@ public class PyTestRunnerTest extends LightPlatformTestCase {
assertEquals(8, result.length);
}
public void testDependent() throws ExecutionException {
final File testDir = new File(PathManager.getHomePath(), "plugins/python/testData/testRunner");
String[] result = runUTRunner(testDir.getPath(), new File(testDir, "dependentTests/my_class_test.py").getPath());
assertEquals(3, result.length);
}
private static String[] runUTRunner(String workDir, String... args) throws ExecutionException {
File helpersDir = new File(PathManager.getHomePath(), "plugins/python/helpers");
File utRunner = new File(helpersDir, "pycharm/utrunner.py");
@@ -89,10 +95,9 @@ public class PyTestRunnerTest extends LightPlatformTestCase {
File jythonJar = new File(PathManager.getHomePath(), "plugins/python/lib/jython.jar");
parameters.getClassPath().add(jythonJar.getPath());
parameters.getProgramParametersList().add("-Dpython.path=" + pythonPath + ";" + workDir);
parameters.getProgramParametersList().addAll(args);
parameters.setWorkingDirectory(workDir);
parameters.getVMParametersList().add("-Dpython.path=" + pythonPath);
final StringBuilder stdout = new StringBuilder();
final StringBuilder stderr = new StringBuilder();