Commits

Benjamin Peterson committed a7e4bf1

rewrite file newlines when the python parser is picky

  • Participants
  • Parent commits c5970c0

Comments (0)

Files changed (3)

 Changes between 2.1.1 and [NEXT VERSION]
 ----------------------------------------
 
+- fix assertion rewriting on files with windows newlines on some Python versions
 - refine test discovery by package/module name (--pyargs), thanks Florian Mayer
 - fix issue69 / assertion rewriting fixed on some boolean operations
 - fix issue68 / packages now work with assertion rewriting

_pytest/assertion/rewrite.py

 PYC_EXT = ".py" + "c" if __debug__ else "o"
 PYC_TAIL = "." + PYTEST_TAG + PYC_EXT
 
+REWRITE_NEWLINES = sys.version_info[:2] != (2, 7) and sys.version_info < (3, 2)
+
 class AssertionRewritingHook(object):
     """Import hook which rewrites asserts."""
 
         fp.close()
     return True
 
+RN = "\r\n".encode("utf-8")
+N = "\n".encode("utf-8")
+
 def _rewrite_test(state, fn):
     """Try to read and rewrite *fn* and return the code object."""
     try:
         source = fn.read("rb")
     except EnvironmentError:
         return None
+    # On Python versions which are not 2.7 and less than or equal to 3.1, the
+    # parser expects *nix newlines.
+    if REWRITE_NEWLINES:
+        source = source.replace(RN, N) + N
     try:
         tree = ast.parse(source)
     except SyntaxError:

testing/test_assertrewrite.py

 def test_rewritten():
     assert "@py_builtins" in globals()""")
         assert testdir.runpytest().ret == 0
+
+    def test_translate_newlines(self, testdir):
+        content = "def test_rewritten():\r\n assert '@py_builtins' in globals()"
+        b = content.encode("utf-8")
+        testdir.tmpdir.join("test_newlines.py").write(b, "wb")
+        assert testdir.runpytest().ret == 0