Skip to content

Commit

Permalink
Handle missing dir skip in dynlib tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ncoghlan committed Nov 5, 2024
1 parent bfa2012 commit 1acc15b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/venvstacks/_injected/postinstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ def generate_pyvenv_cfg(base_python_path: Path, py_version: str) -> str:


def generate_sitecustomize(
pylib_paths: Sequence[Path], dynlib_paths: Sequence[Path]
pylib_paths: Sequence[Path],
dynlib_paths: Sequence[Path],
*,
skip_missing_dynlib_paths: bool = True,
) -> str | None:
"""Generate `sitecustomize.py` contents for given linked environment directories"""
sc_contents = [_SITE_CUSTOMIZE_HEADER]
Expand All @@ -119,10 +122,13 @@ def generate_sitecustomize(
"from os import add_dll_directory",
]
for dynlib_path in dynlib_paths:
if not dynlib_path.exists():
if skip_missing_dynlib_paths and not dynlib_path.exists():
# Nothing added DLLs to this folder at build time, so skip it
continue
dynlib_contents.append(f"add_dll_directory({str(dynlib_path)!r})")
# (add_dll_directory fails if the specified folder doesn't exist)
dynlib_entry = f"# Skipping {str(dynlib_path)!r} (no such directory)"
else:
dynlib_entry = f"add_dll_directory({str(dynlib_path)!r})"
dynlib_contents.append(dynlib_entry)
dynlib_contents.append("")
sc_contents.extend(dynlib_contents)
if len(sc_contents) == 1:
Expand Down
28 changes: 28 additions & 0 deletions tests/test_postinstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,46 @@ def _make_dynlib_paths() -> tuple[list[Path], str]:
return dynlib_paths, expected_lines


def _make_missing_dynlib_paths() -> tuple[list[Path], str]:
dynlib_dirs = [f"dynlib{n}" for n in range(5)]
dynlib_paths = [Path(d) for d in dynlib_dirs]
expected_lines = "\n".join(
f"# Skipping {d!r} (no such directory)" for d in dynlib_dirs
)
return dynlib_paths, expected_lines


def test_sitecustomize() -> None:
pylib_paths, expected_lines = _make_pylib_paths()
sc_text = postinstall.generate_sitecustomize(pylib_paths, [])
assert sc_text is not None
assert sc_text.startswith(postinstall._SITE_CUSTOMIZE_HEADER)
assert expected_lines in sc_text
assert "add_dll_directory(" not in sc_text
assert "# Skipping" not in sc_text
assert compile(sc_text, "_sitecustomize.py", "exec") is not None


def test_sitecustomize_with_dynlib() -> None:
pylib_paths, expected_pylib_lines = _make_pylib_paths()
dynlib_paths, expected_dynlib_lines = _make_dynlib_paths()
sc_text = postinstall.generate_sitecustomize(
pylib_paths, dynlib_paths, skip_missing_dynlib_paths=False
)
assert sc_text is not None
assert sc_text.startswith(postinstall._SITE_CUSTOMIZE_HEADER)
assert expected_pylib_lines in sc_text
if hasattr(os, "add_dll_directory"):
assert expected_dynlib_lines in sc_text
else:
assert "add_dll_directory(" not in sc_text
assert "# Skipping" not in sc_text
assert compile(sc_text, "_sitecustomize.py", "exec") is not None


def test_sitecustomize_with_missing_dynlib() -> None:
pylib_paths, expected_pylib_lines = _make_pylib_paths()
dynlib_paths, expected_dynlib_lines = _make_missing_dynlib_paths()
sc_text = postinstall.generate_sitecustomize(pylib_paths, dynlib_paths)
assert sc_text is not None
assert sc_text.startswith(postinstall._SITE_CUSTOMIZE_HEADER)
Expand All @@ -68,4 +95,5 @@ def test_sitecustomize_with_dynlib() -> None:
assert expected_dynlib_lines in sc_text
else:
assert "add_dll_directory(" not in sc_text
assert "# Skipping" not in sc_text
assert compile(sc_text, "_sitecustomize.py", "exec") is not None

0 comments on commit 1acc15b

Please sign in to comment.