Skip to content

Commit

Permalink
Merge pull request #285 from stephenfin/bug/1813147
Browse files Browse the repository at this point in the history
Treat bytes and strings differently
  • Loading branch information
mtreinish committed Feb 19, 2020
2 parents ff9b688 + 97fe81d commit d79b9e8
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 33 deletions.
20 changes: 14 additions & 6 deletions stestr/commands/last.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ def get_parser(self, prog_name):
dest='all_attachments',
help='If set print all text attachment contents on'
' a successful test execution')
parser.add_argument('--show-binary-attachments', action='store_true',
dest='show_binary_attachments',
help='If set, show non-text attachments. This is '
'generally only useful for debug purposes.')
return parser

def take_action(self, parsed_args):
Expand Down Expand Up @@ -108,12 +112,13 @@ def take_action(self, parsed_args):
repo_url=self.app_args.repo_url,
subunit_out=args.subunit, pretty_out=pretty_out,
color=color, suppress_attachments=suppress_attachments,
all_attachments=all_attachments)
all_attachments=all_attachments,
show_binary_attachments=args.show_binary_attachments)


def last(repo_type='file', repo_url=None, subunit_out=False, pretty_out=True,
color=False, stdout=sys.stdout, suppress_attachments=False,
all_attachments=False):
all_attachments=False, show_binary_attachments=False):
"""Show the last run loaded into a a repository
This function will print the results from the last run in the repository
Expand All @@ -137,6 +142,8 @@ def last(repo_type='file', repo_url=None, subunit_out=False, pretty_out=True,
will not print attachments on successful test execution.
:param bool all_attachments: When set true subunit_trace will print all
text attachments on successful test execution.
:param bool show_binary_attachments: When set to true, subunit_trace will
print binary attachments in addition to text attachments.
:return return_code: The exit code for the command. 0 for success and > 0
for failures.
Expand Down Expand Up @@ -182,10 +189,11 @@ def last(repo_type='file', repo_url=None, subunit_out=False, pretty_out=True,
failed = not results.wasSuccessful(summary)
else:
stream = latest_run.get_subunit_stream()
failed = subunit_trace.trace(stream, stdout, post_fails=True,
color=color,
suppress_attachments=suppress_attachments,
all_attachments=all_attachments)
failed = subunit_trace.trace(
stream, stdout, post_fails=True, color=color,
suppress_attachments=suppress_attachments,
all_attachments=all_attachments,
show_binary_attachments=show_binary_attachments)
if failed:
return 1
else:
Expand Down
21 changes: 15 additions & 6 deletions stestr/commands/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ def get_parser(self, prog_name):
dest='all_attachments',
help='If set print all text attachment contents on'
' a successful test execution')
parser.add_argument('--show-binary-attachments', action='store_true',
dest='show_binary_attachments',
help='If set, show non-text attachments. This is '
'generally only useful for debug purposes.')
return parser

def take_action(self, parsed_args):
Expand Down Expand Up @@ -134,14 +138,15 @@ def take_action(self, parsed_args):
pretty_out=pretty_out, color=color,
stdout=stdout, abbreviate=abbreviate,
suppress_attachments=suppress_attachments, serial=True,
all_attachments=all_attachments)
all_attachments=all_attachments,
show_binary_attachments=args.show_binary_attachments)


def load(force_init=False, in_streams=None,
partial=False, subunit_out=False, repo_type='file', repo_url=None,
run_id=None, streams=None, pretty_out=False, color=False,
stdout=sys.stdout, abbreviate=False, suppress_attachments=False,
serial=False, all_attachments=False):
serial=False, all_attachments=False, show_binary_attachments=False):
"""Load subunit streams into a repository
This function will load subunit streams into the repository. It will
Expand Down Expand Up @@ -172,6 +177,8 @@ def load(force_init=False, in_streams=None,
will not print attachments on successful test execution.
:param bool all_attachments: When set true subunit_trace will print all
text attachments on successful test execution.
:param bool show_binary_attachments: When set to true, subunit_trace will
print binary attachments in addition to text attachments.
:return return_code: The exit code for the command. 0 for success and > 0
for failures.
Expand Down Expand Up @@ -233,7 +240,8 @@ def make_tests():
stream, non_subunit_name='stdout')
result = _load_case(inserter, repo, case, subunit_out, pretty_out,
color, stdout, abbreviate,
suppress_attachments, all_attachments)
suppress_attachments, all_attachments,
show_binary_attachments)
if result or retval:
retval = 1
else:
Expand All @@ -242,14 +250,14 @@ def make_tests():
case = testtools.ConcurrentStreamTestSuite(make_tests)
retval = _load_case(inserter, repo, case, subunit_out, pretty_out,
color, stdout, abbreviate, suppress_attachments,
all_attachments)
all_attachments, show_binary_attachments)

return retval


def _load_case(inserter, repo, case, subunit_out, pretty_out,
color, stdout, abbreviate, suppress_attachments,
all_attachments):
all_attachments, show_binary_attachments):
if subunit_out:
output_result, summary_result = output.make_result(inserter.get_id,
output=stdout)
Expand All @@ -258,7 +266,8 @@ def _load_case(inserter, repo, case, subunit_out, pretty_out,
functools.partial(subunit_trace.show_outcome, stdout,
enable_color=color, abbreviate=abbreviate,
suppress_attachments=suppress_attachments,
all_attachments=all_attachments))
all_attachments=all_attachments,
show_binary_attachments=show_binary_attachments))
summary_result = testtools.StreamSummary()
output_result = testtools.CopyStreamResult([outcomes, summary_result])
output_result = testtools.StreamResultRouter(output_result)
Expand Down
30 changes: 22 additions & 8 deletions stestr/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ def get_parser(self, prog_name):
dest='all_attachments',
help='If set print all text attachment contents on'
' a successful test execution')
parser.add_argument('--show-binary-attachments', action='store_true',
dest='show_binary_attachments',
help='If set, show non-text attachments. This is '
'generally only useful for debug purposes.')
return parser

def take_action(self, parsed_args):
Expand Down Expand Up @@ -244,7 +248,9 @@ def take_action(self, parsed_args):
filters=filters, pretty_out=pretty_out, color=color,
stdout=stdout, abbreviate=abbreviate,
suppress_attachments=suppress_attachments,
all_attachments=all_attachments, pdb=args.pdb)
all_attachments=all_attachments,
show_binary_attachments=args.show_binary_attachments,
pdb=args.pdb)

# Always output slowest test info if requested, regardless of other
# test run options
Expand Down Expand Up @@ -285,7 +291,8 @@ def run_command(config='.stestr.conf', repo_type='file',
no_discover=False, random=False, combine=False, filters=None,
pretty_out=True, color=False, stdout=sys.stdout,
abbreviate=False, suppress_attachments=False,
all_attachments=False, pdb=False):
all_attachments=False, show_binary_attachments=True,
pdb=False):
"""Function to execute the run command
This function implements the run command. It will run the tests specified
Expand Down Expand Up @@ -348,6 +355,8 @@ def run_command(config='.stestr.conf', repo_type='file',
will not print attachments on successful test execution.
:param bool all_attachments: When set true subunit_trace will print all
text attachments on successful test execution.
:param bool show_binary_attachments: When set to true, subunit_trace will
print binary attachments in addition to text attachments.
:param str pdb: Takes in a single test_id to bypasses test
discover and just execute the test specified without launching any
additional processes. A file name may be used in place of a test name.
Expand Down Expand Up @@ -430,7 +439,8 @@ def run_tests():
pretty_out=pretty_out,
color=color, stdout=stdout, abbreviate=abbreviate,
suppress_attachments=suppress_attachments,
all_attachments=all_attachments)
all_attachments=all_attachments,
show_binary_attachments=show_binary_attachments)

if not until_failure:
return run_tests()
Expand Down Expand Up @@ -475,7 +485,8 @@ def run_tests():
pretty_out=pretty_out,
color=color, stdout=stdout, abbreviate=abbreviate,
suppress_attachments=suppress_attachments,
all_attachments=all_attachments)
all_attachments=all_attachments,
show_binary_attachments=show_binary_attachments)

if failing or analyze_isolation:
ids = _find_failing(repo)
Expand Down Expand Up @@ -525,7 +536,8 @@ def run_tests():
repo_type=repo_type, repo_url=repo_url,
pretty_out=pretty_out, color=color, abbreviate=abbreviate,
stdout=stdout, suppress_attachments=suppress_attachments,
all_attachments=all_attachments)
all_attachments=all_attachments,
show_binary_attachments=show_binary_attachments)
if run_result > result:
result = run_result
return result
Expand All @@ -540,7 +552,8 @@ def run_tests():
stdout=stdout,
abbreviate=abbreviate,
suppress_attachments=suppress_attachments,
all_attachments=all_attachments)
all_attachments=all_attachments,
show_binary_attachments=show_binary_attachments)
else:
# Where do we source data about the cause of conflicts.
latest_run = repo.get_latest_run()
Expand Down Expand Up @@ -585,7 +598,7 @@ def _run_tests(cmd, until_failure,
subunit_out=False, combine_id=None, repo_type='file',
repo_url=None, pretty_out=True, color=False, stdout=sys.stdout,
abbreviate=False, suppress_attachments=False,
all_attachments=False):
all_attachments=False, show_binary_attachments=False):
"""Run the tests cmd was parameterised with."""
cmd.setUp()
try:
Expand All @@ -603,7 +616,8 @@ def run_tests():
pretty_out=pretty_out, color=color, stdout=stdout,
abbreviate=abbreviate,
suppress_attachments=suppress_attachments,
all_attachments=all_attachments)
all_attachments=all_attachments,
show_binary_attachments=show_binary_attachments)

if not until_failure:
return run_tests()
Expand Down
37 changes: 24 additions & 13 deletions stestr/subunit_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ def find_worker(test):


# Print out stdout/stderr if it exists, always
def print_attachments(stream, test, all_channels=False):
def print_attachments(stream, test, all_channels=False,
show_binary_attachments=False):
"""Print out subunit attachments.
Print out subunit attachments that contain content. This
Expand All @@ -117,13 +118,18 @@ def print_attachments(stream, test, all_channels=False):
name = name.split(':')[0]
if detail.content_type.type == 'test':
detail.content_type.type = 'text'
if (all_channels or name in channels) and detail.as_text():
if all_channels or name in channels:
title = "Captured %s:" % name
stream.write("\n%s\n%s\n" % (title, ('~' * len(title))))

# indent attachment lines 4 spaces to make them visually
# offset
for line in detail.as_text().split('\n'):
stream.write(" %s\n" % line)
if detail.content_type.type == 'text':
for line in detail.iter_text():
stream.write(" %s\n" % line)
elif show_binary_attachments: # binary
for line in detail.iter_bytes():
stream.write(" %s\n" % line)


def find_test_run_time_diff(test_id, run_time):
Expand Down Expand Up @@ -152,7 +158,7 @@ def find_test_run_time_diff(test_id, run_time):
def show_outcome(stream, test, print_failures=False, failonly=False,
enable_diff=False, threshold='0', abbreviate=False,
enable_color=False, suppress_attachments=False,
all_attachments=False):
all_attachments=False, show_binary_attachments=True):
global RESULTS
status = test['status']
# TODO(sdague): ask lifeless why on this?
Expand Down Expand Up @@ -189,7 +195,9 @@ def show_outcome(stream, test, print_failures=False, failonly=False,
color.write('FAILED', 'red')
stream.write('\n')
if not print_failures:
print_attachments(stream, test, all_channels=True)
print_attachments(
stream, test, all_channels=True,
show_binary_attachments=show_binary_attachments)
elif not failonly:
if status == 'success' or status == 'xfail':
if abbreviate:
Expand All @@ -207,10 +215,9 @@ def show_outcome(stream, test, print_failures=False, failonly=False,
color.write('ok', 'green')
stream.write('\n')
if not suppress_attachments:
if not all_attachments:
print_attachments(stream, test)
else:
print_attachments(stream, test, all_channels=True)
print_attachments(
stream, test, all_channels=all_attachments,
show_binary_attachments=show_binary_attachments)
elif status == 'skip':
if abbreviate:
color.write('S', 'blue')
Expand All @@ -230,7 +237,9 @@ def show_outcome(stream, test, print_failures=False, failonly=False,
stream.write('{%s} %s [%s] ... %s\n' % (
worker, name, duration, test['status']))
if not print_failures:
print_attachments(stream, test, all_channels=True)
print_attachments(
stream, test, all_channels=True,
show_binary_attachments=show_binary_attachments)

stream.flush()

Expand Down Expand Up @@ -354,7 +363,8 @@ def parse_args():

def trace(stdin, stdout, print_failures=False, failonly=False,
enable_diff=False, abbreviate=False, color=False, post_fails=False,
no_summary=False, suppress_attachments=False, all_attachments=False):
no_summary=False, suppress_attachments=False, all_attachments=False,
show_binary_attachments=False):
stream = subunit.ByteStreamToStreamResult(
stdin, non_subunit_name='stdout')
outcomes = testtools.StreamToDict(
Expand All @@ -365,7 +375,8 @@ def trace(stdin, stdout, print_failures=False, failonly=False,
abbreviate=abbreviate,
enable_color=color,
suppress_attachments=suppress_attachments,
all_attachments=all_attachments))
all_attachments=all_attachments,
show_binary_attachments=show_binary_attachments))
summary = testtools.StreamSummary()
result = testtools.CopyStreamResult([outcomes, summary])
result = testtools.StreamResultRouter(result)
Expand Down

0 comments on commit d79b9e8

Please sign in to comment.