Skip to content

Commit 2eec6d2

Browse files
committed
Improved edge case handling and test
1 parent 198a73f commit 2eec6d2

File tree

2 files changed

+61
-26
lines changed

2 files changed

+61
-26
lines changed

src/manage/install_command.py

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -216,34 +216,62 @@ def _calc(prefix, filename, calculate_dest=calculate_dest):
216216

217217

218218
def _create_shortcut_pep514(cmd, install, shortcut):
219-
from .pep514utils import update_registry
220-
update_registry(cmd.pep514_root, install, shortcut, cmd.tags)
219+
try:
220+
from .pep514utils import update_registry
221+
root = cmd.pep514_root
222+
except (ImportError, AttributeError):
223+
LOGGER.debug("Skipping PEP 514 creation.", exc_info=True)
224+
return
225+
update_registry(root, install, shortcut, cmd.tags)
221226

222227

223228
def _cleanup_shortcut_pep514(cmd, install_shortcut_pairs):
224-
from .pep514utils import cleanup_registry
225-
cleanup_registry(cmd.pep514_root, {s["Key"] for i, s in install_shortcut_pairs}, cmd.tags)
229+
try:
230+
from .pep514utils import cleanup_registry
231+
root = cmd.pep514_root
232+
except (ImportError, AttributeError):
233+
LOGGER.debug("Skipping PEP 514 cleanup.", exc_info=True)
234+
return
235+
cleanup_registry(root, {s["Key"] for i, s in install_shortcut_pairs}, getattr(cmd, "tags", None))
226236

227237

228238
def _create_start_shortcut(cmd, install, shortcut):
229-
from .startutils import create_one
230-
create_one(cmd.start_folder, install, shortcut, cmd.tags)
239+
try:
240+
from .startutils import create_one
241+
root = cmd.start_folder
242+
except (ImportError, AttributeError):
243+
LOGGER.debug("Skipping Start shortcut creation.", exc_info=True)
244+
return
245+
create_one(root, install, shortcut, cmd.tags)
231246

232247

233248
def _cleanup_start_shortcut(cmd, install_shortcut_pairs):
234-
from .startutils import cleanup
235-
cleanup(cmd.start_folder, [s for i, s in install_shortcut_pairs], cmd.tags)
249+
try:
250+
from .startutils import cleanup
251+
root = cmd.start_folder
252+
except (ImportError, AttributeError):
253+
LOGGER.debug("Skipping Start shortcut cleanup.", exc_info=True)
254+
return
255+
cleanup(root, [s for i, s in install_shortcut_pairs], getattr(cmd, "tags", None))
236256

237257

238258
def _create_arp_entry(cmd, install, shortcut):
239259
# ARP = Add/Remove Programs
240-
from .arputils import create_one
241-
create_one(install, shortcut, cmd.tags)
260+
try:
261+
from .arputils import create_one
262+
except ImportError:
263+
LOGGER.debug("Skipping ARP entry creation.", exc_info=True)
264+
return
265+
create_one(install, shortcut, getattr(cmd, "tags", None))
242266

243267

244268
def _cleanup_arp_entries(cmd, install_shortcut_pairs):
245-
from .arputils import cleanup
246-
cleanup([i for i, s in install_shortcut_pairs], cmd.tags)
269+
try:
270+
from .arputils import cleanup
271+
except ImportError:
272+
LOGGER.debug("Skipping ARP entry cleanup.", exc_info=True)
273+
return
274+
cleanup([i for i, s in install_shortcut_pairs], getattr(cmd, "tags", None))
247275

248276

249277
def _create_entrypoints(cmd, install, shortcut):
@@ -279,7 +307,7 @@ def update_all_shortcuts(cmd, *, _aliasutils=None):
279307
try:
280308
aliases.extend(_aliasutils.calculate_aliases(cmd, i))
281309
except LookupError:
282-
LOGGER.warn("Failed to process aliases for %s.", i["display-name"])
310+
LOGGER.warn("Failed to process aliases for %s.", i.get("display-name", i["id"]))
283311
LOGGER.debug("TRACEBACK", exc_info=True)
284312
_aliasutils.create_aliases(cmd, aliases)
285313
_aliasutils.cleanup_aliases(cmd, preserve=aliases)
@@ -301,7 +329,8 @@ def update_all_shortcuts(cmd, *, _aliasutils=None):
301329
shortcut_written.setdefault(s["kind"], []).append((i, s))
302330

303331
for k, (_, cleanup) in SHORTCUT_HANDLERS.items():
304-
cleanup(cmd, shortcut_written.get(k, []))
332+
if cleanup:
333+
cleanup(cmd, shortcut_written.get(k, []))
305334

306335

307336
def print_cli_shortcuts(cmd):

tests/test_install_command.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ class Cmd:
176176
def get_installs(self):
177177
return [
178178
{
179+
"id": "test",
179180
"alias": [
180181
{"name": "python3.exe", "target": "p.exe"},
181182
{"name": "pythonw3.exe", "target": "pw.exe", "windowed": 1},
@@ -189,22 +190,27 @@ def get_installs(self):
189190
(prefix / "p.exe").write_bytes(b"")
190191
(prefix / "pw.exe").write_bytes(b"")
191192

192-
written = []
193-
def create_alias(*a):
194-
written.append(a)
195193

196-
monkeypatch.setattr(IC, "SHORTCUT_HANDLERS", {
197-
"site-dirs": (lambda *a: None,) * 2,
198-
})
194+
created = []
199195

200-
IC.update_all_shortcuts(Cmd(), _create_alias=create_alias)
196+
class AliasUtils:
197+
import manage.aliasutils as AU
198+
calculate_aliases = staticmethod(AU.calculate_aliases)
199+
200+
@staticmethod
201+
def create_aliases(cmd, aliases):
202+
created.extend(aliases)
203+
204+
@staticmethod
205+
def cleanup_aliases(cmd, preserve):
206+
pass
207+
208+
IC.update_all_shortcuts(Cmd(), _aliasutils=AliasUtils)
201209

202210
if default:
203211
# Main test: python.exe and pythonw.exe are added in automatically
204-
assert sorted(w[2]["name"] for w in written) == ["python.exe", "python3.exe", "pythonw.exe", "pythonw3.exe"]
212+
assert sorted(a.name for a in created) == ["python", "python3.exe", "pythonw", "pythonw3.exe"]
205213
else:
206-
assert sorted(w[2]["name"] for w in written) == ["python3.exe", "pythonw3.exe"]
214+
assert sorted(a.name for a in created) == ["python3.exe", "pythonw3.exe"]
207215
# Ensure we still only have the two targets
208-
assert set(w[3].name for w in written) == {"p.exe", "pw.exe"}
209-
# Ensure we got an empty set passed in each time
210-
assert [w[4] for w in written] == [set()] * len(written)
216+
assert set(a.target for a in created) == {"p.exe", "pw.exe"}

0 commit comments

Comments
 (0)