Skip to content

Commit dee551d

Browse files
committed
Tests
1 parent 4d3561d commit dee551d

File tree

2 files changed

+116
-16
lines changed

2 files changed

+116
-16
lines changed

src/manage/install_command.py

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,36 @@ def _restore_site(cmd, state):
528528
LOGGER.verbose("TRACEBACK", exc_info=True)
529529

530530

531+
def _sanitise_install(cmd, install):
532+
"""Prepares install metadata for storing locally.
533+
534+
This includes:
535+
* filtering out disabled shortcuts
536+
* preserving original shortcuts
537+
* sanitising URLs
538+
"""
539+
540+
if "shortcuts" in install:
541+
# This saves our original set of shortcuts, so a later repair operation
542+
# can enable those that were originally disabled.
543+
shortcuts = install.setdefault("__original-shortcuts", install["shortcuts"])
544+
if cmd.enable_shortcut_kinds or cmd.disable_shortcut_kinds:
545+
orig_shortcuts = shortcuts
546+
shortcuts = []
547+
for s in orig_shortcuts:
548+
if cmd.enable_shortcut_kinds and s["kind"] not in cmd.enable_shortcut_kinds:
549+
continue
550+
if cmd.disable_shortcut_kinds and s["kind"] in cmd.disable_shortcut_kinds:
551+
continue
552+
shortcuts.append(s)
553+
install["shortcuts"] = shortcuts
554+
555+
install["url"] = sanitise_url(install["url"])
556+
# If there's a non-empty and non-default source, sanitise it
557+
if install.get("source") and install["source"] != cmd.fallback_source:
558+
install["source"] = sanitise_url(install["source"])
559+
560+
531561
def _install_one(cmd, source, install, *, target=None):
532562
if cmd.repair:
533563
LOGGER.info("Repairing %s.", install['display-name'])
@@ -596,22 +626,7 @@ def _install_one(cmd, source, install, *, target=None):
596626
)
597627
raise
598628

599-
if "shortcuts" in install:
600-
# This saves our original set of shortcuts, so a later repair operation
601-
# can enable those that were originally disabled.
602-
shortcuts = install.setdefault("__original-shortcuts", install["shortcuts"])
603-
if cmd.enable_shortcut_kinds:
604-
shortcuts = [s for s in shortcuts
605-
if s["kind"] in cmd.enable_shortcut_kinds]
606-
if cmd.disable_shortcut_kinds:
607-
shortcuts = [s for s in shortcuts
608-
if s["kind"] not in cmd.disable_shortcut_kinds]
609-
install["shortcuts"] = shortcuts
610-
611-
install["url"] = sanitise_url(install["url"])
612-
# If there's a non-empty and non-default source, sanitise it
613-
if install.get("source") and install["source"] != cmd.fallback_source:
614-
install["source"] = sanitise_url(install["source"])
629+
_sanitise_install(cmd, install)
615630

616631
LOGGER.debug("Write __install__.json to %s", dest)
617632
with open(dest / "__install__.json", "w", encoding="utf-8") as f:

tests/test_install_command.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,3 +328,88 @@ def test_install_from_script(tmp_path, assert_log):
328328
assert_log.skip_until("Installing %s", ["Test 1.1 (32)"]),
329329
("Tag: %s\\\\%s", ["Test", "1.1-32"]),
330330
)
331+
332+
333+
def test_sanitise_install_urls():
334+
class Cmd:
335+
enable_shortcut_kinds = []
336+
disable_shortcut_kinds = []
337+
fallback_source = None
338+
339+
i = {
340+
"url": "http://user:placeholder@example.com/package.zip",
341+
"source": "http://user:placeholder@example.com/index.json",
342+
}
343+
344+
IC._sanitise_install(Cmd, i)
345+
346+
assert i["url"] == "http://example.com/package.zip"
347+
assert i["source"] == "http://example.com/index.json"
348+
349+
350+
def test_sanitise_install_fallback_urls():
351+
class Cmd:
352+
enable_shortcut_kinds = []
353+
disable_shortcut_kinds = []
354+
fallback_source = "http://user:placeholder@example.com/index.json"
355+
356+
i = {
357+
"url": "http://user:placeholder@example.com/package.zip",
358+
"source": "http://user:placeholder@example.com/index.json",
359+
}
360+
361+
IC._sanitise_install(Cmd, i)
362+
363+
assert i["url"] == "http://example.com/package.zip"
364+
assert i["source"] == "http://user:placeholder@example.com/index.json"
365+
366+
367+
def test_sanitise_install_shortcuts():
368+
class Cmd:
369+
enable_shortcut_kinds = []
370+
disable_shortcut_kinds = []
371+
fallback_source = None
372+
373+
i = {
374+
"url": "",
375+
"shortcuts": [dict(kind=a) for a in "abc"],
376+
}
377+
378+
IC._sanitise_install(Cmd, i)
379+
380+
assert [j["kind"] for j in i["shortcuts"]] == ["a", "b", "c"]
381+
assert [j["kind"] for j in i["__original-shortcuts"]] == ["a", "b", "c"]
382+
383+
384+
def test_sanitise_install_shortcuts_disable():
385+
class Cmd:
386+
enable_shortcut_kinds = []
387+
disable_shortcut_kinds = ["b"]
388+
fallback_source = None
389+
390+
i = {
391+
"url": "",
392+
"shortcuts": [dict(kind=a) for a in "abc"],
393+
}
394+
395+
IC._sanitise_install(Cmd, i)
396+
397+
assert [j["kind"] for j in i["shortcuts"]] == ["a", "c"]
398+
assert [j["kind"] for j in i["__original-shortcuts"]] == ["a", "b", "c"]
399+
400+
401+
def test_sanitise_install_shortcuts_enable():
402+
class Cmd:
403+
enable_shortcut_kinds = ["b"]
404+
disable_shortcut_kinds = []
405+
fallback_source = None
406+
407+
i = {
408+
"url": "",
409+
"shortcuts": [dict(kind=a) for a in "abc"],
410+
}
411+
412+
IC._sanitise_install(Cmd, i)
413+
414+
assert [j["kind"] for j in i["shortcuts"]] == ["b"]
415+
assert [j["kind"] for j in i["__original-shortcuts"]] == ["a", "b", "c"]

0 commit comments

Comments
 (0)