Skip to content

Commit

Permalink
Fixed tests and test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
wolph committed Jan 25, 2024
1 parent 9a16b73 commit 16c4fce
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 18 deletions.
6 changes: 2 additions & 4 deletions progressbar/bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,8 @@ def _determine_enable_colors(
else:
enable_colors = progressbar.env.ColorSupport.NONE
break
else: # pragma: no cover
# This scenario should never occur because `is_ansi_terminal`
# should always be `True` or `False`
raise ValueError('Unable to determine color support')
else:
enable_colors = False

elif enable_colors is True:
enable_colors = progressbar.env.ColorSupport.XTERM_256
Expand Down
29 changes: 25 additions & 4 deletions progressbar/terminal/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,24 @@ class WindowsColors(enum.Enum):

@staticmethod
def from_rgb(rgb: types.Tuple[int, int, int]):
"""Find the closest ConsoleColor to the given RGB color."""
'''
Find the closest WindowsColors to the given RGB color.
>>> WindowsColors.from_rgb((0, 0, 0))
<WindowsColors.BLACK: (0, 0, 0)>
>>> WindowsColors.from_rgb((255, 255, 255))
<WindowsColors.INTENSE_WHITE: (255, 255, 255)>
>>> WindowsColors.from_rgb((0, 255, 0))
<WindowsColors.INTENSE_GREEN: (0, 255, 0)>
>>> WindowsColors.from_rgb((45, 45, 45))
<WindowsColors.BLACK: (0, 0, 0)>
>>> WindowsColors.from_rgb((128, 0, 128))
<WindowsColors.MAGENTA: (128, 0, 128)>
'''

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


class WindowsColor:
'''
Windows compatible color class for when ANSI is not supported.
Currently a no-op because it is not possible to buffer these colors.
>>> WindowsColor(WindowsColors.RED)('test')
'test'
'''
__slots__ = 'color',

def __init__(self, color: Color):
Expand Down Expand Up @@ -543,9 +567,6 @@ class DummyColor:
def __call__(self, text):
return text

def __getattr__(self, item):
return self

def __repr__(self):
return 'DummyColor()'

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ exclude_lines = [
'if __name__ == .__main__.:',
'if types.TYPE_CHECKING:',
'@typing.overload',
'if os.name == .nt.:',
]

[tool.pyright]
Expand Down
17 changes: 17 additions & 0 deletions tests/test_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ def test_colors():
assert rgb.hex
assert rgb.to_ansi_16 is not None
assert rgb.to_ansi_256 is not None
assert rgb.to_windows is not None
assert color.underline
assert color.fg
assert color.bg
Expand Down Expand Up @@ -304,6 +305,22 @@ def test_apply_colors(text, fg, bg, fg_none, bg_none, percentage, expected,
)


def test_windows_colors(monkeypatch):
monkeypatch.setattr(env, 'COLOR_SUPPORT', env.ColorSupport.WINDOWS)
assert (
apply_colors(
'test',
fg=colors.red,
bg=colors.red,
fg_none=colors.red,
bg_none=colors.red,
percentage=1,
)
== 'test'
)
colors.red.underline('test')


def test_ansi_color(monkeypatch):
color = progressbar.terminal.Color(
colors.red.rgb,
Expand Down
17 changes: 7 additions & 10 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def test_is_ansi_terminal(monkeypatch):
monkeypatch.delenv('PROGRESSBAR_IS_TERMINAL', raising=False)
monkeypatch.delenv('JPY_PARENT_PID', raising=False)

assert progressbar.env.is_ansi_terminal(fd) is False
assert not progressbar.env.is_ansi_terminal(fd)
assert progressbar.env.is_ansi_terminal(fd, True) is True
assert progressbar.env.is_ansi_terminal(fd, False) is False

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

# Sanity check
assert progressbar.env.is_ansi_terminal(fd) is False
assert not progressbar.env.is_ansi_terminal(fd)

monkeypatch.setenv('PROGRESSBAR_IS_TERMINAL', 'true')
assert progressbar.env.is_ansi_terminal(fd) is False
assert not progressbar.env.is_ansi_terminal(fd)
monkeypatch.setenv('PROGRESSBAR_IS_TERMINAL', 'false')
assert progressbar.env.is_ansi_terminal(fd) is False
assert not progressbar.env.is_ansi_terminal(fd)
monkeypatch.delenv('PROGRESSBAR_IS_TERMINAL')

# Sanity check
assert progressbar.env.is_ansi_terminal(fd) is False
assert not progressbar.env.is_ansi_terminal(fd)

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

def raise_error():
raise RuntimeError('test')
fd.isatty = raise_error
if os.name == 'nt':
assert progressbar.env.is_ansi_terminal(fd) is None
else:
assert progressbar.env.is_ansi_terminal(fd) is False
assert not progressbar.env.is_ansi_terminal(fd)

0 comments on commit 16c4fce

Please sign in to comment.