Files
livekit-agents/python/tests/test_cli_logging_encoding.py
T

62 lines
1.8 KiB
Python

from __future__ import annotations
import io
import json
import logging
import sys
import pytest
from livekit.agents.cli import setup_logging
pytestmark = pytest.mark.unit
def test_cli_logging_preserves_all_unicode_on_cp1252_stdout(monkeypatch):
output = io.BytesIO()
stdout = io.TextIOWrapper(output, encoding="cp1252")
root = logging.getLogger()
monkeypatch.setattr(root, "handlers", [])
monkeypatch.setattr(root, "level", logging.NOTSET)
monkeypatch.setattr(sys, "stdout", stdout)
monkeypatch.setattr(logging, "raiseExceptions", False)
setup_logging("info")
logging.getLogger().info("café عاوز 🗣️")
stdout.flush()
record = json.loads(output.getvalue().decode("cp1252"))
assert record["message"] == "café عاوز 🗣️"
def test_cli_logging_setup_preserves_locale_encoded_stdout_for_print(monkeypatch):
output = io.BytesIO()
stdout = io.TextIOWrapper(output, encoding="iso8859-1")
root = logging.getLogger()
monkeypatch.setattr(root, "handlers", [])
monkeypatch.setattr(root, "level", logging.NOTSET)
monkeypatch.setattr(sys, "stdout", stdout)
setup_logging("info")
print("café")
stdout.flush()
assert output.getvalue() == b"caf\xe9\n"
def test_production_cli_logging_keeps_raw_unicode_on_utf8_stdout(monkeypatch):
output = io.BytesIO()
stdout = io.TextIOWrapper(output, encoding="utf-8")
root = logging.getLogger()
monkeypatch.setattr(root, "handlers", [])
monkeypatch.setattr(root, "level", logging.NOTSET)
monkeypatch.setattr(sys, "stdout", stdout)
monkeypatch.setattr(logging, "raiseExceptions", False)
setup_logging("info")
logging.getLogger().info("café")
stdout.flush()
assert "café" in output.getvalue().decode("utf-8")
assert json.loads(output.getvalue())["message"] == "café"