"""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"。 cut_pic 已抽到 underlying_operations_pkg/image_ops.py。 主类方法 op.cut_pic 是 delegation wrapper,inspect.getsource 取到的是 wrapper 源码; 所以这里直接读 image_ops.py 文件源码做断言。 """ import os as _os repo_root = _os.path.dirname(_os.path.dirname(_os.path.abspath(__file__))) image_ops_path = _os.path.join( repo_root, "douyin", "core", "underlying_operations_pkg", "image_ops.py" ) with open(image_ops_path, "r", encoding="utf-8") as f: src = f.read() assert 'os.path.dirname(__file__) + "/pic"' not in src, ( "image_ops.cut_pic 仍在用 dirname(__file__) 推路径," "会指向错误位置,应该用 path_config.get_pic_dir()" ) assert 'os.path.dirname(__file__) + "/pic/' not in src, ( "image_ops.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