Do not end a progress bar that never drew a frame

This commit is contained in:
J. Nick Koston
2026-08-19 15:13:04 -05:00
parent d34eed6d2d
commit b361342bde
2 changed files with 16 additions and 1 deletions
+7 -1
View File
@@ -768,7 +768,13 @@ class BatchDownloadProgress:
return update
def done(self) -> None:
if self._bar is not None and self._bar.last_progress != 100:
# Nothing to end unless a frame was drawn and it was not the final
# one (update(1) already emitted its own newline).
if (
self._bar is not None
and self._bar.last_progress is not None
and self._bar.last_progress != 100
):
self._bar.done()
@@ -1197,6 +1197,15 @@ class TestBatchDownloadProgress:
progress.done()
assert stream.getvalue().endswith("50% \n")
def test_done_before_any_frame_writes_nothing(self) -> None:
"""A batch aborted before any tracker fired must not emit a stray
newline for a bar that was never drawn."""
stream = io.StringIO()
stream.isatty = lambda: True # type: ignore[method-assign]
with patch("esphome.helpers.sys.stderr", stream):
BatchDownloadProgress("Downloading", 10).done()
assert stream.getvalue() == ""
def test_done_after_full_bar_adds_nothing(self) -> None:
stream = io.StringIO()
stream.isatty = lambda: True # type: ignore[method-assign]