feat: 导出问题清单 CSV,方便回源盘排查
- 每次备份只要有"改名 / 跳过 / 复制失败"的条目,就在目标目录生成 .udbak-report-<时间>.csv - 列:类型、源相对路径、目标相对路径、处理、原因、大小(字节)、源修改时间;UTF-8 BOM,Excel 直接打开不乱码 - 只保留最近 10 份;清理阶段不会把这些文件当多余文件删掉 - GUI:新增"问题清单"按钮打开最近一份;完成弹窗给出 CSV 路径与整理好的条数 - 新增 4 项 CSV 相关测试(共 48 项)
This commit is contained in:
1 parent
333e7e44e5
commit
ff052ecdda
4 files changed
+188
-14
No files matched your search
@@ -3,6 +3,8 @@
|
||||
|
||||
运行: python -m unittest discover -s tests -p "test_*.py" -v
|
||||
"""
|
||||
import csv
|
||||
import io
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
@@ -190,5 +192,83 @@ class TestSanitize(unittest.TestCase):
|
||||
self.assertEqual(ub.name_problem("ok.txt"), (False, None))
|
||||
|
||||
|
||||
class TestProblemCsv(Base):
|
||||
def _csv_rows(self, path):
|
||||
with io.open(path, encoding="utf-8-sig", newline="") as f:
|
||||
return list(csv.reader(f))
|
||||
|
||||
def test_no_problem_no_csv(self):
|
||||
write(os.path.join(self.src, "ok.txt"), b"ok")
|
||||
ok, stats = self.run_engine()
|
||||
self.assertTrue(ok)
|
||||
self.assertIsNone(stats["report_csv"])
|
||||
self.assertEqual([n for n in os.listdir(self.dst) if n.startswith(ub.REPORT_PREFIX)], [])
|
||||
|
||||
def test_skipped_name_goes_to_csv(self):
|
||||
write(os.path.join(self.src, "good.txt"), b"good")
|
||||
eng = ub.BackupEngine(self.src, self.dst, log_callback=lambda m: None)
|
||||
real_scan = eng._scan
|
||||
|
||||
def scan_with_illegal():
|
||||
files, dirs = real_scan()
|
||||
files.append((os.path.join(self.src, 'bad"name.txt'),
|
||||
'bad"name.txt', 7, 1700000000 * 10 ** 9))
|
||||
return files, dirs
|
||||
|
||||
eng._scan = scan_with_illegal
|
||||
ok, stats = eng.run()
|
||||
self.assertTrue(ok)
|
||||
self.assertTrue(stats["report_csv"])
|
||||
self.assertTrue(os.path.isfile(stats["report_csv"]))
|
||||
rows = self._csv_rows(stats["report_csv"])
|
||||
self.assertEqual(rows[0][:5], ["类型", "源相对路径", "目标相对路径", "处理", "原因"])
|
||||
self.assertEqual(rows[0][5:], ["大小(字节)", "源修改时间"])
|
||||
body = [r for r in rows[1:] if "bad" in r[1]]
|
||||
self.assertEqual(len(body), 1)
|
||||
self.assertEqual(body[0][0], "文件")
|
||||
self.assertEqual(body[0][3], "已跳过")
|
||||
self.assertIn("非法字符", body[0][4])
|
||||
self.assertEqual(body[0][5], "7")
|
||||
self.assertTrue(body[0][6].startswith("2023-11-15"))
|
||||
|
||||
def test_renamed_goes_to_csv(self):
|
||||
write(os.path.join(self.src, "report.bin"), b"data")
|
||||
real_copy = ub.BackupEngine._copy_one
|
||||
|
||||
def failing(self_, src, dst, size, mtime_ns):
|
||||
if os.path.basename(dst) == "report.bin":
|
||||
raise OSError(22, "invalid name")
|
||||
return real_copy(self_, src, dst, size, mtime_ns)
|
||||
|
||||
with mock.patch.object(ub.BackupEngine, "_copy_one", failing), \
|
||||
mock.patch.object(ub, "sanitize_component", lambda n: "report_fixed.bin"):
|
||||
ok, stats = self.run_engine()
|
||||
self.assertTrue(ok)
|
||||
rows = self._csv_rows(stats["report_csv"])
|
||||
body = [r for r in rows[1:] if r[1] == "report.bin"]
|
||||
self.assertEqual(len(body), 1)
|
||||
self.assertEqual(body[0][3], "已改名")
|
||||
self.assertEqual(body[0][2], "report_fixed.bin")
|
||||
|
||||
def test_csv_is_not_cleaned_up(self):
|
||||
write(os.path.join(self.src, "good.txt"), b"good")
|
||||
eng = ub.BackupEngine(self.src, self.dst, log_callback=lambda m: None)
|
||||
real_scan = eng._scan
|
||||
|
||||
def scan_with_illegal():
|
||||
files, dirs = real_scan()
|
||||
files.append((os.path.join(self.src, "x?.txt"), "x?.txt", 1, 0))
|
||||
return files, dirs
|
||||
|
||||
eng._scan = scan_with_illegal
|
||||
ok, stats = eng.run()
|
||||
csv_path = stats["report_csv"]
|
||||
self.assertTrue(os.path.isfile(csv_path))
|
||||
# 再跑一次全量:清理阶段不能把 .udbak-report-*.csv 当"多余文件"
|
||||
ok2, _ = self.run_engine()
|
||||
self.assertTrue(ok2)
|
||||
self.assertTrue(os.path.isfile(csv_path))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main(verbosity=2)
|
||||
Reference in new issue
Block a user