Merge "Split functionality out of main"

This commit is contained in:
Jenkins 2016-05-24 19:25:41 +00:00 committed by Gerrit Code Review
commit a04c851f35
2 changed files with 40 additions and 17 deletions

View File

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

View File

@ -14,13 +14,16 @@
# under the License. # under the License.
from datetime import datetime as dt from datetime import datetime as dt
import io
import os import os
import subprocess import subprocess
import sys
from ddt import data from ddt import data
from ddt import ddt from ddt import ddt
from ddt import unpack from ddt import unpack
from mock import patch from mock import patch
import six
from os_testr import subunit_trace from os_testr import subunit_trace
from os_testr.tests import base from os_testr.tests import base
@ -79,3 +82,15 @@ class TestSubunitTrace(base.TestCase):
with open(regular_stream, 'rb') as stream: with open(regular_stream, 'rb') as stream:
p.communicate(stream.read()) p.communicate(stream.read())
self.assertEqual(0, p.returncode) 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)