Skip to content

Commit

Permalink
Merge pull request #260 from masayukig/use-stringio
Browse files Browse the repository at this point in the history
Use StringIO instead of tempfile
  • Loading branch information
mtreinish committed Jul 25, 2019
2 parents 75431d9 + 19b45a1 commit 8412557
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
20 changes: 10 additions & 10 deletions stestr/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def output_tests(tests, output=sys.stdout):

for test in tests:
id_str = test.id()
output.write(id_str)
output.write(six.text_type(id_str))
output.write(six.text_type('\n'))


Expand Down Expand Up @@ -139,16 +139,16 @@ def output_summary(successful, tests, tests_delta, time, time_delta, values,


def output_stream(stream, output=sys.stdout):
_binary_stdout = subunit.make_stream_binary(output)
_binary_stdout = subunit.make_stream_binary(output)
contents = stream.read(65536)
assert type(contents) is bytes, \
"Bad stream contents %r" % type(contents)
# If there are unflushed bytes in the text wrapper, we need to sync..
output.flush()
while contents:
_binary_stdout.write(contents)
contents = stream.read(65536)
assert type(contents) is bytes, \
"Bad stream contents %r" % type(contents)
# If there are unflushed bytes in the text wrapper, we need to sync..
output.flush()
while contents:
_binary_stdout.write(contents)
contents = stream.read(65536)
_binary_stdout.flush()
_binary_stdout.flush()


class ReturnCodeToSubunit(object):
Expand Down
22 changes: 9 additions & 13 deletions stestr/tests/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# License for the specific language governing permissions and limitations
# under the License.

import tempfile
import io

from stestr import output
from stestr.tests import base
Expand All @@ -27,10 +27,9 @@ def test_output_table(self):
"-------- ---------- ------------------\n" \
"1 0000000002 foo\n" \
"bar 6 This is a content.\n"
with tempfile.TemporaryFile('w+t') as f:
with io.StringIO() as f:
output.output_table(table, f)
f.seek(0)
actual = f.read()
actual = f.getvalue()
self.assertEqual(expected, actual)

def test_output_tests(self):
Expand All @@ -43,34 +42,31 @@ def id(self):

tests = [Test('a'), Test('b'), Test('foo')]
expected = "a\nb\nfoo\n"
with tempfile.TemporaryFile('w+t') as f:
with io.StringIO() as f:
output.output_tests(tests, f)
f.seek(0)
actual = f. read()
actual = f.getvalue()
self.assertEqual(expected, actual)

def test_output_summary_passed(self):
expected = 'Ran 10 (+5) tests in 1.100s (+0.100s)\n' \
'PASSED (id=99 (+1), id=100 (+2))\n'
with tempfile.TemporaryFile('w+t') as f:
with io.StringIO() as f:
output.output_summary(
successful=True, tests=10, tests_delta=5,
time=1.1, time_delta=0.1,
values=[('id', 99, 1), ('id', '100', 2)],
output=f)
f.seek(0)
actual = f.read()
actual = f.getvalue()
self.assertEqual(expected, actual)

def test_output_summary_failed(self):
expected = 'Ran 10 (+5) tests in 1.100s (+0.100s)\n' \
'FAILED (id=99 (+1), id=100 (+2))\n'
with tempfile.TemporaryFile('w+t') as f:
with io.StringIO() as f:
output.output_summary(
successful=False, tests=10, tests_delta=5,
time=1.1, time_delta=0.1,
values=[('id', 99, 1), ('id', '100', 2)],
output=f)
f.seek(0)
actual = f.read()
actual = f.getvalue()
self.assertEqual(expected, actual)

0 comments on commit 8412557

Please sign in to comment.