diff --git a/src/manage/pep514utils.py b/src/manage/pep514utils.py index 91ccb72..1976653 100644 --- a/src/manage/pep514utils.py +++ b/src/manage/pep514utils.py @@ -224,18 +224,20 @@ def _is_tag_managed(company_key, tag_name, *, creating=False, allow_warn=True): def _split_root(root_name): if not root_name: LOGGER.verbose("Skipping registry shortcuts as PEP 514 registry root is not set.") - return + return None, None hive_name, _, name = root_name.partition("\\") try: hive = getattr(winreg, hive_name.upper()) except AttributeError: - LOGGER.verbose("Skipping registry shortcuts as %s\\%s is not a valid key", root_name) - return + LOGGER.verbose("Skipping registry shortcuts as %s\\%s is not a valid key", root_name, hive_name) + return None, None return hive, name def update_registry(root_name, install, data, warn_for=[]): hive, name = _split_root(root_name) + if not hive or not name: + return with winreg.CreateKey(hive, name) as root: allow_warn = install_matches_any(install, warn_for) 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=[]): def cleanup_registry(root_name, keep, warn_for=[]): LOGGER.debug("Cleaning up registry entries") hive, name = _split_root(root_name) + if not hive or not name: + return with _reg_open(hive, name, writable=True) as root: for company_name in list(_iter_keys(root)): any_left = False diff --git a/src/manage/uninstall_command.py b/src/manage/uninstall_command.py index abaebb6..8fefa6c 100644 --- a/src/manage/uninstall_command.py +++ b/src/manage/uninstall_command.py @@ -106,7 +106,8 @@ def execute(cmd): _do_purge_global_dir(cmd.global_dir, warn_msg.format("global commands")) LOGGER.info("Purging all shortcuts") for _, cleanup in SHORTCUT_HANDLERS.values(): - cleanup(cmd, []) + if cleanup: + cleanup(cmd, []) LOGGER.debug("END uninstall_command.execute") return diff --git a/tests/conftest.py b/tests/conftest.py index 9e79973..c4ec097 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -148,10 +148,16 @@ def localserver(): p.wait(5) +REG_TEST_ROOT = r"Software\Python\PyManagerTesting" + + class FakeConfig: def __init__(self, global_dir, installs=[]): - self.root = global_dir.parent if global_dir else None self.global_dir = global_dir + self.root = global_dir.parent if global_dir else None + self.download_dir = self.root / "_cache" if self.root else None + self.start_folder = self.root / "_start" if self.root else None + self.pep514_root = REG_TEST_ROOT self.confirm = False self.installs = list(installs) self.shebang_can_run_anything = True @@ -175,15 +181,14 @@ def get_install_to_run(self, tag, *, windowed=False): return [i for i in self.installs if (not tag or i["tag"] == tag) and (not company or i["company"] == company)][0] + def ask_yn(self, question): + return False if self.confirm else True @pytest.fixture def fake_config(tmp_path): return FakeConfig(tmp_path / "bin") -REG_TEST_ROOT = r"Software\Python\PyManagerTesting" - - class RegistryFixture: def __init__(self, hive, root): self.hive = hive diff --git a/tests/test_uninstall_command.py b/tests/test_uninstall_command.py index 6edb26c..a106d61 100644 --- a/tests/test_uninstall_command.py +++ b/tests/test_uninstall_command.py @@ -17,3 +17,11 @@ def test_purge_global_dir(monkeypatch, registry, tmp_path): assert registry.getvalueandkind("", "Path") == ( rf"C:\A;{tmp_path}\X;C:\B;%PTH%;C:\%D%\E", winreg.REG_SZ) assert not list(tmp_path.iterdir()) + + +def test_null_purge(fake_config): + cmd = fake_config + cmd.args = ["--purge"] + cmd.confirm = False + cmd.purge = True + UC.execute(cmd)