diff --git a/python/helpers/pycharm/teamcity/messages.py b/python/helpers/pycharm/teamcity/messages.py index 7ddb4078aff1..3e528407cadb 100644 --- a/python/helpers/pycharm/teamcity/messages.py +++ b/python/helpers/pycharm/teamcity/messages.py @@ -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()): diff --git a/python/testData/testRunner/env/pytest/monkeyPatch/test_test.py b/python/testData/testRunner/env/pytest/monkeyPatch/test_test.py new file mode 100644 index 000000000000..d35060ba58d7 --- /dev/null +++ b/python/testData/testRunner/env/pytest/monkeyPatch/test_test.py @@ -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' \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/env/python/testing/PythonPyTestingTest.java b/python/testSrc/com/jetbrains/env/python/testing/PythonPyTestingTest.java index f9180d4592c7..8551ef5a4f10 100644 --- a/python/testSrc/com/jetbrains/env/python/testing/PythonPyTestingTest.java +++ b/python/testSrc/com/jetbrains/env/python/testing/PythonPyTestingTest.java @@ -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(){ + runPythonTest(new SetupTearDownTestTask() { @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(){ + runPythonTest(new RunModuleAsFileTask() { @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("/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 {