Skip to content

Commit

Permalink
BUG: Tee doesn't have encoding or errors attrs.
Browse files Browse the repository at this point in the history
On Python3, distutils.log uses the encoding and errors attribute of
sys.stdout and stderr.  Tests error out when nose is run with xunit and
it replaces the stdout/stderr with a Tee.
  • Loading branch information
prabhuramachandran committed Aug 26, 2015
1 parent 716d822 commit 14cbbc3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
5 changes: 3 additions & 2 deletions nose/plugins/xunit.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,12 @@ def exc_message(exc_info):

class Tee(object):
def __init__(self, encoding, *args):
self._encoding = encoding
self.encoding = encoding
self._streams = args
self.errors = None

def write(self, data):
data = force_unicode(data, self._encoding)
data = force_unicode(data, self.encoding)
for s in self._streams:
s.write(data)

Expand Down
25 changes: 21 additions & 4 deletions unit_tests/test_xunit.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from nose.pyversion import UNICODE_STRINGS
from nose.tools import eq_
from nose.plugins.xunit import Xunit, escape_cdata, id_split
from nose.plugins.xunit import Xunit, escape_cdata, id_split, Tee
from nose.exc import SkipTest
from nose.config import Config

Expand Down Expand Up @@ -67,11 +67,29 @@ def test_file_from_opt(self):
(options, args) = parser.parse_args(["--xunit-file=blagojevich.xml"])
eq_(options.xunit_file, "blagojevich.xml")

class TestTee(unittest.TestCase):
def setUp(self):
self.orig_stderr = sys.stderr
sys.stderr = Tee('utf-8', self.orig_stderr)

def tearDown(self):
sys.stderr = self.orig_stderr

def test_tee_has_error_and_encoding_attributes(self):
tee = Tee('utf-8', sys.stdout)
self.assertTrue(hasattr(tee, 'encoding'))
self.assertTrue(hasattr(tee, 'errors'))

def test_tee_works_with_distutils_log(self):
from distutils.log import Log, DEBUG
l = Log(DEBUG)
l.warn('Test')

class TestXMLOutputWithXML(unittest.TestCase):

def setUp(self):
self.xmlfile = os.path.abspath(
os.path.join(os.path.dirname(__file__),
os.path.join(os.path.dirname(__file__),
'support', 'xunit.xml'))
parser = optparse.OptionParser()
self.x = Xunit()
Expand Down Expand Up @@ -215,7 +233,7 @@ def test_non_utf8_error(self):
test = mktest()
self.x.beforeTest(test)
try:
raise RuntimeError(chr(128)) # cannot encode as utf8
raise RuntimeError(chr(128)) # cannot encode as utf8
except RuntimeError:
some_err = sys.exc_info()
self.x.addError(test, some_err)
Expand Down Expand Up @@ -309,4 +327,3 @@ def test_addSuccess_early(self):
assert '<?xml version="1.0" encoding="UTF-8"?>' in result
assert ('<testcase classname="test_xunit.TC" '
'name="runTest" time="0') in result

0 comments on commit 14cbbc3

Please # to comment.