fix: 文件被占用或磁盘不足时改为中文提示

This commit is contained in:
2026-10-25 07:20:39 +08:00
parent 72b7c05fb8
commit 22733f221c
2 changed files with 63 additions and 5 deletions
+34 -1
View File
@@ -4,7 +4,8 @@ import os
import threading
from pathlib import Path
from app.features.download.journal import CsvJournal
from app.features.download.journal import CsvJournal, explain_os_error
from app.platform.client import NqiError
def test_progress_recovers_by_streaming_and_truncates_only_a_bad_tail(tmp_path: Path, monkeypatch):
@@ -103,6 +104,38 @@ def test_commit_many_is_thread_safe(tmp_path: Path):
assert {row[0] for row in rows[1:]} == {"甲", "乙"}
def test_file_errors_use_chinese(tmp_path: Path, monkeypatch):
busy = OSError("busy")
busy.winerror = 32
locked = OSError("locked")
locked.winerror = 33
full = OSError("full")
full.winerror = 112
full_errno = OSError("full")
full_errno.errno = 28
assert explain_os_error(busy) == "文件被占用,请先关闭 Excel 等程序"
assert explain_os_error(locked) == "文件被占用,请先关闭 Excel 等程序"
assert explain_os_error(full) == "磁盘空间不足"
assert explain_os_error(full_errno) == "磁盘空间不足"
journal = CsvJournal(tmp_path / "out.csv", tmp_path / "progress.jsonl", ["栅格"])
journal.open()
def deny(_fd):
error = PermissionError(13, "busy")
error.winerror = 32
raise error
monkeypatch.setattr("app.features.download.journal.os.fsync", deny)
try:
journal.write_rows("1", [["1"]])
except NqiError as exc:
assert str(exc) == "文件被占用,请先关闭 Excel 等程序"
else:
raise AssertionError("占用文件时应给出中文")
journal.close()
def _patch_fsync(monkeypatch):
calls = []
original = os.fsync