diff --git a/_pytest/pytester.py b/_pytest/pytester.py index 5442a1398..36713bf2c 100644 --- a/_pytest/pytester.py +++ b/_pytest/pytester.py @@ -246,8 +246,14 @@ class TmpTestdir: ret = None for name, value in items: p = self.tmpdir.join(name).new(ext=ext) - source = py.builtin._totext(py.code.Source(value)).strip() - content = source.encode("utf-8") # + "\n" + source = py.code.Source(value) + def my_totext(s, encoding="utf-8"): + if py.builtin._isbytes(s): + s = py.builtin._totext(s, encoding=encoding) + return s + source_unicode = "\n".join([my_totext(line) for line in source.lines]) + source = py.builtin._totext(source_unicode) + content = source.strip().encode("utf-8") # + "\n" #content = content.rstrip() + "\n" p.write(content, "wb") if ret is None: diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index 06288e899..d19879527 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -458,28 +458,29 @@ class TestAssertionRewriteHookDetails(object): @pytest.mark.skipif("sys.version_info[0] >= 3") def test_detect_coding_cookie(self, testdir): - testdir.tmpdir.join("test_cookie.py").write("""# -*- coding: utf-8 -*- -u"St\xc3\xa4d" -def test_rewritten(): - assert "@py_builtins" in globals()""", "wb") + testdir.makepyfile(test_cookie=u""" + # -*- coding: utf-8 -*- + u"St\xc3\xa4d" + def test_rewritten(): + assert "@py_builtins" in globals()""".encode('utf-8')) assert testdir.runpytest().ret == 0 @pytest.mark.skipif("sys.version_info[0] >= 3") def test_detect_coding_cookie_second_line(self, testdir): - testdir.tmpdir.join("test_cookie.py").write("""#!/usr/bin/env python -# -*- coding: utf-8 -*- -u"St\xc3\xa4d" -def test_rewritten(): - assert "@py_builtins" in globals()""", "wb") + testdir.makepyfile(test_cookie=u""" + # -*- coding: utf-8 -*- + u"St\xc3\xa4d" + def test_rewritten(): + assert "@py_builtins" in globals()""".encode('utf-8')) assert testdir.runpytest().ret == 0 @pytest.mark.skipif("sys.version_info[0] >= 3") def test_detect_coding_cookie_crlf(self, testdir): - testdir.tmpdir.join("test_cookie.py").write("""#!/usr/bin/env python -# -*- coding: utf-8 -*- -u"St\xc3\xa4d" -def test_rewritten(): - assert "@py_builtins" in globals()""".replace("\n", "\r\n"), "wb") + testdir.makepyfile(test_cookie=u""" + # -*- coding: utf-8 -*- + u"St\xc3\xa4d" + def test_rewritten(): + assert "@py_builtins" in globals()""".encode('utf-8')) assert testdir.runpytest().ret == 0 def test_sys_meta_path_munged(self, testdir):