Skip to content

Commit

Permalink
Merge pull request #7276 from chrahunt/refactor/shared-test-scripts
Browse files Browse the repository at this point in the history
Share script fixtures across tests
  • Loading branch information
chrahunt authored Nov 2, 2019
2 parents 448303a + 0c4625b commit 39d16b6
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 162 deletions.
74 changes: 48 additions & 26 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,52 +272,69 @@ def virtualenv_template(request, tmpdir_factory, pip_src,
yield venv


@pytest.fixture(scope="session")
def virtualenv_factory(virtualenv_template):
def factory(tmpdir):
return VirtualEnvironment(tmpdir, virtualenv_template)

return factory


@pytest.fixture
def virtualenv(virtualenv_template, tmpdir, isolate):
def virtualenv(virtualenv_factory, tmpdir):
"""
Return a virtual environment which is unique to each test function
invocation created inside of a sub directory of the test function's
temporary directory. The returned object is a
``tests.lib.venv.VirtualEnvironment`` object.
"""
venv_location = tmpdir.joinpath("workspace", "venv")
yield VirtualEnvironment(venv_location, virtualenv_template)
yield virtualenv_factory(tmpdir.joinpath("workspace", "venv"))


@pytest.fixture
def with_wheel(virtualenv, wheel_install):
install_egg_link(virtualenv, 'wheel', wheel_install)


@pytest.fixture(scope="session")
def script_factory(virtualenv_factory, deprecated_python):
def factory(tmpdir, virtualenv=None):
if virtualenv is None:
virtualenv = virtualenv_factory(tmpdir.joinpath("venv"))
return PipTestEnvironment(
# The base location for our test environment
tmpdir,

# Tell the Test Environment where our virtualenv is located
virtualenv=virtualenv,

# Do not ignore hidden files, they need to be checked as well
ignore_hidden=False,

# We are starting with an already empty directory
start_clear=False,

# We want to ensure no temporary files are left behind, so the
# PipTestEnvironment needs to capture and assert against temp
capture_temp=True,
assert_no_temp=True,

# Deprecated python versions produce an extra deprecation warning
pip_expect_warning=deprecated_python,
)

return factory


@pytest.fixture
def script(tmpdir, virtualenv, deprecated_python):
def script(tmpdir, virtualenv, script_factory):
"""
Return a PipTestEnvironment which is unique to each test function and
will execute all commands inside of the unique virtual environment for this
test function. The returned object is a
``tests.lib.scripttest.PipTestEnvironment``.
"""
return PipTestEnvironment(
# The base location for our test environment
tmpdir.joinpath("workspace"),

# Tell the Test Environment where our virtualenv is located
virtualenv=virtualenv,

# Do not ignore hidden files, they need to be checked as well
ignore_hidden=False,

# We are starting with an already empty directory
start_clear=False,

# We want to ensure no temporary files are left behind, so the
# PipTestEnvironment needs to capture and assert against temp
capture_temp=True,
assert_no_temp=True,

# Deprecated python versions produce an extra deprecation warning
pip_expect_warning=deprecated_python,
)
return script_factory(tmpdir.joinpath("workspace"), virtualenv)


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


@pytest.fixture(scope="session")
def shared_data(tmpdir_factory):
return TestData.copy(Path(str(tmpdir_factory.mktemp("data"))))


@pytest.fixture
def data(tmpdir):
return TestData.copy(tmpdir.joinpath("data"))
Expand Down Expand Up @@ -359,7 +381,7 @@ def in_memory_pip():
return InMemoryPip()


@pytest.fixture
@pytest.fixture(scope="session")
def deprecated_python():
"""Used to indicate whether pip deprecated this python version"""
return sys.version_info[:2] in [(2, 7)]
Loading

0 comments on commit 39d16b6

Please sign in to comment.