新增测试文件: - tests/test_core/test_parser.py - 测试 parse_input/process_content/output_result - tests/test_core/test_markdown_extra.py - 测试 extract_title_content/search_markdown - tests/test_utils/test_encoding_detection.py - 测试编码检测模块 - tests/test_readers/test_html_downloader.py - 测试HTML下载器 修改: - tests/conftest.py - 移除pytest.skip(),所有CLI测试在缺少依赖时直接失败(与HTML测试行为一致)
47 lines
1.4 KiB
Python
47 lines
1.4 KiB
Python
"""测试 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
|