Skip to content

Commit

Permalink
Merge an amended version of #945.
Browse files Browse the repository at this point in the history
Tee doesn't have encoding and errors attributes.
  • Loading branch information
jszakmeister committed Nov 28, 2015
2 parents bee3081 + 1082b8c commit 5b02f5a
Show file tree
Hide file tree
Showing 2 changed files with 33 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
34 changes: 30 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,38 @@ 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):
try:
from distutils.log import Log, DEBUG
except ImportError:
raise SkipTest("distutils.log not available; skipping")

l = Log(DEBUG)
try:
l.warn('Test')
except Exception, e:
self.fail(
"Exception raised while writing to distutils.log: %s" % (e,))


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 +242,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 +336,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 5b02f5a

Please # to comment.