| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import os |
| 4 |
from typing import ClassVar |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
import progressbar |
| 9 |
import progressbar.terminal |
| 10 |
from progressbar import env, terminal, widgets |
| 11 |
from progressbar.terminal import Colors, apply_colors, colors |
| 12 |
|
| 13 |
ENVIRONMENT_VARIABLES = [ |
| 14 |
'PROGRESSBAR_ENABLE_COLORS', |
| 15 |
'FORCE_COLOR', |
| 16 |
'COLORTERM', |
| 17 |
'TERM', |
| 18 |
'JUPYTER_COLUMNS', |
| 19 |
'JUPYTER_LINES', |
| 20 |
'JPY_PARENT_PID', |
| 21 |
] |
| 22 |
|
| 23 |
|
| 24 |
@pytest.fixture(autouse=True) |
| 25 |
def clear_env(monkeypatch: pytest.MonkeyPatch) -> None: |
| 26 |
# Clear all environment variables that might affect the tests |
| 27 |
for variable in ENVIRONMENT_VARIABLES: |
| 28 |
monkeypatch.delenv(variable, raising=False) |
| 29 |
|
| 30 |
monkeypatch.setattr(env, 'JUPYTER', False) |
| 31 |
|
| 32 |
|
| 33 |
@pytest.mark.parametrize( |
| 34 |
'variable', |
| 35 |
[ |
| 36 |
'PROGRESSBAR_ENABLE_COLORS', |
| 37 |
'FORCE_COLOR', |
| 38 |
], |
| 39 |
) |
| 40 |
def test_color_environment_variables( |
| 41 |
monkeypatch: pytest.MonkeyPatch, |
| 42 |
variable: str, |
| 43 |
) -> None: |
| 44 |
if os.name == 'nt': |
| 45 |
# Windows has special handling so we need to disable that to make the |
| 46 |
# tests work properly |
| 47 |
monkeypatch.setattr(os, 'name', 'posix') |
| 48 |
|
| 49 |
monkeypatch.setattr( |
| 50 |
env, |
| 51 |
'COLOR_SUPPORT', |
| 52 |
env.ColorSupport.XTERM_256, |
| 53 |
) |
| 54 |
|
| 55 |
monkeypatch.setenv(variable, 'true') |
| 56 |
bar = progressbar.ProgressBar() |
| 57 |
assert not env.is_ansi_terminal(bar.fd) |
| 58 |
assert not bar.is_ansi_terminal |
| 59 |
assert bar.enable_colors |
| 60 |
|
| 61 |
monkeypatch.setenv(variable, 'false') |
| 62 |
bar = progressbar.ProgressBar() |
| 63 |
assert not bar.enable_colors |
| 64 |
|
| 65 |
monkeypatch.setenv(variable, '') |
| 66 |
bar = progressbar.ProgressBar() |
| 67 |
assert not bar.enable_colors |
| 68 |
|
| 69 |
|
| 70 |
@pytest.mark.parametrize( |
| 71 |
'variable', |
| 72 |
[ |
| 73 |
'FORCE_COLOR', |
| 74 |
'PROGRESSBAR_ENABLE_COLORS', |
| 75 |
'COLORTERM', |
| 76 |
'TERM', |
| 77 |
], |
| 78 |
) |
| 79 |
@pytest.mark.parametrize( |
| 80 |
'value', |
| 81 |
[ |
| 82 |
'', |
| 83 |
'truecolor', |
| 84 |
'24bit', |
| 85 |
'256', |
| 86 |
'xterm-256', |
| 87 |
'xterm', |
| 88 |
], |
| 89 |
) |
| 90 |
def test_color_support_from_env(monkeypatch, variable, value) -> None: |
| 91 |
if os.name == 'nt': |
| 92 |
# Windows has special handling so we need to disable that to make the |
| 93 |
# tests work properly |
| 94 |
monkeypatch.setattr(os, 'name', 'posix') |
| 95 |
|
| 96 |
monkeypatch.setenv(variable, value) |
| 97 |
env.ColorSupport.from_env() |
| 98 |
|
| 99 |
|
| 100 |
@pytest.mark.parametrize( |
| 101 |
'variable', |
| 102 |
[ |
| 103 |
'JUPYTER_COLUMNS', |
| 104 |
'JUPYTER_LINES', |
| 105 |
], |
| 106 |
) |
| 107 |
def test_color_support_from_env_jupyter(monkeypatch, variable) -> None: |
| 108 |
monkeypatch.setattr(env, 'JUPYTER', True) |
| 109 |
assert env.ColorSupport.from_env() == env.ColorSupport.XTERM_TRUECOLOR |
| 110 |
|
| 111 |
# Sanity check |
| 112 |
monkeypatch.setattr(env, 'JUPYTER', False) |
| 113 |
if os.name == 'nt': |
| 114 |
assert env.ColorSupport.from_env() == env.ColorSupport.WINDOWS |
| 115 |
else: |
| 116 |
assert env.ColorSupport.from_env() == env.ColorSupport.NONE |
| 117 |
|
| 118 |
|
| 119 |
def test_enable_colors_flags() -> None: |
| 120 |
bar = progressbar.ProgressBar(enable_colors=True) |
| 121 |
assert bar.enable_colors |
| 122 |
|
| 123 |
bar = progressbar.ProgressBar(enable_colors=False) |
| 124 |
assert not bar.enable_colors |
| 125 |
|
| 126 |
bar = progressbar.ProgressBar( |
| 127 |
enable_colors=env.ColorSupport.XTERM_TRUECOLOR, |
| 128 |
) |
| 129 |
assert bar.enable_colors |
| 130 |
|
| 131 |
with pytest.raises(ValueError): |
| 132 |
progressbar.ProgressBar(enable_colors=12345) |
| 133 |
|
| 134 |
|
| 135 |
class _TestFixedColorSupport(progressbar.widgets.WidgetBase): |
| 136 |
_fixed_colors: ClassVar[widgets.TFixedColors] = widgets.TFixedColors( |
| 137 |
fg_none=progressbar.widgets.colors.yellow, |
| 138 |
bg_none=None, |
| 139 |
) |
| 140 |
|
| 141 |
def __call__(self, *args, **kwargs) -> None: |
| 142 |
pass |
| 143 |
|
| 144 |
|
| 145 |
class _TestFixedGradientSupport(progressbar.widgets.WidgetBase): |
| 146 |
_gradient_colors: ClassVar[widgets.TGradientColors] = ( |
| 147 |
widgets.TGradientColors( |
| 148 |
fg=progressbar.widgets.colors.gradient, |
| 149 |
bg=None, |
| 150 |
) |
| 151 |
) |
| 152 |
|
| 153 |
def __call__(self, *args, **kwargs) -> None: |
| 154 |
pass |
| 155 |
|
| 156 |
|
| 157 |
@pytest.mark.parametrize( |
| 158 |
'widget', |
| 159 |
[ |
| 160 |
progressbar.Percentage, |
| 161 |
progressbar.SimpleProgress, |
| 162 |
_TestFixedColorSupport, |
| 163 |
_TestFixedGradientSupport, |
| 164 |
], |
| 165 |
) |
| 166 |
def test_color_widgets(widget) -> None: |
| 167 |
assert widget().uses_colors |
| 168 |
print(f'{widget} has colors? {widget.uses_colors}') |
| 169 |
|
| 170 |
|
| 171 |
def test_color_gradient() -> None: |
| 172 |
gradient = terminal.ColorGradient(colors.red) |
| 173 |
assert gradient.get_color(0) == gradient.get_color(-1) |
| 174 |
assert gradient.get_color(1) == gradient.get_color(2) |
| 175 |
|
| 176 |
assert gradient.get_color(0.5) == colors.red |
| 177 |
|
| 178 |
gradient = terminal.ColorGradient(colors.red, colors.yellow) |
| 179 |
assert gradient.get_color(0) == colors.red |
| 180 |
assert gradient.get_color(1) == colors.yellow |
| 181 |
assert gradient.get_color(0.5) != colors.red |
| 182 |
assert gradient.get_color(0.5) != colors.yellow |
| 183 |
|
| 184 |
gradient = terminal.ColorGradient( |
| 185 |
colors.red, |
| 186 |
colors.yellow, |
| 187 |
interpolate=False, |
| 188 |
) |
| 189 |
assert gradient.get_color(0) == colors.red |
| 190 |
assert gradient.get_color(1) == colors.yellow |
| 191 |
assert gradient.get_color(0.5) == colors.red |
| 192 |
|
| 193 |
|
| 194 |
@pytest.mark.parametrize( |
| 195 |
'widget', |
| 196 |
[ |
| 197 |
progressbar.Counter, |
| 198 |
], |
| 199 |
) |
| 200 |
def test_no_color_widgets(widget) -> None: |
| 201 |
assert not widget().uses_colors |
| 202 |
print(f'{widget} has colors? {widget.uses_colors}') |
| 203 |
|
| 204 |
assert widget( |
| 205 |
fixed_colors=_TestFixedColorSupport._fixed_colors, |
| 206 |
).uses_colors |
| 207 |
assert widget( |
| 208 |
gradient_colors=_TestFixedGradientSupport._gradient_colors, |
| 209 |
).uses_colors |
| 210 |
|
| 211 |
|
| 212 |
def test_colors(monkeypatch) -> None: |
| 213 |
for colors_ in Colors.by_rgb.values(): |
| 214 |
for color in colors_: |
| 215 |
rgb = color.rgb |
| 216 |
assert rgb.rgb |
| 217 |
assert rgb.hex |
| 218 |
assert rgb.to_ansi_16 is not None |
| 219 |
assert rgb.to_ansi_256 is not None |
| 220 |
assert rgb.to_windows is not None |
| 221 |
|
| 222 |
with monkeypatch.context() as context: |
| 223 |
context.setattr(env, 'COLOR_SUPPORT', env.ColorSupport.XTERM) |
| 224 |
assert color.underline |
| 225 |
context.setattr(env, 'COLOR_SUPPORT', env.ColorSupport.WINDOWS) |
| 226 |
assert color.underline |
| 227 |
|
| 228 |
assert color.fg |
| 229 |
assert color.bg |
| 230 |
assert str(color) |
| 231 |
assert str(rgb) |
| 232 |
assert color('test') |
| 233 |
|
| 234 |
|
| 235 |
def test_color() -> None: |
| 236 |
color = colors.red |
| 237 |
if os.name != 'nt': |
| 238 |
assert color('x') == color.fg('x') != 'x' |
| 239 |
assert color.fg('x') != color.bg('x') != 'x' |
| 240 |
assert color.fg('x') != color.underline('x') != 'x' |
| 241 |
# Color hashes are based on the RGB value |
| 242 |
assert hash(color) == hash(terminal.Color(color.rgb, None, None, None)) |
| 243 |
Colors.register(color.rgb) |
| 244 |
|
| 245 |
|
| 246 |
@pytest.mark.parametrize( |
| 247 |
'rgb,hls', |
| 248 |
[ |
| 249 |
(terminal.RGB(0, 0, 0), terminal.HSL(0, 0, 0)), |
| 250 |
(terminal.RGB(255, 255, 255), terminal.HSL(0, 0, 100)), |
| 251 |
(terminal.RGB(255, 0, 0), terminal.HSL(0, 100, 50)), |
| 252 |
(terminal.RGB(0, 255, 0), terminal.HSL(120, 100, 50)), |
| 253 |
(terminal.RGB(0, 0, 255), terminal.HSL(240, 100, 50)), |
| 254 |
(terminal.RGB(255, 255, 0), terminal.HSL(60, 100, 50)), |
| 255 |
(terminal.RGB(0, 255, 255), terminal.HSL(180, 100, 50)), |
| 256 |
(terminal.RGB(255, 0, 255), terminal.HSL(300, 100, 50)), |
| 257 |
(terminal.RGB(128, 128, 128), terminal.HSL(0, 0, 50)), |
| 258 |
(terminal.RGB(128, 0, 0), terminal.HSL(0, 100, 25)), |
| 259 |
(terminal.RGB(128, 128, 0), terminal.HSL(60, 100, 25)), |
| 260 |
(terminal.RGB(0, 128, 0), terminal.HSL(120, 100, 25)), |
| 261 |
(terminal.RGB(128, 0, 128), terminal.HSL(300, 100, 25)), |
| 262 |
(terminal.RGB(0, 128, 128), terminal.HSL(180, 100, 25)), |
| 263 |
(terminal.RGB(0, 0, 128), terminal.HSL(240, 100, 25)), |
| 264 |
(terminal.RGB(192, 192, 192), terminal.HSL(0, 0, 75)), |
| 265 |
], |
| 266 |
) |
| 267 |
def test_rgb_to_hls(rgb, hls) -> None: |
| 268 |
assert terminal.HSL.from_rgb(rgb) == hls |
| 269 |
|
| 270 |
|
| 271 |
@pytest.mark.parametrize( |
| 272 |
'text, fg, bg, fg_none, bg_none, percentage, expected', |
| 273 |
[ |
| 274 |
('test', None, None, None, None, None, 'test'), |
| 275 |
('test', None, None, None, None, 1, 'test'), |
| 276 |
( |
| 277 |
'test', |
| 278 |
None, |
| 279 |
None, |
| 280 |
None, |
| 281 |
colors.red, |
| 282 |
None, |
| 283 |
'\x1b[48;5;9mtest\x1b[49m', |
| 284 |
), |
| 285 |
( |
| 286 |
'test', |
| 287 |
None, |
| 288 |
colors.green, |
| 289 |
None, |
| 290 |
colors.red, |
| 291 |
None, |
| 292 |
'\x1b[48;5;9mtest\x1b[49m', |
| 293 |
), |
| 294 |
('test', None, colors.red, None, None, 1, '\x1b[48;5;9mtest\x1b[49m'), |
| 295 |
('test', None, colors.red, None, None, None, 'test'), |
| 296 |
( |
| 297 |
'test', |
| 298 |
colors.green, |
| 299 |
None, |
| 300 |
colors.red, |
| 301 |
None, |
| 302 |
None, |
| 303 |
'\x1b[38;5;9mtest\x1b[39m', |
| 304 |
), |
| 305 |
( |
| 306 |
'test', |
| 307 |
colors.green, |
| 308 |
colors.red, |
| 309 |
None, |
| 310 |
None, |
| 311 |
1, |
| 312 |
'\x1b[48;5;9m\x1b[38;5;2mtest\x1b[39m\x1b[49m', |
| 313 |
), |
| 314 |
('test', colors.red, None, None, None, 1, '\x1b[38;5;9mtest\x1b[39m'), |
| 315 |
('test', colors.red, None, None, None, None, 'test'), |
| 316 |
('test', colors.red, colors.red, None, None, None, 'test'), |
| 317 |
( |
| 318 |
'test', |
| 319 |
colors.red, |
| 320 |
colors.yellow, |
| 321 |
None, |
| 322 |
None, |
| 323 |
1, |
| 324 |
'\x1b[48;5;11m\x1b[38;5;9mtest\x1b[39m\x1b[49m', |
| 325 |
), |
| 326 |
( |
| 327 |
'test', |
| 328 |
colors.red, |
| 329 |
colors.yellow, |
| 330 |
None, |
| 331 |
None, |
| 332 |
1, |
| 333 |
'\x1b[48;5;11m\x1b[38;5;9mtest\x1b[39m\x1b[49m', |
| 334 |
), |
| 335 |
], |
| 336 |
) |
| 337 |
def test_apply_colors( |
| 338 |
text: str, |
| 339 |
fg, |
| 340 |
bg, |
| 341 |
fg_none, |
| 342 |
bg_none, |
| 343 |
percentage: float | None, |
| 344 |
expected, |
| 345 |
monkeypatch, |
| 346 |
) -> None: |
| 347 |
monkeypatch.setattr( |
| 348 |
env, |
| 349 |
'COLOR_SUPPORT', |
| 350 |
env.ColorSupport.XTERM_256, |
| 351 |
) |
| 352 |
assert ( |
| 353 |
apply_colors( |
| 354 |
text, |
| 355 |
fg=fg, |
| 356 |
bg=bg, |
| 357 |
fg_none=fg_none, |
| 358 |
bg_none=bg_none, |
| 359 |
percentage=percentage, |
| 360 |
) |
| 361 |
== expected |
| 362 |
) |
| 363 |
|
| 364 |
|
| 365 |
def test_windows_colors(monkeypatch) -> None: |
| 366 |
monkeypatch.setattr(env, 'COLOR_SUPPORT', env.ColorSupport.WINDOWS) |
| 367 |
assert ( |
| 368 |
apply_colors( |
| 369 |
'test', |
| 370 |
fg=colors.red, |
| 371 |
bg=colors.red, |
| 372 |
fg_none=colors.red, |
| 373 |
bg_none=colors.red, |
| 374 |
percentage=1, |
| 375 |
) |
| 376 |
== 'test' |
| 377 |
) |
| 378 |
colors.red.underline('test') |
| 379 |
|
| 380 |
|
| 381 |
def test_ansi_color(monkeypatch) -> None: |
| 382 |
color = progressbar.terminal.Color( |
| 383 |
colors.red.rgb, |
| 384 |
colors.red.hls, |
| 385 |
'red-ansi', |
| 386 |
None, |
| 387 |
) |
| 388 |
|
| 389 |
for color_support in { |
| 390 |
env.ColorSupport.NONE, |
| 391 |
env.ColorSupport.XTERM, |
| 392 |
env.ColorSupport.XTERM_256, |
| 393 |
env.ColorSupport.XTERM_TRUECOLOR, |
| 394 |
}: |
| 395 |
monkeypatch.setattr( |
| 396 |
env, |
| 397 |
'COLOR_SUPPORT', |
| 398 |
color_support, |
| 399 |
) |
| 400 |
assert color.ansi is not None or color_support == env.ColorSupport.NONE |
| 401 |
|
| 402 |
|
| 403 |
def test_sgr_call() -> None: |
| 404 |
assert progressbar.terminal.encircled('test') == '\x1b[52mtest\x1b[54m' |