Skip to content
Myr

test_termui.py

This file

Path

tests/test_termui.py

369 lines · 11.1 KiB · Python

Last changed by

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

History of this file

Take it

The walk stopped after 200 commits: lines older than ae5386b point at it.

Merge pull request #706 from jojje/issue_651Dan Sullybefore adoption21d5125

line 1why, one step back
1import time

Changelog for #557Markus Unterwaditzerbefore adoptionae5386b

line 2why, one step back
2

Merge branch '7.x'David Lordbefore adoption7239794

lines 3–4why, one step back
3import pytest
4

Merge branch '7.x'David Lordbefore adoptiona438322

lines 5–6why, one step back
5import click._termui_impl
6from click._compat import WIN

Changelog for #557Markus Unterwaditzerbefore adoptionae5386b

line 7why, one step back
7

Merge branch '7.x'David Lordbefore adoptiona438322

line 8why, one step back
8

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 9why, one step back
9class FakeClock:

Merge pull request #487 from con-f-use/masterDan Sullybefore adoptioncb29f88

lines 10–19why, one step back
10 def __init__(self):
11 self.now = time.time()1213 def advance_time(self, seconds=1):14 self.now += seconds1516 def time(self):17 return self.now1819

Merge branch '7.x'David Lordbefore adoptiona438322

lines 20–27why, one step back
20def _create_progress(length=10, length_known=True, **kwargs):
21 progress = click.progressbar(tuple(range(length)))22 for key, value in kwargs.items():23 setattr(progress, key, value)24 progress.length_known = length_known25 return progress2627

Changelog for #557Markus Unterwaditzerbefore adoptionae5386b

line 28why, one step back
28def test_progressbar_strip_regression(runner, monkeypatch):

Merge pull request #487 from con-f-use/masterDan Sullybefore adoptioncb29f88

line 29why, one step back
29 fake_clock = FakeClock()

Merge branch '7.x'David Lordbefore adoption7239794

line 30why, one step back
30 label = " padded line"

Changelog for #557Markus Unterwaditzerbefore adoptionae5386b

lines 31–33why, one step back
31
32 @click.command()33 def cli():

Merge branch '7.x'David Lordbefore adoptiona438322

line 34why, one step back
34 with _create_progress(label=label) as progress:

Merge branch '7.x'David Lordbefore adoption7239794

line 35why, one step back
35 for _ in progress:

Merge pull request #487 from con-f-use/masterDan Sullybefore adoptioncb29f88

line 36why, one step back
36 fake_clock.advance_time()

Changelog for #557Markus Unterwaditzerbefore adoptionae5386b

line 37why, one step back
37

Merge branch '7.x'David Lordbefore adoption7239794

lines 38–39why, one step back
38 monkeypatch.setattr(time, "time", fake_clock.time)
39 monkeypatch.setattr(click._termui_impl, "isatty", lambda _: True)

Changelog for #557Markus Unterwaditzerbefore adoptionae5386b

lines 40–43why, one step back
40 assert label in runner.invoke(cli, []).output
414243def test_progressbar_length_hint(runner, monkeypatch):

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 44why, one step back
44 class Hinted:

Changelog for #557Markus Unterwaditzerbefore adoptionae5386b

lines 45–61why, one step back
45 def __init__(self, n):
46 self.items = list(range(n))4748 def __length_hint__(self):49 return len(self.items)5051 def __iter__(self):52 return self5354 def __next__(self):55 if self.items:56 return self.items.pop()57 else:58 raise StopIteration5960 next = __next__61

Merge pull request #487 from con-f-use/masterDan Sullybefore adoptioncb29f88

lines 62–63why, one step back
62 fake_clock = FakeClock()
63

Changelog for #557Markus Unterwaditzerbefore adoptionae5386b

lines 64–65why, one step back
64 @click.command()
65 def cli():

Merge branch '7.x'David Lordbefore adoption7239794

lines 66–67why, one step back
66 with click.progressbar(Hinted(10), label="test") as progress:
67 for _ in progress:

Merge pull request #487 from con-f-use/masterDan Sullybefore adoptioncb29f88

line 68why, one step back
68 fake_clock.advance_time()

Changelog for #557Markus Unterwaditzerbefore adoptionae5386b

line 69why, one step back
69

Merge branch '7.x'David Lordbefore adoption7239794

lines 70–71why, one step back
70 monkeypatch.setattr(time, "time", fake_clock.time)
71 monkeypatch.setattr(click._termui_impl, "isatty", lambda _: True)

Changelog for #557Markus Unterwaditzerbefore adoptionae5386b

lines 72–75why, one step back
72 result = runner.invoke(cli, [])
73 assert result.exception is None7475

Merge pull request #863 from georgeyk/progressbar-to-stderrDan Sullybefore adoptionda4a508

line 76why, one step back
76def test_progressbar_hidden(runner, monkeypatch):

Merge pull request #487 from con-f-use/masterDan Sullybefore adoptioncb29f88

line 77why, one step back
77 fake_clock = FakeClock()

Merge branch '7.x'David Lordbefore adoption7239794

line 78why, one step back
78 label = "whatever"

Merge pull request #863 from georgeyk/progressbar-to-stderrDan Sullybefore adoptionda4a508

lines 79–81why, one step back
79
80 @click.command()81 def cli():

Merge branch '7.x'David Lordbefore adoptiona438322

line 82why, one step back
82 with _create_progress(label=label) as progress:

Merge branch '7.x'David Lordbefore adoption7239794

line 83why, one step back
83 for _ in progress:

Merge pull request #487 from con-f-use/masterDan Sullybefore adoptioncb29f88

line 84why, one step back
84 fake_clock.advance_time()

Merge pull request #863 from georgeyk/progressbar-to-stderrDan Sullybefore adoptionda4a508

line 85why, one step back
85

Merge branch '7.x'David Lordbefore adoption7239794

lines 86–88why, one step back
86 monkeypatch.setattr(time, "time", fake_clock.time)
87 monkeypatch.setattr(click._termui_impl, "isatty", lambda _: False)88 assert runner.invoke(cli, []).output == ""

Merge pull request #863 from georgeyk/progressbar-to-stderrDan Sullybefore adoptionda4a508

lines 89–90why, one step back
89
90

Merge branch '7.x'David Lordbefore adoption7239794

line 91why, one step back
91@pytest.mark.parametrize("avg, expected", [([], 0.0), ([1, 4], 2.5)])

Merge branch '7.x'David Lordbefore adoptiona438322

lines 92–96why, one step back
92def test_progressbar_time_per_iteration(runner, avg, expected):
93 with _create_progress(2, avg=avg) as progress:94 assert progress.time_per_iteration == expected9596

Merge branch '7.x'David Lordbefore adoption7239794

line 97why, one step back
97@pytest.mark.parametrize("finished, expected", [(False, 5), (True, 0)])

Merge branch '7.x'David Lordbefore adoptiona438322

lines 98–102why, one step back
98def test_progressbar_eta(runner, finished, expected):
99 with _create_progress(2, finished=finished, avg=[1, 4]) as progress:100 assert progress.eta == expected101102

Merge branch '7.x'David Lordbefore adoption7239794

lines 103–114why, one step back
103@pytest.mark.parametrize(
104 "eta, expected",105 [106 (0, "00:00:00"),107 (30, "00:00:30"),108 (90, "00:01:30"),109 (900, "00:15:00"),110 (9000, "02:30:00"),111 (99999999999, "1157407d 09:46:39"),112 (None, ""),113 ],114)

Merge branch '7.x'David Lordbefore adoptiona438322

lines 115–119why, one step back
115def test_progressbar_format_eta(runner, eta, expected):
116 with _create_progress(1, eta_known=eta is not None, avg=[eta]) as progress:117 assert progress.format_eta() == expected118119

Merge branch '7.x'David Lordbefore adoption7239794

line 120why, one step back
120@pytest.mark.parametrize("pos, length", [(0, 5), (-1, 1), (5, 5), (6, 5), (4, 0)])

Merge branch '7.x'David Lordbefore adoptiona438322

lines 121–124why, one step back
121def test_progressbar_format_pos(runner, pos, length):
122 with _create_progress(length, length_known=length != 0, pos=pos) as progress:123 result = progress.format_pos()124 if progress.length_known:

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 125why, one step back
125 assert result == f"{pos}/{length}"

Merge branch '7.x'David Lordbefore adoptiona438322

lines 126–129why, one step back
126 else:
127 assert result == str(pos)128129

Merge branch '7.x'David Lordbefore adoption7239794

lines 130–138why, one step back
130@pytest.mark.parametrize(
131 "length, finished, pos, avg, expected",132 [133 (8, False, 7, 0, "#######-"),134 (0, True, 8, 0, "########"),135 (0, False, 8, 0, "--------"),136 (0, False, 5, 3, "#-------"),137 ],138)

Merge branch '7.x'David Lordbefore adoptiona438322

line 139why, one step back
139def test_progressbar_format_bar(runner, length, finished, pos, avg, expected):

Merge branch '7.x'David Lordbefore adoption7239794

lines 140–142why, one step back
140 with _create_progress(
141 length, length_known=length != 0, width=8, pos=pos, finished=finished, avg=[avg]142 ) as progress:

Merge branch '7.x'David Lordbefore adoptiona438322

lines 143–145why, one step back
143 assert progress.format_bar() == expected
144145

Merge branch '7.x'David Lordbefore adoption7239794

lines 146–166why, one step back
146@pytest.mark.parametrize(
147 "length, length_known, show_percent, show_pos, pos, expected",148 [149 (0, True, True, True, 0, " [--------] 0/0 0%"),150 (0, True, False, True, 0, " [--------] 0/0"),151 (0, True, False, False, 0, " [--------]"),152 (0, False, False, False, 0, " [--------]"),153 (8, True, True, True, 8, " [########] 8/8 100%"),154 ],155)156def test_progressbar_format_progress_line(157 runner, length, length_known, show_percent, show_pos, pos, expected158):159 with _create_progress(160 length,161 length_known,162 width=8,163 show_percent=show_percent,164 pos=pos,165 show_pos=show_pos,166 ) as progress:

Merge branch '7.x'David Lordbefore adoptiona438322

lines 167–169why, one step back
167 assert progress.format_progress_line() == expected
168169

Merge branch '7.x'David Lordbefore adoption7239794

line 170why, one step back
170@pytest.mark.parametrize("test_item", ["test", None])

Merge branch '7.x'David Lordbefore adoptiona438322

lines 171–174why, one step back
171def test_progressbar_format_progress_line_with_show_func(runner, test_item):
172 def item_show_func(item):173 return item174

Merge branch '7.x'David Lordbefore adoption7239794

lines 175–177why, one step back
175 with _create_progress(
176 item_show_func=item_show_func, current_item=test_item177 ) as progress:

Merge branch '7.x'David Lordbefore adoptiona438322

lines 178–184why, one step back
178 if test_item:
179 assert progress.format_progress_line().endswith(test_item)180 else:181 assert progress.format_progress_line().endswith(progress.format_pct())182183184def test_progressbar_init_exceptions(runner):

Merge branch '7.x'David Lordbefore adoption7239794

line 185why, one step back
185 with pytest.raises(TypeError, match="iterable or length is required"):

Merge branch '7.x'David Lordbefore adoptiona438322

lines 186–189why, one step back
186 click.progressbar()
187188189def test_progressbar_iter_outside_with_exceptions(runner):

Merge branch '7.x'David Lordbefore adoption7239794

line 190why, one step back
190 with pytest.raises(RuntimeError, match="with block"):

Merge branch '7.x'David Lordbefore adoptiona438322

lines 191–194why, one step back
191 progress = click.progressbar(length=2)
192 iter(progress)193194

Merge branch '7.x'David Lordbefore adoption2269786

lines 195–199why, one step back
195def test_progressbar_is_iterator(runner, monkeypatch):
196 fake_clock = FakeClock()197198 @click.command()199 def cli():

Merge branch '7.x'David Lordbefore adoption7239794

line 200why, one step back
200 with click.progressbar(range(10), label="test") as progress:

Merge branch '7.x'David Lordbefore adoption2269786

lines 201–207why, one step back
201 while True:
202 try:203 next(progress)204 fake_clock.advance_time()205 except StopIteration:206 break207

Merge branch '7.x'David Lordbefore adoption7239794

lines 208–209why, one step back
208 monkeypatch.setattr(time, "time", fake_clock.time)
209 monkeypatch.setattr(click._termui_impl, "isatty", lambda _: True)

Merge branch '7.x'David Lordbefore adoption2269786

lines 210–214why, one step back
210
211 result = runner.invoke(cli, [])212 assert result.exception is None213214

Merge branch 'Rovanion-master'Markus Unterwaditzerbefore adoption67f39e4

lines 215–216why, one step back
215def test_choices_list_in_prompt(runner, monkeypatch):
216 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

lines 217–219why, one step back
217 @click.option(
218 "-g", type=click.Choice(["none", "day", "week", "month"]), prompt=True219 )

Merge branch 'Rovanion-master'Markus Unterwaditzerbefore adoption67f39e4

lines 220–223why, one step back
220 def cli_with_choices(g):
221 pass222223 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

lines 224–229why, one step back
224 @click.option(
225 "-g",226 type=click.Choice(["none", "day", "week", "month"]),227 prompt=True,228 show_choices=False,229 )

Merge branch 'Rovanion-master'Markus Unterwaditzerbefore adoption67f39e4

lines 230–232why, one step back
230 def cli_without_choices(g):
231 pass232

Merge branch '7.x'David Lordbefore adoption7239794

lines 233–234why, one step back
233 result = runner.invoke(cli_with_choices, [], input="none")
234 assert "(none, day, week, month)" in result.output

Merge branch 'Rovanion-master'Markus Unterwaditzerbefore adoption67f39e4

line 235why, one step back
235

Merge branch '7.x'David Lordbefore adoption7239794

lines 236–237why, one step back
236 result = runner.invoke(cli_without_choices, [], input="none")
237 assert "(none, day, week, month)" not in result.output

Merge branch '7.x'David Lordbefore adoption2269786

lines 238–240why, one step back
238
239240@pytest.mark.parametrize(

Merge branch '7.x'David Lordbefore adoption7239794

line 241why, one step back
241 "file_kwargs", [{"mode": "rt"}, {"mode": "rb"}, {"lazy": True}]

Merge branch '7.x'David Lordbefore adoption2269786

lines 242–249why, one step back
242)
243def test_file_prompt_default_format(runner, file_kwargs):244 @click.command()245 @click.option("-f", default=__file__, prompt="file", type=click.File(**file_kwargs))246 def cli(f):247 click.echo(f.name)248249 result = runner.invoke(cli)

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 250why, one step back
250 assert result.output == f"file [{__file__}]: \n{__file__}\n"

Merge branch 'Rovanion-master'Markus Unterwaditzerbefore adoption67f39e4

lines 251–252why, one step back
251
252

Changelog for #557Markus Unterwaditzerbefore adoptionae5386b

line 253why, one step back
253def test_secho(runner):

CliRunner: add stdout / stderr, allow separating standard streams (#868)Dave Carlsonbefore adoptionfdceb39

line 254why, one step back
254 with runner.isolation() as outstreams:

Changelog for #557Markus Unterwaditzerbefore adoptionae5386b

line 255why, one step back
255 click.secho(None, nl=False)

CliRunner: add stdout / stderr, allow separating standard streams (#868)Dave Carlsonbefore adoptionfdceb39

line 256why, one step back
256 bytes = outstreams[0].getvalue()

Merge branch '7.x'David Lordbefore adoption7239794

line 257why, one step back
257 assert bytes == b""

Merge pull request #706 from jojje/issue_651Dan Sullybefore adoption21d5125

lines 258–274why, one step back
258
259260def test_progressbar_yields_all_items(runner):261 with click.progressbar(range(3)) as progress:262 assert len(list(progress)) == 3263264265def test_progressbar_update(runner, monkeypatch):266 fake_clock = FakeClock()267268 @click.command()269 def cli():270 with click.progressbar(range(4)) as progress:271 for _ in progress:272 fake_clock.advance_time()273 print("")274

Merge branch '7.x'David Lordbefore adoption7239794

lines 275–276why, one step back
275 monkeypatch.setattr(time, "time", fake_clock.time)
276 monkeypatch.setattr(click._termui_impl, "isatty", lambda _: True)

Merge pull request #706 from jojje/issue_651Dan Sullybefore adoption21d5125

lines 277–278why, one step back
277 output = runner.invoke(cli, []).output
278

Merge branch '7.x'David Lordbefore adoption7239794

line 279why, one step back
279 lines = [line for line in output.split("\n") if "[" in line]

Merge pull request #706 from jojje/issue_651Dan Sullybefore adoption21d5125

line 280why, one step back
280

Merge branch '7.x'David Lordbefore adoption7239794

lines 281–284why, one step back
281 assert " 25% 00:00:03" in lines[0]
282 assert " 50% 00:00:02" in lines[1]283 assert " 75% 00:00:01" in lines[2]284 assert "100% " in lines[3]

Merge branch '7.x'David Lordbefore adoptiona438322

lines 285–286why, one step back
285
286

Merge pull request #1332 from thecav/update-current-itemDavid Lordbefore adoption716a5be

lines 287–291why, one step back
287def test_progressbar_item_show_func(runner, monkeypatch):
288 fake_clock = FakeClock()289290 @click.command()291 def cli():

Merge branch '7.x'David Lordbefore adoption7239794

line 292why, one step back
292 with click.progressbar(

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 293why, one step back
293 range(4), item_show_func=lambda x: f"Custom {x}"

Merge branch '7.x'David Lordbefore adoption7239794

line 294why, one step back
294 ) as progress:

Merge pull request #1332 from thecav/update-current-itemDavid Lordbefore adoption716a5be

lines 295–298why, one step back
295 for _ in progress:
296 fake_clock.advance_time()297 print("")298

Merge branch '7.x'David Lordbefore adoption7239794

lines 299–300why, one step back
299 monkeypatch.setattr(time, "time", fake_clock.time)
300 monkeypatch.setattr(click._termui_impl, "isatty", lambda _: True)

Merge pull request #1332 from thecav/update-current-itemDavid Lordbefore adoption716a5be

lines 301–302why, one step back
301 output = runner.invoke(cli, []).output
302

Merge branch '7.x'David Lordbefore adoption7239794

line 303why, one step back
303 lines = [line for line in output.split("\n") if "[" in line]

Merge pull request #1332 from thecav/update-current-itemDavid Lordbefore adoption716a5be

line 304why, one step back
304

Merge branch '7.x'David Lordbefore adoption7239794

lines 305–308why, one step back
305 assert "Custom 0" in lines[0]
306 assert "Custom 1" in lines[1]307 assert "Custom 2" in lines[2]308 assert "Custom None" in lines[3]

Merge pull request #1332 from thecav/update-current-itemDavid Lordbefore adoption716a5be

lines 309–315why, one step back
309
310311def test_progressbar_update_with_item_show_func(runner, monkeypatch):312 fake_clock = FakeClock()313314 @click.command()315 def cli():

Merge branch '7.x'David Lordbefore adoption7239794

line 316why, one step back
316 with click.progressbar(

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 317why, one step back
317 length=6, item_show_func=lambda x: f"Custom {x}"

Merge branch '7.x'David Lordbefore adoption7239794

line 318why, one step back
318 ) as progress:

Merge pull request #1332 from thecav/update-current-itemDavid Lordbefore adoption716a5be

lines 319–323why, one step back
319 while not progress.finished:
320 fake_clock.advance_time()321 progress.update(2, progress.pos)322 print("")323

Merge branch '7.x'David Lordbefore adoption7239794

lines 324–325why, one step back
324 monkeypatch.setattr(time, "time", fake_clock.time)
325 monkeypatch.setattr(click._termui_impl, "isatty", lambda _: True)

Merge pull request #1332 from thecav/update-current-itemDavid Lordbefore adoption716a5be

lines 326–327why, one step back
326 output = runner.invoke(cli, []).output
327

Merge branch '7.x'David Lordbefore adoption7239794

line 328why, one step back
328 lines = [line for line in output.split("\n") if "[" in line]

Merge pull request #1332 from thecav/update-current-itemDavid Lordbefore adoption716a5be

line 329why, one step back
329

Merge branch '7.x'David Lordbefore adoption7239794

lines 330–332why, one step back
330 assert "Custom 0" in lines[0]
331 assert "Custom 2" in lines[1]332 assert "Custom 4" in lines[2]

Merge pull request #1332 from thecav/update-current-itemDavid Lordbefore adoption716a5be

lines 333–334why, one step back
333
334

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 335why, one step back
335@pytest.mark.parametrize("key_char", ("h", "H", "é", "À", " ", "字", "àH", "àR"))

Merge branch '7.x'David Lordbefore adoption7239794

lines 336–337why, one step back
336@pytest.mark.parametrize("echo", [True, False])
337@pytest.mark.skipif(not WIN, reason="Tests user-input using the msvcrt module.")

Merge branch '7.x'David Lordbefore adoptiona438322

line 338why, one step back
338def test_getchar_windows(runner, monkeypatch, key_char, echo):

Merge branch '7.x'David Lordbefore adoption7239794

lines 339–341why, one step back
339 monkeypatch.setattr(click._termui_impl.msvcrt, "getwche", lambda: key_char)
340 monkeypatch.setattr(click._termui_impl.msvcrt, "getwch", lambda: key_char)341 monkeypatch.setattr(click.termui, "_getchar", None)

Merge branch '7.x'David Lordbefore adoptiona438322

lines 342–344why, one step back
342 assert click.getchar(echo) == key_char
343344

Merge branch '7.x'David Lordbefore adoption7239794

line 345why, one step back
345@pytest.mark.parametrize(

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 346why, one step back
346 "special_key_char, key_char", [("\x00", "a"), ("\x00", "b"), ("\xe0", "c")]

Merge branch '7.x'David Lordbefore adoption7239794

lines 347–350why, one step back
347)
348@pytest.mark.skipif(349 not WIN, reason="Tests special character inputs using the msvcrt module."350)

Merge branch '7.x'David Lordbefore adoptiona438322

lines 351–352why, one step back
351def test_getchar_special_key_windows(runner, monkeypatch, special_key_char, key_char):
352 ordered_inputs = [key_char, special_key_char]

Merge branch '7.x'David Lordbefore adoption7239794

lines 353–356why, one step back
353 monkeypatch.setattr(
354 click._termui_impl.msvcrt, "getwch", lambda: ordered_inputs.pop()355 )356 monkeypatch.setattr(click.termui, "_getchar", None)

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 357why, one step back
357 assert click.getchar() == f"{special_key_char}{key_char}"

Merge branch '7.x'David Lordbefore adoptiona438322

lines 358–359why, one step back
358
359

Merge branch '7.x'David Lordbefore adoption7239794

line 360why, one step back
360@pytest.mark.parametrize(

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 361why, one step back
361 ("key_char", "exc"), [("\x03", KeyboardInterrupt), ("\x1a", EOFError)],

Merge branch '7.x'David Lordbefore adoption7239794

lines 362–368why, one step back
362)
363@pytest.mark.skipif(not WIN, reason="Tests user-input using the msvcrt module.")364def test_getchar_windows_exceptions(runner, monkeypatch, key_char, exc):365 monkeypatch.setattr(click._termui_impl.msvcrt, "getwch", lambda: key_char)366 monkeypatch.setattr(click.termui, "_getchar", None)367368 with pytest.raises(exc):

Merge branch '7.x'David Lordbefore adoptiona438322

line 369why, one step back
369 click.getchar()