Skip to content

Commit a763c3e

Browse files
committed
Handle some error cases during uninstall purge.
Fixes #288
1 parent 2bdea91 commit a763c3e

File tree

4 files changed

+26
-8
lines changed

4 files changed

+26
-8
lines changed

src/manage/pep514utils.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,18 +224,20 @@ def _is_tag_managed(company_key, tag_name, *, creating=False, allow_warn=True):
224224
def _split_root(root_name):
225225
if not root_name:
226226
LOGGER.verbose("Skipping registry shortcuts as PEP 514 registry root is not set.")
227-
return
227+
return None, None
228228
hive_name, _, name = root_name.partition("\\")
229229
try:
230230
hive = getattr(winreg, hive_name.upper())
231231
except AttributeError:
232-
LOGGER.verbose("Skipping registry shortcuts as %s\\%s is not a valid key", root_name)
233-
return
232+
LOGGER.verbose("Skipping registry shortcuts as %s\\%s is not a valid key", root_name, hive_name)
233+
return None, None
234234
return hive, name
235235

236236

237237
def update_registry(root_name, install, data, warn_for=[]):
238238
hive, name = _split_root(root_name)
239+
if not hive or not name:
240+
return
239241
with winreg.CreateKey(hive, name) as root:
240242
allow_warn = install_matches_any(install, warn_for)
241243
if _is_tag_managed(root, data["Key"], creating=True, allow_warn=allow_warn):
@@ -258,6 +260,8 @@ def update_registry(root_name, install, data, warn_for=[]):
258260
def cleanup_registry(root_name, keep, warn_for=[]):
259261
LOGGER.debug("Cleaning up registry entries")
260262
hive, name = _split_root(root_name)
263+
if not hive or not name:
264+
return
261265
with _reg_open(hive, name, writable=True) as root:
262266
for company_name in list(_iter_keys(root)):
263267
any_left = False

src/manage/uninstall_command.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ def execute(cmd):
106106
_do_purge_global_dir(cmd.global_dir, warn_msg.format("global commands"))
107107
LOGGER.info("Purging all shortcuts")
108108
for _, cleanup in SHORTCUT_HANDLERS.values():
109-
cleanup(cmd, [])
109+
if cleanup:
110+
cleanup(cmd, [])
110111
LOGGER.debug("END uninstall_command.execute")
111112
return
112113

tests/conftest.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,16 @@ def localserver():
148148
p.wait(5)
149149

150150

151+
REG_TEST_ROOT = r"Software\Python\PyManagerTesting"
152+
153+
151154
class FakeConfig:
152155
def __init__(self, global_dir, installs=[]):
153-
self.root = global_dir.parent if global_dir else None
154156
self.global_dir = global_dir
157+
self.root = global_dir.parent if global_dir else None
158+
self.download_dir = self.root / "_cache" if self.root else None
159+
self.start_folder = self.root / "_start" if self.root else None
160+
self.pep514_root = REG_TEST_ROOT
155161
self.confirm = False
156162
self.installs = list(installs)
157163
self.shebang_can_run_anything = True
@@ -175,15 +181,14 @@ def get_install_to_run(self, tag, *, windowed=False):
175181
return [i for i in self.installs
176182
if (not tag or i["tag"] == tag) and (not company or i["company"] == company)][0]
177183

184+
def ask_yn(self, question):
185+
return False if self.confirm else True
178186

179187
@pytest.fixture
180188
def fake_config(tmp_path):
181189
return FakeConfig(tmp_path / "bin")
182190

183191

184-
REG_TEST_ROOT = r"Software\Python\PyManagerTesting"
185-
186-
187192
class RegistryFixture:
188193
def __init__(self, hive, root):
189194
self.hive = hive

tests/test_uninstall_command.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,11 @@ def test_purge_global_dir(monkeypatch, registry, tmp_path):
1717
assert registry.getvalueandkind("", "Path") == (
1818
rf"C:\A;{tmp_path}\X;C:\B;%PTH%;C:\%D%\E", winreg.REG_SZ)
1919
assert not list(tmp_path.iterdir())
20+
21+
22+
def test_null_purge(fake_config):
23+
cmd = fake_config
24+
cmd.args = ["--purge"]
25+
cmd.confirm = False
26+
cmd.purge = True
27+
UC.execute(cmd)

0 commit comments

Comments
 (0)