Exemplo n.º 1
0
def test_install_does_not_extrapolate_environ(PipenvInstance, pypi):
    """Ensure environment variables are not expanded in lock file.
    """
    with temp_environ(), PipenvInstance(pypi=pypi, chdir=True) as p:
        os.environ["PYPI_URL"] = pypi.url

        with open(p.pipfile_path, "w") as f:
            f.write(
                """
[[source]]
url = '${PYPI_URL}/simple'
verify_ssl = true
name = 'mockpi'
            """
            )

        # Ensure simple install does not extrapolate.
        c = p.pipenv("install")
        assert c.return_code == 0
        assert p.pipfile["source"][0]["url"] == "${PYPI_URL}/simple"
        assert p.lockfile["_meta"]["sources"][0]["url"] == "${PYPI_URL}/simple"

        # Ensure package install does not extrapolate.
        c = p.pipenv("install six")
        assert c.return_code == 0
        assert p.pipfile["source"][0]["url"] == "${PYPI_URL}/simple"
        assert p.lockfile["_meta"]["sources"][0]["url"] == "${PYPI_URL}/simple"
Exemplo n.º 2
0
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(venv_name)

        with temp_environ(), TemporaryDirectory(
            prefix='pipenv-', suffix='temp_workon_home'
        ) as workon_home:
            os.environ['WORKON_HOME'] = workon_home.name
            if 'PIPENV_VENV_IN_PROJECT' in os.environ:
                del os.environ['PIPENV_VENV_IN_PROJECT']

            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 = normalize_drive(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 == normalize_drive(venv_expected_path)
Exemplo n.º 3
0
    def test_multiprocess_bug_and_install(self):
        with temp_environ():
            os.environ['PIPENV_MAX_SUBPROCESS'] = '2'

            with PipenvInstance() as p:
                with open(p.pipfile_path, 'w') as f:
                    contents = """
[packages]
requests = "*"
records = "*"
tpfd = "*"
                    """.strip()
                    f.write(contents)

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

                assert 'requests' in p.lockfile['default']
                assert 'idna' in p.lockfile['default']
                assert 'urllib3' in p.lockfile['default']
                assert 'certifi' in p.lockfile['default']
                assert 'records' in p.lockfile['default']
                assert 'tpfd' in p.lockfile['default']
                assert 'parse' in p.lockfile['default']

                c = p.pipenv('run python -c "import requests; import idna; import certifi; import records; import tpfd; import parse;"')
                assert c.return_code == 0
Exemplo n.º 4
0
def test_install_does_not_extrapolate_environ(PipenvInstance, pypi):
    """Ensure environment variables are not expanded in lock file.
    """
    with temp_environ(), PipenvInstance(pypi=pypi, chdir=True) as p:
        os.environ['PYPI_URL'] = pypi.url

        with open(p.pipfile_path, 'w') as f:
            f.write("""
[[source]]
url = '${PYPI_URL}/simple'
verify_ssl = true
name = 'mockpi'
            """)

        # Ensure simple install does not extrapolate.
        c = p.pipenv('install')
        assert c.return_code == 0
        assert p.pipfile['source'][0]['url'] == '${PYPI_URL}/simple'
        assert p.lockfile['_meta']['sources'][0]['url'] == '${PYPI_URL}/simple'

        # Ensure package install does not extrapolate.
        c = p.pipenv('install six')
        assert c.return_code == 0
        assert p.pipfile['source'][0]['url'] == '${PYPI_URL}/simple'
        assert p.lockfile['_meta']['sources'][0]['url'] == '${PYPI_URL}/simple'
Exemplo n.º 5
0
def test_private_index_mirror_lock_requirements(PipenvInstance):
    # Don't use the local fake pypi
    with temp_environ(), PipenvInstance(chdir=True) as p:
        # Using pypi.python.org as pipenv-test-public-package is not
        # included in the local pypi mirror
        mirror_url = os.environ.pop('PIPENV_TEST_INDEX', "https://pypi.kennethreitz.org/simple")
        # os.environ.pop('PIPENV_TEST_INDEX', None)
        with open(p.pipfile_path, 'w') as f:
            contents = """
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[[source]]
url = "https://test.pypi.org/simple"
verify_ssl = true
name = "testpypi"

[packages]
six = {version = "*", index = "testpypi"}
requests = "*"
            """.strip()
            f.write(contents)
        c = p.pipenv('install --pypi-mirror {0}'.format(mirror_url))
        assert c.return_code == 0
        c = p.pipenv('lock -r --pypi-mirror {0}'.format(mirror_url))
        assert c.return_code == 0
        assert '-i https://pypi.org/simple' in c.out.strip()
        assert '--extra-index-url https://test.pypi.org/simple' in c.out.strip()
        # Mirror url should not have replaced source URLs
        assert '-i {0}'.format(mirror_url) not in c.out.strip()
        assert '--extra-index-url {}'.format(mirror_url) not in c.out.strip()
Exemplo n.º 6
0
def test_private_index_mirror_lock_requirements(PipenvInstance_NoPyPI):
    # Don't use the local fake pypi
    with temp_environ(), PipenvInstance_NoPyPI(chdir=True) as p:
        # Using pypi.python.org as pipenv-test-public-package is not
        # included in the local pypi mirror
        mirror_url = os.environ.pop('PIPENV_TEST_INDEX',
                                    "https://pypi.kennethreitz.org/simple")
        # os.environ.pop('PIPENV_TEST_INDEX', None)
        with open(p.pipfile_path, 'w') as f:
            contents = """
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[[source]]
url = "https://test.pypi.org/simple"
verify_ssl = true
name = "testpypi"

[packages]
six = {version = "*", index = "testpypi"}
fake-package = "*"
            """.strip()
            f.write(contents)
        c = p.pipenv(f'install --pypi-mirror {mirror_url}')
        assert c.returncode == 0
        c = p.pipenv(f'lock -r --pypi-mirror {mirror_url}')
        assert c.returncode == 0
        assert f'-i {mirror_url}' in c.stdout.strip()
        assert '--extra-index-url https://test.pypi.org/simple' in c.stdout.strip(
        )
        assert f'--extra-index-url {mirror_url}' not in c.stdout.strip()
Exemplo n.º 7
0
def test_lock_updated_source(PipenvInstance, pypi):

    with PipenvInstance(pypi=pypi) as p:
        with open(p.pipfile_path, 'w') as f:
            contents = """
[[source]]
url = "{url}/${{MY_ENV_VAR}}"

[packages]
requests = "==2.14.0"
            """.strip().format(url=pypi.url)
            f.write(contents)

        with temp_environ():
            os.environ['MY_ENV_VAR'] = 'simple'
            c = p.pipenv('lock')
            assert c.return_code == 0
            assert 'requests' in p.lockfile['default']

        with open(p.pipfile_path, 'w') as f:
            contents = """
[[source]]
url = "{url}/simple"

[packages]
requests = "==2.14.0"
            """.strip().format(url=pypi.url)
            f.write(contents)

        c = p.pipenv('lock')
        assert c.return_code == 0
        assert 'requests' in p.lockfile['default']
Exemplo n.º 8
0
def test_lock_updated_source(PipenvInstance):

    with PipenvInstance() as p:
        with open(p.pipfile_path, 'w') as f:
            contents = """
[[source]]
url = "{url}/${{MY_ENV_VAR}}"

[packages]
requests = "==2.14.0"
            """.strip().format(url=p.pypi)
            f.write(contents)

        with temp_environ():
            os.environ['MY_ENV_VAR'] = 'simple'
            c = p.pipenv('lock')
            assert c.returncode == 0
            assert 'requests' in p.lockfile['default']

        with open(p.pipfile_path, 'w') as f:
            contents = """
[[source]]
url = "{url}/simple"

[packages]
requests = "==2.14.0"
            """.strip().format(url=p.pypi)
            f.write(contents)

        c = p.pipenv('lock')
        assert c.returncode == 0
        assert 'requests' in p.lockfile['default']
Exemplo n.º 9
0
    def test_environment_variable_value_does_not_change_hash(self, pypi):
        with PipenvInstance(chdir=True, pypi=pypi) as p:
            with temp_environ():
                with open(p.pipfile_path, 'w') as f:
                    f.write("""
[[source]]
url = 'https://${PYPI_USERNAME}:${PYPI_PASSWORD}@pypi.python.org/simple'
verify_ssl = true
name = 'pypi'

[requires]
python_version = '2.7'

[packages]
flask = "==0.12.2"
""")
                os.environ['PYPI_USERNAME'] = '******'
                os.environ['PYPI_PASSWORD'] = '******'
                assert Project().get_lockfile_hash() is None
                c = p.pipenv('install')
                lock_hash = Project().get_lockfile_hash()
                assert lock_hash is not None
                assert lock_hash == Project().calculate_pipfile_hash()
                # sanity check on pytest
                assert 'PYPI_USERNAME' not in str(pipfile.load(p.pipfile_path))
                assert c.return_code == 0
                assert Project().get_lockfile_hash() == Project(
                ).calculate_pipfile_hash()
                os.environ['PYPI_PASSWORD'] = '******'
                assert Project().get_lockfile_hash() == Project(
                ).calculate_pipfile_hash()
                with open(p.pipfile_path, 'a') as f:
                    f.write('requests = "==2.14.0"\n')
                assert Project().get_lockfile_hash() != Project(
                ).calculate_pipfile_hash()
Exemplo n.º 10
0
def test_venv_in_project(PipenvInstance, pypi):
    with temp_environ():
        os.environ['PIPENV_VENV_IN_PROJECT'] = '1'
        with PipenvInstance(pypi=pypi) as p:
            c = p.pipenv('install requests')
            assert c.return_code == 0
            assert normalize_drive(p.path) in p.pipenv('--venv').out
Exemplo n.º 11
0
def test_venv_in_project(PipenvInstance):
    with temp_environ():
        os.environ['PIPENV_VENV_IN_PROJECT'] = '1'
        with PipenvInstance() as p:
            c = p.pipenv('install requests')
            assert c.returncode == 0
            assert normalize_drive(p.path) in p.pipenv('--venv').stdout
Exemplo n.º 12
0
def test_install_does_not_extrapolate_environ(PipenvInstance, pypi):
    """This test is deisgned to make sure that pipenv ignores requirements.txt files
    for projects that already exist (already have a Pipfile) as well as for times when a
    package name is passed in to the install command."""
    with temp_environ(), PipenvInstance(pypi=pypi, chdir=True) as p:
        os.environ['PYPI_URL'] = pypi.url

        with open(p.pipfile_path, 'w') as f:
            f.write("""
[[source]]
url = '${PYPI_URL}/simple'
verify_ssl = true
name = 'mockpi'
            """)

        # Ensure simple install does not extrapolate.
        c = p.pipenv('install')
        assert c.return_code == 0
        assert p.pipfile['source'][0]['url'] == '${PYPI_URL}/simple'
        assert p.lockfile['_meta']['sources'][0]['url'] == '${PYPI_URL}/simple'

        # Ensure package install does not extrapolate.
        c = p.pipenv('install six')
        assert c.return_code == 0
        assert p.pipfile['source'][0]['url'] == '${PYPI_URL}/simple'
        assert p.lockfile['_meta']['sources'][0]['url'] == '${PYPI_URL}/simple'
Exemplo n.º 13
0
    def test_multiprocess_bug_and_install(self):
        with temp_environ():
            os.environ['PIPENV_MAX_SUBPROCESS'] = '2'

            with PipenvInstance() as p:
                with open(p.pipfile_path, 'w') as f:
                    contents = """
[packages]
requests = "*"
records = "*"
tpfd = "*"
                    """.strip()
                    f.write(contents)

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

                assert 'requests' in p.lockfile['default']
                assert 'idna' in p.lockfile['default']
                assert 'urllib3' in p.lockfile['default']
                assert 'certifi' in p.lockfile['default']
                assert 'records' in p.lockfile['default']
                assert 'tpfd' in p.lockfile['default']
                assert 'parse' in p.lockfile['default']

                c = p.pipenv('run python -c "import requests; import idna; import certifi; import records; import tpfd; import parse;"')
                assert c.return_code == 0
Exemplo n.º 14
0
def test_private_index_mirror_lock_requirements(PipenvInstance):
    # Don't use the local fake pypi
    with temp_environ(), PipenvInstance(chdir=True) as p:
        # Using pypi.python.org as pipenv-test-public-package is not
        # included in the local pypi mirror
        mirror_url = "https://pypi.python.org/simple"
        os.environ.pop('PIPENV_TEST_INDEX', None)
        with open(p.pipfile_path, 'w') as f:
            contents = """
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[[source]]
url = "https://test.pypi.org/simple"
verify_ssl = true
name = "testpypi"

[packages]
pipenv-test-private-package = {version = "*", index = "testpypi"}
requests = "*"
            """.strip()
            f.write(contents)
        c = p.pipenv('install --pypi-mirror {0}'.format(mirror_url))
        assert c.return_code == 0
        c = p.pipenv('lock -r --pypi-mirror {0}'.format(mirror_url))
        assert c.return_code == 0
        assert '-i https://pypi.org/simple' in c.out.strip()
        assert '--extra-index-url https://test.pypi.org/simple' in c.out.strip(
        )
        # Mirror url should not have replaced source URLs
        assert '-i {0}'.format(mirror_url) not in c.out.strip()
        assert '--extra-index-url {}'.format(mirror_url) not in c.out.strip()
Exemplo n.º 15
0
def test_mirror_uninstall(PipenvInstance):
    with temp_environ(), PipenvInstance(chdir=True) as p:

        mirror_url = os.environ.pop(
            "PIPENV_TEST_INDEX", "https://pypi.python.org/simple"
        )
        assert "pypi.org" not in mirror_url

        c = p.pipenv("install Django==1.11.13 --pypi-mirror {0}".format(mirror_url))
        assert c.return_code == 0
        assert "django" in p.pipfile["packages"]
        assert "django" in p.lockfile["default"]
        assert "pytz" in p.lockfile["default"]
        # Ensure the --pypi-mirror parameter hasn't altered the Pipfile or Pipfile.lock sources
        assert len(p.pipfile["source"]) == 1
        assert len(p.lockfile["_meta"]["sources"]) == 1
        assert "https://pypi.org/simple" == p.pipfile["source"][0]["url"]
        assert "https://pypi.org/simple" == p.lockfile["_meta"]["sources"][0]["url"]

        c = p.pipenv("run python -m django --version")
        assert c.return_code == 0

        c = p.pipenv("uninstall Django --pypi-mirror {0}".format(mirror_url))
        assert c.return_code == 0
        assert "django" not in p.pipfile["dev-packages"]
        assert "django" not in p.lockfile["develop"]
        assert p.lockfile["develop"] == {}
        # Ensure the --pypi-mirror parameter hasn't altered the Pipfile or Pipfile.lock sources
        assert len(p.pipfile["source"]) == 1
        assert len(p.lockfile["_meta"]["sources"]) == 1
        assert "https://pypi.org/simple" == p.pipfile["source"][0]["url"]
        assert "https://pypi.org/simple" == p.lockfile["_meta"]["sources"][0]["url"]

        c = p.pipenv("run python -m django --version")
        assert c.return_code > 0
Exemplo n.º 16
0
 def test_ssh_dependency_links_install(self, PipenvInstance, pypi):
     with temp_environ(), PipenvInstance(pypi=pypi, chdir=True) as p:
         os.environ['PIP_PROCESS_DEPENDENCY_LINKS'] = '1'
         TestDependencyLinks.helper_dependency_links_install_test(
             p,
             'git+ssh://[email protected]/atzannes/[email protected]#egg=test-private-dependency-v0.1'
         )
Exemplo n.º 17
0
def test_install_does_not_extrapolate_environ(PipenvInstance, pypi):
    """Ensure environment variables are not expanded in lock file.
    """
    with temp_environ(), PipenvInstance(pypi=pypi, chdir=True) as p:
        os.environ["PYPI_URL"] = pypi.url

        with open(p.pipfile_path, "w") as f:
            f.write("""
[[source]]
url = '${PYPI_URL}/simple'
verify_ssl = true
name = 'mockpi'
            """)

        # Ensure simple install does not extrapolate.
        c = p.pipenv("install")
        assert c.return_code == 0
        assert p.pipfile["source"][0]["url"] == "${PYPI_URL}/simple"
        assert p.lockfile["_meta"]["sources"][0]["url"] == "${PYPI_URL}/simple"

        # Ensure package install does not extrapolate.
        c = p.pipenv("install six")
        assert c.return_code == 0
        assert p.pipfile["source"][0]["url"] == "${PYPI_URL}/simple"
        assert p.lockfile["_meta"]["sources"][0]["url"] == "${PYPI_URL}/simple"
Exemplo n.º 18
0
 def test_ssh_dependency_links_install(self, PipenvInstance, pypi):
     with temp_environ(), PipenvInstance(pypi=pypi, chdir=True) as p:
         os.environ['PIP_PROCESS_DEPENDENCY_LINKS'] = '1'
         os.environ["PIP_NO_BUILD_ISOLATION"] = '1'
         TestDirectDependencies.helper_dependency_links_install_test(
             p,
             'test-private-dependency@ git+ssh://[email protected]/atzannes/[email protected]'
         )
Exemplo n.º 19
0
    def test_venv_in_project(self):

        with temp_environ():
            os.environ['PIPENV_VENV_IN_PROJECT'] = '1'
            with PipenvInstance() as p:
                c = p.pipenv('install requests')
                assert c.return_code == 0

                assert p.path in p.pipenv('--venv').out
Exemplo n.º 20
0
def test_load_dot_env_warns_if_file_doesnt_exist(capsys):
    with temp_environ(), TemporaryDirectory(prefix='pipenv-',
                                            suffix='') as tempdir:
        dotenv_path = os.path.join(tempdir.name, 'does-not-exist.env')
        with mock.patch('pipenv.environments.PIPENV_DOTENV_LOCATION',
                        dotenv_path):
            load_dot_env()
        output, err = capsys.readouterr()
        assert 'Warning' in err
Exemplo n.º 21
0
    def test_venv_in_project(self):

        with temp_environ():
            os.environ['PIPENV_VENV_IN_PROJECT'] = '1'
            with PipenvInstance() as p:
                c = p.pipenv('install requests')
                assert c.return_code == 0

                assert p.path in p.pipenv('--venv').out
Exemplo n.º 22
0
 def test_https_dependency_links_install(self, PipenvInstance, pypi):
     """Ensure dependency_links are parsed and installed (needed for private repo dependencies).
     """
     with temp_environ(), PipenvInstance(pypi=pypi, chdir=True) as p:
         os.environ['PIP_PROCESS_DEPENDENCY_LINKS'] = '1'
         TestDependencyLinks.helper_dependency_links_install_test(
             p,
             'git+https://github.com/atzannes/[email protected]#egg=test-private-dependency-v0.1'
         )
Exemplo n.º 23
0
 def test_https_dependency_links_install(self, PipenvInstance, pypi):
     """Ensure dependency_links are parsed and installed (needed for private repo dependencies).
     """
     with temp_environ(), PipenvInstance(pypi=pypi, chdir=True) as p:
         os.environ["PIP_NO_BUILD_ISOLATION"] = '1'
         TestDirectDependencies.helper_dependency_links_install_test(
             p,
             'test-private-dependency@ git+https://github.com/atzannes/[email protected]'
         )
Exemplo n.º 24
0
def test_load_dot_env_warns_if_file_doesnt_exist(monkeypatch, capsys, project):
    with temp_environ(), monkeypatch.context() as m, TemporaryDirectory(prefix='pipenv-', suffix='') as tempdir:
        if os.name == "nt":
            import click
            is_console = False
            m.setattr(click._winconsole, "_is_console", lambda x: is_console)
        dotenv_path = os.path.join(tempdir, 'does-not-exist.env')
        project.s.PIPENV_DOTENV_LOCATION = str(dotenv_path)
        load_dot_env(project)
        output, err = capsys.readouterr()
        assert 'Warning' in err
Exemplo n.º 25
0
def test_venv_at_project_root(PipenvInstance):
    with temp_environ():
        with PipenvInstance(chdir=True) as p:
            os.environ['PIPENV_VENV_IN_PROJECT'] = '1'
            c = p.pipenv('install')
            assert c.return_code == 0
            assert normalize_drive(p.path) in p.pipenv('--venv').out
            del os.environ['PIPENV_VENV_IN_PROJECT']
            os.mkdir('subdir')
            os.chdir('subdir')
            # should still detect installed
            assert normalize_drive(p.path) in p.pipenv('--venv').out
Exemplo n.º 26
0
def test_venv_at_project_root(PipenvInstance):
    with temp_environ():
        with PipenvInstance(chdir=True) as p:
            os.environ['PIPENV_VENV_IN_PROJECT'] = '1'
            c = p.pipenv('install')
            assert c.return_code == 0
            assert normalize_drive(p.path) in p.pipenv('--venv').out
            del os.environ['PIPENV_VENV_IN_PROJECT']
            os.mkdir('subdir')
            os.chdir('subdir')
            # should still detect installed
            assert normalize_drive(p.path) in p.pipenv('--venv').out
Exemplo n.º 27
0
def test_load_dot_env_from_environment_variable_location(capsys):
    with temp_environ(), TemporaryDirectory(prefix='pipenv-',
                                            suffix='') as tempdir:
        dotenv_path = os.path.join(tempdir.name, 'test.env')
        key, val = 'SOME_KEY', 'some_value'
        with open(dotenv_path, 'w') as f:
            f.write('{}={}'.format(key, val))

        with mock.patch('pipenv.environments.PIPENV_DOTENV_LOCATION',
                        dotenv_path):
            load_dot_env()
        assert os.environ[key] == val
Exemplo n.º 28
0
def test_mirror_lock_sync(PipenvInstance, pypi):
    with temp_environ(), PipenvInstance(chdir=True) as p:
        mirror_url = os.environ.pop('PIPENV_TEST_INDEX', "https://pypi.kennethreitz.org/simple")
        assert 'pypi.org' not in mirror_url
        with open(p.pipfile_path, 'w') as f:
            f.write("""
[packages]
six = "*"
            """.strip())
        c = p.pipenv('lock --pypi-mirror {0}'.format(mirror_url))
        assert c.return_code == 0
        c = p.pipenv('sync --pypi-mirror {0}'.format(mirror_url))
        assert c.return_code == 0
Exemplo n.º 29
0
def test_mirror_lock_sync(PipenvInstance, pypi):
    with temp_environ(), PipenvInstance(chdir=True) as p:
        mirror_url = os.environ.pop('PIPENV_TEST_INDEX', "https://pypi.kennethreitz.org/simple")
        assert 'pypi.org' not in mirror_url
        with open(p.pipfile_path, 'w') as f:
            f.write("""
[packages]
six = "*"
            """.strip())
        c = p.pipenv('lock --pypi-mirror {0}'.format(mirror_url))
        assert c.return_code == 0
        c = p.pipenv('sync --pypi-mirror {0}'.format(mirror_url))
        assert c.return_code == 0
Exemplo n.º 30
0
def test_directory_with_leading_dash(raw_venv, PipenvInstance):
    with temp_environ():
        with PipenvInstance(chdir=True, venv_in_project=False, name="-project-with-dash") as p:
            if "PIPENV_VENV_IN_PROJECT" in os.environ:
                del os.environ['PIPENV_VENV_IN_PROJECT']
            c = p.pipenv('run pip freeze')
            assert c.return_code == 0
            c = p.pipenv('--venv')
            assert c.return_code == 0
            venv_path = c.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')
Exemplo n.º 31
0
def test_load_dot_env_from_environment_variable_location(monkeypatch, capsys, project):
    with temp_environ(), monkeypatch.context() as m, TemporaryDirectory(prefix='pipenv-', suffix='') as tempdir:
        if os.name == "nt":
            import click
            is_console = False
            m.setattr(click._winconsole, "_is_console", lambda x: is_console)
        dotenv_path = os.path.join(tempdir, 'test.env')
        key, val = 'SOME_KEY', 'some_value'
        with open(dotenv_path, 'w') as f:
            f.write(f'{key}={val}')

        project.s.PIPENV_DOTENV_LOCATION = str(dotenv_path)
        load_dot_env(project)
        assert os.environ[key] == val
Exemplo n.º 32
0
def test_install_venv_project_directory(PipenvInstance, pypi):
    """Test the project functionality during virtualenv creation.
    """
    with PipenvInstance(pypi=pypi, chdir=True) as p:
        with temp_environ(), TemporaryDirectory(
            prefix="pipenv-", suffix="temp_workon_home"
        ) as workon_home:
            os.environ["WORKON_HOME"] = workon_home.name
            if "PIPENV_VENV_IN_PROJECT" in os.environ:
                del os.environ["PIPENV_VENV_IN_PROJECT"]
            c = p.pipenv("install six")
            assert c.return_code == 0
            project = Project()
            assert Path(project.virtualenv_location).joinpath(".project").exists()
Exemplo n.º 33
0
def test_install_venv_project_directory(PipenvInstance, pypi):
    """Test pew's project functionality during virtualenv creation.  Since .venv
    virtualenvs are not created with pew, we need to swap to a workon_home based
    virtualenv for this test"""
    with PipenvInstance(pypi=pypi, chdir=True) as p:
        with temp_environ(), TemporaryDirectory(
                prefix='pipenv-', suffix='temp_workon_home') as workon_home:
            os.environ['WORKON_HOME'] = workon_home.name
            if 'PIPENV_VENV_IN_PROJECT' in os.environ:
                del os.environ['PIPENV_VENV_IN_PROJECT']
            c = p.pipenv('install six')
            assert c.return_code == 0
            project = Project()
            assert Path(
                project.virtualenv_location).joinpath('.project').exists()
Exemplo n.º 34
0
    def test_install_doesnt_leave_tmpfiles(self):
        with temp_environ():
            os.environ['PIPENV_MAX_SUBPROCESS'] = '2'

            with PipenvInstance() as p:
                with open(p.pipfile_path, 'w') as f:
                    contents = """
[packages]
records = "*"
                    """.strip()
                    f.write(contents)

                c = p.pipenv('install')
                assert c.return_code == 0
                assert os.listdir(p.tmpdir) == []
Exemplo n.º 35
0
def test_directory_with_leading_dash(PipenvInstance):
    def mocked_mkdtemp(suffix, prefix, dir):
        if suffix == '-project':
            prefix = '-dir-with-leading-dash'
        return mkdtemp(suffix, prefix, dir)

    with mock.patch('pipenv._compat.mkdtemp', side_effect=mocked_mkdtemp):
        with temp_environ(), PipenvInstance(chdir=True) as p:
            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')
Exemplo n.º 36
0
def test_doesnt_load_dot_env_if_disabled(capsys):
    with temp_environ(), TemporaryDirectory(prefix='pipenv-',
                                            suffix='') as tempdir:
        dotenv_path = os.path.join(tempdir.name, 'test.env')
        key, val = 'SOME_KEY', 'some_value'
        with open(dotenv_path, 'w') as f:
            f.write('{}={}'.format(key, val))

        with mock.patch('pipenv.environments.PIPENV_DOTENV_LOCATION',
                        dotenv_path):
            with mock.patch('pipenv.environments.PIPENV_DONT_LOAD_ENV', '1'):
                load_dot_env()
            assert key not in os.environ

            load_dot_env()
            assert key in os.environ
Exemplo n.º 37
0
def virtualenv(pathlib_tmpdir):
    virtualenv_path = pathlib_tmpdir / "venv"
    with temp_environ():
        c = delegator.run("virtualenv {}".format(virtualenv_path), block=True)
        assert c.return_code == 0
        for name in ("bin", "Scripts"):
            activate_this = virtualenv_path / name / "activate_this.py"
            if activate_this.exists():
                with open(str(activate_this)) as f:
                    code = compile(f.read(), str(activate_this), "exec")
                    exec(code, dict(__file__=str(activate_this)))
                break
        else:
            raise VirtualenvActivationException("Can't find the activate_this.py script.")
        os.environ["VIRTUAL_ENV"] = str(virtualenv_path)
        yield virtualenv_path
Exemplo n.º 38
0
 def test_venv_at_project_root(self):
     def _assert_venv_at_project_root(p):
         c = p.pipenv('--venv')
         assert c.return_code == 0
         assert p.path in c.out
     with temp_environ():
         with PipenvInstance(chdir=True, pipfile=False) as p:
             os.environ['PIPENV_VENV_IN_PROJECT'] = '1'
             c = p.pipenv('install')
             assert c.return_code == 0
             _assert_venv_at_project_root(p)
             del os.environ['PIPENV_VENV_IN_PROJECT']
             os.mkdir('subdir')
             os.chdir('subdir')
             # should still detect installed
             _assert_venv_at_project_root(p)
Exemplo n.º 39
0
def test_pipfile_envvar_expansion(PipenvInstance):
    with PipenvInstance(chdir=True) as p:
        with temp_environ():
            with open(p.pipfile_path, 'w') as f:
                f.write("""
[[source]]
url = 'https://${TEST_HOST}/simple'
verify_ssl = false
name = "pypi"

[packages]
pytz = "*"
                """.strip())
            os.environ['TEST_HOST'] = 'localhost:5000'
            project = Project()
            assert project.sources[0]['url'] == 'https://localhost:5000/simple'
            assert 'localhost:5000' not in str(pipfile.load(p.pipfile_path))
Exemplo n.º 40
0
def test_install_venv_project_directory(PipenvInstance, pypi):
    """Test the project functionality during virtualenv creation.
    """
    with PipenvInstance(pypi=pypi, chdir=True) as p:
        with temp_environ(), TemporaryDirectory(
            prefix="pipenv-", suffix="temp_workon_home"
        ) as workon_home:
            os.environ["WORKON_HOME"] = workon_home.name
            if "PIPENV_VENV_IN_PROJECT" in os.environ:
                del os.environ["PIPENV_VENV_IN_PROJECT"]

            c = p.pipenv("install six")
            assert c.return_code == 0

            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()
Exemplo n.º 41
0
def test_mirror_install(PipenvInstance, pypi):
    with temp_environ(), PipenvInstance(chdir=True) as p:
        mirror_url = os.environ.pop(
            "PIPENV_TEST_INDEX", "https://pypi.python.org/simple"
        )
        assert "pypi.org" not in mirror_url
        # This should sufficiently demonstrate the mirror functionality
        # since pypi.org is the default when PIPENV_TEST_INDEX is unset.
        c = p.pipenv("install requests --pypi-mirror {0}".format(mirror_url))
        assert c.return_code == 0
        # Ensure the --pypi-mirror parameter hasn't altered the Pipfile or Pipfile.lock sources
        assert len(p.pipfile["source"]) == 1
        assert len(p.lockfile["_meta"]["sources"]) == 1
        assert "https://pypi.org/simple" == p.pipfile["source"][0]["url"]
        assert "https://pypi.org/simple" == p.lockfile["_meta"]["sources"][0]["url"]

        assert "requests" in p.pipfile["packages"]
        assert "requests" in p.lockfile["default"]
        assert "chardet" in p.lockfile["default"]
        assert "idna" in p.lockfile["default"]
        assert "urllib3" in p.lockfile["default"]
        assert "certifi" in p.lockfile["default"]
Exemplo n.º 42
0
 def test_shell_nested_venv_in_project(self):
     import subprocess
     with temp_environ():
         os.environ['PIPENV_VENV_IN_PROJECT'] = '1'
         os.environ['PIPENV_IGNORE_VIRTUALENVS'] = '1'
         os.environ['PIPENV_SHELL_COMPAT'] = '1'
         with PipenvInstance(chdir=True) as p:
             # Signal to pew to look in the project directory for the environment
             os.environ['WORKON_HOME'] = p.path
             project = Project()
             c = p.pipenv('install requests')
             assert c.return_code == 0
             assert 'requests' in p.pipfile['packages']
             assert 'requests' in p.lockfile['default']
             # Check that .venv now shows in pew's managed list
             pew_list = delegator.run('pew ls')
             assert '.venv' in pew_list.out
             # Check for the venv directory
             c = delegator.run('pew dir .venv')
             # Compare pew's virtualenv path to what we expect
             venv_path = get_windows_path(project.project_directory, '.venv')
             # os.path.normpath will normalize slashes
             assert venv_path == os.path.normpath(c.out.strip())
             # Have pew run 'pip freeze' in the virtualenv
             # This is functionally the same as spawning a subshell
             # If we can do this we can theoretically amke a subshell
             # This test doesn't work on *nix
             if os.name == 'nt':
                 args = ['pew', 'in', '.venv', 'pip', 'freeze']
                 process = subprocess.Popen(
                     args,
                     shell=True,
                     universal_newlines=True,
                     stdin=subprocess.PIPE,
                     stdout=subprocess.PIPE,
                     stderr=subprocess.PIPE
                 )
                 out, _ = process.communicate()
                 assert any(req.startswith('requests') for req in out.splitlines()) is True
Exemplo n.º 43
0
def test_mirror_uninstall(PipenvInstance, pypi):
    with temp_environ(), PipenvInstance(chdir=True) as p:

        mirror_url = os.environ.pop(
            "PIPENV_TEST_INDEX", "https://pypi.python.org/simple"
        )
        assert "pypi.org" not in mirror_url

        c = p.pipenv("install requests --pypi-mirror {0}".format(mirror_url))
        assert c.return_code == 0
        assert "requests" in p.pipfile["packages"]
        assert "requests" in p.lockfile["default"]
        assert "chardet" in p.lockfile["default"]
        assert "idna" in p.lockfile["default"]
        assert "urllib3" in p.lockfile["default"]
        assert "certifi" in p.lockfile["default"]
        # Ensure the --pypi-mirror parameter hasn't altered the Pipfile or Pipfile.lock sources
        assert len(p.pipfile["source"]) == 1
        assert len(p.lockfile["_meta"]["sources"]) == 1
        assert "https://pypi.org/simple" == p.pipfile["source"][0]["url"]
        assert "https://pypi.org/simple" == p.lockfile["_meta"]["sources"][0]["url"]

        c = p.pipenv("uninstall requests --pypi-mirror {0}".format(mirror_url))
        assert c.return_code == 0
        assert "requests" not in p.pipfile["dev-packages"]
        assert "requests" not in p.lockfile["develop"]
        assert "chardet" not in p.lockfile["develop"]
        assert "idna" not in p.lockfile["develop"]
        assert "urllib3" not in p.lockfile["develop"]
        assert "certifi" not in p.lockfile["develop"]
        # Ensure the --pypi-mirror parameter hasn't altered the Pipfile or Pipfile.lock sources
        assert len(p.pipfile["source"]) == 1
        assert len(p.lockfile["_meta"]["sources"]) == 1
        assert "https://pypi.org/simple" == p.pipfile["source"][0]["url"]
        assert "https://pypi.org/simple" == p.lockfile["_meta"]["sources"][0]["url"]

        c = p.pipenv("run python -m requests.help")
        assert c.return_code > 0
Exemplo n.º 44
0
def test_multiprocess_bug_and_install(PipenvInstance, pypi):
    with temp_environ():
        os.environ["PIPENV_MAX_SUBPROCESS"] = "2"

        with PipenvInstance(pypi=pypi, chdir=True) as p:
            with open(p.pipfile_path, "w") as f:
                contents = """
[packages]
pytz = "*"
six = "*"
urllib3 = "*"
                """.strip()
                f.write(contents)

            c = p.pipenv("install")
            assert c.return_code == 0

            assert "pytz" in p.lockfile["default"]
            assert "six" in p.lockfile["default"]
            assert "urllib3" in p.lockfile["default"]

            c = p.pipenv('run python -c "import six; import pytz; import urllib3;"')
            assert c.return_code == 0
Exemplo n.º 45
0
    def test_resolve_system_python_no_virtualenv(self):
        """Ensure we don't error out when we are in a folder off of / and doing an install using --system,
        which used to cause the resolver and PIP_PYTHON_PATH to point at /bin/python
        
        Sample dockerfile:
        FROM python:3.6-alpine3.6

        RUN set -ex && pip install pipenv --upgrade
        RUN set -ex && mkdir /app
        COPY Pipfile /app/Pipfile

        WORKDIR /app
        """
        with temp_environ():
            os.environ['PIPENV_IGNORE_VIRTUALENVS'] = '1'
            os.environ['PIPENV_USE_SYSTEM'] = '1'
            with PipenvInstance(chdir=True) as p:
                os.chdir('/tmp')
                c = p.pipenv('install --system xlrd')
                assert c.return_code == 0
                for fn in ['Pipfile', 'Pipfile.lock']:
                    path = os.path.join('/tmp', fn)
                    if os.path.exists(path):
                        os.unlink(path)
Exemplo n.º 46
0
def test_environment_variable_value_does_not_change_hash(PipenvInstance, pypi):
    with PipenvInstance(chdir=True, pypi=pypi) as p:
        with temp_environ():
            with open(p.pipfile_path, 'w') as f:
                f.write("""
[[source]]
url = 'https://${PYPI_USERNAME}:${PYPI_PASSWORD}@pypi.org/simple'
verify_ssl = true
name = 'pypi'
[requires]
python_version = '2.7'
[packages]
flask = "==0.12.2"
""")
            project = Project()

            os.environ['PYPI_USERNAME'] = '******'
            os.environ['PYPI_PASSWORD'] = '******'
            assert project.get_lockfile_hash() is None

            c = p.pipenv('install')
            lock_hash = project.get_lockfile_hash()
            assert lock_hash is not None
            assert lock_hash == project.calculate_pipfile_hash()

            # sanity check on pytest
            assert 'PYPI_USERNAME' not in str(pipfile.load(p.pipfile_path))
            assert c.return_code == 0
            assert project.get_lockfile_hash() == project.calculate_pipfile_hash()

            os.environ['PYPI_PASSWORD'] = '******'
            assert project.get_lockfile_hash() == project.calculate_pipfile_hash()

            with open(p.pipfile_path, 'a') as f:
                f.write('requests = "==2.14.0"\n')
            assert project.get_lockfile_hash() != project.calculate_pipfile_hash()
Exemplo n.º 47
0
def test_bad_mirror_install(PipenvInstance, pypi):
    with temp_environ(), PipenvInstance(chdir=True) as p:
        # This demonstrates that the mirror parameter is being used
        os.environ.pop("PIPENV_TEST_INDEX", None)
        c = p.pipenv("install requests --pypi-mirror https://pypi.example.org")
        assert c.return_code != 0