underlying_operations.cut_pic L474-721 是 248 行图片裁剪管道: - 默认/命名两个分支共享重试 3 次的截图加载验证 - 默认分支裁出 cut.png 或 ocr/<name>.png;命名分支裁出 pic/<target>/<name>.png - 写入 _ocr_coords_cache 供 analyse_pic_word 调试图像引用 - SAVE_CUT_PIC_DEBUG_IMAGES=1 时保存 cut_pic 调试图 抽到 underlying_operations_pkg/image_ops.py: - cut_pic(self, left_up, right_down, target, name) -> bool - _ensure_parent_dir / _validate_coords / _save_cut_debug_images 内部 helper 主类方法替换为 4 行委托。 更新两个旧的源码断言测试以适配抽离: - test_cut_pic_path:从 inspect.getsource(op.cut_pic) 改为读 image_ops.py 文件源码 - test_runtime_path_anchors:把 path_config.get_pic_dir() 检查范围从单一文件 扩展到 underlying_operations.py + image_ops.py + ocr_pipeline.py 三处 1361 passed. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
100 lines
3.8 KiB
Python
100 lines
3.8 KiB
Python
"""underlying_operations.cut_pic 路径修复回归测试。
|
||
|
||
cut_pic 历史 bug:使用 os.path.dirname(__file__) + "/pic" 推路径,
|
||
误指向 douyin/core/pic/ 而不是真正的 <project_root>/pic/,导致
|
||
"截图文件不存在" 错误。修复后改用 path_config.get_pic_dir() 和
|
||
path_config.get_screenshot_path()。
|
||
|
||
本测试不依赖真实截图,只验证 cut_pic 找路径时使用了正确的根目录。
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import inspect
|
||
import os
|
||
|
||
import pytest
|
||
|
||
|
||
def test_cut_pic_uses_path_config_not_local_dirname():
|
||
"""cut_pic 实现源码不能再用 os.path.dirname(__file__) + "/pic"。
|
||
|
||
cut_pic 已抽到 underlying_operations_pkg/image_ops.py。
|
||
主类方法 op.cut_pic 是 delegation wrapper,inspect.getsource 取到的是 wrapper 源码;
|
||
所以这里直接读 image_ops.py 文件源码做断言。
|
||
"""
|
||
import os as _os
|
||
repo_root = _os.path.dirname(_os.path.dirname(_os.path.abspath(__file__)))
|
||
image_ops_path = _os.path.join(
|
||
repo_root, "douyin", "core", "underlying_operations_pkg", "image_ops.py"
|
||
)
|
||
with open(image_ops_path, "r", encoding="utf-8") as f:
|
||
src = f.read()
|
||
|
||
assert 'os.path.dirname(__file__) + "/pic"' not in src, (
|
||
"image_ops.cut_pic 仍在用 dirname(__file__) 推路径,"
|
||
"会指向错误位置,应该用 path_config.get_pic_dir()"
|
||
)
|
||
assert 'os.path.dirname(__file__) + "/pic/' not in src, (
|
||
"image_ops.cut_pic target 分支仍在用 dirname(__file__) 推路径"
|
||
)
|
||
# 至少一处使用 path_config
|
||
assert "path_config.get_pic_dir" in src or "path_config.get_screenshot_path" in src
|
||
|
||
|
||
def test_cut_pic_path_resolves_to_project_root():
|
||
"""走通流程:path_config.get_screenshot_path 与项目根 pic/screenshot.png 一致。"""
|
||
from douyin.core import path_config
|
||
|
||
expected = os.path.join(path_config.get_project_root(), "pic", "screenshot.png")
|
||
assert os.path.normpath(path_config.get_screenshot_path()) == os.path.normpath(
|
||
expected
|
||
), "path_config.get_screenshot_path 应指向 <project_root>/pic/screenshot.png"
|
||
|
||
|
||
def test_cut_pic_pic_dir_is_project_pic():
|
||
from douyin.core import path_config
|
||
|
||
expected = os.path.join(path_config.get_project_root(), "pic")
|
||
assert os.path.normpath(path_config.get_pic_dir()) == os.path.normpath(expected)
|
||
|
||
|
||
def test_cut_pic_returns_false_when_screenshot_missing(tmp_path, monkeypatch):
|
||
"""截图不存在时 cut_pic 重试 max_retries 次后应返回 False / 抛 FileNotFoundError 兜底为 False。"""
|
||
from douyin.core import path_config
|
||
from douyin.core.underlying_operations import underlying_operations
|
||
|
||
# 把 pic 目录指向一个空目录
|
||
fake_pic_dir = tmp_path / "fake_pic"
|
||
fake_pic_dir.mkdir()
|
||
monkeypatch.setattr(path_config, "get_pic_dir", lambda: str(fake_pic_dir))
|
||
monkeypatch.setattr(
|
||
path_config,
|
||
"get_screenshot_path",
|
||
lambda: str(fake_pic_dir / "screenshot.png"),
|
||
)
|
||
|
||
op = underlying_operations()
|
||
result = op.cut_pic((0, 0), (10, 10))
|
||
# 截图不存在 → 应返回 False(不抛到调用方)
|
||
assert result is False
|
||
|
||
|
||
def test_cut_pic_falls_through_three_retries(tmp_path, monkeypatch):
|
||
"""日志里"尝试 3 次"应反映 max_retries=3 的语义。"""
|
||
from douyin.core import path_config
|
||
from douyin.core.underlying_operations import underlying_operations
|
||
|
||
fake_pic_dir = tmp_path / "fake_pic"
|
||
fake_pic_dir.mkdir()
|
||
monkeypatch.setattr(path_config, "get_pic_dir", lambda: str(fake_pic_dir))
|
||
monkeypatch.setattr(
|
||
path_config,
|
||
"get_screenshot_path",
|
||
lambda: str(fake_pic_dir / "screenshot.png"),
|
||
)
|
||
|
||
op = underlying_operations()
|
||
# 不抛异常即可(内部 max_retries=3 + sleep 0.1*3 ≈ 0.6s)
|
||
result = op.cut_pic()
|
||
assert result is False
|