From f47ae749817df967e1ba2c07e67eda49ae328077 Mon Sep 17 00:00:00 2001 From: Marc Abramowitz Date: Fri, 28 Mar 2014 09:44:18 -0700 Subject: [PATCH] Make makepyfile accept UTF-8 so a few cookie tests in test_assertrewrite.py don't need to be dedented. --HG-- branch : makepyfile_utf8 --- _pytest/pytester.py | 10 ++++++++-- testing/test_assertrewrite.py | 29 +++++++++++++++-------------- 2 files changed, 23 insertions(+), 16 deletions(-) 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):