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>
161 lines
6.6 KiB
Python
161 lines
6.6 KiB
Python
"""路径配置回归测试:源码不再用 os.path.dirname(__file__) 推根级目录
|
||
|
||
cut_pic 历史 bug 之后,再清查全代码库找出同类型隐患:
|
||
fallback 分支用 dirname(__file__) 推 "pic" / "logs" / "state" 这种
|
||
项目根级目录,会指向错误位置(推到 douyin/core/pic/ 等子目录)。
|
||
|
||
本测试在源代码层面验证:所有可能被运行的关键文件,要么用 path_config,
|
||
要么走相对路径 + ".." 的兜底(不会指错位置)。
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import os
|
||
import re
|
||
|
||
import pytest
|
||
|
||
|
||
REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||
|
||
|
||
def _read(*parts: str) -> str:
|
||
p = os.path.join(REPO_ROOT, *parts)
|
||
with open(p, "r", encoding="utf-8") as f:
|
||
return f.read()
|
||
|
||
|
||
# ============================================================
|
||
# underlying_operations.py
|
||
# ============================================================
|
||
|
||
class TestUnderlyingOperationsPaths:
|
||
"""关键路径变量都通过 path_config 派生,不再裸用 dirname(__file__) 拼根级目录。"""
|
||
|
||
def test_screenshot_log_uses_path_config(self):
|
||
src = _read("douyin", "core", "underlying_operations.py")
|
||
# 必须在某处用 path_config.get_logs_dir
|
||
assert "path_config.get_logs_dir()" in src, (
|
||
"screenshot_calls.jsonl 应通过 path_config.get_logs_dir() 派生"
|
||
)
|
||
|
||
def test_ocr_category_dir_uses_path_config(self):
|
||
"""OCR category 目录与 cut_pic 都用 path_config.get_pic_dir 派生路径。
|
||
|
||
cut_pic 已抽到 underlying_operations_pkg/image_ops.py,
|
||
OCR category 已抽到 underlying_operations_pkg/ocr_pipeline.py。
|
||
在所有 3 个文件至少一处使用 path_config.get_pic_dir() 即视为通过。
|
||
"""
|
||
sources = [
|
||
_read("douyin", "core", "underlying_operations.py"),
|
||
_read("douyin", "core", "underlying_operations_pkg", "image_ops.py"),
|
||
_read("douyin", "core", "underlying_operations_pkg", "ocr_pipeline.py"),
|
||
]
|
||
combined = "\n".join(sources)
|
||
assert "path_config.get_pic_dir()" in combined
|
||
|
||
def test_no_dirname_pic_pattern_anywhere(self):
|
||
"""整个文件不应再出现 `dirname(__file__) ... "pic"` 这种**单步**拼接。
|
||
|
||
允许相对回退(带 "..")的兜底分支。
|
||
"""
|
||
src = _read("douyin", "core", "underlying_operations.py")
|
||
# 简单检查:不能再有 `dirname(__file__) + "/pic"` 字面写法
|
||
assert 'os.path.dirname(__file__) + "/pic"' not in src
|
||
# `os.path.join(os.path.dirname(__file__), "pic"` 也不应出现(纯 pic 单步拼)
|
||
# 兜底 path_config 失败的相对路径含 ".." 是允许的
|
||
bad_pattern = re.compile(
|
||
r"os\.path\.join\(\s*os\.path\.dirname\(__file__\)\s*,\s*\"pic\"\s*[,)]"
|
||
)
|
||
assert not bad_pattern.search(src)
|
||
|
||
|
||
# ============================================================
|
||
# douyin_fudai.py
|
||
# ============================================================
|
||
|
||
class TestDouyinFudaiPaths:
|
||
def test_actions_log_uses_path_config(self):
|
||
src = _read("douyin", "core", "douyin_fudai.py")
|
||
# 修复后用 path_config.get_actions_log_path
|
||
assert "path_config" in src and "get_actions_log_path()" in src
|
||
|
||
def test_reservations_state_path_uses_path_config(self):
|
||
"""reservations.json 必须用 path_config(兜底也保留,但首选 path_config)。"""
|
||
src = _read("douyin", "core", "douyin_fudai.py")
|
||
assert "get_reservations_path()" in src
|
||
|
||
def test_actions_log_no_naive_dirname_pattern(self):
|
||
"""actions.log 兜底分支必须用 ".." 回到项目根,不能再裸拼 'logs/actions.log'。"""
|
||
src = _read("douyin", "core", "douyin_fudai.py")
|
||
# 取所有 actions.log 拼路径的代码块(最近的 3 行内含 dirname(__file__))
|
||
bad_blocks = re.findall(
|
||
r"os\.path\.join\(\s*os\.path\.dirname\(__file__\)\s*,\s*\"logs\"[^)]*\"actions\.log\"[^)]*\)",
|
||
src,
|
||
)
|
||
for block in bad_blocks:
|
||
# 兜底分支允许,但必须含 ".." 表示往上回退
|
||
assert '".."' in block, (
|
||
f"actions.log 路径拼接缺少 '..' 回退:{block!r}"
|
||
)
|
||
|
||
|
||
# ============================================================
|
||
# fudai_detail_room_title.py / read_once.py
|
||
# ============================================================
|
||
|
||
class TestFudaiDetailScreenshotFallbacks:
|
||
"""两个文件的 except 分支都是兜底;修复后用 .. 回到项目根。"""
|
||
|
||
def test_room_title_fallback_uses_relative_root(self):
|
||
src = _read("douyin", "fudai", "core", "fudai_detail_room_title.py")
|
||
# 找所有 dirname/.../pic/screenshot.png 拼接块
|
||
blocks = re.findall(
|
||
r"os\.path\.join\(\s*os\.path\.dirname\(__file__\)[^)]*?\"pic\"[^)]*?\"screenshot\.png\"[^)]*?\)",
|
||
src,
|
||
re.DOTALL,
|
||
)
|
||
# 至少要有一处兜底,且每处都必须含 ".."
|
||
assert blocks, "fudai_detail_room_title.py 缺兜底分支"
|
||
for block in blocks:
|
||
assert '".."' in block, (
|
||
f"fudai_detail_room_title.py 拼 pic/screenshot.png 缺 '..' 回退:{block!r}"
|
||
)
|
||
|
||
def test_read_once_fallback_uses_relative_root(self):
|
||
src = _read("douyin", "fudai", "core", "fudai_detail_orchestrator_pkg", "read_once.py")
|
||
blocks = re.findall(
|
||
r"os\.path\.join\(\s*os\.path\.dirname\(__file__\)[^)]*?\"pic\"[^)]*?\"screenshot\.png\"[^)]*?\)",
|
||
src,
|
||
re.DOTALL,
|
||
)
|
||
assert blocks
|
||
for block in blocks:
|
||
assert '".."' in block
|
||
|
||
|
||
# ============================================================
|
||
# 反向:path_config 仍然把所有路径锚到 PROJECT_ROOT
|
||
# ============================================================
|
||
|
||
class TestPathConfigAnchorsToProjectRoot:
|
||
def test_pic_dir_under_project_root(self):
|
||
from douyin.core import path_config
|
||
|
||
root = os.path.normpath(path_config.get_project_root())
|
||
pic = os.path.normpath(path_config.get_pic_dir())
|
||
# pic 应该是 root/pic,不能更深
|
||
assert pic == os.path.join(root, "pic")
|
||
|
||
def test_logs_dir_under_project_root(self):
|
||
from douyin.core import path_config
|
||
|
||
root = os.path.normpath(path_config.get_project_root())
|
||
logs = os.path.normpath(path_config.get_logs_dir())
|
||
assert logs == os.path.join(root, "logs")
|
||
|
||
def test_actions_log_under_logs(self):
|
||
from douyin.core import path_config
|
||
|
||
actions = os.path.normpath(path_config.get_actions_log_path())
|
||
assert actions.endswith(os.path.join("logs", "actions.log"))
|