Skip to content

Commit b0b4c12

Browse files
committed
pytest-virtualenv: Add delete_workspace parameter (closes: #195)
1 parent 020d284 commit b0b4c12

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

pytest-virtualenv/pytest_virtualenv.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,16 @@ class VirtualEnv(Workspace):
108108
path to the virtualenv base dir
109109
env : 'list'
110110
environment variables used in creation of virtualenv
111-
111+
delete_workspace: `None or bool`
112+
If True then the workspace will be deleted
113+
If False then the workspace will be kept
114+
If None (default) then the workspace will be deleted if workspace is also None, but it will be kept otherwise
112115
"""
113116
# TODO: update to use pip, remove distribute
114-
def __init__(self, env=None, workspace=None, name='.env', python=None, args=None):
115-
Workspace.__init__(self, workspace)
117+
def __init__(self, env=None, workspace=None, name='.env', python=None, args=None, delete_workspace=None):
118+
if delete_workspace is None:
119+
delete_workspace = workspace is None
120+
Workspace.__init__(self, workspace, delete_workspace)
116121
self.virtualenv = self.workspace / name
117122
self.args = args or []
118123
if sys.platform == 'win32':

pytest-virtualenv/tests/integration/test_tmpvirtualenv.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import pathlib
23
import subprocess
34
import sys
45
import textwrap
@@ -73,3 +74,45 @@ def test_install_latest():
7374
with venv.VirtualEnv() as v:
7475
v.install_package("flask")
7576
assert v.installed_packages()["Flask"].version != "1.1.1"
77+
78+
79+
def test_keep_named_workspace(tmp_path):
80+
workspace = tmp_path / "new-workspace"
81+
workspace.mkdir()
82+
with venv.VirtualEnv(workspace=str(workspace)) as v:
83+
pass
84+
assert workspace.exists()
85+
86+
87+
def test_really_keep_named_workspace(tmp_path):
88+
workspace = tmp_path / "new-workspace"
89+
workspace.mkdir()
90+
with venv.VirtualEnv(workspace=str(workspace), delete_workspace=False) as v:
91+
pass
92+
assert workspace.exists()
93+
94+
95+
def test_delete_named_workspace(tmp_path):
96+
workspace = tmp_path / "new-workspace"
97+
workspace.mkdir()
98+
with venv.VirtualEnv(workspace=str(workspace), delete_workspace=True) as v:
99+
pass
100+
assert not workspace.exists()
101+
102+
103+
def test_delete_unamed_workspace():
104+
with venv.VirtualEnv() as v:
105+
workspace = pathlib.Path(v.workspace)
106+
assert not workspace.exists()
107+
108+
109+
def test_really_delete_unamed_workspace():
110+
with venv.VirtualEnv(delete_workspace=True) as v:
111+
workspace = pathlib.Path(v.workspace)
112+
assert not workspace.exists()
113+
114+
115+
def test_keep_unamed_workspace():
116+
with venv.VirtualEnv(delete_workspace=False) as v:
117+
workspace = pathlib.Path(v.workspace)
118+
assert workspace.exists()

0 commit comments

Comments
 (0)