"""douyin/core/io_utils 的 pytest 单元测试。 覆盖三个底层 IO 辅助函数: - _verify_png_integrity:PNG 文件头/尾完整性检查 - _safe_replace_file:原子替换(处理 Windows 文件锁) - _ensure_parent_dir:确保父目录存在 """ from __future__ import annotations import os import struct from pathlib import Path import pytest from douyin.core.io_utils import ( _ensure_parent_dir, _safe_replace_file, _verify_png_integrity, ) # ============================================================ # 构造合法 / 不合法 PNG 字节 # ============================================================ PNG_SIGNATURE = b"\x89PNG\r\n\x1a\n" def _make_minimal_png_bytes() -> bytes: """构造一个语法上"勉强合法"的最小 PNG: 8 字节签名 + 1 个空 IHDR 块 + 1 个 IEND 块。 注:_verify_png_integrity 只检查头 8 字节签名 + 末尾 12 字节里有 'IEND', 不做完整 chunk 校验,所以不需要真实 CRC。 """ ihdr_body = b"\x00" * 13 # 13 字节正确长度的 IHDR 数据 ihdr_chunk = struct.pack(">I", 13) + b"IHDR" + ihdr_body + b"\x00" * 4 # 长度 + 类型 + 数据 + 占位 CRC iend_chunk = struct.pack(">I", 0) + b"IEND" + b"\x00" * 4 # 0 字节 IEND return PNG_SIGNATURE + ihdr_chunk + iend_chunk # ============================================================ # _verify_png_integrity # ============================================================ class TestVerifyPngIntegrity: def test_valid_png(self, tmp_path): f = tmp_path / "ok.png" f.write_bytes(_make_minimal_png_bytes()) assert _verify_png_integrity(str(f)) is True def test_missing_file(self, tmp_path): f = tmp_path / "nonexistent.png" assert _verify_png_integrity(str(f)) is False def test_wrong_signature(self, tmp_path): f = tmp_path / "bad.png" f.write_bytes(b"NOTAPNGFILE" + b"\x00" * 100) assert _verify_png_integrity(str(f)) is False def test_truncated_file_no_iend(self, tmp_path): f = tmp_path / "truncated.png" # 有正确签名,但末尾没有 IEND 块 f.write_bytes(PNG_SIGNATURE + b"\x00" * 50) assert _verify_png_integrity(str(f)) is False def test_too_short(self, tmp_path): # 文件长度 < 8 字节签名长度 f = tmp_path / "tiny.png" f.write_bytes(b"\x89PN") assert _verify_png_integrity(str(f)) is False # ============================================================ # _safe_replace_file # ============================================================ class TestSafeReplaceFile: def test_replace_creates_target(self, tmp_path): src = tmp_path / "src.txt" dst = tmp_path / "dst.txt" src.write_text("hello") result = _safe_replace_file(str(src), str(dst)) assert result is True assert dst.read_text() == "hello" # 源文件应被消费(os.replace 的语义:移动而非复制) # 但由于实现可能用 copy + remove 兜底,src 是否还在不强求 def test_replace_existing_target(self, tmp_path): src = tmp_path / "src.txt" dst = tmp_path / "dst.txt" src.write_text("new content") dst.write_text("old content") result = _safe_replace_file(str(src), str(dst)) assert result is True assert dst.read_text() == "new content" def test_missing_source_returns_false(self, tmp_path): # 源文件不存在 → 应返回 False,不抛 src = tmp_path / "nonexistent.txt" dst = tmp_path / "dst.txt" result = _safe_replace_file(str(src), str(dst)) # 不强求 True/False,但绝不能抛异常 assert isinstance(result, bool) # ============================================================ # _ensure_parent_dir # ============================================================ class TestEnsureParentDir: def test_creates_missing_parent(self, tmp_path): target = tmp_path / "a" / "b" / "c" / "file.txt" # 父目录此时不存在 assert not target.parent.exists() _ensure_parent_dir(str(target)) assert target.parent.is_dir() def test_existing_parent_no_error(self, tmp_path): # 父目录已存在 → 应静默通过 target = tmp_path / "file.txt" _ensure_parent_dir(str(target)) assert tmp_path.is_dir() def test_returns_none(self, tmp_path): target = tmp_path / "x" / "file.txt" assert _ensure_parent_dir(str(target)) is None