Fix extra erase of 2 output lines when rolling print is still empty The reset function erase (len(last_lines) + 2) number of lines, where the extra 2 lines is for the starting and ending line such as '========== Rolling subprocess output =========='. When last_lines is still empty, these lines haven't been printed to the screen, and thus should not be erased. This CL adds a check for the emptiness of the rolling print buffer to avoid extra erasing. Test: atest unittests Change-Id: I79f72bb4cdf580e052ea88d322d6b400a1c915b4
diff --git a/atest/atest_utils.py b/atest/atest_utils.py index d8266b8..d46b2db 100644 --- a/atest/atest_utils.py +++ b/atest/atest_utils.py
@@ -311,9 +311,8 @@ is_rolling = True def reset_output(): - if not is_rolling: - return - io_output.write(_BASH_CLEAR_PREVIOUS_LINE_CODE * (len(last_lines) + 2)) + if is_rolling and last_lines: + io_output.write(_BASH_CLEAR_PREVIOUS_LINE_CODE * (len(last_lines) + 2)) def write_output(new_lines: list[str]): if not is_rolling:
diff --git a/atest/atest_utils_unittest.py b/atest/atest_utils_unittest.py index 243b6b5..6fae039 100755 --- a/atest/atest_utils_unittest.py +++ b/atest/atest_utils_unittest.py
@@ -137,6 +137,21 @@ io_output.getvalue(), ) + @mock.patch.object(atest_utils, 'get_terminal_size', return_value=(5, -1)) + def test_stream_io_output_no_lines_written_no_lines_cleared(self, _): + """Test when nothing is written, no lines are cleared.""" + io_input = StringIO() + io_output = StringIO() + + atest_utils.stream_io_output( + io_input, max_lines=2, io_output=io_output, is_io_output_atty=True + ) + + self.assertNotIn( + atest_utils._BASH_CLEAR_PREVIOUS_LINE_CODE, + io_output.getvalue(), + ) + class ConcatenatePathTest(unittest.TestCase): """Class that tests path concatenation."""