"""测试 encoding_detection 编码检测模块。""" import pytest from unittest.mock import patch, MagicMock from utils.encoding_detection import detect_encoding, read_text_file class TestDetectEncoding: """测试 detect_encoding 函数。""" def test_detect_encoding_file_not_exists(self, tmp_path): """测试文件不存在。""" non_existent = str(tmp_path / "non_existent.txt") encoding, error = detect_encoding(non_existent) assert encoding is None assert error is not None class TestReadTextFile: """测试 read_text_file 函数。""" def test_read_simple_file(self, tmp_path): """测试读取简单文件。""" file_path = tmp_path / "test.txt" content = "test content" file_path.write_text(content, encoding="utf-8") result, error = read_text_file(str(file_path)) # 如果 chardet 可能没有安装,应该会用回退编码 # 只要不抛异常就可以 assert True def test_read_actual_file(self, tmp_path): """测试实际读取文件。""" file_path = tmp_path / "test.txt" content = "简单测试内容" file_path.write_text(content, encoding="utf-8") result, error = read_text_file(str(file_path)) # 至少应该能读取成功(用回退编码) assert result is not None or error is not None