Skip to content

Commit 81808f3

Browse files
committed
More test coverage
1 parent 36118d8 commit 81808f3

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

src/manage/aliasutils.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -248,17 +248,21 @@ def _scan(prefix, dirs):
248248
yield from _scan_one(root)
249249

250250

251-
def scan_and_create_entrypoints(cmd, install, shortcut, _create_alias=create_alias):
251+
def scan_and_create_entrypoints(cmd, install, shortcut, _create_alias=create_alias, _scan=_scan):
252252
prefix = install["prefix"]
253253
known = cmd.scratch.setdefault("entrypointutils.known", set())
254254

255255
aliases = list(install.get("alias", ()))
256256
alias_1 = [a for a in aliases if not a.get("windowed")]
257-
alias_2 = [a for a in aliases if a.get("windowed")]
258-
259257
# If no windowed targets, we'll use the non-windowed one
260-
targets = [prefix / a["target"] for a in [*alias_1[:1], *alias_2[:1], *alias_1[:1]]]
261-
if len(targets) < 2:
258+
alias_2 = [a for a in aliases if a.get("windowed")] or alias_1
259+
260+
targets = [
261+
(prefix / alias_1[0]["target"]) if alias_1 else None,
262+
(prefix / alias_2[0]["target"]) if alias_2 else None,
263+
]
264+
265+
if not any(targets):
262266
LOGGER.debug("No suitable alias found for %s. Skipping entrypoints",
263267
install["id"])
264268
return
@@ -271,8 +275,12 @@ def scan_and_create_entrypoints(cmd, install, shortcut, _create_alias=create_ali
271275
known.add(n)
272276

273277
# Copy the launcher template and create a standard __target__ file
274-
_create_alias(cmd, install, alias, targets[alias.get("windowed", 0)],
275-
script_code=code)
278+
target = targets[1 if alias.get("windowed", 0) else 0]
279+
if not target:
280+
LOGGER.debug("No suitable alias found for %s. Skipping this " +
281+
"entrypoint", alias["name"])
282+
continue
283+
_create_alias(cmd, install, alias, target, script_code=code)
276284

277285

278286
def cleanup_entrypoints(cmd, install_shortcut_pairs):

tests/test_alias.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,46 @@ def test_scan_create_entrypoints(fake_config, tmp_path):
279279
assert "from a import main" in expect["script_code"]
280280

281281

282+
@pytest.mark.parametrize("alias_set", ["none", "one", "onew", "two"])
283+
def test_scan_create_entrypoints_with_alias(fake_config, tmp_path, alias_set):
284+
# In this test, we fake the scan, but vary the set of aliases associated
285+
# with the installed runtime.
286+
# If there are no aliases, we shouldn't create any entrypoints.
287+
# If we have a non-windowed alias, we'll use that for both.
288+
# If we have a windowed alias, we'll only create windowed entrypoints.
289+
# If we have both, we'll use the appropriate one
290+
291+
def fake_scan(*a):
292+
return [(dict(name="a", windowed=0), "CODE"),
293+
(dict(name="aw", windowed=1), "CODE")]
294+
295+
alias = {
296+
"none": [],
297+
"one": [dict(target="test.exe", windowed=0)],
298+
"onew": [dict(target="testw.exe", windowed=1)],
299+
"two": [dict(target="test.exe", windowed=0),
300+
dict(target="testw.exe", windowed=1)],
301+
}[alias_set]
302+
303+
expect = {
304+
"none": [],
305+
"one": [("a", "test.exe"), ("aw", "test.exe")],
306+
"onew": [("aw", "testw.exe")],
307+
"two": [("a", "test.exe"), ("aw", "testw.exe")],
308+
}[alias_set]
309+
310+
created = []
311+
AU.scan_and_create_entrypoints(
312+
fake_config,
313+
dict(prefix=fake_config.root, id="test", alias=alias),
314+
{},
315+
_create_alias=lambda *a, **kw: created.append((a, kw)),
316+
_scan=fake_scan,
317+
)
318+
names = [(c[0][2]["name"], c[0][3].name) for c in created]
319+
assert names == expect
320+
321+
282322
def test_scan_entrypoints(tmp_path):
283323
site = tmp_path / "site"
284324
A = site / "a.dist-info"

0 commit comments

Comments
 (0)