Split functionality out of main

This commit splits out the actual output generation functionality from
the main function. It also adds parameters for specifying a stdin and
stdout file object. This enables external access to the output
generation within python, which could be useful for integrating
subunit-trace output with your own test runner.

Change-Id: Id9fc13b9b70c5b2cc47b48eee6426e7e312eff7f
This commit is contained in:
step6829 2016-02-23 11:14:23 -05:00 committed by Matthew Treinish
parent c0e03d3e31
commit 7c2bb825c4
No known key found for this signature in database
GPG Key ID: FD12A0F214C9E177
2 changed files with 40 additions and 17 deletions

View File

@ -344,21 +344,22 @@ def parse_args():
return parser.parse_args()
def main():
args = parse_args()
def trace(stdin, stdout, print_failures=False, failonly=False,
enable_diff=False, abbreviate=False, color=False, post_fails=False,
no_summary=False):
stream = subunit.ByteStreamToStreamResult(
sys.stdin, non_subunit_name='stdout')
stdin, non_subunit_name='stdout')
outcomes = testtools.StreamToDict(
functools.partial(show_outcome, sys.stdout,
print_failures=args.print_failures,
failonly=args.failonly,
enable_diff=args.enable_diff,
abbreviate=args.abbreviate,
enable_color=args.color))
functools.partial(show_outcome, stdout,
print_failures=print_failures,
failonly=failonly,
enable_diff=enable_diff,
abbreviate=abbreviate,
enable_color=color))
summary = testtools.StreamSummary()
result = testtools.CopyStreamResult([outcomes, summary])
result = testtools.StreamResultRouter(result)
cat = subunit.test_results.CatFiles(sys.stdout)
cat = subunit.test_results.CatFiles(stdout)
result.add_rule(cat, 'test_id', test_id=None)
start_time = datetime.datetime.utcnow()
result.startTestRun()
@ -371,18 +372,25 @@ def main():
if count_tests('status', '.*') == 0:
print("The test run didn't actually run any tests")
exit(1)
if args.post_fails:
print_fails(sys.stdout)
if not args.no_summary:
print_summary(sys.stdout, elapsed_time)
return 1
if post_fails:
print_fails(stdout)
if not no_summary:
print_summary(stdout, elapsed_time)
# NOTE(mtreinish): Ideally this should live in testtools streamSummary
# this is just in place until the behavior lands there (if it ever does)
if count_tests('status', '^success$') == 0:
print("\nNo tests were successful during the run")
exit(1)
exit(0 if summary.wasSuccessful() else 1)
return 1
return 0 if summary.wasSuccessful() else 1
def main():
args = parse_args()
exit(trace(sys.stdin, sys.stdout, args.print_failures, args.failonly,
args.enable_diff, args.abbreviate, args.color, args.post_fails,
args.no_summary))
if __name__ == '__main__':

View File

@ -14,13 +14,16 @@
# under the License.
from datetime import datetime as dt
import io
import os
import subprocess
import sys
from ddt import data
from ddt import ddt
from ddt import unpack
from mock import patch
import six
from os_testr import subunit_trace
from os_testr.tests import base
@ -79,3 +82,15 @@ class TestSubunitTrace(base.TestCase):
with open(regular_stream, 'rb') as stream:
p.communicate(stream.read())
self.assertEqual(0, p.returncode)
def test_trace(self):
regular_stream = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
'sample_streams/successful.subunit')
bytes_ = io.BytesIO()
with open(regular_stream, 'rb') as stream:
bytes_.write(six.binary_type(stream.read()))
bytes_.seek(0)
stdin = io.TextIOWrapper(io.BufferedReader(bytes_))
returncode = subunit_trace.trace(stdin, sys.stdout)
self.assertEqual(0, returncode)