From 4b4b384fe089f2291f989f8bf2fdb62d45b63ac8 Mon Sep 17 00:00:00 2001 From: David Noetzel Date: Mon, 25 Jun 2018 14:39:50 -0500 Subject: [PATCH 1/2] Add failing test --- tests/integration/test_pipenv.py | 34 ++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/tests/integration/test_pipenv.py b/tests/integration/test_pipenv.py index 00f291e2e7..9531a2d328 100644 --- a/tests/integration/test_pipenv.py +++ b/tests/integration/test_pipenv.py @@ -2,12 +2,19 @@ XXX: Try our best to reduce tests in this file. """ +import os +from tempfile import gettempdir, mkdtemp + +import mock +import pytest from pipenv.core import activate_virtualenv from pipenv.project import Project - -import pytest +try: + from pathlib import Path +except ImportError: + from pipenv.vendor.pathlib2 import Path @pytest.mark.code @@ -79,3 +86,26 @@ def test_update_locks(PipenvInstance, pypi): assert c.return_code == 0 lines = c.out.splitlines() assert 'requests==2.18.4' in [l.strip() for l in lines] + + +@pytest.mark.cli +def test_directory_with_leading_dash(PipenvInstance): + def mocked_mkdtemp(suffix, prefix, dir): + if suffix == '-project': + temp_dir = Path(gettempdir()) / '-dir-with-leading-dash' + temp_dir.mkdir() + return str(temp_dir) + else: + return mkdtemp(suffix, prefix, dir) + + with mock.patch('pipenv._compat.mkdtemp', side_effect=mocked_mkdtemp): + with PipenvInstance(chdir=True) as p: + # This environment variable is set in the context manager and will + # cause pipenv to use virtualenv, not pew. + del os.environ['PIPENV_VENV_IN_PROJECT'] + p.pipenv('--python python') + venv_path = p.pipenv('--venv').out.strip() + assert os.path.isdir(venv_path) + # Manually clean up environment, since PipenvInstance assumes that + # the virutalenv is in the project directory. + p.pipenv('--rm') From d3513ee3826b34d49c59b105e68f40f79bf031c2 Mon Sep 17 00:00:00 2001 From: David Noetzel Date: Mon, 25 Jun 2018 14:40:55 -0500 Subject: [PATCH 2/2] Allow virtual env creation in dir with leading dash Invoke pew with a double dash separator ("--"), to make it clear that the virtualenv name is a positional argument. Since the virtualenv name includes all or at least the beginning of the directory name, trying to create a virtualenv in a directory with a leading dash in its name will cause pew to erroneously parse the virtualenv name as an optional argument. Adding the separator causes the virtualenv name to be parsed correctly. Fixes #439 --- pipenv/core.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pipenv/core.py b/pipenv/core.py index d97e89a7a3..ea56cbe25f 100644 --- a/pipenv/core.py +++ b/pipenv/core.py @@ -914,7 +914,6 @@ def do_create_virtualenv(python=None, site_packages=False): '-m', 'pipenv.pew', 'new', - project.virtualenv_name, '-d', '-a', project.project_directory, @@ -932,6 +931,8 @@ def do_create_virtualenv(python=None, site_packages=False): err=True, ) cmd = cmd + ['-p', python] + if not project.is_venv_in_project(): + cmd = cmd + ['--', project.virtualenv_name] # Actually create the virtualenv. with spinner(): try: