Skip to content

Commit

Permalink
Improve coverages for _DuplicateWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
ikedam authored and dnozay committed May 9, 2018
1 parent 6ebfcdd commit 55b0b9d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
48 changes: 48 additions & 0 deletions tests/testsuite.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@

from xmlrunner.unittest import unittest
import xmlrunner
from xmlrunner.result import _DuplicateWriter
from xmlrunner.result import _XMLTestResult
import doctest
import tests.doctest_example
from six import StringIO, BytesIO
from tempfile import mkdtemp
from tempfile import mkstemp
from shutil import rmtree
from glob import glob
from xml.dom import minidom
from lxml import etree
import os
import os.path


Expand Down Expand Up @@ -573,3 +576,48 @@ def test_xmlrunner_hold_traceback(self):
runner = self._test_xmlrunner(suite)
countAfterTest = sys.getrefcount(self.DummyRefCountTest.dummy)
self.assertEqual(countBeforeTest, countAfterTest)


class DuplicateWriterTestCase(unittest.TestCase):
def setUp(self):
fd, self.file = mkstemp()
self.fh = os.fdopen(fd, 'w')
self.buffer = StringIO()
self.writer = _DuplicateWriter(self.fh, self.buffer)

def tearDown(self):
self.buffer.close()
self.fh.close()
os.unlink(self.file)

def getFirstContent(self):
with open(self.file, 'r') as f:
return f.read()

def getSecondContent(self):
return self.buffer.getvalue()

def test_flush(self):
self.writer.write('foobarbaz')
self.writer.flush()
self.assertEqual(self.getFirstContent(), self.getSecondContent())

def test_writable(self):
self.assertTrue(self.writer.writable())

def test_writelines(self):
self.writer.writelines([
'foo\n',
'bar\n',
'baz\n',
])
self.writer.flush()
self.assertEqual(self.getFirstContent(), self.getSecondContent())

def test_write(self):
# try long buffer (1M)
buffer = 'x' * (1024 * 1024)
wrote = self.writer.write(buffer)
self.writer.flush()
self.assertEqual(self.getFirstContent(), self.getSecondContent())
self.assertEqual(wrote, len(self.getSecondContent()))
19 changes: 14 additions & 5 deletions xmlrunner/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def testcase_name(test_method):
return result


class _DuplicateWriter(io.RawIOBase):
class _DuplicateWriter(io.TextIOBase):
"""
Duplicate output from the first handle to the second handle
Expand All @@ -107,11 +107,20 @@ def writelines(self, lines):
self._second.writelines(lines)

def write(self, b):
len = self._first.write(b)
if isinstance(self._first, io.TextIOBase):
wrote = self._first.write(b)

# expected to always succeed to write
self._second.write(b[:len])
return len
if wrote is not None:
# expected to always succeed to write
self._second.write(b[:wrote])

return wrote
else:
# file-like object in Python2
# It doesn't return wrote bytes.
self._first.write(b)
self._second.write(b)
return len(b)


class _TestInfo(object):
Expand Down

0 comments on commit 55b0b9d

Please sign in to comment.