-
Notifications
You must be signed in to change notification settings - Fork 416
Expand file tree
/
Copy pathtest_cleanup_manifest.py
More file actions
76 lines (61 loc) · 2.9 KB
/
Copy pathtest_cleanup_manifest.py
File metadata and controls
76 lines (61 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import json
import tempfile
import unittest
from pathlib import Path
from utils.cleanupManifest import cleanup_result_summary, delete_path_for_manifest, write_cleanup_manifest
def write_file(path, content=b"x"):
path.parent.mkdir(parents=True, exist_ok=True)
path.write_bytes(content)
return path
class CleanupManifestTest(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.root = Path(self.tmp.name)
def tearDown(self):
self.tmp.cleanup()
def test_delete_records_success_skips_and_failures(self):
ok_file = write_file(self.root / "old-video.mp4", b"video")
protected_file = write_file(self.root / "message.db", b"sqlite")
busy_file = write_file(self.root / "busy.tmp", b"busy")
trashed = []
def fake_trash(path):
if Path(path).name == "busy.tmp":
raise RuntimeError("file is busy")
trashed.append(path)
records = [
delete_path_for_manifest(str(ok_file), "file", direct_delete=False, trash_func=fake_trash),
delete_path_for_manifest(str(protected_file), "file", direct_delete=False, trash_func=fake_trash),
delete_path_for_manifest(str(busy_file), "file", direct_delete=False, trash_func=fake_trash),
]
result = cleanup_result_summary(records, direct_delete=False)
by_name = {Path(row["path"]).name: row for row in result["records"]}
self.assertEqual(by_name["old-video.mp4"]["status"], "trashed")
self.assertEqual(by_name["message.db"]["status"], "skipped")
self.assertEqual(by_name["message.db"]["reason"], "protected_extension")
self.assertEqual(by_name["busy.tmp"]["status"], "failed")
self.assertEqual(by_name["busy.tmp"]["error"], "file is busy")
self.assertEqual(result["processed_count"], 1)
self.assertEqual(result["skipped_count"], 1)
self.assertEqual(result["failed_count"], 1)
self.assertEqual([Path(path).name for path in trashed], ["old-video.mp4"])
def test_write_cleanup_manifest_persists_json_summary(self):
records = [
{
"path": "/tmp/old-video.mp4",
"type": "file",
"action": "trash",
"status": "trashed",
"size_bytes": 5,
"size": "5 B",
"error": "",
}
]
result = cleanup_result_summary(records, direct_delete=False)
manifest_path = write_cleanup_manifest(result, self.root / "cleanup_manifests")
payload = json.loads(Path(manifest_path).read_text(encoding="utf-8"))
self.assertEqual(payload["schema_version"], 1)
self.assertEqual(payload["action"], "trash")
self.assertEqual(payload["processed_count"], 1)
self.assertEqual(payload["records"][0]["path"], "/tmp/old-video.mp4")
if __name__ == "__main__":
unittest.main()