Implement support for NO_COLOR/FORCE_COLOR env with isatty and journald checks for autodetection.
This commit is contained in:
+27
-1
@@ -76,8 +76,10 @@ def test_extra_import_paths_ignores_other_python_versions(tmp_path, monkeypatch)
|
||||
assert str(other_site) not in sys.path
|
||||
|
||||
|
||||
def test_cli_snapshot_line_format(tmp_path, capsys):
|
||||
def test_cli_snapshot_line_format(tmp_path, capsys, monkeypatch):
|
||||
"""Snapshot lines are timestamped and colored with metadata."""
|
||||
monkeypatch.setenv("FORCE_COLOR", "1")
|
||||
monkeypatch.delenv("NO_COLOR", raising=False)
|
||||
path = tmp_path / "test.kantadb"
|
||||
ts = datetime(2026, 8, 12, 10, 6, 52, 375398, tzinfo=UTC)
|
||||
mtime = datetime(2026, 8, 12, 9, 0, 0, tzinfo=UTC)
|
||||
@@ -105,6 +107,30 @@ def test_cli_snapshot_line_format(tmp_path, capsys):
|
||||
assert "\x1b[38;5;242m 13 B" in err
|
||||
|
||||
|
||||
def test_cli_strips_ansi_without_color_support(tmp_path, capsys, monkeypatch):
|
||||
"""Without a tty and with NO_COLOR set, output contains no ANSI codes."""
|
||||
monkeypatch.setenv("NO_COLOR", "1")
|
||||
monkeypatch.delenv("FORCE_COLOR", raising=False)
|
||||
path = tmp_path / "test.kantadb"
|
||||
ts = datetime(2026, 8, 12, 10, 6, 52, 375398, tzinfo=UTC)
|
||||
serializer = JsonSerializer()
|
||||
framer = LineFramer()
|
||||
|
||||
snapshot = Snapshot(ts=ts, v=1, m=None, state={"counter": 5})
|
||||
change = ChangeRecord(ts=ts, a="inc", v=1, u="user1", diff={"counter": 6})
|
||||
data = framer.frame_snapshot(
|
||||
serializer.encode(snapshot), record_offset=0
|
||||
) + framer.frame_change(serializer.encode(change), record_offset=0)
|
||||
path.write_bytes(data)
|
||||
|
||||
code = main([str(path)])
|
||||
assert code == 0
|
||||
|
||||
err = capsys.readouterr().err
|
||||
assert "\x1b[" not in err
|
||||
assert "snapshot s0" in err
|
||||
|
||||
|
||||
def test_import_dotted_from_file_path(tmp_path):
|
||||
"""--data can be a filesystem path with an optional colon-separated symbol."""
|
||||
module = tmp_path / "models.py"
|
||||
|
||||
+24
-1
@@ -127,6 +127,25 @@ def test_default_emit_created_and_migrated(capsys):
|
||||
assert "🛢️ x.kantadb migrated v0 -> v1: migrate_v1 (rename)" in err
|
||||
|
||||
|
||||
def test_default_emit_strips_ansi_without_color_support(capsys, monkeypatch):
|
||||
"""NO_COLOR output contains no ANSI codes; FORCE_COLOR keeps them."""
|
||||
logging.getLogger("kanta").handlers.clear()
|
||||
configure_logging()
|
||||
monkeypatch.setenv("NO_COLOR", "1")
|
||||
monkeypatch.delenv("FORCE_COLOR", raising=False)
|
||||
emit_event(_change_event(diff={"counter": 1}))
|
||||
err = capsys.readouterr().err
|
||||
assert "\x1b[" not in err
|
||||
assert "counter" in err
|
||||
|
||||
logging.getLogger("kanta").handlers.clear()
|
||||
configure_logging()
|
||||
monkeypatch.setenv("FORCE_COLOR", "1")
|
||||
monkeypatch.delenv("NO_COLOR", raising=False)
|
||||
emit_event(_change_event(diff={"counter": 1}))
|
||||
assert "\x1b[" in capsys.readouterr().err
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_logemit_receives_transaction_events(tmp_path, format_config):
|
||||
path = tmp_path / "test.db"
|
||||
@@ -242,7 +261,11 @@ async def test_logmigr_failure_does_not_break_open(tmp_path, format_config):
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_aborted_transaction_emits_event(tmp_path, format_config, caplog):
|
||||
async def test_aborted_transaction_emits_event(
|
||||
tmp_path, format_config, caplog, monkeypatch
|
||||
):
|
||||
monkeypatch.setenv("FORCE_COLOR", "1")
|
||||
monkeypatch.delenv("NO_COLOR", raising=False)
|
||||
path = tmp_path / "test.db"
|
||||
kanta = make_kanta(path, Data, format_config)
|
||||
events = []
|
||||
|
||||
@@ -71,7 +71,9 @@ def test_log_change_no_diff(capsys):
|
||||
assert "test" in captured.err
|
||||
|
||||
|
||||
def test_log_change_appends_extra_string(capsys):
|
||||
def test_log_change_appends_extra_string(capsys, monkeypatch):
|
||||
monkeypatch.setenv("FORCE_COLOR", "1")
|
||||
monkeypatch.delenv("NO_COLOR", raising=False)
|
||||
kanta_logger = logging.getLogger("kanta")
|
||||
kanta_logger.handlers.clear()
|
||||
configure_logging()
|
||||
|
||||
Reference in New Issue
Block a user