From d48ce2af511851cc8e29caa08dd028efb1245b62 Mon Sep 17 00:00:00 2001 From: "Ilya.Kazakevich" Date: Fri, 24 Aug 2018 17:54:51 +0300 Subject: [PATCH] PY-27267: swap diff in pytest (Sync tcmessages and add test) Actual and expected are in wrong order. Fixed on tcmessages side, test added. Use swapdiff in pytest.ini to switch it back --- .../helpers/pycharm/teamcity/pytest_plugin.py | 17 ++++++++---- .../testRunner/env/pytest/diff/test_diff.py | 2 ++ .../python/testing/PythonPyTestingTest.java | 26 +++++++++++++++++++ 3 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 python/testData/testRunner/env/pytest/diff/test_diff.py diff --git a/python/helpers/pycharm/teamcity/pytest_plugin.py b/python/helpers/pycharm/teamcity/pytest_plugin.py index 056596473095..3b8aba55fd1d 100644 --- a/python/helpers/pycharm/teamcity/pytest_plugin.py +++ b/python/helpers/pycharm/teamcity/pytest_plugin.py @@ -25,7 +25,7 @@ from teamcity import diff_tools diff_tools.patch_unittest_diff() -def fetch_diff_error_from_message(err_message): +def fetch_diff_error_from_message(err_message, swap_diff): line_with_diff = None diff_error_message = None lines = err_message.split("\n") @@ -41,7 +41,11 @@ def fetch_diff_error_from_message(err_message): parts = [x.strip() for x in line_with_diff.split("==")] parts = [s[1:-1] if s.startswith("'") or s.startswith('"') else s for s in parts] # Pytest cuts too long lines, no need to check is_too_big - return diff_tools.EqualsAssertionError(parts[0], parts[1], diff_error_message) + expected, actual = parts[1], parts[0] + + if swap_diff: + expected, actual = actual, expected + return diff_tools.EqualsAssertionError(expected, actual, diff_error_message) else: return None @@ -71,6 +75,7 @@ def pytest_addoption(parser): kwargs.update({"type": "bool"}) parser.addini("skippassedoutput", **kwargs) + parser.addini("swapdiff", **kwargs) def pytest_configure(config): @@ -89,7 +94,8 @@ def pytest_configure(config): config._teamcityReporting = EchoTeamCityMessages( output_capture_enabled, coverage_controller, - skip_passed_output + skip_passed_output, + bool(config.getini('swapdiff')) ) config.pluginmanager.register(config._teamcityReporting) @@ -110,7 +116,7 @@ def _get_coverage_controller(config): class EchoTeamCityMessages(object): - def __init__(self, output_capture_enabled, coverage_controller, skip_passed_output): + def __init__(self, output_capture_enabled, coverage_controller, skip_passed_output, swap_diff): self.coverage_controller = coverage_controller self.output_capture_enabled = output_capture_enabled self.skip_passed_output = skip_passed_output @@ -120,6 +126,7 @@ class EchoTeamCityMessages(object): self.max_reported_output_size = 1 * 1024 * 1024 self.reported_output_chunk_size = 50000 + self.swap_diff = swap_diff def get_id_from_location(self, location): if type(location) is not tuple or len(location) != 3 or not hasattr(location[2], "startswith"): @@ -258,7 +265,7 @@ class EchoTeamCityMessages(object): if err_message.startswith("assert"): err_message = "AssertionError: " + err_message if err_message.startswith("AssertionError:"): - diff_error = fetch_diff_error_from_message(err_message) + diff_error = fetch_diff_error_from_message(err_message, self.swap_diff) except Exception: pass diff --git a/python/testData/testRunner/env/pytest/diff/test_diff.py b/python/testData/testRunner/env/pytest/diff/test_diff.py new file mode 100644 index 000000000000..b2c8c6411dde --- /dev/null +++ b/python/testData/testRunner/env/pytest/diff/test_diff.py @@ -0,0 +1,2 @@ +def test_diff(): + assert "actual" == "expected" \ 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 a54107901d04..3ab74b2337a3 100644 --- a/python/testSrc/com/jetbrains/env/python/testing/PythonPyTestingTest.java +++ b/python/testSrc/com/jetbrains/env/python/testing/PythonPyTestingTest.java @@ -66,6 +66,32 @@ public final class PythonPyTestingTest extends PyEnvTestCase { }); } + + @Test + public void testDiff() { + runPythonTest( + new PyProcessWithConsoleTestTask("/testRunner/env/pytest/diff", SdkCreationType.EMPTY_SDK) { + + @NotNull + @Override + protected PyTestTestProcessRunner createProcessRunner() { + return new PyTestTestProcessRunner("test_diff.py", 1); + } + + @Override + protected void checkTestResults(@NotNull final PyTestTestProcessRunner runner, + @NotNull final String stdout, + @NotNull final String stderr, + @NotNull final String all) { + + final String expectedConsoleText = "Expected :expected\n" + + "Actual :actual\n" + + " "; + Assert.assertThat("No diff", runner.getAllConsoleText(), Matchers.containsString(expectedConsoleText)); + } + }); + } + /** * Provides existing .xml and checks that configuration is able to parse it */