PY-23567: Update Leonid's runners to support time functions monkeypatching

If user monkeypatches strftime PyCharm should survive it
This commit is contained in:
Ilya.Kazakevich
2017-04-17 17:09:00 +03:00
parent 306b551b7e
commit ccbb81dc82
3 changed files with 52 additions and 6 deletions
+12 -3
View File
@@ -1,6 +1,6 @@
# coding=utf-8
import sys
import datetime
import time
if sys.version_info < (3, ):
# Python 2
@@ -10,6 +10,11 @@ else:
text_type = str
# Capture some time functions to allow monkeypatching them in tests
_time = time.time
_localtime = time.localtime
_strftime = time.strftime
_quote = {"'": "|'", "|": "||", "\n": "|n", "\r": "|r", '[': '|[', ']': '|]'}
def escape_value(value):
@@ -17,7 +22,7 @@ def escape_value(value):
class TeamcityServiceMessages(object):
def __init__(self, output=sys.stdout, now=datetime.datetime.now, encoding='auto'):
def __init__(self, output=sys.stdout, now=_time, encoding='auto'):
if sys.version_info < (3, ) or not hasattr(output, 'buffer'):
self.output = output
else:
@@ -52,7 +57,11 @@ class TeamcityServiceMessages(object):
return escape_value(self.decode(value))
def message(self, messageName, **properties):
timestamp = self.now().strftime("%Y-%m-%dT%H:%M:%S.") + "%03d" % (self.now().microsecond / 1000)
current_time = self.now()
(current_time_int, current_time_fraction) = divmod(current_time, 1)
current_time_struct = _localtime(current_time_int)
timestamp = _strftime("%Y-%m-%dT%H:%M:%S.", current_time_struct) + "%03d" % (int(current_time_fraction * 1000))
message = ("##teamcity[%s timestamp='%s'" % (messageName, timestamp))
for k in sorted(properties.keys()):
@@ -0,0 +1,15 @@
import time
import pytest
@pytest.fixture()
def patch_time_strftime(monkeypatch):
def mockreturn(*args):
return '2016-7-25_8-54-30'
monkeypatch.setattr(time, 'strftime', mockreturn)
def test_monkeypatch(patch_time_strftime):
assert time.strftime('%Y-%m-%d_%H-%M-%S') == '2016-7-25_8-54-30'
@@ -12,7 +12,6 @@ import com.intellij.util.PathUtil;
import com.jetbrains.env.EnvTestTagsRequired;
import com.jetbrains.env.PyEnvTestCase;
import com.jetbrains.env.PyProcessWithConsoleTestTask;
import com.jetbrains.env.ut.PyNoseTestProcessRunner;
import com.jetbrains.env.ut.PyTestTestProcessRunner;
import com.jetbrains.python.psi.PyFunction;
import com.jetbrains.python.sdkTools.SdkCreationType;
@@ -42,7 +41,7 @@ public final class PythonPyTestingTest extends PyEnvTestCase {
// Ensures setup/teardown does not break anything
@Test
public void testSetupTearDown() throws Exception {
runPythonTest(new SetupTearDownTestTask<PyTestTestProcessRunner>(){
runPythonTest(new SetupTearDownTestTask<PyTestTestProcessRunner>() {
@NotNull
@Override
protected PyTestTestProcessRunner createProcessRunner() throws Exception {
@@ -56,7 +55,7 @@ public final class PythonPyTestingTest extends PyEnvTestCase {
*/
@Test
public void testRunModuleAsFile() throws Exception {
runPythonTest(new RunModuleAsFileTask<PyTestTestProcessRunner>(){
runPythonTest(new RunModuleAsFileTask<PyTestTestProcessRunner>() {
@NotNull
@Override
protected PyTestTestProcessRunner createProcessRunner() throws Exception {
@@ -77,6 +76,29 @@ public final class PythonPyTestingTest extends PyEnvTestCase {
});
}
// Ensure test survives patched strftime
@Test
public void testMonkeyPatch() throws Exception {
runPythonTest(
new PyProcessWithConsoleTestTask<PyTestTestProcessRunner>("/testRunner/env/pytest/monkeyPatch", SdkCreationType.EMPTY_SDK) {
@NotNull
@Override
protected PyTestTestProcessRunner createProcessRunner() throws Exception {
return new PyTestTestProcessRunner("test_test.py", 0);
}
@Override
protected void checkTestResults(@NotNull PyTestTestProcessRunner runner,
@NotNull String stdout,
@NotNull String stderr,
@NotNull String all) {
assertEquals("Monkeypatch broke the test: " + stderr, 1, runner.getPassedTestsCount());
}
});
}
// Ensure slow test is not run when -m "not slow" is provided
@Test
public void testMarkerWithSpaces() throws Exception {