-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify_config_safety.py
More file actions
66 lines (53 loc) · 1.92 KB
/
Copy pathverify_config_safety.py
File metadata and controls
66 lines (53 loc) · 1.92 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
import sys
import os
import time
# Add current dir to path to import config
sys.path.append(os.getcwd())
from config import Config, SETTINGS_PATH
def test_safe_config():
print("--- START Config Safety Test ---")
# Clean state
if SETTINGS_PATH.exists():
try: os.remove(SETTINGS_PATH)
except (OSError, PermissionError): pass
backup_path = SETTINGS_PATH.with_suffix(".json.bak")
if backup_path.exists():
try: os.remove(backup_path)
except (OSError, PermissionError): pass
# 1. Init Config (creates defaults)
conf = Config()
conf.set("ui.theme", "test_theme")
print(f"1. Init & Save: theme={conf.get('ui.theme')}")
# 2. Modify & Save (should create Backup)
conf.set("ui.theme", "new_theme")
print(f"2. Modify & Save: theme={conf.get('ui.theme')}")
if not backup_path.exists():
print("FAIL: No backup created")
return
else:
print("PASS: Backup created")
# 3. Corrupt settings.json
with open(SETTINGS_PATH, "a") as f:
f.write("GARBAGE DATA")
print("3. Corrupted settings.json")
# 4. Reload (Should trigger Recovery)
print("4. Reloading Config...")
conf2 = Config()
val = conf2.get("ui.theme")
# Expect 'test_theme' because backup contains state BEFORE last save
if val == "test_theme":
print(f"PASS: Recovered value '{val}' from backup")
elif val == "new_theme":
print(f"WARN: Recovered CURRENT value (backup overwrote file?): {val}")
elif val == "light":
print(f"FAIL: Reset to defaults (Recovery failed)")
else:
print(f"ERROR: Unexpected value '{val}'")
# Cleanup
try:
if SETTINGS_PATH.exists(): os.remove(SETTINGS_PATH)
if backup_path.exists(): os.remove(backup_path)
except (OSError, PermissionError): pass
print("--- END Config Safety Test ---")
if __name__ == "__main__":
test_safe_config()