From a09d24f1a5a6eea463f88d5cee8f46271fb70530 Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Mon, 30 Aug 2021 17:13:18 -0400 Subject: [PATCH] Fix inprogress test detection In #04 we attempted to add an error message when a test fails prior to reporting its final status (like in case of a segfault). However, that PR didn't work as intended because the condition to check if there were any tests inprogress was incorrect. It relied on the count_tests() function in subunit_trace which was returning zero as it was called there even when tests were left in the inprogress state. However, this call wasn't actually needed because we call get_stuck_in_progress() right before the condition which collects any test ids from the subunit stream that are left inprogress. This commit fixes the issue by just checking that the output list from get_stuck_in_progress() has entries. Then to ensure this doesn't regress in the future the unittest added in PR #304 is updated to actually assert we write out the error message as expected. Previously it only checked the return code was a failure (which it was even before #304) which is what let this issue slip in. --- stestr/subunit_trace.py | 4 ++-- stestr/tests/test_subunit_trace.py | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/stestr/subunit_trace.py b/stestr/subunit_trace.py index 0d3c1dd..a89ea11 100644 --- a/stestr/subunit_trace.py +++ b/stestr/subunit_trace.py @@ -426,8 +426,8 @@ def trace(stdin, stdout, print_failures=False, failonly=False, print("\nNo tests were successful during the run", file=sys.stderr) return 1 in_progress = get_stuck_in_progress() - if count_tests('status', '^inprogress$') > 0: - print("\nThe following tests exited without returning a status \n" + if in_progress: + print("\nThe following tests exited without returning a status\n" "and likely segfaulted or crashed Python:", file=sys.stderr) for test in in_progress: print("\n\t* %s" % test, file=sys.stderr) diff --git a/stestr/tests/test_subunit_trace.py b/stestr/tests/test_subunit_trace.py index c7fbe86..5e8d461 100644 --- a/stestr/tests/test_subunit_trace.py +++ b/stestr/tests/test_subunit_trace.py @@ -122,6 +122,23 @@ def test_trace_with_stuck_inprogress(self): timestamp=dt.now(UTC)) stream.stopTestRun() output.seek(0) + # capture stderr for test + stderr = io.StringIO() + sys_err = sys.stderr + sys.stderr = stderr + + def restore_stderr(): + sys.stderr = sys_err + + self.addCleanup(restore_stderr) stdin = io.TextIOWrapper(io.BufferedReader(output)) returncode = subunit_trace.trace(stdin, sys.stdout) self.assertEqual(1, returncode) + stderr.seek(0) + expected = """ +The following tests exited without returning a status +and likely segfaulted or crashed Python: + +\t* test_segfault +""" + self.assertEqual(stderr.read(), expected)