diff --git a/os_testr/subunit_trace.py b/os_testr/subunit_trace.py index f194c6e..89b071f 100755 --- a/os_testr/subunit_trace.py +++ b/os_testr/subunit_trace.py @@ -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__': diff --git a/os_testr/tests/test_subunit_trace.py b/os_testr/tests/test_subunit_trace.py index 736dff9..462bceb 100644 --- a/os_testr/tests/test_subunit_trace.py +++ b/os_testr/tests/test_subunit_trace.py @@ -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)