Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Venv Test Fixes #2773

Merged
merged 19 commits into from
Aug 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion pipenv/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
is_star,
get_workon_home,
is_virtual_environment,
looks_like_dir
)
from .environments import (
PIPENV_MAX_DEPTH,
Expand Down Expand Up @@ -267,7 +268,17 @@ def virtualenv_exists(self):
def get_location_for_virtualenv(self):
if self.is_venv_in_project():
return os.path.join(self.project_directory, ".venv")
return str(get_workon_home().joinpath(self.virtualenv_name))

name = self.virtualenv_name
if self.project_directory:
venv_path = os.path.join(self.project_directory, ".venv")
if os.path.exists(venv_path) and not os.path.isdir(".venv"):
with io.open(venv_path, "r") as f:
name = f.read().strip()
# Assume file's contents is a path if it contains slashes.
if looks_like_dir(name):
return Path(name).absolute().as_posix()
return str(get_workon_home().joinpath(name))

def get_installed_packages(self):
from . import PIPENV_ROOT, PIPENV_VENDOR, PIPENV_PATCHED
Expand Down
5 changes: 5 additions & 0 deletions pipenv/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1376,3 +1376,8 @@ def chdir(path):
yield
finally:
os.chdir(prev_cwd)


def looks_like_dir(path):
seps = (sep for sep in (os.path.sep, os.path.altsep) if sep is not None)
return any(sep in path for sep in seps)
1 change: 1 addition & 0 deletions run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set -eo pipefail

export PYTHONIOENCODING="utf-8"
export LANG=C.UTF-8
export PIP_PROCESS_DEPENDENCY_LINKS="1"

prefix() {
sed "s/^/ $1: /"
Expand Down
7 changes: 6 additions & 1 deletion tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class ResourceWarning(Warning):
pass


HAS_WARNED_GITHUB = False


def check_internet():
try:
# Kenneth represents the Internet LGTM.
Expand All @@ -40,13 +43,15 @@ def check_github_ssh():
res = True if c.return_code == 1 else False
except Exception:
pass
if not res:
global HAS_WARNED_GITHUB
if not res and not HAS_WARNED_GITHUB:
warnings.warn(
'Cannot connect to GitHub via SSH', ResourceWarning
)
warnings.warn(
'Will skip tests requiring SSH access to GitHub', ResourceWarning
)
HAS_WARNED_GITHUB = True
return res


Expand Down
53 changes: 43 additions & 10 deletions tests/integration/test_dot_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,17 @@ def test_reuse_previous_venv(PipenvInstance, pypi):
assert c.return_code == 0
assert normalize_drive(p.path) in p.pipenv('--venv').out


@pytest.mark.dotvenv
def test_venv_file_exists(PipenvInstance, pypi):
"""Tests virtualenv creation & package installation when a .venv file exists
at the project root.
@pytest.mark.parametrize('venv_name', ('test-venv', os.path.join('foo', 'test-venv')))
def test_venv_file(venv_name, PipenvInstance, pypi):
"""Tests virtualenv creation when a .venv file exists at the project root
and contains a venv name.
"""
with PipenvInstance(pypi=pypi, chdir=True) as p:
file_path = os.path.join(p.path, '.venv')
with open(file_path, 'w') as f:
f.write('')
f.write(venv_name)

with temp_environ(), TemporaryDirectory(
prefix='pipenv-', suffix='temp_workon_home'
Expand All @@ -58,12 +60,43 @@ def test_venv_file_exists(PipenvInstance, pypi):
if 'PIPENV_VENV_IN_PROJECT' in os.environ:
del os.environ['PIPENV_VENV_IN_PROJECT']

c = p.pipenv('install requests')
c = p.pipenv('install')
assert c.return_code == 0

c = p.pipenv('--venv')
assert c.return_code == 0
venv_loc = Path(c.out.strip()).absolute()
assert venv_loc.exists()
assert venv_loc.joinpath('.project').exists()
venv_path = venv_loc.as_posix()
if os.path.sep in venv_name:
venv_expected_path = Path(p.path).joinpath(venv_name).absolute().as_posix()
else:
venv_expected_path = Path(workon_home.name).joinpath(venv_name).absolute().as_posix()
assert venv_path == venv_expected_path


@pytest.mark.dotvenv
def test_venv_file_with_path(PipenvInstance, pypi):
"""Tests virtualenv creation when a .venv file exists at the project root
and contains an absolute path.
"""
with temp_environ(), PipenvInstance(chdir=True, pypi=pypi) as p:
with TemporaryDirectory(
prefix='pipenv-', suffix='-test_venv'
) as venv_path:
if 'PIPENV_VENV_IN_PROJECT' in os.environ:
del os.environ['PIPENV_VENV_IN_PROJECT']

file_path = os.path.join(p.path, '.venv')
with open(file_path, 'w') as f:
f.write(venv_path.name)

c = p.pipenv('install')
assert c.return_code == 0
c = p.pipenv('--venv')
assert c.return_code == 0
venv_loc = Path(c.out.strip())

venv_loc = None
for line in c.err.splitlines():
if line.startswith('Virtualenv location:'):
venv_loc = Path(line.split(':', 1)[-1].strip())
assert venv_loc is not None
assert venv_loc.joinpath('.project').exists()
assert venv_loc == Path(venv_path.name)
2 changes: 1 addition & 1 deletion tests/integration/test_install_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_basic_install(PipenvInstance, pypi):
@pytest.mark.install
@flaky
def test_mirror_install(PipenvInstance, pypi):
with temp_environ(), PipenvInstance(chdir=True) as p:
with temp_environ(), PipenvInstance(chdir=True, pypi=pypi) as p:
mirror_url = os.environ.pop(
"PIPENV_TEST_INDEX", "https://pypi.python.org/simple"
)
Expand Down
2 changes: 1 addition & 1 deletion tests/pytest-pypi/pytest_pypi/certs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
def where():
"""Return the preferred certificate bundle."""
# vendored bundle inside Requests
return os.path.join(os.path.dirname(__file__), 'certs', 'cacert.pem')
return os.path.join(os.path.abspath(os.path.dirname(__file__)), 'certs', 'cacert.pem')

if __name__ == '__main__':
print(where())