Coverage for tests / test_progress_bar.py: 100%
244 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-11-18 11:12 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-11-18 11:12 +0000
1"""
2Tests for the Progress bar functionality.
3Created after vendoring Click to ensure test coverage is back up to 100%.
4"""
6import io 1defgabchij
7import shutil 1defgabchij
9import pytest 1defgabchij
10import typer 1defgabchij
11from typer import progressbar 1defgabchij
12from typer._click import _termui_impl 1defgabchij
13from typer.testing import CliRunner 1defgabchij
15runner = CliRunner() 1defgabchij
18def _fake_clock(monkeypatch: pytest.MonkeyPatch) -> list[float]: 1defgabchij
19 clock = [0.0] 1defgabchij
20 monkeypatch.setattr(_termui_impl.time, "time", lambda: clock[0]) 1defgabchij
21 return clock 1defgabchij
24def _pbar(**kw): 1defgabchij
25 return progressbar(file=kw.pop("file", io.StringIO()), **kw) 1defgabchij
28@pytest.mark.parametrize( 1defgabchij
29 ("iterable", "length", "hidden", "label", "expected_count"),
30 [
31 (["a", "b"], None, False, "Processing", 2),
32 (None, 3, False, "Counting", 3),
33 (["x", "y"], None, True, "Hidden", 2),
34 ],
35)
36def test_progressbar(iterable, length, hidden, label, expected_count): 1defgabchij
37 app = typer.Typer() 1defgabchij
39 @app.command() 1defgabchij
40 def main(): 1defgabchij
41 bar_out = io.StringIO() 1defgabchij
42 count = 0 1defgabchij
43 with progressbar( 1defgabchij
44 iterable, length=length, hidden=hidden, label=label, file=bar_out
45 ) as bar:
46 for _ in bar: 1defgabchij
47 count += 1 1defgabchij
48 typer.echo(f"count={count}") 1defgabchij
49 typer.echo(f"bar={bar_out.getvalue()!r}") 1defgabchij
51 result = runner.invoke(app, []) 1defgabchij
52 assert result.exit_code == 0, result.output 1defgabchij
53 assert f"count={expected_count}" in result.stdout 1defgabchij
54 assert (label in result.stdout) == (not hidden) 1defgabchij
57@pytest.mark.parametrize( 1defgabchij
58 ("label", "pbar_kw", "must_contain", "must_not_contain"),
59 [
60 pytest.param(
61 "TTY",
62 {
63 "show_pos": True,
64 "show_percent": True,
65 "item_show_func": lambda item: f"item={item}",
66 },
67 ("TTY", "1/1", "100%", "item=x"),
68 (),
69 ),
70 pytest.param(
71 "HeurPct",
72 {},
73 ("HeurPct", "100%"),
74 ("1/1",),
75 ),
76 pytest.param(
77 "HeurPos",
78 {"show_pos": True},
79 ("HeurPos", "1/1"),
80 ("100%",),
81 ),
82 ],
83)
84def test_progressbar_tty( 1defgabchij
85 monkeypatch, label: str, pbar_kw: dict, must_contain, must_not_contain
86):
87 monkeypatch.setattr(_termui_impl, "isatty", lambda f: True) 1defgabchij
88 _fake_clock(monkeypatch) 1defgabchij
90 app = typer.Typer() 1defgabchij
92 @app.command() 1defgabchij
93 def main(): 1defgabchij
94 bar_out = io.StringIO() 1defgabchij
95 with progressbar( 1defgabchij
96 ["x"],
97 label=label,
98 file=bar_out,
99 bar_template="%(label)s %(info)s",
100 width=1,
101 **pbar_kw,
102 ) as bar:
103 for _ in bar: 1defgabchij
104 pass 1defgabchij
105 typer.echo(bar_out.getvalue()) 1defgabchij
107 result = runner.invoke(app, []) 1defgabchij
108 assert result.exit_code == 0, result.output 1defgabchij
109 for part in must_contain: 1defgabchij
110 assert part in result.stdout 1defgabchij
111 for part in must_not_contain: 1defgabchij
112 assert part not in result.stdout 1defgabchij
115def test_progressbar_tty_show_eta(monkeypatch): 1defgabchij
116 monkeypatch.setattr(_termui_impl, "isatty", lambda f: True) 1defgabchij
117 clock = _fake_clock(monkeypatch) 1defgabchij
118 clock[0] = 1_000.0 1defgabchij
120 app = typer.Typer() 1defgabchij
122 @app.command() 1defgabchij
123 def main(): 1defgabchij
124 bar_out = io.StringIO() 1defgabchij
125 with progressbar( 1defgabchij
126 ["a", "b"],
127 label="ETA",
128 file=bar_out,
129 show_pos=True,
130 show_percent=False,
131 show_eta=True,
132 bar_template="%(label)s %(info)s",
133 width=1,
134 ) as bar:
135 for i, _ in enumerate(bar): 1defgabchij
136 if i == 0: 1defgabchij
137 clock[0] = 1_001.0 1defgabchij
138 typer.echo(bar_out.getvalue()) 1defgabchij
140 result = runner.invoke(app, []) 1defgabchij
141 assert result.exit_code == 0, result.output 1defgabchij
142 for part in ("ETA", "1/2", "00:00:01"): 1defgabchij
143 assert part in result.stdout 1defgabchij
146def test_progressbar_autowidth(monkeypatch): 1defgabchij
147 monkeypatch.setattr(_termui_impl, "isatty", lambda f: True) 1defgabchij
148 call = [0] 1defgabchij
149 real_get_terminal_size = shutil.get_terminal_size 1defgabchij
151 def fake_get_terminal_size(*args, **kwargs): 1defgabchij
152 # Pytest (and others) call get_terminal_size(fallback=...); only stub no-arg calls
153 if args or kwargs: 1defgabchij
154 return real_get_terminal_size(*args, **kwargs) 1abc
155 col = 120 if call[0] == 0 else 40 1defgabchij
156 call[0] += 1 1defgabchij
157 return type("TS", (), {"columns": col, "lines": 24})() 1defgabchij
159 monkeypatch.setattr(shutil, "get_terminal_size", fake_get_terminal_size) 1defgabchij
161 state: dict[str, object] = {} 1defgabchij
163 app = typer.Typer() 1defgabchij
165 @app.command() 1defgabchij
166 def main(): 1defgabchij
167 out = io.StringIO() 1defgabchij
168 with progressbar(["a", "b"], width=0, label="AW", file=out) as bar: 1defgabchij
169 state["autowidth"] = bar.autowidth 1defgabchij
170 for _ in bar: 1defgabchij
171 pass 1defgabchij
172 state["call_count"] = call[0] 1defgabchij
173 state["out"] = out.getvalue() 1defgabchij
174 typer.echo("done") 1defgabchij
176 result = runner.invoke(app, []) 1defgabchij
177 assert result.exit_code == 0, result.output 1defgabchij
178 assert state["autowidth"] is True 1defgabchij
179 assert state["call_count"] >= 2 1defgabchij
180 out = str(state["out"]) 1defgabchij
181 assert "\r" in out and "AW" in out 1defgabchij
182 assert "0%" in out and "50%" in out and "100%" in out 1defgabchij
185def test_progress_bar_iter(): 1defgabchij
186 not_entered = _pbar(iterable=[1, 2], length=2) 1defgabchij
187 with pytest.raises(RuntimeError, match="with block"): 1defgabchij
188 iter(not_entered) 1defgabchij
190 entered = _pbar(iterable=[10, 20], length=2) 1defgabchij
191 with entered: 1defgabchij
192 iterator = iter(entered) 1defgabchij
193 assert next(iterator) == 10 1defgabchij
194 assert next(entered) == 20 1defgabchij
195 with pytest.raises(StopIteration): 1defgabchij
196 next(iterator) 1defgabchij
199def test_progress_bar_time(monkeypatch): 1defgabchij
200 clock = _fake_clock(monkeypatch) 1defgabchij
201 state: dict[str, object] = {} 1defgabchij
202 clock[0] = 1_000.0 1defgabchij
204 app = typer.Typer() 1defgabchij
206 @app.command() 1defgabchij
207 def main(): 1defgabchij
208 bar = _pbar(iterable=None, length=10) 1defgabchij
209 state["tpi0"] = bar.time_per_iteration 1defgabchij
210 clock[0] = 1_000.5 1defgabchij
211 bar.make_step(1) 1defgabchij
212 state["avg_after_one"] = list(bar.avg) 1defgabchij
213 state["tpi_after_one"] = bar.time_per_iteration 1defgabchij
214 clock[0] = 1_001.0 1defgabchij
215 bar.make_step(1) 1defgabchij
216 state["pos2"] = bar.pos 1defgabchij
217 state["avg2"] = list(bar.avg) 1defgabchij
218 state["tpi2"] = bar.time_per_iteration 1defgabchij
219 clock[0] = 1_002.0 1defgabchij
220 bar.make_step(1) 1defgabchij
221 state["avg3"] = list(bar.avg) 1defgabchij
222 state["tpi3"] = bar.time_per_iteration 1defgabchij
223 typer.echo("ok") 1defgabchij
225 result = runner.invoke(app, []) 1defgabchij
226 assert result.exit_code == 0, result.output 1defgabchij
227 assert state["tpi0"] == 0.0 1defgabchij
228 assert state["avg_after_one"] == [] and state["tpi_after_one"] == 0.0 1defgabchij
229 assert state["pos2"] == 2 1defgabchij
230 assert state["avg2"] == [(1_001.0 - 1_000.0) / 2.0] 1defgabchij
231 assert state["tpi2"] == pytest.approx(0.5) 1defgabchij
232 assert state["avg3"] == [0.5, (1_002.0 - 1_000.0) / 3.0] 1defgabchij
233 assert state["tpi3"] == pytest.approx(sum(state["avg3"]) / 2.0) # type: ignore[arg-type] 1defgabchij
236def test_progress_bar_time_zero_steps(monkeypatch): 1defgabchij
237 clock = _fake_clock(monkeypatch) 1defgabchij
238 state: dict[str, object] = {} 1defgabchij
239 clock[0] = 2_000.0 1defgabchij
241 app = typer.Typer() 1defgabchij
243 @app.command() 1defgabchij
244 def main(): 1defgabchij
245 bar = _pbar(iterable=None, length=3) 1defgabchij
246 clock[0] = 2_001.0 1defgabchij
247 bar.make_step(0) 1defgabchij
248 state["pos"] = bar.pos 1defgabchij
249 state["avg"] = list(bar.avg) 1defgabchij
250 state["tpi"] = bar.time_per_iteration 1defgabchij
251 typer.echo("ok") 1defgabchij
253 result = runner.invoke(app, []) 1defgabchij
254 assert result.exit_code == 0, result.output 1defgabchij
255 assert state["pos"] == 0 1defgabchij
256 assert state["avg"] == [1.0] 1defgabchij
257 assert state["tpi"] == pytest.approx(1.0) 1defgabchij
260def test_progress_bar_eta(monkeypatch): 1defgabchij
261 state: dict[str, object] = {} 1defgabchij
263 app = typer.Typer() 1defgabchij
265 @app.command() 1defgabchij
266 def main(): 1defgabchij
267 state["eta0"] = _pbar(iterable=[1, 2], length=None).eta 1defgabchij
269 done = _pbar(iterable=None, length=5) 1defgabchij
270 done.pos, done.finished = 2, True 1defgabchij
271 state["eta_done"] = done.eta 1defgabchij
273 fresh = _pbar(iterable=None, length=5) 1defgabchij
274 state["eta_known_fresh"] = fresh.eta_known 1defgabchij
275 state["fmt_eta_fresh"] = fresh.format_eta() 1defgabchij
277 clock = _fake_clock(monkeypatch) 1defgabchij
278 clock[0] = 5_000.0 1defgabchij
279 bar = _pbar(iterable=None, length=10) 1defgabchij
280 clock[0] = 5_001.0 1defgabchij
281 bar.make_step(3) 1defgabchij
282 state["pos3"] = bar.pos 1defgabchij
283 state["eta_after"] = bar.eta 1defgabchij
284 state["tpi"] = bar.time_per_iteration 1defgabchij
286 cases_out = [] 1defgabchij
287 for t0, t1, length, n_steps, _expected_fmt, expected_eta_int in ( 1defgabchij
288 (9_000.0, 9_001.0, 10, 1, "00:00:09", None),
289 (1_000.0, 100_000.0, 2, 1, "1d 03:30:00", 99_000),
290 ):
291 clock2 = _fake_clock(monkeypatch) 1defgabchij
292 clock2[0] = t0 1defgabchij
293 b = _pbar(iterable=None, length=length) 1defgabchij
294 clock2[0] = t1 1defgabchij
295 b.make_step(n_steps) 1defgabchij
296 cases_out.append( 1defgabchij
297 (
298 b.eta_known,
299 b.format_eta(),
300 int(b.eta) if expected_eta_int is not None else None,
301 )
302 )
304 state["cases"] = cases_out 1defgabchij
306 clock3 = _fake_clock(monkeypatch) 1defgabchij
307 clock3[0] = 3_000.0 1defgabchij
308 b2 = _pbar(iterable=None, length=2) 1defgabchij
309 clock3[0] = 3_001.0 1defgabchij
310 b2.make_step(1) 1defgabchij
311 state["fmt_before_finish"] = b2.format_eta() 1defgabchij
312 b2.finish() 1defgabchij
313 state["fmt_after_finish"] = b2.format_eta() 1defgabchij
314 typer.echo("ok") 1defgabchij
316 result = runner.invoke(app, []) 1defgabchij
317 assert result.exit_code == 0, result.output 1defgabchij
318 assert state["eta0"] == 0.0 1defgabchij
319 assert state["eta_done"] == 0.0 1defgabchij
320 assert not state["eta_known_fresh"] and state["fmt_eta_fresh"] == "" 1defgabchij
321 assert state["pos3"] == 3 1defgabchij
322 assert state["eta_after"] == pytest.approx(state["tpi"] * (10 - 3)) # type: ignore[operator] 1defgabchij
324 (ek1, fmt1, ei1), (ek2, fmt2, ei2) = state["cases"] # type: ignore[misc] 1defgabchij
325 assert ek1 and fmt1 == "00:00:09" and ei1 is None 1defgabchij
326 assert ek2 and fmt2 == "1d 03:30:00" and ei2 == 99_000 1defgabchij
328 assert state["fmt_before_finish"] != "" 1defgabchij
329 assert state["fmt_after_finish"] == "" 1defgabchij
332@pytest.mark.parametrize( 1defgabchij
333 ("width", "fill_char", "empty_char", "expected_bar", "finished", "sample_timing"),
334 [
335 pytest.param(4, "X", "-", "XXXX", True, False, id="finished"),
336 pytest.param(4, "#", "-", "----", False, False, id="no_timing_yet"),
337 pytest.param(5, "*", ".", None, False, True, id="indeterminate"),
338 ],
339)
340def test_progress_bar_unknown_length( 1defgabchij
341 monkeypatch,
342 width: int,
343 fill_char: str,
344 empty_char: str,
345 expected_bar: str | None,
346 finished: bool,
347 sample_timing: bool,
348):
349 clock: list[float] | None = _fake_clock(monkeypatch) if sample_timing else None 1defgabchij
350 if clock is not None: 1defgabchij
351 clock[0] = 100.0 1defgabchij
353 state: dict[str, object] = {} 1defgabchij
355 class _IterableWithoutLength: 1defgabchij
356 def __iter__(self): 1defgabchij
357 return iter((1, 2, 3)) 1defgabchij
359 app = typer.Typer() 1defgabchij
361 @app.command() 1defgabchij
362 def main(): 1defgabchij
363 bar = _pbar( 1defgabchij
364 iterable=_IterableWithoutLength(),
365 length=None,
366 width=width,
367 fill_char=fill_char,
368 empty_char=empty_char,
369 )
370 assert bar.length is None 1defgabchij
372 if sample_timing: 1defgabchij
373 assert clock is not None 1defgabchij
374 clock[0] = 101.0 1defgabchij
375 bar.make_step(1) 1defgabchij
376 assert bar.time_per_iteration > 0 1defgabchij
377 rendered = bar.format_bar() 1defgabchij
378 assert len(rendered) == width 1defgabchij
379 assert rendered.count(fill_char) == 1 1defgabchij
380 assert rendered.count(empty_char) == width - 1 1defgabchij
381 state["branch"] = "sample_timing" 1defgabchij
382 elif finished: 1defgabchij
383 bar.finished = True 1defgabchij
384 state["bar"] = bar.format_bar() 1defgabchij
385 state["branch"] = "finished" 1defgabchij
386 else:
387 assert bar.time_per_iteration == 0.0 1defgabchij
388 state["bar"] = bar.format_bar() 1defgabchij
389 state["branch"] = "no_timing" 1defgabchij
390 typer.echo("ok") 1defgabchij
392 result = runner.invoke(app, []) 1defgabchij
393 assert result.exit_code == 0, result.output 1defgabchij
394 if sample_timing: 1defgabchij
395 assert state["branch"] == "sample_timing" 1defgabchij
396 else:
397 assert state["bar"] == expected_bar 1defgabchij