fix(paths): 修同类型 5 处路径推导 bug + 加回归测试
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>
This commit is contained in:
1 parent
52b811d0cb
commit
07eee6da52
5 files changed
+225
-20
No files matched your search
@@ -646,9 +646,15 @@ class fudai_analyse:
|
||||
|
||||
start_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
|
||||
try:
|
||||
actions_log_path = os.path.join(
|
||||
os.path.dirname(__file__), "logs", "actions.log"
|
||||
)
|
||||
try:
|
||||
from douyin.core import path_config as _pc
|
||||
actions_log_path = _pc.get_actions_log_path()
|
||||
except Exception:
|
||||
# 兜底:path_config 不可用时退化(不会推到 douyin/core/logs,沿用 PROJECT_ROOT/logs)
|
||||
actions_log_path = os.path.join(
|
||||
os.path.dirname(__file__), "..", "..", "logs", "actions.log"
|
||||
)
|
||||
actions_log_path = os.path.normpath(actions_log_path)
|
||||
log_dir = os.path.dirname(actions_log_path)
|
||||
if log_dir:
|
||||
os.makedirs(log_dir, exist_ok=True)
|
||||
@@ -721,9 +727,15 @@ class fudai_analyse:
|
||||
try:
|
||||
end_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
|
||||
|
||||
actions_log_path = os.path.join(
|
||||
os.path.dirname(__file__), "logs", "actions.log"
|
||||
)
|
||||
try:
|
||||
from douyin.core import path_config as _pc
|
||||
actions_log_path = _pc.get_actions_log_path()
|
||||
except Exception:
|
||||
actions_log_path = os.path.normpath(
|
||||
os.path.join(
|
||||
os.path.dirname(__file__), "..", "..", "logs", "actions.log"
|
||||
)
|
||||
)
|
||||
log_dir = os.path.dirname(actions_log_path)
|
||||
if log_dir:
|
||||
os.makedirs(log_dir, exist_ok=True)
|
||||
@@ -749,9 +761,15 @@ class fudai_analyse:
|
||||
try:
|
||||
end_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
|
||||
|
||||
actions_log_path = os.path.join(
|
||||
os.path.dirname(__file__), "logs", "actions.log"
|
||||
)
|
||||
try:
|
||||
from douyin.core import path_config as _pc
|
||||
actions_log_path = _pc.get_actions_log_path()
|
||||
except Exception:
|
||||
actions_log_path = os.path.normpath(
|
||||
os.path.join(
|
||||
os.path.dirname(__file__), "..", "..", "logs", "actions.log"
|
||||
)
|
||||
)
|
||||
log_dir = os.path.dirname(actions_log_path)
|
||||
if log_dir:
|
||||
os.makedirs(log_dir, exist_ok=True)
|
||||
|
||||
@@ -146,9 +146,16 @@ class underlying_operations:
|
||||
except Exception:
|
||||
self._screenshot_call_cache = []
|
||||
# 日志文件路径(JSON Lines 格式)
|
||||
self._screenshot_log_path = os.path.join(
|
||||
os.path.dirname(__file__), "logs", "screenshot_calls.jsonl"
|
||||
)
|
||||
try:
|
||||
from douyin.core import path_config
|
||||
self._screenshot_log_path = os.path.join(
|
||||
path_config.get_logs_dir(), "screenshot_calls.jsonl"
|
||||
)
|
||||
except Exception:
|
||||
# path_config 不可用时退化到 dirname(保留旧行为兼容)
|
||||
self._screenshot_log_path = os.path.join(
|
||||
os.path.dirname(__file__), "logs", "screenshot_calls.jsonl"
|
||||
)
|
||||
@staticmethod
|
||||
def _adb() -> str:
|
||||
adb = str(os.environ.get("ADB_EXECUTABLE", "adb") or "adb")
|
||||
@@ -1057,8 +1064,8 @@ class underlying_operations:
|
||||
except Exception:
|
||||
category = "others"
|
||||
|
||||
base_dir = os.path.dirname(__file__)
|
||||
pic_base = os.path.join(base_dir, "pic")
|
||||
from douyin.core import path_config
|
||||
pic_base = path_config.get_pic_dir()
|
||||
category_dir = os.path.join(pic_base, "ocr", category)
|
||||
os.makedirs(category_dir, exist_ok=True)
|
||||
|
||||
|
||||
@@ -168,8 +168,18 @@ def read_fudai_detail_once(analyser: Any) -> Tuple[str, Tuple, dict]:
|
||||
|
||||
pic_path = path_config.get_screenshot_path()
|
||||
except Exception:
|
||||
pic_path = os.path.join(
|
||||
os.path.dirname(__file__), "pic", "screenshot.png"
|
||||
# 兜底:导入失败时显式拼到项目根的 pic/,
|
||||
# 不要用 dirname(__file__) 拼"pic"(那样会推到 douyin/fudai/core/fudai_detail_orchestrator_pkg/pic)
|
||||
pic_path = os.path.normpath(
|
||||
os.path.join(
|
||||
os.path.dirname(__file__),
|
||||
"..",
|
||||
"..",
|
||||
"..",
|
||||
"..",
|
||||
"pic",
|
||||
"screenshot.png",
|
||||
)
|
||||
)
|
||||
|
||||
if os.path.exists(pic_path) and os.path.getsize(pic_path) >= 1024:
|
||||
@@ -213,8 +223,18 @@ def read_fudai_detail_once(analyser: Any) -> Tuple[str, Tuple, dict]:
|
||||
|
||||
pic_path = path_config.get_screenshot_path()
|
||||
except Exception:
|
||||
pic_path = os.path.join(
|
||||
os.path.dirname(__file__), "pic", "screenshot.png"
|
||||
# 兜底:导入失败时显式拼到项目根的 pic/,
|
||||
# 不要用 dirname(__file__) 拼"pic"(那样会推到 douyin/fudai/core/fudai_detail_orchestrator_pkg/pic)
|
||||
pic_path = os.path.normpath(
|
||||
os.path.join(
|
||||
os.path.dirname(__file__),
|
||||
"..",
|
||||
"..",
|
||||
"..",
|
||||
"..",
|
||||
"pic",
|
||||
"screenshot.png",
|
||||
)
|
||||
)
|
||||
|
||||
wait_start = time.time()
|
||||
|
||||
@@ -211,8 +211,17 @@ def read_live_room_title_core(
|
||||
|
||||
pic_path = path_config.get_screenshot_path()
|
||||
except Exception:
|
||||
pic_path = os.path.join(
|
||||
os.path.dirname(__file__), "pic", "screenshot.png"
|
||||
# 兜底:导入失败时显式拼到项目根的 pic/,
|
||||
# 不要用 dirname(__file__) 拼"pic"(那样会推到 douyin/fudai/core/pic)
|
||||
pic_path = os.path.normpath(
|
||||
os.path.join(
|
||||
os.path.dirname(__file__),
|
||||
"..",
|
||||
"..",
|
||||
"..",
|
||||
"pic",
|
||||
"screenshot.png",
|
||||
)
|
||||
)
|
||||
|
||||
if os.path.exists(pic_path) and os.path.getsize(pic_path) >= 1024:
|
||||
|
||||
@@ -0,0 +1,151 @@
|
||||
"""路径配置回归测试:源码不再用 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"))
|
||||
Reference in new issue
Block a user