pytest-dev / unittest2pytest

helps rewriting Python `unittest` test-cases into `pytest` test-cases
GNU General Public License v3.0
128 stars 27 forks source link

Output of invalid indents when comments appear inside implicit string continuations #17

Open jayvdb opened 7 years ago

jayvdb commented 7 years ago

A comment inside a string breaks the tools as it unnecessarily appends line continuations to strings

i.e. this chokes

foo('a'
    # foo
    'b')

A real example

-            self.assertEqual('\n' + STR_PROJECT_WIDE + '\n\n'
-                             '**** t [Section: ] ****' + '\n\n'
-                             '!    ! [Severity: NORMAL]\n'
-                             '!    ! msg\n'
-                             # Second results file isn't there, no context is
-                             # printed, only a warning log message which we
-                             # don't catch
-                             '\n**** t [Section: ] ****' + '\n\n'
-                             '!    ! [Severity: NORMAL]\n'
+            assert '\n' + STR_PROJECT_WIDE + '\n\n' \
+                             '**** t [Section: ] ****' + '\n\n' \
+                             '!    ! [Severity: NORMAL]\n' \
+                             '!    ! msg\n' \
+                             # Second results file isn't there, no context is \
+                             # printed, only a warning log message which we \
+                             # don't catch \
+                             '\n**** t [Section: ] ****' + '\n\n' \
+                             '!    ! [Severity: NORMAL]\n' \
                              '!    ! {0}\n'.format(
                                  highlight_text(self.no_color, 'msg',
-                                                style=BackgroundMessageStyle)),
-                             stdout.getvalue())
+                                                style=BackgroundMessageStyle)) == \
+                             stdout.getvalue()

From https://github.com/coala/coala/blob/8a25983/tests/output/ConsoleInteractionTest.py#L623

Result: https://travis-ci.org/jayvdb/coala/jobs/246949584#L9052

(Another bug coming about the other problem in that build log.)

htgoebel commented 7 years ago

The fixes need to be changed to not use line-continuation if one of the expressions contains a comment. Instead the expressions can be wrapped in (…). Maybe its easier to always wrap the expressions.

We ought to check is other fixes are effected, too.