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 fileTake 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 back1import timeChangelog for #557Markus Unterwaditzerbefore adoptionae5386b
line 2why, one step back2Merge branch '7.x'David Lordbefore adoption7239794
lines 3–4why, one step back3import pytest4Merge branch '7.x'David Lordbefore adoptiona438322
lines 5–6why, one step back5import click._termui_impl6from click._compat import WINChangelog for #557Markus Unterwaditzerbefore adoptionae5386b
line 7why, one step back7Merge branch '7.x'David Lordbefore adoptiona438322
line 8why, one step back8Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 9why, one step back9class FakeClock:Merge pull request #487 from con-f-use/masterDan Sullybefore adoptioncb29f88
lines 10–19why, one step back10 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.now1819Merge branch '7.x'David Lordbefore adoptiona438322
lines 20–27why, one step back20def _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 progress2627Changelog for #557Markus Unterwaditzerbefore adoptionae5386b
line 28why, one step back28def test_progressbar_strip_regression(runner, monkeypatch):Merge pull request #487 from con-f-use/masterDan Sullybefore adoptioncb29f88
line 29why, one step back29 fake_clock = FakeClock()Merge branch '7.x'David Lordbefore adoption7239794
line 30why, one step back30 label = " padded line"Changelog for #557Markus Unterwaditzerbefore adoptionae5386b
lines 31–33why, one step back3132 @click.command()33 def cli():Merge branch '7.x'David Lordbefore adoptiona438322
line 34why, one step back34 with _create_progress(label=label) as progress:Merge branch '7.x'David Lordbefore adoption7239794
line 35why, one step back35 for _ in progress:Merge pull request #487 from con-f-use/masterDan Sullybefore adoptioncb29f88
line 36why, one step back36 fake_clock.advance_time()Changelog for #557Markus Unterwaditzerbefore adoptionae5386b
line 37why, one step back37Merge branch '7.x'David Lordbefore adoption7239794
lines 38–39why, one step back38 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 back40 assert label in runner.invoke(cli, []).output414243def test_progressbar_length_hint(runner, monkeypatch):Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 44why, one step back44 class Hinted:Changelog for #557Markus Unterwaditzerbefore adoptionae5386b
lines 45–61why, one step back45 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__61Merge pull request #487 from con-f-use/masterDan Sullybefore adoptioncb29f88
lines 62–63why, one step back62 fake_clock = FakeClock()63Changelog for #557Markus Unterwaditzerbefore adoptionae5386b
lines 64–65why, one step back64 @click.command()65 def cli():Merge branch '7.x'David Lordbefore adoption7239794
lines 66–67why, one step back66 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 back68 fake_clock.advance_time()Changelog for #557Markus Unterwaditzerbefore adoptionae5386b
line 69why, one step back69Merge branch '7.x'David Lordbefore adoption7239794
lines 70–71why, one step back70 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 back72 result = runner.invoke(cli, [])73 assert result.exception is None7475Merge pull request #863 from georgeyk/progressbar-to-stderrDan Sullybefore adoptionda4a508
line 76why, one step back76def test_progressbar_hidden(runner, monkeypatch):Merge pull request #487 from con-f-use/masterDan Sullybefore adoptioncb29f88
line 77why, one step back77 fake_clock = FakeClock()Merge branch '7.x'David Lordbefore adoption7239794
line 78why, one step back78 label = "whatever"Merge pull request #863 from georgeyk/progressbar-to-stderrDan Sullybefore adoptionda4a508
lines 79–81why, one step back7980 @click.command()81 def cli():Merge branch '7.x'David Lordbefore adoptiona438322
line 82why, one step back82 with _create_progress(label=label) as progress:Merge branch '7.x'David Lordbefore adoption7239794
line 83why, one step back83 for _ in progress:Merge pull request #487 from con-f-use/masterDan Sullybefore adoptioncb29f88
line 84why, one step back84 fake_clock.advance_time()Merge pull request #863 from georgeyk/progressbar-to-stderrDan Sullybefore adoptionda4a508
line 85why, one step back85Merge branch '7.x'David Lordbefore adoption7239794
lines 86–88why, one step back86 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 back8990Merge branch '7.x'David Lordbefore adoption7239794
line 91why, one step back91@pytest.mark.parametrize("avg, expected", [([], 0.0), ([1, 4], 2.5)])Merge branch '7.x'David Lordbefore adoptiona438322
lines 92–96why, one step back92def test_progressbar_time_per_iteration(runner, avg, expected):93 with _create_progress(2, avg=avg) as progress:94 assert progress.time_per_iteration == expected9596Merge branch '7.x'David Lordbefore adoption7239794
line 97why, one step back97@pytest.mark.parametrize("finished, expected", [(False, 5), (True, 0)])Merge branch '7.x'David Lordbefore adoptiona438322
lines 98–102why, one step back98def test_progressbar_eta(runner, finished, expected):99 with _create_progress(2, finished=finished, avg=[1, 4]) as progress:100 assert progress.eta == expected101102Merge branch '7.x'David Lordbefore adoption7239794
lines 103–114why, one step back103@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 back115def 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() == expected118119Merge branch '7.x'David Lordbefore adoption7239794
line 120why, one step back120@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 back121def 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 back125 assert result == f"{pos}/{length}"Merge branch '7.x'David Lordbefore adoptiona438322
lines 126–129why, one step back126 else:127 assert result == str(pos)128129Merge branch '7.x'David Lordbefore adoption7239794
lines 130–138why, one step back130@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 back139def test_progressbar_format_bar(runner, length, finished, pos, avg, expected):Merge branch '7.x'David Lordbefore adoption7239794
lines 140–142why, one step back140 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 back143 assert progress.format_bar() == expected144145Merge branch '7.x'David Lordbefore adoption7239794
lines 146–166why, one step back146@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 back167 assert progress.format_progress_line() == expected168169Merge branch '7.x'David Lordbefore adoption7239794
line 170why, one step back170@pytest.mark.parametrize("test_item", ["test", None])Merge branch '7.x'David Lordbefore adoptiona438322
lines 171–174why, one step back171def test_progressbar_format_progress_line_with_show_func(runner, test_item):172 def item_show_func(item):173 return item174Merge branch '7.x'David Lordbefore adoption7239794
lines 175–177why, one step back175 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 back178 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 back185 with pytest.raises(TypeError, match="iterable or length is required"):Merge branch '7.x'David Lordbefore adoptiona438322
lines 186–189why, one step back186 click.progressbar()187188189def test_progressbar_iter_outside_with_exceptions(runner):Merge branch '7.x'David Lordbefore adoption7239794
line 190why, one step back190 with pytest.raises(RuntimeError, match="with block"):Merge branch '7.x'David Lordbefore adoptiona438322
lines 191–194why, one step back191 progress = click.progressbar(length=2)192 iter(progress)193194Merge branch '7.x'David Lordbefore adoption2269786
lines 195–199why, one step back195def 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 back200 with click.progressbar(range(10), label="test") as progress:Merge branch '7.x'David Lordbefore adoption2269786
lines 201–207why, one step back201 while True:202 try:203 next(progress)204 fake_clock.advance_time()205 except StopIteration:206 break207Merge branch '7.x'David Lordbefore adoption7239794
lines 208–209why, one step back208 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 back210211 result = runner.invoke(cli, [])212 assert result.exception is None213214Merge branch 'Rovanion-master'Markus Unterwaditzerbefore adoption67f39e4
lines 215–216why, one step back215def test_choices_list_in_prompt(runner, monkeypatch):216 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
lines 217–219why, one step back217 @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 back220 def cli_with_choices(g):221 pass222223 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
lines 224–229why, one step back224 @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 back230 def cli_without_choices(g):231 pass232Merge branch '7.x'David Lordbefore adoption7239794
lines 233–234why, one step back233 result = runner.invoke(cli_with_choices, [], input="none")234 assert "(none, day, week, month)" in result.outputMerge branch 'Rovanion-master'Markus Unterwaditzerbefore adoption67f39e4
line 235why, one step back235Merge branch '7.x'David Lordbefore adoption7239794
lines 236–237why, one step back236 result = runner.invoke(cli_without_choices, [], input="none")237 assert "(none, day, week, month)" not in result.outputMerge branch '7.x'David Lordbefore adoption2269786
lines 238–240why, one step back238239240@pytest.mark.parametrize(Merge branch '7.x'David Lordbefore adoption7239794
line 241why, one step back241 "file_kwargs", [{"mode": "rt"}, {"mode": "rb"}, {"lazy": True}]Merge branch '7.x'David Lordbefore adoption2269786
lines 242–249why, one step back242)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 back250 assert result.output == f"file [{__file__}]: \n{__file__}\n"Merge branch 'Rovanion-master'Markus Unterwaditzerbefore adoption67f39e4
lines 251–252why, one step back251252Changelog for #557Markus Unterwaditzerbefore adoptionae5386b
line 253why, one step back253def test_secho(runner):CliRunner: add stdout / stderr, allow separating standard streams (#868)Dave Carlsonbefore adoptionfdceb39
line 254why, one step back254 with runner.isolation() as outstreams:Changelog for #557Markus Unterwaditzerbefore adoptionae5386b
line 255why, one step back255 click.secho(None, nl=False)CliRunner: add stdout / stderr, allow separating standard streams (#868)Dave Carlsonbefore adoptionfdceb39
line 256why, one step back256 bytes = outstreams[0].getvalue()Merge branch '7.x'David Lordbefore adoption7239794
line 257why, one step back257 assert bytes == b""Merge pull request #706 from jojje/issue_651Dan Sullybefore adoption21d5125
lines 258–274why, one step back258259260def 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("")274Merge branch '7.x'David Lordbefore adoption7239794
lines 275–276why, one step back275 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 back277 output = runner.invoke(cli, []).output278Merge branch '7.x'David Lordbefore adoption7239794
line 279why, one step back279 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 back280Merge branch '7.x'David Lordbefore adoption7239794
lines 281–284why, one step back281 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 back285286Merge pull request #1332 from thecav/update-current-itemDavid Lordbefore adoption716a5be
lines 287–291why, one step back287def 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 back292 with click.progressbar(Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 293why, one step back293 range(4), item_show_func=lambda x: f"Custom {x}"Merge branch '7.x'David Lordbefore adoption7239794
line 294why, one step back294 ) as progress:Merge pull request #1332 from thecav/update-current-itemDavid Lordbefore adoption716a5be
lines 295–298why, one step back295 for _ in progress:296 fake_clock.advance_time()297 print("")298Merge branch '7.x'David Lordbefore adoption7239794
lines 299–300why, one step back299 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 back301 output = runner.invoke(cli, []).output302Merge branch '7.x'David Lordbefore adoption7239794
line 303why, one step back303 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 back304Merge branch '7.x'David Lordbefore adoption7239794
lines 305–308why, one step back305 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 back309310311def 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 back316 with click.progressbar(Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 317why, one step back317 length=6, item_show_func=lambda x: f"Custom {x}"Merge branch '7.x'David Lordbefore adoption7239794
line 318why, one step back318 ) as progress:Merge pull request #1332 from thecav/update-current-itemDavid Lordbefore adoption716a5be
lines 319–323why, one step back319 while not progress.finished:320 fake_clock.advance_time()321 progress.update(2, progress.pos)322 print("")323Merge branch '7.x'David Lordbefore adoption7239794
lines 324–325why, one step back324 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 back326 output = runner.invoke(cli, []).output327Merge branch '7.x'David Lordbefore adoption7239794
line 328why, one step back328 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 back329Merge branch '7.x'David Lordbefore adoption7239794
lines 330–332why, one step back330 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 back333334Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 335why, one step back335@pytest.mark.parametrize("key_char", ("h", "H", "é", "À", " ", "字", "àH", "àR"))Merge branch '7.x'David Lordbefore adoption7239794
lines 336–337why, one step back336@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 back338def test_getchar_windows(runner, monkeypatch, key_char, echo):Merge branch '7.x'David Lordbefore adoption7239794
lines 339–341why, one step back339 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 back342 assert click.getchar(echo) == key_char343344Merge branch '7.x'David Lordbefore adoption7239794
line 345why, one step back345@pytest.mark.parametrize(Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 346why, one step back346 "special_key_char, key_char", [("\x00", "a"), ("\x00", "b"), ("\xe0", "c")]Merge branch '7.x'David Lordbefore adoption7239794
lines 347–350why, one step back347)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 back351def 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 back353 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 back357 assert click.getchar() == f"{special_key_char}{key_char}"Merge branch '7.x'David Lordbefore adoptiona438322
lines 358–359why, one step back358359Merge branch '7.x'David Lordbefore adoption7239794
line 360why, one step back360@pytest.mark.parametrize(Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 361why, one step back361 ("key_char", "exc"), [("\x03", KeyboardInterrupt), ("\x1a", EOFError)],Merge branch '7.x'David Lordbefore adoption7239794
lines 362–368why, one step back362)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 back369 click.getchar()