2013-08-22 13:49:53 +02:00
|
|
|
import shutil
|
|
|
|
|
2013-08-22 04:28:15 +02:00
|
|
|
import py
|
|
|
|
import pytest
|
|
|
|
|
2013-08-24 12:43:01 +02:00
|
|
|
from tests.lib import SRC_DIR, TestData
|
2013-08-22 04:28:15 +02:00
|
|
|
from tests.lib.path import Path
|
2013-08-22 06:39:07 +02:00
|
|
|
from tests.lib.scripttest import PipTestEnvironment
|
|
|
|
from tests.lib.venv import VirtualEnvironment
|
2013-08-22 04:28:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def tmpdir(request):
|
|
|
|
"""
|
|
|
|
Return a temporary directory path object which is unique to each test
|
|
|
|
function invocation, created as a sub directory of the base temporary
|
|
|
|
directory. The returned object is a ``tests.lib.path.Path`` object.
|
|
|
|
|
|
|
|
This is taken from pytest itself but modified to return our typical
|
2014-02-08 03:35:37 +01:00
|
|
|
path object instead of py.path.local as well as deleting the temporary
|
|
|
|
directories at the end of each test case.
|
2013-08-22 04:28:15 +02:00
|
|
|
"""
|
|
|
|
name = request.node.name
|
|
|
|
name = py.std.re.sub("[\W]", "_", name)
|
|
|
|
tmp = request.config._tmpdirhandler.mktemp(name, numbered=True)
|
2014-02-08 03:35:37 +01:00
|
|
|
|
|
|
|
# Clear out the temporary directory after the test has finished using it.
|
|
|
|
# This should prevent us from needing a multiple gigabyte temporary
|
|
|
|
# directory while running the tests.
|
|
|
|
request.addfinalizer(lambda: shutil.rmtree(str(tmp), ignore_errors=True))
|
|
|
|
|
2014-04-28 16:38:39 +02:00
|
|
|
return Path(str(tmp))
|
2013-08-22 06:39:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2013-08-22 13:49:53 +02:00
|
|
|
def virtualenv(tmpdir, monkeypatch):
|
2013-08-22 06:39:07 +02:00
|
|
|
"""
|
|
|
|
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.
|
|
|
|
"""
|
2013-08-22 13:49:53 +02:00
|
|
|
# Force shutil to use the older method of rmtree that didn't use the fd
|
|
|
|
# functions. These seem to fail on Travis (and only on Travis).
|
|
|
|
monkeypatch.setattr(shutil, "_use_fd_functions", False, raising=False)
|
2013-08-24 12:43:01 +02:00
|
|
|
|
|
|
|
# Copy over our source tree so that each virtual environment is self
|
|
|
|
# contained
|
|
|
|
pip_src = tmpdir.join("pip_src").abspath
|
2014-01-28 15:17:51 +01:00
|
|
|
shutil.copytree(
|
|
|
|
SRC_DIR,
|
|
|
|
pip_src,
|
2013-08-24 12:43:01 +02:00
|
|
|
ignore=shutil.ignore_patterns(
|
2013-08-28 11:32:57 +02:00
|
|
|
"*.pyc", "tests", "pip.egg-info", "build", "dist", ".tox",
|
2013-08-24 12:43:01 +02:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
# Create the virtual environment
|
|
|
|
venv = VirtualEnvironment.create(
|
|
|
|
tmpdir.join("workspace", "venv"),
|
|
|
|
pip_source_dir=pip_src,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Undo our monkeypatching of shutil
|
2013-08-22 13:49:53 +02:00
|
|
|
monkeypatch.undo()
|
|
|
|
|
|
|
|
return venv
|
2013-08-22 06:39:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def script(tmpdir, virtualenv):
|
|
|
|
"""
|
|
|
|
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
|
2013-08-23 12:42:31 +02:00
|
|
|
tmpdir.join("workspace"),
|
2013-08-22 06:39:07 +02:00
|
|
|
|
|
|
|
# Tell the Test Environment where our virtualenv is located
|
|
|
|
virtualenv=virtualenv.location,
|
|
|
|
|
|
|
|
# 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,
|
|
|
|
)
|
2013-08-23 13:09:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def data(tmpdir):
|
|
|
|
return TestData.copy(tmpdir.join("data"))
|