Skip to content

Commit 39d16b6

Browse files
authored
Merge pull request #7276 from chrahunt/refactor/shared-test-scripts
Share script fixtures across tests
2 parents 448303a + 0c4625b commit 39d16b6

File tree

3 files changed

+186
-162
lines changed

3 files changed

+186
-162
lines changed

tests/conftest.py

+48-26
Original file line numberDiff line numberDiff line change
@@ -272,52 +272,69 @@ def virtualenv_template(request, tmpdir_factory, pip_src,
272272
yield venv
273273

274274

275+
@pytest.fixture(scope="session")
276+
def virtualenv_factory(virtualenv_template):
277+
def factory(tmpdir):
278+
return VirtualEnvironment(tmpdir, virtualenv_template)
279+
280+
return factory
281+
282+
275283
@pytest.fixture
276-
def virtualenv(virtualenv_template, tmpdir, isolate):
284+
def virtualenv(virtualenv_factory, tmpdir):
277285
"""
278286
Return a virtual environment which is unique to each test function
279287
invocation created inside of a sub directory of the test function's
280288
temporary directory. The returned object is a
281289
``tests.lib.venv.VirtualEnvironment`` object.
282290
"""
283-
venv_location = tmpdir.joinpath("workspace", "venv")
284-
yield VirtualEnvironment(venv_location, virtualenv_template)
291+
yield virtualenv_factory(tmpdir.joinpath("workspace", "venv"))
285292

286293

287294
@pytest.fixture
288295
def with_wheel(virtualenv, wheel_install):
289296
install_egg_link(virtualenv, 'wheel', wheel_install)
290297

291298

299+
@pytest.fixture(scope="session")
300+
def script_factory(virtualenv_factory, deprecated_python):
301+
def factory(tmpdir, virtualenv=None):
302+
if virtualenv is None:
303+
virtualenv = virtualenv_factory(tmpdir.joinpath("venv"))
304+
return PipTestEnvironment(
305+
# The base location for our test environment
306+
tmpdir,
307+
308+
# Tell the Test Environment where our virtualenv is located
309+
virtualenv=virtualenv,
310+
311+
# Do not ignore hidden files, they need to be checked as well
312+
ignore_hidden=False,
313+
314+
# We are starting with an already empty directory
315+
start_clear=False,
316+
317+
# We want to ensure no temporary files are left behind, so the
318+
# PipTestEnvironment needs to capture and assert against temp
319+
capture_temp=True,
320+
assert_no_temp=True,
321+
322+
# Deprecated python versions produce an extra deprecation warning
323+
pip_expect_warning=deprecated_python,
324+
)
325+
326+
return factory
327+
328+
292329
@pytest.fixture
293-
def script(tmpdir, virtualenv, deprecated_python):
330+
def script(tmpdir, virtualenv, script_factory):
294331
"""
295332
Return a PipTestEnvironment which is unique to each test function and
296333
will execute all commands inside of the unique virtual environment for this
297334
test function. The returned object is a
298335
``tests.lib.scripttest.PipTestEnvironment``.
299336
"""
300-
return PipTestEnvironment(
301-
# The base location for our test environment
302-
tmpdir.joinpath("workspace"),
303-
304-
# Tell the Test Environment where our virtualenv is located
305-
virtualenv=virtualenv,
306-
307-
# Do not ignore hidden files, they need to be checked as well
308-
ignore_hidden=False,
309-
310-
# We are starting with an already empty directory
311-
start_clear=False,
312-
313-
# We want to ensure no temporary files are left behind, so the
314-
# PipTestEnvironment needs to capture and assert against temp
315-
capture_temp=True,
316-
assert_no_temp=True,
317-
318-
# Deprecated python versions produce an extra deprecation warning
319-
pip_expect_warning=deprecated_python,
320-
)
337+
return script_factory(tmpdir.joinpath("workspace"), virtualenv)
321338

322339

323340
@pytest.fixture(scope="session")
@@ -326,6 +343,11 @@ def common_wheels():
326343
return DATA_DIR.joinpath('common_wheels')
327344

328345

346+
@pytest.fixture(scope="session")
347+
def shared_data(tmpdir_factory):
348+
return TestData.copy(Path(str(tmpdir_factory.mktemp("data"))))
349+
350+
329351
@pytest.fixture
330352
def data(tmpdir):
331353
return TestData.copy(tmpdir.joinpath("data"))
@@ -359,7 +381,7 @@ def in_memory_pip():
359381
return InMemoryPip()
360382

361383

362-
@pytest.fixture
384+
@pytest.fixture(scope="session")
363385
def deprecated_python():
364386
"""Used to indicate whether pip deprecated this python version"""
365387
return sys.version_info[:2] in [(2, 7)]

0 commit comments

Comments
 (0)