refine internal test support for unicode-related bits (used by a test in pytest-pep8)

This commit is contained in:
holger krekel 2012-09-20 10:57:23 +02:00
parent 6cb3281ddd
commit 22dc47d9f9
3 changed files with 34 additions and 24 deletions

View File

@ -1,2 +1,2 @@
# #
__version__ = '2.3.0.dev13' __version__ = '2.3.0.dev14'

View File

@ -2,6 +2,7 @@
import py, pytest import py, pytest
import sys, os import sys, os
import codecs
import re import re
import inspect import inspect
import time import time
@ -244,8 +245,10 @@ class TmpTestdir:
ret = None ret = None
for name, value in items: for name, value in items:
p = self.tmpdir.join(name).new(ext=ext) p = self.tmpdir.join(name).new(ext=ext)
source = py.builtin._totext(py.code.Source(value)).lstrip() source = py.builtin._totext(py.code.Source(value)).strip()
p.write(source.encode("utf-8"), "wb") content = source.encode("utf-8") # + "\n"
#content = content.rstrip() + "\n"
p.write(content, "wb")
if ret is None: if ret is None:
ret = p ret = p
return ret return ret
@ -440,28 +443,35 @@ class TmpTestdir:
p1 = self.tmpdir.join("stdout") p1 = self.tmpdir.join("stdout")
p2 = self.tmpdir.join("stderr") p2 = self.tmpdir.join("stderr")
print_("running", cmdargs, "curdir=", py.path.local()) print_("running", cmdargs, "curdir=", py.path.local())
f1 = p1.open("wb") f1 = codecs.open(str(p1), "w", encoding="utf8")
f2 = p2.open("wb") f2 = codecs.open(str(p2), "w", encoding="utf8")
now = time.time() try:
popen = self.popen(cmdargs, stdout=f1, stderr=f2, now = time.time()
close_fds=(sys.platform != "win32")) popen = self.popen(cmdargs, stdout=f1, stderr=f2,
ret = popen.wait() close_fds=(sys.platform != "win32"))
f1.close() ret = popen.wait()
f2.close() finally:
out = p1.read("rb") f1.close()
out = getdecoded(out).splitlines() f2.close()
err = p2.read("rb") f1 = codecs.open(str(p1), "r", encoding="utf8")
err = getdecoded(err).splitlines() f2 = codecs.open(str(p2), "r", encoding="utf8")
def dump_lines(lines, fp): try:
try: out = f1.read().splitlines()
for line in lines: err = f2.read().splitlines()
py.builtin.print_(line, file=fp) finally:
except UnicodeEncodeError: f1.close()
print("couldn't print to %s because of encoding" % (fp,)) f2.close()
dump_lines(out, sys.stdout) self._dump_lines(out, sys.stdout)
dump_lines(err, sys.stderr) self._dump_lines(err, sys.stderr)
return RunResult(ret, out, err, time.time()-now) return RunResult(ret, out, err, time.time()-now)
def _dump_lines(self, lines, fp):
try:
for line in lines:
py.builtin.print_(line, file=fp)
except UnicodeEncodeError:
print("couldn't print to %s because of encoding" % (fp,))
def runpybin(self, scriptname, *args): def runpybin(self, scriptname, *args):
fullargs = self._getpybinargs(scriptname) + args fullargs = self._getpybinargs(scriptname) + args
return self.run(*fullargs) return self.run(*fullargs)

View File

@ -24,7 +24,7 @@ def main():
name='pytest', name='pytest',
description='py.test: simple powerful testing with Python', description='py.test: simple powerful testing with Python',
long_description = long_description, long_description = long_description,
version='2.3.0.dev13', version='2.3.0.dev14',
url='http://pytest.org', url='http://pytest.org',
license='MIT license', license='MIT license',
platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'], platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'],