Merge pull request #1315 from icreatedanaccount/expand-termui-unittests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Expand unit testing around termui (getchar testing for Windows)
authored by Stephen Rosen · committed by GitHub · parents 9ae1614, 53980cc · Browse files · 2 files·+147 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+147 added−2 removed
- tests:+145 added−2 removed
- other:+2 added−0 removed
Provenance
- Commit
2d42cd4- Tree
f86cc58- Author
Stephen Rosen <sirosen@globus.org>- Committer
GitHub <noreply@github.com>- Parent 1
9ae1614- Parent 2
53980cc
2 files changed+147 added−2 removed
modified:tests/test_termui.py+145 added-2 removed
1+# -*- coding: utf-8 -*-2+import pytest3+4 import click5 import time6 7+import click._termui_impl8+from click._compat import WIN9 10+11 class FakeClock(object):12 def __init__(self):13 self.now = time.time()14 3 unmodified lines18 def time(self):19 return self.now20 21 22+def _create_progress(length=10, length_known=True, **kwargs):23+ progress = click.progressbar(tuple(range(length)))24+ for key, value in kwargs.items():25+ setattr(progress, key, value)26+ progress.length_known = length_known27+ return progress28+29+30 def test_progressbar_strip_regression(runner, monkeypatch):31 fake_clock = FakeClock()32 label = ' padded line'33 34 @click.command()35 def cli():22- with click.progressbar(tuple(range(10)), label=label) as progress:36+ with _create_progress(label=label) as progress:37 for thing in progress:38 fake_clock.advance_time()39 40 monkeypatch.setattr(time, 'time', fake_clock.time)39 unmodified lines80 label = 'whatever'81 82 @click.command()83 def cli():70- with click.progressbar(tuple(range(10)), label=label) as progress:84+ with _create_progress(label=label) as progress:85 for thing in progress:86 fake_clock.advance_time()87 88 monkeypatch.setattr(time, 'time', fake_clock.time)89 monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: False)90 assert runner.invoke(cli, []).output == ''91 92 93+@pytest.mark.parametrize('avg, expected', [([], 0.0), ([1, 4], 2.5)])94+def test_progressbar_time_per_iteration(runner, avg, expected):95+ with _create_progress(2, avg=avg) as progress:96+ assert progress.time_per_iteration == expected97+98+99+@pytest.mark.parametrize('finished, expected', [(False, 5), (True, 0)])100+def test_progressbar_eta(runner, finished, expected):101+ with _create_progress(2, finished=finished, avg=[1, 4]) as progress:102+ assert progress.eta == expected103+104+105+@pytest.mark.parametrize('eta, expected',106+ [(0, '00:00:00'), (30, '00:00:30'), (90, '00:01:30'), (900, '00:15:00'),107+ (9000, '02:30:00'), (99999999999, '1157407d 09:46:39'), (None, '')])108+def test_progressbar_format_eta(runner, eta, expected):109+ with _create_progress(1, eta_known=eta is not None, avg=[eta]) as progress:110+ assert progress.format_eta() == expected111+112+113+@pytest.mark.parametrize('pos, length', [(0, 5), (-1, 1), (5, 5), (6, 5), (4, 0)])114+def test_progressbar_format_pos(runner, pos, length):115+ with _create_progress(length, length_known=length != 0, pos=pos) as progress:116+ result = progress.format_pos()117+ if progress.length_known:118+ assert result == '%s/%s' % (pos, length)119+ else:120+ assert result == str(pos)121+122+123+@pytest.mark.parametrize('length, finished, pos, avg, expected',124+ [(8, False, 7, 0, '#######-'),125+ (0, True, 8, 0, '########'),126+ (0, False, 8, 0, '--------'),127+ (0, False, 5, 3, '#-------')128+ ])129+def test_progressbar_format_bar(runner, length, finished, pos, avg, expected):130+ with _create_progress(length,131+ length_known=length != 0,132+ width=8,133+ pos=pos,134+ finished=finished,135+ avg=[avg]) as progress:136+ assert progress.format_bar() == expected137+138+139+@pytest.mark.parametrize('length, length_known, show_percent, show_pos, pos, expected',140+ [(0, True, True, True, 0, ' [--------] 0/0 0%'),141+ (0, True, False, True, 0, ' [--------] 0/0'),142+ (0, True, False, False, 0, ' [--------]'),143+ (0, False, False, False, 0, ' [--------]'),144+ (8, True, True, True, 8, ' [########] 8/8 100%')145+ ])146+def test_progressbar_format_progress_line(runner, length, length_known, show_percent, show_pos, pos, expected):147+ with _create_progress(length,148+ length_known,149+ width=8,150+ show_percent=show_percent,151+ pos=pos,152+ show_pos=show_pos) as progress:153+ assert progress.format_progress_line() == expected154+155+156+@pytest.mark.parametrize('test_item', ['test', None])157+def test_progressbar_format_progress_line_with_show_func(runner, test_item):158+159+ def item_show_func(item):160+ return item161+162+ with _create_progress(item_show_func=item_show_func, current_item=test_item) as progress:163+ if test_item:164+ assert progress.format_progress_line().endswith(test_item)165+ else:166+ assert progress.format_progress_line().endswith(progress.format_pct())167+168+169+def test_progressbar_init_exceptions(runner):170+ try:171+ click.progressbar()172+ except TypeError as e:173+ assert str(e) == 'iterable or length is required'174+ else:175+ assert False, 'Expected an exception because unspecified arguments'176+177+178+def test_progressbar_iter_outside_with_exceptions(runner):179+ try:180+ progress = click.progressbar(length=2)181+ iter(progress)182+ except RuntimeError as e:183+ assert str(e) == 'You need to use progress bars in a with block.'184+ else:185+ assert False, 'Expected an exception because of abort-related inputs.'186+187+188 def test_choices_list_in_prompt(runner, monkeypatch):189 @click.command()190 @click.option('-g', type=click.Choice(['none', 'day', 'week', 'month']),191 prompt=True)44 unmodified lines236 assert ' 25% 00:00:03' in lines[0]237 assert ' 50% 00:00:02' in lines[1]238 assert ' 75% 00:00:01' in lines[2]239 assert '100% ' in lines[3]240+241+242+@pytest.mark.parametrize('key_char', ['h', 'H', 'é', 'À', ' ', '字', 'àH', 'àR'])243+@pytest.mark.parametrize('echo', [True, False])244+@pytest.mark.skipif(not WIN, reason='Tests user-input using the msvcrt module.')245+def test_getchar_windows(runner, monkeypatch, key_char, echo):246+ monkeypatch.setattr(click._termui_impl.msvcrt, 'getwche', lambda: key_char)247+ monkeypatch.setattr(click._termui_impl.msvcrt, 'getwch', lambda: key_char)248+ monkeypatch.setattr(click.termui, '_getchar', None)249+ assert click.getchar(echo) == key_char250+251+252+@pytest.mark.parametrize('special_key_char, key_char', [(u'\x00', 'a'), (u'\x00', 'b'), (u'\xe0', 'c')])253+@pytest.mark.skipif(not WIN, reason='Tests special character inputs using the msvcrt module.')254+def test_getchar_special_key_windows(runner, monkeypatch, special_key_char, key_char):255+ ordered_inputs = [key_char, special_key_char]256+ monkeypatch.setattr(click._termui_impl.msvcrt, 'getwch', lambda: ordered_inputs.pop())257+ monkeypatch.setattr(click.termui, '_getchar', None)258+ assert click.getchar() == special_key_char + key_char259+260+261+@pytest.mark.parametrize('key_char', [u'\x03', u'\x1a'])262+@pytest.mark.skipif(not WIN, reason='Tests user-input using the msvcrt module.')263+def test_getchar_windows_exceptions(runner, monkeypatch, key_char):264+ monkeypatch.setattr(click._termui_impl.msvcrt, 'getwch', lambda: key_char)265+ monkeypatch.setattr(click.termui, '_getchar', None)266+ try:267+ click.getchar()268+ except KeyboardInterrupt:269+ assert key_char == u'\x03'270+ except EOFError:271+ assert key_char == u'\x1a'272+ else:273+ assert False, 'Expected an exception because of abort-specific inputs.'modified:CHANGES.rst+2 added
9 unmodified lines10 - Add ``no_args_is_help`` option to ``click.Command``, defaults to False (`#1167`_)11 - Fix link in ``unicode_literals`` error message. (`#1151`_)12 - Add support for colored output on UNIX Jupyter notebooks. (`#1185`_)13 - Remove unused compat shim for ``bytes``. (`#1195`_)14+- Expand unit testing around termui, especially getchar (Windows). (`#1116`_)15 16 .. _#1167: https://github.com/pallets/click/pull/116717 .. _#1151: https://github.com/pallets/click/pull/115118 .. _#1185: https://github.com/pallets/click/issues/118519 .. _#1195: https://github.com/pallets/click/pull/119520+.. _#1116: https://github.com/pallets/click/issues/111621 22 Version 7.023 -----------24 628 unmodified lines