From 2dfbb3f57b2b19888bf2f1114cf4cb8d9488904b Mon Sep 17 00:00:00 2001 From: "m.benchchaoui@cloudbau.de" Date: Wed, 20 Nov 2013 11:44:40 +0100 Subject: [PATCH] Fix extra new line that break from progress bar The new line should be writed to stdout only when there is a progress bar displayed. Change-Id: If0e62cd5a3734ed67d66d285267c101b7caeea77 Closes-Bug: #1253042 --- glanceclient/common/progressbar.py | 12 ++++++++---- tests/test_progressbar.py | 24 ++++++++++++++++++++---- tests/utils.py | 7 +++++++ 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/glanceclient/common/progressbar.py b/glanceclient/common/progressbar.py index 8b8fd444..8b2b703e 100644 --- a/glanceclient/common/progressbar.py +++ b/glanceclient/common/progressbar.py @@ -58,8 +58,10 @@ class VerboseFileWrapper(_ProgressBarBase): if data: self._display_progress_bar(len(data)) else: - # Break to a new line from the progress bar for incoming output. - sys.stdout.write('\n') + if self._show_progress: + # Break to a new line from the progress bar for incoming + # output. + sys.stdout.write('\n') return data @@ -82,6 +84,8 @@ class VerboseIteratorWrapper(_ProgressBarBase): self._display_progress_bar(len(data)) return data except StopIteration: - # Break to a new line from the progress bar for incoming output. - sys.stdout.write('\n') + if self._show_progress: + # Break to a new line from the progress bar for incoming + # output. + sys.stdout.write('\n') raise diff --git a/tests/test_progressbar.py b/tests/test_progressbar.py index 7f4ae848..0dd23b69 100644 --- a/tests/test_progressbar.py +++ b/tests/test_progressbar.py @@ -34,8 +34,8 @@ class TestProgressBarWrapper(testtools.TestCase): data = list(progressbar.VerboseIteratorWrapper(iterator, size)) self.assertEqual(data, ['X'] * 100) self.assertEqual( - output.getvalue().strip(), - '[%s>] 100%%' % ('=' * 29) + output.getvalue(), + '[%s>] 100%%\n' % ('=' * 29) ) finally: sys.stdout = saved_stdout @@ -52,8 +52,24 @@ class TestProgressBarWrapper(testtools.TestCase): while chunk: chunk = file_obj.read(chunksize) self.assertEqual( - output.getvalue().strip(), - '[%s>] 100%%' % ('=' * 29) + output.getvalue(), + '[%s>] 100%%\n' % ('=' * 29) ) finally: sys.stdout = saved_stdout + + def test_iter_file_no_tty(self): + size = 98304 + file_obj = StringIO.StringIO('X' * size) + saved_stdout = sys.stdout + try: + sys.stdout = output = test_utils.FakeNoTTYStdout() + file_obj = progressbar.VerboseFileWrapper(file_obj, size) + chunksize = 1024 + chunk = file_obj.read(chunksize) + while chunk: + chunk = file_obj.read(chunksize) + # If stdout is not a tty progress bar should do nothing. + self.assertEqual(output.getvalue(), '') + finally: + sys.stdout = saved_stdout diff --git a/tests/utils.py b/tests/utils.py index 8e1689c1..134968f1 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -110,3 +110,10 @@ class FakeTTYStdout(StringIO.StringIO): self.seek(0) data = data[1:] return StringIO.StringIO.write(self, data) + + +class FakeNoTTYStdout(FakeTTYStdout): + """A Fake stdout that is not a TTY device.""" + + def isatty(self): + return False