Skip to content

Commit f47ae74

Browse files
committed
Make makepyfile accept UTF-8 so a few cookie tests in test_assertrewrite.py
don't need to be dedented. --HG-- branch : makepyfile_utf8
1 parent e061ace commit f47ae74

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

Diff for: _pytest/pytester.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,14 @@ def _makefile(self, ext, args, kwargs):
246246
ret = None
247247
for name, value in items:
248248
p = self.tmpdir.join(name).new(ext=ext)
249-
source = py.builtin._totext(py.code.Source(value)).strip()
250-
content = source.encode("utf-8") # + "\n"
249+
source = py.code.Source(value)
250+
def my_totext(s, encoding="utf-8"):
251+
if py.builtin._isbytes(s):
252+
s = py.builtin._totext(s, encoding=encoding)
253+
return s
254+
source_unicode = "\n".join([my_totext(line) for line in source.lines])
255+
source = py.builtin._totext(source_unicode)
256+
content = source.strip().encode("utf-8") # + "\n"
251257
#content = content.rstrip() + "\n"
252258
p.write(content, "wb")
253259
if ret is None:

Diff for: testing/test_assertrewrite.py

+15-14
Original file line numberDiff line numberDiff line change
@@ -458,28 +458,29 @@ def test_assume_ascii(self, testdir):
458458

459459
@pytest.mark.skipif("sys.version_info[0] >= 3")
460460
def test_detect_coding_cookie(self, testdir):
461-
testdir.tmpdir.join("test_cookie.py").write("""# -*- coding: utf-8 -*-
462-
u"St\xc3\xa4d"
463-
def test_rewritten():
464-
assert "@py_builtins" in globals()""", "wb")
461+
testdir.makepyfile(test_cookie=u"""
462+
# -*- coding: utf-8 -*-
463+
u"St\xc3\xa4d"
464+
def test_rewritten():
465+
assert "@py_builtins" in globals()""".encode('utf-8'))
465466
assert testdir.runpytest().ret == 0
466467

467468
@pytest.mark.skipif("sys.version_info[0] >= 3")
468469
def test_detect_coding_cookie_second_line(self, testdir):
469-
testdir.tmpdir.join("test_cookie.py").write("""#!/usr/bin/env python
470-
# -*- coding: utf-8 -*-
471-
u"St\xc3\xa4d"
472-
def test_rewritten():
473-
assert "@py_builtins" in globals()""", "wb")
470+
testdir.makepyfile(test_cookie=u"""
471+
# -*- coding: utf-8 -*-
472+
u"St\xc3\xa4d"
473+
def test_rewritten():
474+
assert "@py_builtins" in globals()""".encode('utf-8'))
474475
assert testdir.runpytest().ret == 0
475476

476477
@pytest.mark.skipif("sys.version_info[0] >= 3")
477478
def test_detect_coding_cookie_crlf(self, testdir):
478-
testdir.tmpdir.join("test_cookie.py").write("""#!/usr/bin/env python
479-
# -*- coding: utf-8 -*-
480-
u"St\xc3\xa4d"
481-
def test_rewritten():
482-
assert "@py_builtins" in globals()""".replace("\n", "\r\n"), "wb")
479+
testdir.makepyfile(test_cookie=u"""
480+
# -*- coding: utf-8 -*-
481+
u"St\xc3\xa4d"
482+
def test_rewritten():
483+
assert "@py_builtins" in globals()""".encode('utf-8'))
483484
assert testdir.runpytest().ret == 0
484485

485486
def test_sys_meta_path_munged(self, testdir):

0 commit comments

Comments
 (0)