fca6f17a5c
1. 移除对 Python 3.8/3.9 的支持,更新 tox、pyproject.toml 配置 2. 替换 typing 导入为 collections.abc 标准库类型 3. 重构 CLI 系统从 argparse 迁移到 typer 4. 优化代码格式与类型注解,修复多处类型兼容问题 5. 更新依赖声明,移除 graphlib_backport 兼容包
140 lines
5.7 KiB
Python
140 lines
5.7 KiB
Python
"""Tests for cli.screenshot module."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from pyflowx.conditions import Constants
|
|
from pyflowx.ops import screenshot as media
|
|
|
|
|
|
# ---------------------------------------------------------------------- #
|
|
# get_screenshot_path
|
|
# ---------------------------------------------------------------------- #
|
|
class TestGetScreenshotPath:
|
|
"""Test get_screenshot_path function."""
|
|
|
|
def test_get_screenshot_path_with_filename(self, tmp_path: Path) -> None:
|
|
"""Should get screenshot path with filename."""
|
|
with patch.object(Path, "home", return_value=tmp_path):
|
|
result = media.get_screenshot_path("test.png")
|
|
assert result.name == "test.png"
|
|
|
|
def test_get_screenshot_path_without_filename(self, tmp_path: Path) -> None:
|
|
"""Should get screenshot path without filename."""
|
|
with patch.object(Path, "home", return_value=tmp_path):
|
|
result = media.get_screenshot_path()
|
|
assert "screenshot_" in result.name
|
|
assert result.suffix == ".png"
|
|
|
|
|
|
# ---------------------------------------------------------------------- #
|
|
# take_screenshot_full
|
|
# ---------------------------------------------------------------------- #
|
|
class TestTakeScreenshotFull:
|
|
"""Test take_screenshot_full function."""
|
|
|
|
def test_take_screenshot_full_windows(self, tmp_path: Path) -> None:
|
|
"""Should take full screenshot on Windows."""
|
|
with (
|
|
patch.object(Constants, "IS_WINDOWS", True),
|
|
patch.object(Constants, "IS_MACOS", False),
|
|
patch.object(Path, "home", return_value=tmp_path),
|
|
patch("subprocess.run") as mock_run,
|
|
):
|
|
mock_run.return_value = MagicMock(returncode=0)
|
|
media.take_screenshot_full()
|
|
assert mock_run.called
|
|
|
|
def test_take_screenshot_full_macos(self, tmp_path: Path) -> None:
|
|
"""Should take full screenshot on macOS."""
|
|
with (
|
|
patch.object(Constants, "IS_WINDOWS", False),
|
|
patch.object(Constants, "IS_MACOS", True),
|
|
patch.object(Path, "home", return_value=tmp_path),
|
|
patch("subprocess.run") as mock_run,
|
|
):
|
|
mock_run.return_value = MagicMock(returncode=0)
|
|
media.take_screenshot_full()
|
|
assert mock_run.called
|
|
|
|
def test_take_screenshot_full_linux(self, tmp_path: Path) -> None:
|
|
"""Should take full screenshot on Linux."""
|
|
with (
|
|
patch.object(Constants, "IS_WINDOWS", False),
|
|
patch.object(Constants, "IS_MACOS", False),
|
|
patch.object(Path, "home", return_value=tmp_path),
|
|
patch("subprocess.run") as mock_run,
|
|
):
|
|
mock_run.return_value = MagicMock(returncode=0)
|
|
media.take_screenshot_full()
|
|
assert mock_run.called
|
|
|
|
def test_take_screenshot_full_linux_scrot_fallback(self, tmp_path: Path) -> None:
|
|
"""Should fallback to scrot when gnome-screenshot not found."""
|
|
with (
|
|
patch.object(Constants, "IS_WINDOWS", False),
|
|
patch.object(Constants, "IS_MACOS", False),
|
|
patch.object(Path, "home", return_value=tmp_path),
|
|
patch("subprocess.run") as mock_run,
|
|
):
|
|
mock_run.side_effect = [FileNotFoundError(), MagicMock(returncode=0)]
|
|
media.take_screenshot_full()
|
|
assert mock_run.call_count == 2
|
|
|
|
|
|
# ---------------------------------------------------------------------- #
|
|
# take_screenshot_area
|
|
# ---------------------------------------------------------------------- #
|
|
class TestTakeScreenshotArea:
|
|
"""Test take_screenshot_area function."""
|
|
|
|
def test_take_screenshot_area_windows(self, tmp_path: Path) -> None:
|
|
"""Should take area screenshot on Windows."""
|
|
with (
|
|
patch.object(Constants, "IS_WINDOWS", True),
|
|
patch.object(Constants, "IS_MACOS", False),
|
|
patch.object(Path, "home", return_value=tmp_path),
|
|
patch("subprocess.run") as mock_run,
|
|
):
|
|
mock_run.return_value = MagicMock(returncode=0)
|
|
media.take_screenshot_area()
|
|
assert mock_run.called
|
|
|
|
def test_take_screenshot_area_macos(self, tmp_path: Path) -> None:
|
|
"""Should take area screenshot on macOS."""
|
|
with (
|
|
patch.object(Constants, "IS_WINDOWS", False),
|
|
patch.object(Constants, "IS_MACOS", True),
|
|
patch.object(Path, "home", return_value=tmp_path),
|
|
patch("subprocess.run") as mock_run,
|
|
):
|
|
mock_run.return_value = MagicMock(returncode=0)
|
|
media.take_screenshot_area()
|
|
assert mock_run.called
|
|
|
|
def test_take_screenshot_area_linux(self, tmp_path: Path) -> None:
|
|
"""Should take area screenshot on Linux."""
|
|
with (
|
|
patch.object(Constants, "IS_WINDOWS", False),
|
|
patch.object(Constants, "IS_MACOS", False),
|
|
patch.object(Path, "home", return_value=tmp_path),
|
|
patch("subprocess.run") as mock_run,
|
|
):
|
|
mock_run.return_value = MagicMock(returncode=0)
|
|
media.take_screenshot_area()
|
|
assert mock_run.called
|
|
|
|
def test_take_screenshot_area_linux_scrot_fallback(self, tmp_path: Path) -> None:
|
|
"""Should fallback to scrot -s when gnome-screenshot not found."""
|
|
with (
|
|
patch.object(Constants, "IS_WINDOWS", False),
|
|
patch.object(Constants, "IS_MACOS", False),
|
|
patch.object(Path, "home", return_value=tmp_path),
|
|
patch("subprocess.run") as mock_run,
|
|
):
|
|
mock_run.side_effect = [FileNotFoundError(), MagicMock(returncode=0)]
|
|
media.take_screenshot_area()
|
|
assert mock_run.call_count == 2
|