Files
agent-radar 1a3a4ccde1
Creator App Tests / PR Spam Gate (push) Skipped
Frontend Tests / PR Spam Gate (push) Skipped
NPM Format (website & console) / PR Spam Gate (push) Skipped
Pre-commit Checks / PR Spam Gate (push) Skipped
QwenPaw Creator Plugin Release / check-version (push) Canceled after 0s
QwenPaw Creator Plugin Release / upload-oss (push) Canceled after 0s
Tests / PR Spam Gate (push) Skipped
CodeQL / CodeQL (python) (push) Canceled after 0s
CodeQL / CodeQL (typescript) (push) Canceled after 0s
Creator App Tests / Creator Backend Tests (push) Canceled after 0s
Creator App Tests / Creator UI Build & Tests (push) Canceled after 0s
E2E Smoke Tests / Playwright UI Smoke (push) Canceled after 0s
Frontend Tests / Detect console changes (push) Canceled after 0s
NPM Format (website & console) / website format check (push) Canceled after 0s
NPM Format (website & console) / console format check (push) Canceled after 0s
Pre-commit Checks / Pre-commit (push) Canceled after 0s
QwenPaw Data Plugin Release / check-version (push) Canceled after 0s
Tests / Detect code changes (push) Canceled after 0s
Frontend Tests / Vitest Unit Tests (push) Canceled after 0s
Frontend Tests / Frontend Summary (push) Canceled after 0s
QwenPaw Data Plugin Release / upload-oss (push) Canceled after 0s
Tests / Maintainer Approval (push) Canceled after 0s
Tests / Unit Tests - py3.11 - macos-latest (push) Canceled after 0s
Tests / Unit Tests - py3.11 - ubuntu-latest (push) Canceled after 0s
Tests / Unit Tests - py3.13 - ubuntu-latest (push) Canceled after 0s
Tests / Unit Tests - py3.11 - windows-latest (push) Canceled after 0s
Tests / Contract Tests - py3.11 - macos-latest (push) Canceled after 0s
Tests / Contract Tests - py3.11 - ubuntu-latest (push) Canceled after 0s
Tests / Contract Tests - py3.13 - ubuntu-latest (push) Canceled after 0s
Tests / Contract Tests - py3.11 - windows-latest (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - macos-latest - p0 (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - macos-latest - p1 (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - macos-latest - p2 (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - ubuntu-latest - fallback (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - ubuntu-latest - p0 (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - ubuntu-latest - p1 (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - ubuntu-latest - p2 (push) Canceled after 0s
Tests / Integrated Tests - py3.13 - ubuntu-latest - p0 (push) Canceled after 0s
Tests / Integrated Tests - py3.13 - ubuntu-latest - p1 (push) Canceled after 0s
Tests / Integrated Tests - py3.13 - ubuntu-latest - p2 (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - windows-latest - p0 (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - windows-latest - p1 (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - windows-latest - p2 (push) Canceled after 0s
Tests / Coverage Report (push) Canceled after 0s
Tests / Test Summary (push) Canceled after 0s
源码快照 @ 2026-09-01 (source-only, 剔除非源码素材)
2026-09-01 20:01:50 +08:00

166 lines
4.4 KiB
Python

# -*- coding: utf-8 -*-
# pylint: disable=protected-access
from __future__ import annotations
from unittest.mock import MagicMock
import pytest
import qwenpaw.utils.stdio as stdio_module
from qwenpaw.utils.stdio import (
_close_fallback_streams,
_is_stream_usable,
ensure_standard_streams,
)
@pytest.fixture(autouse=True)
def _reset_stdio_state():
"""Reset module-level state between tests."""
stdio_module._FALLBACK_STREAMS.clear()
stdio_module._FALLBACK_STREAMS_BY_ENCODING.clear()
stdio_module._FALLBACK_CLEANUP_REGISTERED = False
yield
stdio_module._FALLBACK_STREAMS.clear()
stdio_module._FALLBACK_STREAMS_BY_ENCODING.clear()
# ---------------------------------------------------------------------------
# _is_stream_usable
# ---------------------------------------------------------------------------
def test_is_stream_usable_none() -> None:
assert _is_stream_usable(None) is False
def test_is_stream_usable_valid() -> None:
stream = MagicMock()
stream.flush.return_value = None
stream.write.return_value = 0
assert _is_stream_usable(stream) is True
def test_is_stream_usable_flush_raises() -> None:
stream = MagicMock()
stream.flush.side_effect = OSError("broken")
assert _is_stream_usable(stream) is False
def test_is_stream_usable_write_raises() -> None:
stream = MagicMock()
stream.flush.return_value = None
stream.write.side_effect = ValueError("closed")
assert _is_stream_usable(stream) is False
def test_is_stream_usable_no_flush_attr() -> None:
stream = MagicMock(spec=[])
assert _is_stream_usable(stream) is False
# ---------------------------------------------------------------------------
# ensure_standard_streams
# ---------------------------------------------------------------------------
def test_ensure_standard_streams_keeps_good_streams(
monkeypatch: pytest.MonkeyPatch,
) -> None:
good_stdout = MagicMock()
good_stdout.flush.return_value = None
good_stdout.write.return_value = 0
good_stderr = MagicMock()
good_stderr.flush.return_value = None
good_stderr.write.return_value = 0
import sys
monkeypatch.setattr(sys, "stdout", good_stdout)
monkeypatch.setattr(sys, "stderr", good_stderr)
ensure_standard_streams()
assert sys.stdout is good_stdout
assert sys.stderr is good_stderr
def test_ensure_standard_streams_replaces_broken(
monkeypatch: pytest.MonkeyPatch,
) -> None:
broken = MagicMock()
broken.flush.side_effect = OSError("broken pipe")
broken.encoding = "utf-8"
import sys
monkeypatch.setattr(sys, "stdout", broken)
monkeypatch.setattr(sys, "stderr", broken)
ensure_standard_streams()
assert sys.stdout is not broken
assert sys.stderr is not broken
assert hasattr(sys.stdout, "write")
def test_ensure_standard_streams_replaces_none(
monkeypatch: pytest.MonkeyPatch,
) -> None:
import sys
monkeypatch.setattr(sys, "stdout", None)
monkeypatch.setattr(sys, "stderr", None)
ensure_standard_streams()
assert sys.stdout is not None
assert sys.stderr is not None
# ---------------------------------------------------------------------------
# fallback caching and cleanup
# ---------------------------------------------------------------------------
def test_fallback_stream_cached_by_encoding(
monkeypatch: pytest.MonkeyPatch,
) -> None:
broken1 = MagicMock()
broken1.flush.side_effect = OSError()
broken1.encoding = "utf-8"
broken2 = MagicMock()
broken2.flush.side_effect = OSError()
broken2.encoding = "utf-8"
import sys
monkeypatch.setattr(sys, "stdout", broken1)
monkeypatch.setattr(sys, "stderr", broken2)
ensure_standard_streams()
assert sys.stdout is sys.stderr
def test_close_fallback_streams_cleans_up() -> None:
mock_stream = MagicMock()
stdio_module._FALLBACK_STREAMS.append(mock_stream)
stdio_module._FALLBACK_STREAMS_BY_ENCODING["utf-8"] = mock_stream
_close_fallback_streams()
mock_stream.close.assert_called_once()
assert len(stdio_module._FALLBACK_STREAMS) == 0
assert len(stdio_module._FALLBACK_STREAMS_BY_ENCODING) == 0
def test_close_fallback_streams_ignores_os_error() -> None:
mock_stream = MagicMock()
mock_stream.close.side_effect = OSError("can't close")
stdio_module._FALLBACK_STREAMS.append(mock_stream)
_close_fallback_streams()
assert len(stdio_module._FALLBACK_STREAMS) == 0