Skip to content
Myr

Expand unit testing around termui (getchar testing for Windows)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by icreatedanaccount · committed by Stephen Rosen · parent 9ae1614 · 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

2 files+147 added−2 removed

modified:tests/test_termui.py+145 added-2 removed
1# -*- coding: utf-8 -*-2import pytest34import click5import time67import click._termui_impl8from click._compat import WIN91011class FakeClock(object):12 def __init__(self):13 self.now = time.time()14
3 unmodified lines
18 def time(self):19 return self.now202122def _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 progress282930def test_progressbar_strip_regression(runner, monkeypatch):31 fake_clock = FakeClock()32 label = ' padded line'3334 @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()3940 monkeypatch.setattr(time, 'time', fake_clock.time)
39 unmodified lines
80 label = 'whatever'8182 @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()8788 monkeypatch.setattr(time, 'time', fake_clock.time)89 monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: False)90 assert runner.invoke(cli, []).output == ''919293@pytest.mark.parametrize('avg, expected', [([], 0.0), ([1, 4], 2.5)])94def test_progressbar_time_per_iteration(runner, avg, expected):95 with _create_progress(2, avg=avg) as progress:96 assert progress.time_per_iteration == expected979899@pytest.mark.parametrize('finished, expected', [(False, 5), (True, 0)])100def test_progressbar_eta(runner, finished, expected):101 with _create_progress(2, finished=finished, avg=[1, 4]) as progress:102 assert progress.eta == expected103104105@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, '')])108def 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() == expected111112113@pytest.mark.parametrize('pos, length', [(0, 5), (-1, 1), (5, 5), (6, 5), (4, 0)])114def 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)121122123@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 ])129def 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() == expected137138139@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 ])146def 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() == expected154155156@pytest.mark.parametrize('test_item', ['test', None])157def test_progressbar_format_progress_line_with_show_func(runner, test_item):158159 def item_show_func(item):160 return item161162 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())167168169def 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'176177178def 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.'186187188def 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 lines
236 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]240241242@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.')245def 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_char250251252@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.')254def 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_char259260261@pytest.mark.parametrize('key_char', [u'\x03', u'\x1a'])262@pytest.mark.skipif(not WIN, reason='Tests user-input using the msvcrt module.')263def 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 lines
10- 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`_)1516.. _#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/11162122Version 7.023-----------24
628 unmodified lines