Skip to content

Commit 16c4fce

Browse files
committed
Fixed tests and test coverage
1 parent 9a16b73 commit 16c4fce

5 files changed

Lines changed: 52 additions & 18 deletions

File tree

progressbar/bar.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,8 @@ def _determine_enable_colors(
256256
else:
257257
enable_colors = progressbar.env.ColorSupport.NONE
258258
break
259-
else: # pragma: no cover
260-
# This scenario should never occur because `is_ansi_terminal`
261-
# should always be `True` or `False`
262-
raise ValueError('Unable to determine color support')
259+
else:
260+
enable_colors = False
263261

264262
elif enable_colors is True:
265263
enable_colors = progressbar.env.ColorSupport.XTERM_256

progressbar/terminal/base.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,24 @@ class WindowsColors(enum.Enum):
199199

200200
@staticmethod
201201
def from_rgb(rgb: types.Tuple[int, int, int]):
202-
"""Find the closest ConsoleColor to the given RGB color."""
202+
'''
203+
Find the closest WindowsColors to the given RGB color.
204+
205+
>>> WindowsColors.from_rgb((0, 0, 0))
206+
<WindowsColors.BLACK: (0, 0, 0)>
207+
208+
>>> WindowsColors.from_rgb((255, 255, 255))
209+
<WindowsColors.INTENSE_WHITE: (255, 255, 255)>
210+
211+
>>> WindowsColors.from_rgb((0, 255, 0))
212+
<WindowsColors.INTENSE_GREEN: (0, 255, 0)>
213+
214+
>>> WindowsColors.from_rgb((45, 45, 45))
215+
<WindowsColors.BLACK: (0, 0, 0)>
216+
217+
>>> WindowsColors.from_rgb((128, 0, 128))
218+
<WindowsColors.MAGENTA: (128, 0, 128)>
219+
'''
203220

204221
def color_distance(rgb1, rgb2):
205222
return sum((c1 - c2) ** 2 for c1, c2 in zip(rgb1, rgb2))
@@ -211,6 +228,13 @@ def color_distance(rgb1, rgb2):
211228

212229

213230
class WindowsColor:
231+
'''
232+
Windows compatible color class for when ANSI is not supported.
233+
Currently a no-op because it is not possible to buffer these colors.
234+
235+
>>> WindowsColor(WindowsColors.RED)('test')
236+
'test'
237+
'''
214238
__slots__ = 'color',
215239

216240
def __init__(self, color: Color):
@@ -543,9 +567,6 @@ class DummyColor:
543567
def __call__(self, text):
544568
return text
545569

546-
def __getattr__(self, item):
547-
return self
548-
549570
def __repr__(self):
550571
return 'DummyColor()'
551572

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ exclude_lines = [
181181
'if __name__ == .__main__.:',
182182
'if types.TYPE_CHECKING:',
183183
'@typing.overload',
184+
'if os.name == .nt.:',
184185
]
185186

186187
[tool.pyright]

tests/test_color.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ def test_colors():
175175
assert rgb.hex
176176
assert rgb.to_ansi_16 is not None
177177
assert rgb.to_ansi_256 is not None
178+
assert rgb.to_windows is not None
178179
assert color.underline
179180
assert color.fg
180181
assert color.bg
@@ -304,6 +305,22 @@ def test_apply_colors(text, fg, bg, fg_none, bg_none, percentage, expected,
304305
)
305306

306307

308+
def test_windows_colors(monkeypatch):
309+
monkeypatch.setattr(env, 'COLOR_SUPPORT', env.ColorSupport.WINDOWS)
310+
assert (
311+
apply_colors(
312+
'test',
313+
fg=colors.red,
314+
bg=colors.red,
315+
fg_none=colors.red,
316+
bg_none=colors.red,
317+
percentage=1,
318+
)
319+
== 'test'
320+
)
321+
colors.red.underline('test')
322+
323+
307324
def test_ansi_color(monkeypatch):
308325
color = progressbar.terminal.Color(
309326
colors.red.rgb,

tests/test_utils.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def test_is_ansi_terminal(monkeypatch):
6868
monkeypatch.delenv('PROGRESSBAR_IS_TERMINAL', raising=False)
6969
monkeypatch.delenv('JPY_PARENT_PID', raising=False)
7070

71-
assert progressbar.env.is_ansi_terminal(fd) is False
71+
assert not progressbar.env.is_ansi_terminal(fd)
7272
assert progressbar.env.is_ansi_terminal(fd, True) is True
7373
assert progressbar.env.is_ansi_terminal(fd, False) is False
7474

@@ -77,16 +77,16 @@ def test_is_ansi_terminal(monkeypatch):
7777
monkeypatch.delenv('JPY_PARENT_PID')
7878

7979
# Sanity check
80-
assert progressbar.env.is_ansi_terminal(fd) is False
80+
assert not progressbar.env.is_ansi_terminal(fd)
8181

8282
monkeypatch.setenv('PROGRESSBAR_IS_TERMINAL', 'true')
83-
assert progressbar.env.is_ansi_terminal(fd) is False
83+
assert not progressbar.env.is_ansi_terminal(fd)
8484
monkeypatch.setenv('PROGRESSBAR_IS_TERMINAL', 'false')
85-
assert progressbar.env.is_ansi_terminal(fd) is False
85+
assert not progressbar.env.is_ansi_terminal(fd)
8686
monkeypatch.delenv('PROGRESSBAR_IS_TERMINAL')
8787

8888
# Sanity check
89-
assert progressbar.env.is_ansi_terminal(fd) is False
89+
assert not progressbar.env.is_ansi_terminal(fd)
9090

9191
# Fake TTY mode for environment testing
9292
fd.isatty = lambda: True
@@ -103,12 +103,9 @@ def test_is_ansi_terminal(monkeypatch):
103103
monkeypatch.setenv('ANSICON', 'true')
104104
assert progressbar.env.is_ansi_terminal(fd) is True
105105
monkeypatch.delenv('ANSICON')
106-
assert progressbar.env.is_ansi_terminal(fd) is False
106+
assert not progressbar.env.is_ansi_terminal(fd)
107107

108108
def raise_error():
109109
raise RuntimeError('test')
110110
fd.isatty = raise_error
111-
if os.name == 'nt':
112-
assert progressbar.env.is_ansi_terminal(fd) is None
113-
else:
114-
assert progressbar.env.is_ansi_terminal(fd) is False
111+
assert not progressbar.env.is_ansi_terminal(fd)

0 commit comments

Comments
 (0)