1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
import click
import time
class FakeClock(object):
def __init__(self):
self.now = time.time()
def advance_time(self, seconds=1):
self.now += seconds
def time(self):
return self.now
def test_progressbar_strip_regression(runner, monkeypatch):
fake_clock = FakeClock()
label = ' padded line'
@click.command()
def cli():
with click.progressbar(tuple(range(10)), label=label) as progress:
for thing in progress:
fake_clock.advance_time()
monkeypatch.setattr(time, 'time', fake_clock.time)
monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: True)
assert label in runner.invoke(cli, []).output
def test_progressbar_length_hint(runner, monkeypatch):
class Hinted(object):
def __init__(self, n):
self.items = list(range(n))
def __length_hint__(self):
return len(self.items)
def __iter__(self):
return self
def __next__(self):
if self.items:
return self.items.pop()
else:
raise StopIteration
next = __next__
fake_clock = FakeClock()
@click.command()
def cli():
with click.progressbar(Hinted(10), label='test') as progress:
for thing in progress:
fake_clock.advance_time()
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'
@click.command()
def cli():
with click.progressbar(tuple(range(10)), label=label) as progress:
for thing 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 == ''
def test_choices_list_in_prompt(runner, monkeypatch):
@click.command()
@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)
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_without_choices, [], input='none')
assert '(none, day, week, month)' not in result.output
def test_secho(runner):
with runner.isolation() as outstreams:
click.secho(None, nl=False)
bytes = outstreams[0].getvalue()
assert bytes == b''
def test_progressbar_yields_all_items(runner):
with click.progressbar(range(3)) as progress:
assert len(list(progress)) == 3
def test_progressbar_update(runner, monkeypatch):
fake_clock = FakeClock()
@click.command()
def cli():
with click.progressbar(range(4)) as progress:
for _ in progress:
fake_clock.advance_time()
print("")
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]
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]
|