Skip to content

Commit 52e9950

Browse files
authored
Avoid crashing when invalid __install__.json file exists. (#299)
Fixes #293
1 parent 4f10964 commit 52e9950

File tree

6 files changed

+45
-10
lines changed

6 files changed

+45
-10
lines changed

.github/workflows/build.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,20 @@ jobs:
182182
PYMANAGER_DEBUG: true
183183
shell: powershell
184184

185+
- name: 'Test purge'
186+
run: |
187+
$env:PYTHON_MANAGER_CONFIG = (gi $env:PYTHON_MANAGER_CONFIG).FullName
188+
pymanager uninstall --purge -y
189+
if (Test-Path test_installs) {
190+
dir -r test_installs
191+
} else {
192+
Write-Host "test_installs directory has been deleted"
193+
}
194+
env:
195+
PYTHON_MANAGER_INCLUDE_UNMANAGED: false
196+
PYTHON_MANAGER_CONFIG: .\test-config.json
197+
PYMANAGER_DEBUG: true
198+
185199
- name: 'Offline bundle download and install'
186200
run: |
187201
pymanager list --online 3 3-32 3-64 3-arm64

ci/release.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,21 @@ stages:
323323
PYTHON_MANAGER_CONFIG: .\test-config.json
324324
PYMANAGER_DEBUG: true
325325
326+
- powershell: |
327+
$env:PYTHON_MANAGER_CONFIG = (gi $env:PYTHON_MANAGER_CONFIG).FullName
328+
pymanager uninstall --purge -y
329+
if (Test-Path test_installs) {
330+
dir -r test_installs
331+
} else {
332+
Write-Host "test_installs directory has been deleted"
333+
}
334+
displayName: 'Test purge'
335+
timeoutInMinutes: 5
336+
env:
337+
PYTHON_MANAGER_INCLUDE_UNMANAGED: false
338+
PYTHON_MANAGER_CONFIG: .\test-config.json
339+
PYMANAGER_DEBUG: true
340+
326341
- powershell: |
327342
pymanager list --online 3 3-32 3-64 3-arm64
328343
pymanager install --download .\bundle 3 3-32 3-64 3-arm64

src/manage/installs.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ def _get_installs(install_dir):
2323
try:
2424
with p.open() as f:
2525
j = json.load(f)
26+
except ValueError:
27+
LOGGER.warn(
28+
"Failed to read install at %s. You may have a broken "
29+
"install, which can be cleaned up by deleting the directory.",
30+
d
31+
)
32+
LOGGER.debug("ERROR", exc_info=True)
33+
continue
2634
except FileNotFoundError:
2735
continue
2836

src/manage/uninstall_command.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,7 @@ def _do_purge_global_dir(global_dir, warn_msg, *, hive=None, subkey="Environment
6666
if not global_dir.is_dir():
6767
return
6868
LOGGER.info("Purging global commands from %s", global_dir)
69-
for f in _iterdir(global_dir):
70-
LOGGER.debug("Purging %s", f)
71-
rmtree(f, after_5s_warning=warn_msg)
69+
rmtree(global_dir, after_5s_warning=warn_msg)
7270

7371

7472
def execute(cmd):

tests/conftest.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,11 @@ def localserver():
152152

153153

154154
class FakeConfig:
155-
def __init__(self, global_dir, installs=[]):
156-
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
155+
def __init__(self, root, installs=[]):
156+
self.root = self.install_dir = root
157+
self.global_dir = root / "bin" if root else None
158+
self.download_dir = root / "_cache" if root else None
159+
self.start_folder = root / "_start" if root else None
160160
self.pep514_root = REG_TEST_ROOT
161161
self.confirm = False
162162
self.installs = list(installs)
@@ -193,7 +193,7 @@ def ask_yn(self, question):
193193

194194
@pytest.fixture
195195
def fake_config(tmp_path):
196-
return FakeConfig(tmp_path / "bin")
196+
return FakeConfig(tmp_path)
197197

198198

199199
class RegistryFixture:

tests/test_uninstall_command.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def test_purge_global_dir(monkeypatch, registry, tmp_path):
1616
UC._do_purge_global_dir(tmp_path, "SLOW WARNING", hive=registry.hive, subkey=registry.root)
1717
assert registry.getvalueandkind("", "Path") == (
1818
rf"C:\A;{tmp_path}\X;C:\B;%PTH%;C:\%D%\E", winreg.REG_SZ)
19-
assert not list(tmp_path.iterdir())
19+
assert not tmp_path.is_dir() or not list(tmp_path.iterdir())
2020

2121

2222
def test_null_purge(fake_config):

0 commit comments

Comments
 (0)