|
| 1 | +"""Regression tests for JSON export progress reporting in CI/automation.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import io |
| 6 | +import logging |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from pykotor.tools.resource_json import ( |
| 11 | + _ExportProgressReporter, |
| 12 | + _format_progress_bar, |
| 13 | + _supports_live_progress, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +class _FakeTty(io.StringIO): |
| 18 | + def isatty(self) -> bool: |
| 19 | + return True |
| 20 | + |
| 21 | + |
| 22 | +def test_format_progress_bar_clamps_percent() -> None: |
| 23 | + assert _format_progress_bar(-5.0) == "-" * len(_format_progress_bar(50.0)) |
| 24 | + assert _format_progress_bar(150.0) == "#" * len(_format_progress_bar(50.0)) |
| 25 | + |
| 26 | + |
| 27 | +@pytest.mark.parametrize("env_name", ["CI", "GITHUB_ACTIONS"]) |
| 28 | +@pytest.mark.parametrize("env_value", ["true", "1", "yes", " TRUE "]) |
| 29 | +def test_supports_live_progress_disabled_in_automation_env( |
| 30 | + monkeypatch: pytest.MonkeyPatch, |
| 31 | + env_name: str, |
| 32 | + env_value: str, |
| 33 | +) -> None: |
| 34 | + monkeypatch.delenv("CI", raising=False) |
| 35 | + monkeypatch.delenv("GITHUB_ACTIONS", raising=False) |
| 36 | + monkeypatch.setenv(env_name, env_value) |
| 37 | + assert _supports_live_progress(_FakeTty()) is False |
| 38 | + |
| 39 | + |
| 40 | +def test_supports_live_progress_enabled_for_tty_outside_ci( |
| 41 | + monkeypatch: pytest.MonkeyPatch, |
| 42 | +) -> None: |
| 43 | + monkeypatch.delenv("CI", raising=False) |
| 44 | + monkeypatch.delenv("GITHUB_ACTIONS", raising=False) |
| 45 | + assert _supports_live_progress(_FakeTty()) is True |
| 46 | + |
| 47 | + |
| 48 | +def test_export_progress_reporter_logs_instead_of_live_updates_in_ci( |
| 49 | + monkeypatch: pytest.MonkeyPatch, |
| 50 | + caplog: pytest.LogCaptureFixture, |
| 51 | +) -> None: |
| 52 | + monkeypatch.setenv("CI", "true") |
| 53 | + caplog.set_level(logging.INFO) |
| 54 | + reporter = _ExportProgressReporter( |
| 55 | + logger=logging.getLogger("test_resource_json_progress"), |
| 56 | + total_resources=10, |
| 57 | + stream=_FakeTty(), |
| 58 | + ) |
| 59 | + |
| 60 | + assert reporter.live_updates is False |
| 61 | + reporter.update(1, "texture.tpc") |
| 62 | + assert caplog.records |
| 63 | + assert "texture.tpc" in caplog.records[0].message |
0 commit comments