55from pathlib import Path
66
77from manage import uninstall_command as UC
8+ from manage .exceptions import FilesInUseError
89
910
1011def test_purge_global_dir (monkeypatch , registry , tmp_path ):
@@ -25,3 +26,55 @@ def test_null_purge(fake_config):
2526 cmd .confirm = False
2627 cmd .purge = True
2728 UC .execute (cmd )
29+
30+
31+ def test_purge_unknown_files (fake_config ):
32+ cmd = fake_config
33+ cmd .args = ["--purge" ]
34+ cmd .confirm = False
35+ cmd .purge = True
36+
37+ unknown_file = cmd .install_dir / "unknown.txt"
38+ unknown_file .write_bytes (b"unknown" )
39+ broken_runtime = cmd .install_dir / "broken-runtime"
40+ broken_runtime .mkdir ()
41+ (broken_runtime / "__install__.json" ).write_text ("invalid" )
42+
43+ UC .execute (cmd )
44+
45+ assert not unknown_file .exists ()
46+ assert not broken_runtime .exists ()
47+
48+
49+ def test_purge_preserves_runtime_in_use (fake_config , monkeypatch ):
50+ cmd = fake_config
51+ cmd .args = ["--purge" ]
52+ cmd .confirm = False
53+ cmd .purge = True
54+
55+ runtime = cmd .install_dir / "runtime"
56+ runtime .mkdir ()
57+ executable = runtime / "python.exe"
58+ executable .write_bytes (b"in use" )
59+ cmd .installs = [{
60+ "display-name" : "Runtime in use" ,
61+ "prefix" : runtime ,
62+ }]
63+
64+ unknown_dir = cmd .install_dir / "unknown"
65+ unknown_dir .mkdir ()
66+ (unknown_dir / "file.txt" ).write_bytes (b"unknown" )
67+
68+ rmtree = UC .rmtree
69+
70+ def locked_rmtree (path , * args , ** kwargs ):
71+ if Path (path ) == runtime :
72+ raise FilesInUseError ([executable ])
73+ return rmtree (path , * args , ** kwargs )
74+
75+ monkeypatch .setattr (UC , "rmtree" , locked_rmtree )
76+
77+ UC .execute (cmd )
78+
79+ assert executable .is_file ()
80+ assert not unknown_dir .exists ()
0 commit comments