Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion python/src/magika/cli/magika_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ def main(
"image": colors.YELLOW,
"video": colors.YELLOW,
"code": colors.LIGHT_BLUE,
"text": colors.DARK_GRAY,
"unknown": colors.DARK_GRAY,
}

# updated only when we need to output in JSON format
Expand Down Expand Up @@ -309,7 +311,7 @@ def main(

if with_colors:
start_color = color_by_group.get(
result.prediction.output.group, colors.WHITE
result.prediction.output.group, colors.DARK_GRAY
)
end_color = colors.RESET
else:
Expand Down
43 changes: 43 additions & 0 deletions python/tests/test_python_magika_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import subprocess
from pathlib import Path

from magika import colors


def test_python_magika_client() -> None:
python_root_dir = Path(__file__).parent.parent
Expand All @@ -29,3 +31,44 @@ def test_python_magika_client() -> None:
# quick test to check there are no crashes
cmd = [str(python_magika_client_path), str(python_magika_client_path)]
subprocess.run(cmd, capture_output=True, check=True)


def test_colored_output_visible_on_light_background_terminals(
tmp_path: Path,
) -> None:
"""Regression test for https://github.com/google/magika/issues/1243.

The fallback color for content type groups not explicitly mapped in
color_by_group must not be bright white (\033[1;37m), which is nearly
invisible on light-background terminals. DARK_GRAY (\033[1;30m) is
visible on both dark and light backgrounds.
"""
python_root_dir = Path(__file__).parent.parent
python_magika_client_path = (
python_root_dir / "src" / "magika" / "cli" / "magika_client.py"
).resolve()

# Write a plain text file — classified as group="text", which previously
# fell through to the bright-white fallback color.
test_file = tmp_path / "test.txt"
test_file.write_text("Hello, world!\n")

result = subprocess.run(
[str(python_magika_client_path), "--colors", str(test_file)],
capture_output=True,
text=True,
check=True,
)
output = result.stdout
# The output must not contain the bright-white ANSI code, which is
# invisible on white/light-background terminals.
assert colors.WHITE not in output, (
f"Output used bright-white ({repr(colors.WHITE)}), which is "
"invisible on light-background terminals. Use DARK_GRAY instead."
)
# DARK_GRAY must be present so that content types in the "text" group
# (and any other unmapped group) are readable on light terminals.
assert colors.DARK_GRAY in output, (
f"Expected DARK_GRAY ({repr(colors.DARK_GRAY)}) in colored output "
"for a plain-text file, but it was not found."
)