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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
from __future__ import annotations
import os
import sys
import time
import pytest
import time_machine
from colorama import Fore
from pytest_mock import MockerFixture
from tox.pytest import CaptureFixture, MonkeyPatch
from tox.util import spinner
@time_machine.travel("2012-01-14", tick=False)
def test_spinner(capfd: CaptureFixture, monkeypatch: MonkeyPatch) -> None:
monkeypatch.setattr(sys.stdout, "isatty", lambda: False)
with spinner.Spinner(refresh_rate=100) as spin:
for _ in range(len(spin.frames)):
spin.stream.write("\n")
spin.render_frame()
spin.stream.write("\n")
out, err = capfd.readouterr()
lines = out.split("\n")
expected = [f"\r{spin.CLEAR_LINE}\r{i} [0]" for i in spin.frames] + [
f"\r{spin.CLEAR_LINE}\r{spin.frames[0]} [0]",
f"\r{spin.CLEAR_LINE}",
]
assert lines == expected
@time_machine.travel("2012-01-14", tick=False)
def test_spinner_disabled(capfd: CaptureFixture) -> None:
with spinner.Spinner(refresh_rate=100, enabled=False) as spin:
spin.add("x")
for _ in range(len(spin.frames)):
spin.render_frame()
spin.finalize("x", "done", Fore.GREEN)
spin.clear()
out, err = capfd.readouterr()
assert out == f"{Fore.GREEN}x: done in 0 seconds{Fore.RESET}{os.linesep}", out
assert err == ""
@time_machine.travel("2012-01-14", tick=False)
def test_spinner_progress(capfd: CaptureFixture, monkeypatch: MonkeyPatch) -> None:
monkeypatch.setattr(sys.stdout, "isatty", lambda: False)
with spinner.Spinner() as spin:
for _ in range(len(spin.frames)): # pragma: no branch
spin.stream.write("\n")
time.sleep(spin.refresh_rate)
out, err = capfd.readouterr()
assert not err
assert len({i.strip() for i in out.split("[0]")}) > len(spin.frames) / 2
@time_machine.travel("2012-01-14", tick=False)
def test_spinner_atty(capfd: CaptureFixture, monkeypatch: MonkeyPatch) -> None:
monkeypatch.setattr(sys.stdout, "isatty", lambda: True)
with spinner.Spinner(refresh_rate=100) as spin:
spin.stream.write("\n")
out, err = capfd.readouterr()
lines = out.split("\n")
posix = os.name == "posix"
expected = [
"{}\r{}\r{} [0]".format("\x1b[?25l" if posix else "", spin.CLEAR_LINE, spin.frames[0]),
"\r\x1b[K{}".format("\x1b[?25h" if posix else ""),
]
assert lines == expected
@time_machine.travel("2012-01-14", tick=False)
@pytest.mark.flaky(max_runs=3, min_passes=1)
def test_spinner_report(capfd: CaptureFixture, monkeypatch: MonkeyPatch) -> None:
monkeypatch.setattr(sys.stdout, "isatty", lambda: False)
with spinner.Spinner(refresh_rate=100) as spin:
spin.stream.write(os.linesep)
spin.add("ok")
spin.add("fail")
spin.add("skip")
spin.succeed("ok")
spin.fail("fail")
spin.skip("skip")
out, err = capfd.readouterr()
lines = out.split(os.linesep)
del lines[0]
expected = [
f"\r{spin.CLEAR_LINE}{Fore.GREEN}ok: OK ✔ in 0 seconds{Fore.RESET}",
f"\r{spin.CLEAR_LINE}{Fore.RED}fail: FAIL ✖ in 0 seconds{Fore.RESET}",
f"\r{spin.CLEAR_LINE}{Fore.YELLOW}skip: SKIP ⚠ in 0 seconds{Fore.RESET}",
f"\r{spin.CLEAR_LINE}",
]
assert lines == expected
assert not err
def test_spinner_long_text(capfd: CaptureFixture, monkeypatch: MonkeyPatch) -> None:
monkeypatch.setattr(sys.stdout, "isatty", lambda: False)
with spinner.Spinner(refresh_rate=100) as spin:
spin.stream.write("\n")
spin.add("a" * 60)
spin.add("b" * 60)
spin.render_frame()
spin.stream.write("\n")
out, err = capfd.readouterr()
assert not err
expected = [
f"\r{spin.CLEAR_LINE}\r{spin.frames[1]} [2] {'a' * 60} |...",
f"\r{spin.CLEAR_LINE}",
]
lines = out.split("\n")
del lines[0]
assert lines == expected
def test_spinner_stdout_not_unicode(capfd: CaptureFixture, mocker: MockerFixture) -> None:
stdout = mocker.patch("tox.util.spinner.sys.stdout")
stdout.encoding = "ascii"
with spinner.Spinner(refresh_rate=100) as spin:
for _ in range(len(spin.frames)): # pragma: no branch
spin.render_frame()
out, err = capfd.readouterr()
assert not err
assert not out
written = "".join({i[0][0] for i in stdout.write.call_args_list})
assert all(f in written for f in spin.frames)
@pytest.mark.parametrize(
("seconds", "expected"),
[
(0, "0 seconds"),
(1.0, "1 second"),
(4.0, "4 seconds"),
(4.13, "4.13 seconds"),
(4.137, "4.14 seconds"),
(42.12345, "42.12 seconds"),
(60, "1 minute"),
(61, "1 minute 1 second"),
(120, "2 minutes"),
(40 * 24 * 60 * 60 + 5 * 60, "40 days 5 minutes"),
(40 * 24 * 60 * 60 + 4 * 60 * 60 + 5 * 60 + 1.5, "40 days 4 hours 5 minutes 1.5 seconds"),
],
)
def test_td_human_readable(seconds: float, expected: str) -> None:
assert spinner.td_human_readable(seconds) == expected
|