Skip to content

Commit f104af2

Browse files
Test package installation in temporary virtual environment (#519)
Resolves #516 Creates a virtual environment in temporary package directory and tests package installation in there rather than installing in parent environment tests are run from. Calling `pip install` command by directly referencing Python executable in created virtual environment seems to work in terms of ensuring package is installed in `site-packages` of temporary virtual environment rather than parent environment (at least I no longer see a `cookiecutter-test` entry when running `pip list` after running tests) and it seemed non trivial to `source` the virtual environment script with `subprocess.run`. --------- Co-authored-by: Patrick J. Roddy <[email protected]>
1 parent 6e25c1b commit f104af2

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ env_run_base = {commands = [
5454
], deps = [
5555
"cookiecutter",
5656
"pytest",
57+
"pytest-venv",
5758
], description = "Test package creation", skip_install = true}
5859
gh.python = {"3.11" = [
5960
"py311",

tests/test_package_generation.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import typing
99

1010
import pytest
11+
import pytest_venv # type: ignore[import-not-found]
1112

1213

1314
def get_all_files_folders(root_path: pathlib.Path) -> set[pathlib.Path]:
@@ -94,6 +95,7 @@ def test_package_generation(
9495

9596
def test_pip_installable(
9697
tmp_path: pathlib.Path,
98+
venv: pytest_venv.VirtualEnvironment,
9799
generate_package: typing.Callable,
98100
) -> None:
99101
"""Test generated package is pip installable."""
@@ -103,12 +105,11 @@ def test_pip_installable(
103105
"project_name": "Cookiecutter Test",
104106
}
105107
generate_package(config=test_config, path=tmp_path)
106-
107-
# Check project directory exists
108108
test_project_dir = tmp_path / "cookiecutter-test"
109+
# Try to install package in virtual environment with pip
109110
pipinstall = subprocess.run( # noqa: S603
110-
[ # noqa: S607
111-
"python",
111+
[
112+
venv.python,
112113
"-m",
113114
"pip",
114115
"install",

0 commit comments

Comments
 (0)