PY-23452: fix sys.path for runners to make same as with real runners

* Python adds script folder as sys.path[0]. Since we run helper "helpers" dir is sys.path[0]. Pop it, and testrunner will patch sys.path as it is done with command line.
* Unittest is launched as module and it adds current dir -- pycharm mimics this behaviour.
This commit is contained in:
Ilya.Kazakevich
2017-04-19 00:09:06 +03:00
parent 65a984d1f3
commit 5f234d7a36
4 changed files with 48 additions and 2 deletions
+4 -2
View File
@@ -362,8 +362,10 @@ def jb_start_tests():
del sys.argv[1:] # Remove all args
NewTeamcityServiceMessages().message('enteredTheMatrix')
# Working dir should be on path, that is how runners work when launched from command line
sys.path.insert(1, os.getcwd())
# PyCharm helpers dir is first dir in sys.path because helper is launched.
# But sys.path should be same as when launched with test runner directly
if os.path.abspath(sys.path[0]) == os.path.abspath(os.environ["PYCHARM_HELPERS_DIR"]):
sys.path.pop(0)
return namespace.path, namespace.target, additional_args
@@ -32,4 +32,6 @@ if __name__ == '__main__':
jb_doc_args("unittests", args)
test_runner = unittestpy.TeamcityTestRunner()
test_runner.buffer = True
# Working dir should be on path, that is how unittest work when launched from command line
sys.path.append(os.getcwd())
main(argv=args, module=None, testRunner=test_runner)
@@ -0,0 +1,10 @@
import unittest
import sys
print("path[0]={0}".format(sys.path[0]))
class SampleTest(unittest.TestCase):
def test_true(self):
self.assertTrue(True)
@@ -31,10 +31,12 @@ import com.jetbrains.python.testing.PythonTestConfigurationsModel;
import com.jetbrains.python.testing.universalTests.PyUniversalUnitTestConfiguration;
import com.jetbrains.python.testing.universalTests.PyUniversalUnitTestFactory;
import com.jetbrains.python.testing.universalTests.TestTargetType;
import org.hamcrest.Matchers;
import org.jetbrains.annotations.NotNull;
import org.junit.Assert;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.List;
@@ -60,6 +62,36 @@ public final class PythonUnitTestingTest extends PyEnvTestCase {
}
/**
* Ensure that sys.path[0] is script folder, not helpers folder
*/
@Test
public void testSysPath() throws Exception {
runPythonTest(new PyUnitTestProcessWithConsoleTestTask("testRunner/env/unit/sysPath", "test_sample.py") {
@NotNull
@Override
protected PyUnitTestProcessRunner createProcessRunner() throws Exception {
return new PyUnitTestProcessRunner(toFullPath(myScriptName), 0);
}
@Override
protected void checkTestResults(@NotNull final PyUnitTestProcessRunner runner,
@NotNull final String stdout,
@NotNull final String stderr,
@NotNull final String all) {
Assert.assertEquals(runner.getFormattedTestTree(), 1, runner.getAllTestsCount());
myFixture.getTempDirFixture().getFile("sysPath");
final VirtualFile folderWithScript = myFixture.getTempDirFixture().getFile(".");
assert folderWithScript != null : "No folder for script " + myScriptName;
Assert.assertThat("sys.path[0] should point to folder with test, while it does not", stdout,
Matchers.containsString(String.format("path[0]=%s", new File(folderWithScript.getPath()).getAbsolutePath())));
}
});
}
/**
* Make sure test rerun works when pattern is enabled (PY-23416)
*/