Exemplo n.º 1
0
def test_venv_in_project(true_value, PipenvInstance):
    with temp_environ():
        os.environ['PIPENV_VENV_IN_PROJECT'] = true_value
        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.º 2
0
def test_empty_venv_file(PipenvInstance):
    """Tests virtualenv creation when an empty .venv file exists at the project root
    """
    with PipenvInstance(chdir=True) as p:
        file_path = os.path.join(p.path, '.venv')
        with open(file_path, 'w'):
            pass

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

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

            c = p.pipenv('--venv')
            assert c.returncode == 0
            venv_loc = Path(c.stdout.strip()).absolute()
            assert venv_loc.exists()
            assert venv_loc.joinpath('.project').exists()
            from pathlib import PurePosixPath
            venv_path = normalize_drive(venv_loc.as_posix())
            venv_path_parent = str(PurePosixPath(venv_path).parent)
            assert venv_path_parent == Path(workon_home).absolute().as_posix()
Exemplo n.º 3
0
def test_venv_file(venv_name, PipenvInstance):
    """Tests virtualenv creation when a .venv file exists at the project root
    and contains a venv name.
    """
    with PipenvInstance(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
            if 'PIPENV_VENV_IN_PROJECT' in os.environ:
                del os.environ['PIPENV_VENV_IN_PROJECT']

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

            c = p.pipenv('--venv')
            assert c.returncode == 0
            venv_loc = Path(c.stdout.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).joinpath(
                    venv_name).absolute().as_posix()
            assert venv_path == normalize_drive(venv_expected_path)
Exemplo n.º 4
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.º 5
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.º 6
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(f"install Django --pypi-mirror {mirror_url}")
        assert c.returncode == 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.returncode == 0

        c = p.pipenv(f"uninstall Django --pypi-mirror {mirror_url}")
        assert c.returncode == 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.returncode > 0
Exemplo n.º 7
0
 def test_ssh_dependency_links_install(self, PipenvInstance):
     with temp_environ(), PipenvInstance(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.º 8
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:
            c = p.pipenv('run pip freeze')
            assert c.returncode == 0
            c = p.pipenv('--venv')
            assert c.returncode == 0
            venv_path = c.stdout.strip()
            assert os.path.isdir(venv_path)
Exemplo n.º 9
0
 def test_https_dependency_links_install(self, PipenvInstance):
     """Ensure dependency_links are parsed and installed (needed for private repo dependencies).
     """
     with temp_environ(), PipenvInstance(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.º 10
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.º 11
0
def test_venv_at_project_root(true_value, PipenvInstance):
    with temp_environ():
        with PipenvInstance(chdir=True) as p:
            os.environ['PIPENV_VENV_IN_PROJECT'] = true_value
            c = p.pipenv('install')
            assert c.returncode == 0
            assert normalize_drive(p.path) in p.pipenv('--venv').stdout
            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').stdout
Exemplo n.º 12
0
def test_venv_in_project_disabled_ignores_venv(false_value, PipenvInstance):
    venv_name = "my_project"
    with temp_environ():
        os.environ['PIPENV_VENV_IN_PROJECT'] = false_value
        with PipenvInstance() 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
                c = p.pipenv('install requests')
                assert c.returncode == 0
                c = p.pipenv('--venv')
                assert c.returncode == 0
                venv_loc = Path(c.stdout.strip()).absolute()
                assert venv_loc.exists()
                assert venv_loc.joinpath('.project').exists()
                venv_path = normalize_drive(venv_loc.as_posix())
                venv_expected_path = Path(workon_home).joinpath(
                    venv_name).absolute().as_posix()
                assert venv_path == normalize_drive(venv_expected_path)
Exemplo n.º 13
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.º 14
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.º 15
0
def test_mirror_lock_sync(PipenvInstance):
    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("""
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[packages]
six = "*"
            """.strip())
        c = p.pipenv(f'lock --pypi-mirror {mirror_url}')
        assert c.returncode == 0
        c = p.pipenv(f'sync --pypi-mirror {mirror_url}')
        assert c.returncode == 0
Exemplo n.º 16
0
def test_scripts(PipenvInstance):
    with PipenvInstance(chdir=True) as p:
        with open(p.pipfile_path, 'w') as f:
            f.write(r"""
[scripts]
printfoo = "python -c \"print('foo')\""
notfoundscript = "randomthingtotally"
appendscript = "cmd arg1"
multicommand = "bash -c \"cd docs && make html\""
            """)
            if os.name == "nt":
                f.write('scriptwithenv = "echo %HELLO%"\n')
            else:
                f.write('scriptwithenv = "echo $HELLO"\n')
        c = p.pipenv('install')
        assert c.returncode == 0
        c = p.pipenv('run printfoo')
        assert c.returncode == 0
        assert c.stdout.strip() == 'foo'
        assert not c.stderr.strip()

        c = p.pipenv('run notfoundscript')
        assert c.returncode != 0
        assert c.stdout == ''
        if os.name != 'nt':  # TODO: Implement this message for Windows.
            assert 'not found' in c.stderr

        project = Project()

        script = project.build_script('multicommand')
        assert script.command == 'bash'
        assert script.args == ['-c', 'cd docs && make html']

        script = project.build_script('appendscript', ['a', 'b'])
        assert script.command == 'cmd'
        assert script.args == ['arg1', 'a', 'b']

        with temp_environ():
            os.environ['HELLO'] = 'WORLD'
            c = p.pipenv("run scriptwithenv")
            assert c.returncode == 0
            if os.name != "nt":  # This doesn't work on CI windows.
                assert c.stdout.strip() == "WORLD"
Exemplo n.º 17
0
def test_get_from_env(arg, prefix, use_negation):
    negated_arg = f"NO_{arg}"
    positive_var = arg
    negative_var = negated_arg
    if prefix:
        negative_var = f"{prefix}_{negative_var}"
        positive_var = f"{prefix}_{positive_var}"
    # set the positive first
    for var_to_set, opposite_var in ((arg, negated_arg), (negated_arg, arg)):
        os.environ.pop(var_to_set, None)
        os.environ.pop(opposite_var, None)
        with temp_environ():
            is_positive = var_to_set == arg
            is_negative = not is_positive
            envvar = positive_var if is_positive else negative_var
            os.environ[envvar] = "true"
            main_expected_value = True if is_positive else None
            if use_negation and not is_positive:
                main_expected_value = False
            # use negation means if the normal variable isnt set we will check
            # for the negated version
            negative_expected_value = (True if is_negative else None)
            if is_positive:
                assert (environments.get_from_env(
                    var_to_set, prefix, check_for_negation=use_negation) is
                        main_expected_value)
                assert (environments.get_from_env(
                    opposite_var, prefix, check_for_negation=use_negation) is
                        negative_expected_value)
            else:
                # var_to_set = negative version i.e. NO_xxxx
                # opposite_var = positive_version i.e. XXXX

                # get NO_BLAH -- expecting this to be True
                assert (environments.get_from_env(
                    var_to_set, prefix, check_for_negation=use_negation) is
                        negative_expected_value)
                # get BLAH -- expecting False if checking for negation
                # but otherwise should be None
                assert (environments.get_from_env(
                    opposite_var, prefix, check_for_negation=use_negation) is
                        main_expected_value)
Exemplo n.º 18
0
def test_lock_missing_cache_entries_gets_all_hashes(PipenvInstance, tmpdir):
    """
    Test locking pathlib2 on python2.7 which needs `scandir`, but fails to resolve when
    using a fresh dependency cache.
    """

    with temp_environ():
        os.environ["PIPENV_CACHE_DIR"] = str(tmpdir.strpath)
        with PipenvInstance(chdir=True) as p:
            p._pipfile.add("pathlib2", "*")
            assert "pathlib2" in p.pipfile["packages"]
            c = p.pipenv("install")
            assert c.returncode == 0, (c.stderr, ("\n".join(
                [f"{k}: {v}\n" for k, v in os.environ.items()])))
            c = p.pipenv("lock --clear")
            assert c.returncode == 0, c.stderr
            assert "pathlib2" in p.lockfile["default"]
            assert "scandir" in p.lockfile["default"]
            assert isinstance(p.lockfile["default"]["scandir"]["hashes"], list)
            assert len(p.lockfile["default"]["scandir"]["hashes"]) > 1
Exemplo n.º 19
0
def test_venv_in_project_default_when_venv_exists(PipenvInstance):
    """Tests virtualenv creation when a .venv file exists at the project root.
    """
    with temp_environ(), PipenvInstance(chdir=True) 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)

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

            assert venv_loc.joinpath('.project').exists()
            assert venv_loc == Path(venv_path)
Exemplo n.º 20
0
def test_environment_variable_value_does_not_change_hash(PipenvInstance):
    with PipenvInstance(chdir=True) 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'

[packages]
six = "*"
""")
            project = Project()

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

            c = p.pipenv('install')
            assert c.returncode == 0
            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.returncode == 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.º 21
0
def test_multiprocess_bug_and_install(PipenvInstance):
    with temp_environ():
        os.environ["PIPENV_MAX_SUBPROCESS"] = "2"

        with PipenvInstance(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.returncode == 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.returncode == 0