diff options
author | David Lord <davidism@gmail.com> | 2020-03-08 08:59:56 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-08 08:59:56 -0700 |
commit | 38ba2e6c7f7a48a7208902e5ee7046622b35b716 (patch) | |
tree | 0fa1b49ed926f18ab3d247c2cacada892908123a /tests/test_termui.py | |
parent | 3d1dcc28e115507f1eafbdb75465223c5da58205 (diff) | |
parent | 718485be48263056e7036ea9a60ce11b47e2fc26 (diff) | |
download | click-38ba2e6c7f7a48a7208902e5ee7046622b35b716.tar.gz |
Merge pull request #1490 from pallets/style
move to src directory, add pre-commit
Diffstat (limited to 'tests/test_termui.py')
-rw-r--r-- | tests/test_termui.py | 231 |
1 files changed, 120 insertions, 111 deletions
diff --git a/tests/test_termui.py b/tests/test_termui.py index c6e06b6..99a6a47 100644 --- a/tests/test_termui.py +++ b/tests/test_termui.py @@ -1,9 +1,8 @@ # -*- coding: utf-8 -*- -import pytest - -import click import time +import pytest + import click._termui_impl from click._compat import WIN @@ -29,16 +28,16 @@ def _create_progress(length=10, length_known=True, **kwargs): def test_progressbar_strip_regression(runner, monkeypatch): fake_clock = FakeClock() - label = ' padded line' + label = " padded line" @click.command() def cli(): with _create_progress(label=label) as progress: - for thing in progress: + for _ in progress: fake_clock.advance_time() - monkeypatch.setattr(time, 'time', fake_clock.time) - monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: True) + monkeypatch.setattr(time, "time", fake_clock.time) + monkeypatch.setattr(click._termui_impl, "isatty", lambda _: True) assert label in runner.invoke(cli, []).output @@ -65,101 +64,118 @@ def test_progressbar_length_hint(runner, monkeypatch): @click.command() def cli(): - with click.progressbar(Hinted(10), label='test') as progress: - for thing in progress: + with click.progressbar(Hinted(10), label="test") as progress: + for _ in progress: fake_clock.advance_time() - monkeypatch.setattr(time, 'time', fake_clock.time) - monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: True) + monkeypatch.setattr(time, "time", fake_clock.time) + monkeypatch.setattr(click._termui_impl, "isatty", lambda _: True) result = runner.invoke(cli, []) assert result.exception is None def test_progressbar_hidden(runner, monkeypatch): fake_clock = FakeClock() - label = 'whatever' + label = "whatever" @click.command() def cli(): with _create_progress(label=label) as progress: - for thing in progress: + for _ in progress: fake_clock.advance_time() - monkeypatch.setattr(time, 'time', fake_clock.time) - monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: False) - assert runner.invoke(cli, []).output == '' + monkeypatch.setattr(time, "time", fake_clock.time) + monkeypatch.setattr(click._termui_impl, "isatty", lambda _: False) + assert runner.invoke(cli, []).output == "" -@pytest.mark.parametrize('avg, expected', [([], 0.0), ([1, 4], 2.5)]) +@pytest.mark.parametrize("avg, expected", [([], 0.0), ([1, 4], 2.5)]) def test_progressbar_time_per_iteration(runner, avg, expected): with _create_progress(2, avg=avg) as progress: assert progress.time_per_iteration == expected -@pytest.mark.parametrize('finished, expected', [(False, 5), (True, 0)]) +@pytest.mark.parametrize("finished, expected", [(False, 5), (True, 0)]) def test_progressbar_eta(runner, finished, expected): with _create_progress(2, finished=finished, avg=[1, 4]) as progress: assert progress.eta == expected -@pytest.mark.parametrize('eta, expected', - [(0, '00:00:00'), (30, '00:00:30'), (90, '00:01:30'), (900, '00:15:00'), - (9000, '02:30:00'), (99999999999, '1157407d 09:46:39'), (None, '')]) +@pytest.mark.parametrize( + "eta, expected", + [ + (0, "00:00:00"), + (30, "00:00:30"), + (90, "00:01:30"), + (900, "00:15:00"), + (9000, "02:30:00"), + (99999999999, "1157407d 09:46:39"), + (None, ""), + ], +) def test_progressbar_format_eta(runner, eta, expected): with _create_progress(1, eta_known=eta is not None, avg=[eta]) as progress: assert progress.format_eta() == expected -@pytest.mark.parametrize('pos, length', [(0, 5), (-1, 1), (5, 5), (6, 5), (4, 0)]) +@pytest.mark.parametrize("pos, length", [(0, 5), (-1, 1), (5, 5), (6, 5), (4, 0)]) def test_progressbar_format_pos(runner, pos, length): with _create_progress(length, length_known=length != 0, pos=pos) as progress: result = progress.format_pos() if progress.length_known: - assert result == '%s/%s' % (pos, length) + assert result == "{}/{}".format(pos, length) else: assert result == str(pos) -@pytest.mark.parametrize('length, finished, pos, avg, expected', - [(8, False, 7, 0, '#######-'), - (0, True, 8, 0, '########'), - (0, False, 8, 0, '--------'), - (0, False, 5, 3, '#-------') - ]) +@pytest.mark.parametrize( + "length, finished, pos, avg, expected", + [ + (8, False, 7, 0, "#######-"), + (0, True, 8, 0, "########"), + (0, False, 8, 0, "--------"), + (0, False, 5, 3, "#-------"), + ], +) def test_progressbar_format_bar(runner, length, finished, pos, avg, expected): - with _create_progress(length, - length_known=length != 0, - width=8, - pos=pos, - finished=finished, - avg=[avg]) as progress: + with _create_progress( + length, length_known=length != 0, width=8, pos=pos, finished=finished, avg=[avg] + ) as progress: assert progress.format_bar() == expected -@pytest.mark.parametrize('length, length_known, show_percent, show_pos, pos, expected', - [(0, True, True, True, 0, ' [--------] 0/0 0%'), - (0, True, False, True, 0, ' [--------] 0/0'), - (0, True, False, False, 0, ' [--------]'), - (0, False, False, False, 0, ' [--------]'), - (8, True, True, True, 8, ' [########] 8/8 100%') - ]) -def test_progressbar_format_progress_line(runner, length, length_known, show_percent, show_pos, pos, expected): - with _create_progress(length, - length_known, - width=8, - show_percent=show_percent, - pos=pos, - show_pos=show_pos) as progress: +@pytest.mark.parametrize( + "length, length_known, show_percent, show_pos, pos, expected", + [ + (0, True, True, True, 0, " [--------] 0/0 0%"), + (0, True, False, True, 0, " [--------] 0/0"), + (0, True, False, False, 0, " [--------]"), + (0, False, False, False, 0, " [--------]"), + (8, True, True, True, 8, " [########] 8/8 100%"), + ], +) +def test_progressbar_format_progress_line( + runner, length, length_known, show_percent, show_pos, pos, expected +): + with _create_progress( + length, + length_known, + width=8, + show_percent=show_percent, + pos=pos, + show_pos=show_pos, + ) as progress: assert progress.format_progress_line() == expected -@pytest.mark.parametrize('test_item', ['test', None]) +@pytest.mark.parametrize("test_item", ["test", None]) def test_progressbar_format_progress_line_with_show_func(runner, test_item): - def item_show_func(item): return item - with _create_progress(item_show_func=item_show_func, current_item=test_item) as progress: + with _create_progress( + item_show_func=item_show_func, current_item=test_item + ) as progress: if test_item: assert progress.format_progress_line().endswith(test_item) else: @@ -167,22 +183,14 @@ def test_progressbar_format_progress_line_with_show_func(runner, test_item): def test_progressbar_init_exceptions(runner): - try: + with pytest.raises(TypeError, match="iterable or length is required"): click.progressbar() - except TypeError as e: - assert str(e) == 'iterable or length is required' - else: - assert False, 'Expected an exception because unspecified arguments' def test_progressbar_iter_outside_with_exceptions(runner): - try: + with pytest.raises(RuntimeError, match="with block"): progress = click.progressbar(length=2) iter(progress) - except RuntimeError as e: - assert str(e) == 'You need to use progress bars in a with block.' - else: - assert False, 'Expected an exception because of abort-related inputs.' def test_progressbar_is_iterator(runner, monkeypatch): @@ -190,7 +198,7 @@ def test_progressbar_is_iterator(runner, monkeypatch): @click.command() def cli(): - with click.progressbar(range(10), label='test') as progress: + with click.progressbar(range(10), label="test") as progress: while True: try: next(progress) @@ -198,8 +206,8 @@ def test_progressbar_is_iterator(runner, monkeypatch): except StopIteration: break - monkeypatch.setattr(time, 'time', fake_clock.time) - monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: True) + monkeypatch.setattr(time, "time", fake_clock.time) + monkeypatch.setattr(click._termui_impl, "isatty", lambda _: True) result = runner.invoke(cli, []) assert result.exception is None @@ -207,31 +215,31 @@ def test_progressbar_is_iterator(runner, monkeypatch): def test_choices_list_in_prompt(runner, monkeypatch): @click.command() - @click.option('-g', type=click.Choice(['none', 'day', 'week', 'month']), - prompt=True) + @click.option( + "-g", type=click.Choice(["none", "day", "week", "month"]), prompt=True + ) def cli_with_choices(g): pass @click.command() - @click.option('-g', type=click.Choice(['none', 'day', 'week', 'month']), - prompt=True, show_choices=False) + @click.option( + "-g", + type=click.Choice(["none", "day", "week", "month"]), + prompt=True, + show_choices=False, + ) def cli_without_choices(g): pass - result = runner.invoke(cli_with_choices, [], input='none') - assert '(none, day, week, month)' in result.output + result = runner.invoke(cli_with_choices, [], input="none") + assert "(none, day, week, month)" in result.output - result = runner.invoke(cli_without_choices, [], input='none') - assert '(none, day, week, month)' not in result.output + result = runner.invoke(cli_without_choices, [], input="none") + assert "(none, day, week, month)" not in result.output @pytest.mark.parametrize( - "file_kwargs", - [ - {"mode": "rt"}, - {"mode": "rb"}, - {"lazy": True}, - ] + "file_kwargs", [{"mode": "rt"}, {"mode": "rb"}, {"lazy": True}] ) def test_file_prompt_default_format(runner, file_kwargs): @click.command() @@ -247,7 +255,7 @@ def test_secho(runner): with runner.isolation() as outstreams: click.secho(None, nl=False) bytes = outstreams[0].getvalue() - assert bytes == b'' + assert bytes == b"" def test_progressbar_yields_all_items(runner): @@ -265,49 +273,50 @@ def test_progressbar_update(runner, monkeypatch): fake_clock.advance_time() print("") - monkeypatch.setattr(time, 'time', fake_clock.time) - monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: True) + monkeypatch.setattr(time, "time", fake_clock.time) + monkeypatch.setattr(click._termui_impl, "isatty", lambda _: True) output = runner.invoke(cli, []).output - lines = [line for line in output.split('\n') if '[' in line] + lines = [line for line in output.split("\n") if "[" in line] - assert ' 25% 00:00:03' in lines[0] - assert ' 50% 00:00:02' in lines[1] - assert ' 75% 00:00:01' in lines[2] - assert '100% ' in lines[3] + assert " 25% 00:00:03" in lines[0] + assert " 50% 00:00:02" in lines[1] + assert " 75% 00:00:01" in lines[2] + assert "100% " in lines[3] -@pytest.mark.parametrize( - 'key_char', (u'h', u'H', u'é', u'À', u' ', u'字', u'àH', u'àR') -) -@pytest.mark.parametrize('echo', [True, False]) -@pytest.mark.skipif(not WIN, reason='Tests user-input using the msvcrt module.') +@pytest.mark.parametrize("key_char", (u"h", u"H", u"é", u"À", u" ", u"字", u"àH", u"àR")) +@pytest.mark.parametrize("echo", [True, False]) +@pytest.mark.skipif(not WIN, reason="Tests user-input using the msvcrt module.") def test_getchar_windows(runner, monkeypatch, key_char, echo): - monkeypatch.setattr(click._termui_impl.msvcrt, 'getwche', lambda: key_char) - monkeypatch.setattr(click._termui_impl.msvcrt, 'getwch', lambda: key_char) - monkeypatch.setattr(click.termui, '_getchar', None) + monkeypatch.setattr(click._termui_impl.msvcrt, "getwche", lambda: key_char) + monkeypatch.setattr(click._termui_impl.msvcrt, "getwch", lambda: key_char) + monkeypatch.setattr(click.termui, "_getchar", None) assert click.getchar(echo) == key_char -@pytest.mark.parametrize('special_key_char, key_char', [(u'\x00', 'a'), (u'\x00', 'b'), (u'\xe0', 'c')]) -@pytest.mark.skipif(not WIN, reason='Tests special character inputs using the msvcrt module.') +@pytest.mark.parametrize( + "special_key_char, key_char", [(u"\x00", "a"), (u"\x00", "b"), (u"\xe0", "c")] +) +@pytest.mark.skipif( + not WIN, reason="Tests special character inputs using the msvcrt module." +) def test_getchar_special_key_windows(runner, monkeypatch, special_key_char, key_char): ordered_inputs = [key_char, special_key_char] - monkeypatch.setattr(click._termui_impl.msvcrt, 'getwch', lambda: ordered_inputs.pop()) - monkeypatch.setattr(click.termui, '_getchar', None) + monkeypatch.setattr( + click._termui_impl.msvcrt, "getwch", lambda: ordered_inputs.pop() + ) + monkeypatch.setattr(click.termui, "_getchar", None) assert click.getchar() == special_key_char + key_char -@pytest.mark.parametrize('key_char', [u'\x03', u'\x1a']) -@pytest.mark.skipif(not WIN, reason='Tests user-input using the msvcrt module.') -def test_getchar_windows_exceptions(runner, monkeypatch, key_char): - monkeypatch.setattr(click._termui_impl.msvcrt, 'getwch', lambda: key_char) - monkeypatch.setattr(click.termui, '_getchar', None) - try: +@pytest.mark.parametrize( + ("key_char", "exc"), [(u"\x03", KeyboardInterrupt), (u"\x1a", EOFError)], +) +@pytest.mark.skipif(not WIN, reason="Tests user-input using the msvcrt module.") +def test_getchar_windows_exceptions(runner, monkeypatch, key_char, exc): + monkeypatch.setattr(click._termui_impl.msvcrt, "getwch", lambda: key_char) + monkeypatch.setattr(click.termui, "_getchar", None) + + with pytest.raises(exc): click.getchar() - except KeyboardInterrupt: - assert key_char == u'\x03' - except EOFError: - assert key_char == u'\x1a' - else: - assert False, 'Expected an exception because of abort-specific inputs.' |