"""路径配置回归测试:源码不再用 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)。 路径解析逻辑已抽到 fudai_modules/init_helpers.py 的 _resolve_reservations_state_path,所以两个文件任一处都算通过。 """ sources = [ _read("douyin", "core", "douyin_fudai.py"), _read("douyin", "core", "fudai_modules", "init_helpers.py"), ] combined = "\n".join(sources) assert "get_reservations_path()" in combined 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"))