cut_pic 路径 bug 之后再做地毯式排查,发现 5 处类似潜在 bug: fallback 分支用 dirname(__file__) 推根级目录,path_config 不可用时 会指向错误位置(推到 douyin/core/pic 等子目录而非项目根 pic/)。 修复: - underlying_operations.py: screenshot_calls.jsonl 路径 + OCR category 目录 改用 path_config.get_logs_dir() / get_pic_dir() - douyin_fudai.py: 2 处 actions.log 兜底路径 优先 path_config.get_actions_log_path(),兜底加 ".." 回到项目根 - fudai_detail_room_title.py: 截图复用 fallback 路径加 ".." 回退 - fudai_detail_orchestrator_pkg/read_once.py: 同上 2 处 fallback 新增 11 个回归测试覆盖: - 关键文件不再用单步 dirname/__file__/pic 写法 - path_config 仍把所有路径锚到 PROJECT_ROOT - 兜底分支必须含 ".." 表示回退 注:douyin_fudai.py:299 的 reservations.json 因 dirname 恰好等于 PROJECT_ROOT/douyin/core,跟 path_config.get_reservations_path() 一致, 不是 bug,未改动。 1226 passed (+11 新增)。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
152 lines
6.2 KiB
Python
152 lines
6.2 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):
|
||
src = _read("douyin", "core", "underlying_operations.py")
|
||
# 之前是 base_dir = dirname(__file__); pic_base = os.path.join(base_dir, "pic")
|
||
# 修复后必须用 get_pic_dir
|
||
assert "path_config.get_pic_dir()" in src
|
||
|
||
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"))
|