diff --git a/douyin/core/underlying_operations.py b/douyin/core/underlying_operations.py index fb3626e..8f36d4a 100644 --- a/douyin/core/underlying_operations.py +++ b/douyin/core/underlying_operations.py @@ -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): diff --git a/tests/test_cut_pic_path.py b/tests/test_cut_pic_path.py new file mode 100644 index 0000000..1caa9c8 --- /dev/null +++ b/tests/test_cut_pic_path.py @@ -0,0 +1,89 @@ +"""underlying_operations.cut_pic 路径修复回归测试。 + +cut_pic 历史 bug:使用 os.path.dirname(__file__) + "/pic" 推路径, +误指向 douyin/core/pic/ 而不是真正的 /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 应指向 /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