Skip to content

Commit

Permalink
Formatting edits
Browse files Browse the repository at this point in the history
  • Loading branch information
ncoghlan committed Nov 5, 2024
1 parent 4ca2d3f commit c9c4684
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/venvstacks/_injected/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ exporting environments.

They are also designed to be importable so that
the build process can access their functionality
without need to duplicate the implementation,
without needing to duplicate the implementation,
and to make them more amenable to unit testing.
1 change: 1 addition & 0 deletions src/venvstacks/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,6 @@ def run_python_command(
result.check_returncode()
return result


def capture_python_output(command: list[str]) -> subprocess.CompletedProcess[str]:
return run_python_command(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
12 changes: 4 additions & 8 deletions src/venvstacks/stacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,13 +778,7 @@ def define_export(
def _run_postinstall(postinstall_path: Path) -> None:
# Post-installation scripts are required to work even when they're
# executed with an entirely unrelated Python installation
command = [
sys.executable,
"-X",
"utf8",
"-I",
str(postinstall_path)
]
command = [sys.executable, "-X", "utf8", "-I", str(postinstall_path)]
capture_python_output(command)

def export_environment(
Expand Down Expand Up @@ -1049,7 +1043,7 @@ def _get_deployed_config(
self,
pylib_paths: Iterable[Path] | None,
dynlib_paths: Iterable[Path] | None,
link_external_base: bool = True
link_external_base: bool = True,
) -> postinstall.LayerConfig:
# Helper for subclass get_deployed_config implementations
base_python_path = self.base_python_path
Expand All @@ -1060,9 +1054,11 @@ def _get_deployed_config(
def from_internal_path(build_path: Path) -> str:
# Absolute path, inside the environment
return str(build_path.relative_to(build_env_path))

def from_external_path(build_path: Path) -> str:
# Absolute path, potentially outside the environment
return str(build_path.relative_to(build_env_path, walk_up=True))

def from_relative_path(relative_build_path: Path) -> str:
# Path relative to the base of the build directory
build_path = self.build_path / relative_build_path
Expand Down
11 changes: 3 additions & 8 deletions tests/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,24 +202,19 @@ def make_mock_index_config(reference_config: PackageIndexConfig | None = None) -
# Running commands in a deployed environment
##############################################


def get_sys_path(env_python: Path) -> list[str]:
command = [
str(env_python),
"-X",
"utf8",
"-Ic",
"import json, sys; print(json.dumps(sys.path))"
"import json, sys; print(json.dumps(sys.path))",
]
result = capture_python_output(command)
return cast(list[str], json.loads(result.stdout))


def run_module(env_python: Path, module_name: str) -> subprocess.CompletedProcess[str]:
command = [
str(env_python),
"-X",
"utf8",
"-Im",
module_name
]
command = [str(env_python), "-X", "utf8", "-Im", module_name]
return capture_python_output(command)
10 changes: 3 additions & 7 deletions tests/test_minimal_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,13 +485,9 @@ def _run_postinstall(self, env_path: Path) -> None:
if postinstall_script.exists():
# Post-installation scripts are required to work even when they're
# executed with an entirely unrelated Python installation
capture_python_output([
sys.executable,
"-X",
"utf8",
"-I",
str(postinstall_script)
])
capture_python_output(
[sys.executable, "-X", "utf8", "-I", str(postinstall_script)]
)

def check_archive_deployment(self, published_paths: PublishedArchivePaths) -> None:
metadata_path, snippet_paths, archive_paths = published_paths
Expand Down
10 changes: 9 additions & 1 deletion tests/test_postinstall.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Tests for venvstacks post-install script generation"""

import os

from pathlib import Path
Expand All @@ -12,6 +13,7 @@
executable = {python_bin}
"""


def test_pyvenv_cfg() -> None:
example_path = Path("/example/python/bin/python")
example_version = "6.28"
Expand All @@ -21,25 +23,30 @@ def test_pyvenv_cfg() -> None:
python_bin=str(example_path),
)
pyvenv_cfg = postinstall.generate_pyvenv_cfg(
example_path, example_version,
example_path,
example_version,
)
assert pyvenv_cfg == expected_pyvenv_cfg


def test_sitecustomize_empty() -> None:
assert postinstall.generate_sitecustomize([], []) is None


def _make_pylib_paths() -> tuple[list[Path], str]:
pylib_dirs = [f"pylib{n}" for n in range(5)]
pylib_paths = [Path(d) for d in pylib_dirs]
expected_lines = "\n".join(f"addsitedir({d!r})" for d in pylib_dirs)
return pylib_paths, expected_lines


def _make_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"add_dll_directory({d!r})" 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, [])
Expand All @@ -49,6 +56,7 @@ def test_sitecustomize() -> None:
assert "add_dll_directory(" 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()
Expand Down

0 comments on commit c9c4684

Please sign in to comment.