Skip to content

Commit db57db2

Browse files
committed
Throw error when trying to activate non-existing venv
1 parent 86cfef8 commit db57db2

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

sphinx_polyversion/pyvenv.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,18 @@ def activate(self, env: dict[str, str]) -> dict[str, str]:
175175
dict[str, str]
176176
The dictionary that was passed with `env`.
177177
178+
Raises
179+
------
180+
FileNotFoundError
181+
If no environment is located at the location `venv`.
182+
178183
"""
184+
if not self.venv.exists():
185+
raise FileNotFoundError(
186+
f"""There is no virtual environment at the path {self.venv}.
187+
Please ensure that the path points to an existing virtual environment, or
188+
supply a creator to automatically create the environment."""
189+
)
179190
env["VIRTUAL_ENV"] = str(self.venv)
180191
env["PATH"] = str(self.venv / "bin") + ":" + env["PATH"]
181192
return env

tests/test_pyvenv.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,20 @@ async def test_creation_without_creator(self, tmp_path: Path):
5757
await env.create_venv()
5858
assert not (location / "bin" / "python").exists()
5959

60+
@pytest.mark.asyncio()
61+
async def test_run_without_creator_no_existing(self, tmp_path: Path):
62+
"""Test running a command without an existing venv and without creator."""
63+
location = tmp_path / "novenv"
64+
65+
async with VirtualPythonEnvironment(tmp_path, "main", location) as env:
66+
with pytest.raises(FileNotFoundError, match="There is no virtual"):
67+
out, err, rc = await env.run(
68+
"python",
69+
"-c",
70+
"import sys; print(sys.prefix)",
71+
stdout=asyncio.subprocess.PIPE,
72+
)
73+
6074
@pytest.mark.asyncio()
6175
async def test_run_without_creator(self, tmp_path: Path):
6276
"""Test running a command in an existing venv."""

0 commit comments

Comments
 (0)