fix(cut_pic): 修截图路径错位 — pic 目录从 douyin/core/pic 改回项目根 pic

cut_pic 历史 bug:用 os.path.dirname(__file__) + "/pic" 推路径,
误指向 douyin/core/pic/(不存在),实际截图保存在 <project_root>/pic/。
日志中频繁出现"截图文件不存在: .../douyin/core/pic/screenshot.png"。

改为:
- 默认分支用 path_config.get_pic_dir() / get_screenshot_path()
- target 分支用 os.path.join(path_config.get_pic_dir(), target)

补 5 个回归测试覆盖:
- 源码不再含旧的 dirname-based 路径
- path_config 路径解析到 <project_root>/pic
- 截图缺失时 cut_pic 返回 False(重试 3 次后兜底)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
CodexandClaude Opus 4.7 committed 2026-05-23 00:47:02 +08:00
1 parent fb82213d53
commit 4ab4504425
2 files changed
+95 -4

No files matched your search

+6 -4
View File
@@ -479,8 +479,9 @@ class underlying_operations:
for attempt in range(max_retries):
try:
if target == "" or target == False:
path = os.path.dirname(__file__) + "/pic"
pic1_path = path + "/screenshot.png"
from douyin.core import path_config
path = path_config.get_pic_dir()
pic1_path = path_config.get_screenshot_path()
# 检查文件是否存在
if not os.path.exists(pic1_path):
@@ -601,8 +602,9 @@ class underlying_operations:
return True
else:
path_target = os.path.dirname(__file__) + "/pic/" + target
pic1_path = path_target + "/screenshot.png"
from douyin.core import path_config
path_target = os.path.join(path_config.get_pic_dir(), target)
pic1_path = os.path.join(path_target, "screenshot.png")
# 检查文件是否存在
if not os.path.exists(pic1_path):
+89
View File
@@ -0,0 +1,89 @@
"""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"。"""
from douyin.core.underlying_operations import underlying_operations
op = underlying_operations()
src = inspect.getsource(op.cut_pic)
assert 'os.path.dirname(__file__) + "/pic"' not in src, (
"cut_pic 仍在用 dirname(__file__) 推路径,"
"会指向 douyin/core/pic/(不存在),应该用 path_config.get_pic_dir()"
)
assert 'os.path.dirname(__file__) + "/pic/' not in src, (
"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