示例#1
0
    def test_expand_existing_env_variables(self, tmpdir: Path,
                                           finder: PackageFinder) -> None:
        template = "https://{}:[email protected]/user/{}/archive/master.zip"

        def make_var(name: str) -> str:
            return f"${{{name}}}"

        env_vars = collections.OrderedDict([
            ("GITHUB_TOKEN", "notarealtoken"),
            ("DO_12_FACTOR", "awwyeah"),
        ])

        with open(tmpdir.joinpath("req1.txt"), "w") as fp:
            fp.write(template.format(*map(make_var, env_vars)))

        # Construct the session outside the monkey-patch, since it access the
        # env
        session = PipSession()
        with mock.patch("pip._internal.req.req_file.os.getenv") as getenv:
            getenv.side_effect = lambda n: env_vars[n]

            reqs = list(
                parse_reqfile(tmpdir.joinpath("req1.txt"),
                              finder=finder,
                              session=session))

        assert len(
            reqs) == 1, "parsing requirement file with env variable failed"

        expected_url = template.format(*env_vars.values())
        assert reqs[0].link is not None
        assert reqs[
            0].link.url == expected_url, "variable expansion in req file failed"
示例#2
0
    def test_join_lines(self, tmpdir: Path, finder: PackageFinder) -> None:
        with open(tmpdir.joinpath("req1.txt"), "w") as fp:
            fp.write("--extra-index-url url1 \\\n--extra-index-url url2")

        list(
            parse_reqfile(tmpdir.joinpath("req1.txt"),
                          finder=finder,
                          session=PipSession()))

        assert finder.index_urls == ["url1", "url2"]
示例#3
0
def test_copy2_fixed_raises_appropriate_errors(create: Callable[[str], None],
                                               error_type: Type[Exception],
                                               tmpdir: Path) -> None:
    src = tmpdir.joinpath("src")
    create(src)
    dest = tmpdir.joinpath("dest")

    with pytest.raises(error_type):
        copy2_fixed(src, dest)

    assert not dest.exists()
示例#4
0
def test_get_pyvenv_cfg_lines_for_pep_405_virtual_environment(
    monkeypatch: pytest.MonkeyPatch,
    tmpdir: Path,
    contents: Optional[str],
    expected: Optional[List[str]],
) -> None:
    monkeypatch.setattr(sys, "prefix", str(tmpdir))
    if contents is not None:
        tmpdir.joinpath("pyvenv.cfg").write_text(contents)

    assert virtualenv._get_pyvenv_cfg_lines() == expected
 def prep(self, tmpdir: Path, data: TestData) -> None:
     self.build_dir = tmpdir.joinpath("build")
     self.download_dir = tmpdir.joinpath("download")
     os.mkdir(self.build_dir)
     os.mkdir(self.download_dir)
     self.dist_file = "simple-1.0.tar.gz"
     self.dist_file2 = "simple-2.0.tar.gz"
     self.dist_path = data.packages.joinpath(self.dist_file)
     self.dist_path2 = data.packages.joinpath(self.dist_file2)
     self.dist_url = Link(path_to_url(self.dist_path))
     self.dist_url2 = Link(path_to_url(self.dist_path2))
     self.no_download = Mock(side_effect=AssertionError)
示例#6
0
    def test_req_file_parse_comment_start_of_line(
            self, tmpdir: Path, finder: PackageFinder) -> None:
        """
        Test parsing comments in a requirements file
        """
        with open(tmpdir.joinpath("req1.txt"), "w") as fp:
            fp.write("# Comment ")

        reqs = list(
            parse_reqfile(tmpdir.joinpath("req1.txt"),
                          finder=finder,
                          session=PipSession()))

        assert not reqs
示例#7
0
    def test_req_file_no_finder(self, tmpdir: Path) -> None:
        """
        Test parsing a requirements file without a finder
        """
        with open(tmpdir.joinpath("req.txt"), "w") as fp:
            fp.write("""
    --find-links https://example.com/
    --index-url https://example.com/
    --extra-index-url https://two.example.com/
    --no-use-wheel
    --no-index
            """)

        parse_reqfile(tmpdir.joinpath("req.txt"), session=PipSession())
示例#8
0
    def test_req_file_parse_egginfo_end_of_line_with_url(
            self, tmpdir: Path, finder: PackageFinder) -> None:
        """
        Test parsing comments in a requirements file
        """
        with open(tmpdir.joinpath("req1.txt"), "w") as fp:
            fp.write("https://example.com/foo.tar.gz#egg=wat")

        reqs = list(
            parse_reqfile(tmpdir.joinpath("req1.txt"),
                          finder=finder,
                          session=PipSession()))

        assert len(reqs) == 1
        assert reqs[0].name == "wat"
def clean_project(tmpdir_factory: pytest.TempdirFactory,
                  data: TestData) -> Path:
    tmpdir = Path(str(tmpdir_factory.mktemp("clean_project")))
    new_project_dir = tmpdir.joinpath("FSPkg")
    path = data.packages.joinpath("FSPkg")
    shutil.copytree(path, new_project_dir)
    return new_project_dir
示例#10
0
    def test_cache_is_enabled(self, tmpdir: Path) -> None:
        cache_directory = tmpdir.joinpath("test-cache")
        session = PipSession(cache=cache_directory)

        assert hasattr(session.adapters["https://"], "cache")

        assert session.adapters["https://"].cache.directory == cache_directory
示例#11
0
def _create_main_file(dir_path: Path,
                      name: Optional[str] = None,
                      output: Optional[str] = None) -> None:
    """
    Create a module with a main() function that prints the given output.
    """
    if name is None:
        name = "version_pkg"
    if output is None:
        output = "0.1"
    text = textwrap.dedent(f"""
        def main():
            print({output!r})
        """)
    filename = f"{name}.py"
    dir_path.joinpath(filename).write_text(text)
示例#12
0
def local_checkout(
    remote_repo: str,
    temp_path: Path,
) -> str:
    """
    :param temp_path: the return value of the tmpdir fixture, which is a
        temp directory Path object unique to each test function invocation,
        created as a sub directory of the base temp directory.
    """
    assert "+" in remote_repo
    vcs_name = remote_repo.split("+", 1)[0]
    repository_name = os.path.basename(remote_repo)

    directory = temp_path.joinpath("cache")
    repo_url_path = os.path.join(directory, repository_name)
    assert not os.path.exists(repo_url_path)

    if not os.path.exists(directory):
        os.mkdir(directory)

    if vcs_name == "svn":
        assert repository_name == "INITools"
        _create_svn_initools_repo(repo_url_path)
        repo_url_path = os.path.join(repo_url_path, "trunk")
    else:
        vcs_backend = vcs.get_backend(vcs_name)
        assert vcs_backend is not None
        vcs_backend.obtain(repo_url_path, url=hide_url(remote_repo), verbosity=0)

    return "{}+{}".format(vcs_name, path_to_url(repo_url_path))
示例#13
0
    def test_multiple_appending_options(self, tmpdir: Path,
                                        finder: PackageFinder,
                                        options: mock.Mock) -> None:
        with open(tmpdir.joinpath("req1.txt"), "w") as fp:
            fp.write("--extra-index-url url1 \n")
            fp.write("--extra-index-url url2 ")

        list(
            parse_reqfile(
                tmpdir.joinpath("req1.txt"),
                finder=finder,
                session=PipSession(),
                options=options,
            ))

        assert finder.index_urls == ["url1", "url2"]
示例#14
0
def test_log_unicode_messages(fixed_time: None, tmpdir: Path) -> None:
    """Tests that logging bytestrings and unicode objects
    don't break logging.
    """
    cmd = FakeCommandWithUnicode()
    log_path = tmpdir.joinpath("log")
    cmd.main(["fake_unicode", "--log", log_path])
示例#15
0
def test_log_file_command_error(fixed_time: None, tmpdir: Path) -> None:
    """Test the --log-file option logs (when there's an error)."""
    cmd = FakeCommand(error=True)
    log_file_path = tmpdir.joinpath("log_file")
    cmd.main(["fake", "--log-file", log_file_path])
    with open(log_file_path) as f:
        assert f.read().startswith("2019-01-17T06:00:37,040 fake")
示例#16
0
def test_log_command_success(fixed_time: None, tmpdir: Path) -> None:
    """Test the --log option logs when command succeeds."""
    cmd = FakeCommand()
    log_path = tmpdir.joinpath("log")
    cmd.main(["fake", "--log", log_path])
    with open(log_path) as f:
        assert f.read().rstrip() == "2019-01-17T06:00:37,040 fake"
示例#17
0
def test_new_resolver_conflict_requirements_file(
    tmpdir: Path, script: PipTestEnvironment
) -> None:
    create_basic_wheel_for_package(script, "base", "1.0")
    create_basic_wheel_for_package(script, "base", "2.0")
    create_basic_wheel_for_package(
        script,
        "pkga",
        "1.0",
        depends=["base==1.0"],
    )
    create_basic_wheel_for_package(
        script,
        "pkgb",
        "1.0",
        depends=["base==2.0"],
    )

    req_file = tmpdir.joinpath("requirements.txt")
    req_file.write_text("pkga\npkgb")

    result = script.pip(
        "install",
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "-r",
        req_file,
        expect_error=True,
    )

    message = "package versions have conflicting dependencies"
    assert message in result.stderr, str(result)
示例#18
0
def test_download_http_url__no_directory_traversal(mock_raise_for_status: Mock,
                                                   tmpdir: Path) -> None:
    """
    Test that directory traversal doesn't happen on download when the
    Content-Disposition header contains a filename with a ".." path part.
    """
    mock_url = "http://www.example.com/whatever.tgz"
    contents = b"downloaded"
    link = Link(mock_url)

    session = Mock()
    resp = MockResponse(contents)
    resp.url = mock_url
    resp.headers = {
        # Set the content-type to a random value to prevent
        # mimetypes.guess_extension from guessing the extension.
        "content-type": "random",
        "content-disposition": 'attachment;filename="../out_dir_file"',
    }
    session.get.return_value = resp
    download = Downloader(session, progress_bar="on")

    download_dir = tmpdir.joinpath("download")
    os.mkdir(download_dir)
    file_path, content_type = download(link, download_dir)
    # The file should be downloaded to download_dir.
    actual = os.listdir(download_dir)
    assert actual == ["out_dir_file"]
    mock_raise_for_status.assert_called_once_with(resp)
示例#19
0
def test_copy_source_tree_with_unreadable_dir_fails(clean_project: Path,
                                                    tmpdir: Path) -> None:
    target = tmpdir.joinpath("target")
    expected_files = get_filelist(clean_project)
    unreadable_file = clean_project.joinpath("bbb")
    make_unreadable_file(unreadable_file)

    with pytest.raises(shutil.Error) as e:
        _copy_source_tree(clean_project, target)

    errored_files = [err[0] for err in e.value.args[0]]
    assert len(errored_files) == 1
    assert unreadable_file in errored_files

    copied_files = get_filelist(target)
    # All files without errors should have been copied.
    assert expected_files == copied_files
示例#20
0
def script_with_launchers(
    tmpdir_factory, script_factory, common_wheels, pip_src
):
    tmpdir = Path(str(tmpdir_factory.mktemp("script_with_launchers")))
    script = script_factory(tmpdir.joinpath("workspace"))
    # Re-install pip so we get the launchers.
    script.pip_install_local('-f', common_wheels, pip_src)
    return script
示例#21
0
def test_copy_source_tree_with_socket(
        clean_project: Path, tmpdir: Path,
        caplog: pytest.LogCaptureFixture) -> None:
    target = tmpdir.joinpath("target")
    expected_files = get_filelist(clean_project)
    socket_path = str(clean_project.joinpath("aaa"))
    make_socket_file(socket_path)

    _copy_source_tree(clean_project, target)

    copied_files = get_filelist(target)
    assert expected_files == copied_files

    # Warning should have been logged.
    assert len(caplog.records) == 1
    record = caplog.records[0]
    assert record.levelname == "WARNING"
    assert socket_path in record.message
示例#22
0
def test_copy_source_tree(clean_project: Path, tmpdir: Path) -> None:
    target = tmpdir.joinpath("target")
    expected_files = get_filelist(clean_project)
    assert len(expected_files) == 3

    _copy_source_tree(clean_project, target)

    copied_files = get_filelist(target)
    assert expected_files == copied_files
示例#23
0
 def prep(self, tmpdir: Path) -> None:
     self.test_file = tmpdir.joinpath("hash.file")
     # Want this big enough to trigger the internal read loops.
     self.test_file_len = 2 * 1024 * 1024
     with open(str(self.test_file), "w") as fp:
         fp.truncate(self.test_file_len)
     self.test_file_hash = (
         "5647f05ec18958947d32874eeb788fa396a05d0bab7c1b71f112ceb7e9b31eee")
     self.test_file_hash_encoded = (
         "sha256=VkfwXsGJWJR9ModO63iPo5agXQurfBtx8RLOt-mzHu4")
示例#24
0
def pip_test_package_script(tmpdir_factory, script_factory, shared_data):
    tmpdir = Path(str(tmpdir_factory.mktemp("pip_test_package")))
    script = script_factory(tmpdir.joinpath("workspace"))
    script.pip('install', '-f', shared_data.find_links, '--no-index',
               'simple==1.0')
    script.pip(
        'install', '-e',
        'git+https://github.com/pypa/pip-test-package.git#egg=pip-test-package'
    )
    return script
示例#25
0
def virtualenv(
    virtualenv_factory: Callable[[Path], VirtualEnvironment], tmpdir: Path
) -> Iterator[VirtualEnvironment]:
    """
    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.
    """
    yield virtualenv_factory(tmpdir.joinpath("workspace", "venv"))
示例#26
0
def virtualenv_template(
    request: pytest.FixtureRequest,
    tmpdir_factory: pytest.TempdirFactory,
    pip_src: Path,
    setuptools_install: Path,
    coverage_install: Path,
) -> Iterator[VirtualEnvironment]:

    venv_type: VirtualEnvironmentType
    if request.config.getoption("--use-venv"):
        venv_type = "venv"
    else:
        venv_type = "virtualenv"

    # Create the virtual environment
    tmpdir = Path(str(tmpdir_factory.mktemp("virtualenv")))
    venv = VirtualEnvironment(tmpdir.joinpath("venv_orig"), venv_type=venv_type)

    # Install setuptools and pip.
    install_egg_link(venv, "setuptools", setuptools_install)
    pip_editable = Path(str(tmpdir_factory.mktemp("pip"))) / "pip"
    shutil.copytree(pip_src, pip_editable, symlinks=True)
    # noxfile.py is Python 3 only
    assert compileall.compile_dir(
        str(pip_editable),
        quiet=1,
        rx=re.compile("noxfile.py$"),
    )
    subprocess.check_call(
        [venv.bin / "python", "setup.py", "-q", "develop"], cwd=pip_editable
    )

    # Install coverage and pth file for executing it in any spawned processes
    # in this virtual environment.
    install_egg_link(venv, "coverage", coverage_install)
    # zz prefix ensures the file is after easy-install.pth.
    with open(venv.site / "zz-coverage-helper.pth", "a") as f:
        f.write("import coverage; coverage.process_startup()")

    # Drop (non-relocatable) launchers.
    for exe in os.listdir(venv.bin):
        if not (
            exe.startswith("python")
            or exe.startswith("libpy")  # Don't remove libpypy-c.so...
        ):
            (venv.bin / exe).unlink()

    # Enable user site packages.
    venv.user_site_packages = True

    # Rename original virtualenv directory to make sure
    # it's not reused by mistake from one of the copies.
    venv_template = tmpdir / "venv_template"
    venv.move(venv_template)
    yield venv
示例#27
0
    def test_detect_symlink_dirs(self, monkeypatch: pytest.MonkeyPatch,
                                 tmpdir: Path) -> None:
        monkeypatch.setattr(pip._internal.req.req_uninstall, "is_local",
                            mock_is_local)

        # construct 2 paths:
        #  tmpdir/dir/file
        #  tmpdir/dirlink/file (where dirlink is a link to dir)
        d = tmpdir.joinpath("dir")
        d.mkdir()
        dlink = tmpdir.joinpath("dirlink")
        os.symlink(d, dlink)
        d.joinpath("file").touch()
        path1 = str(d.joinpath("file"))
        path2 = str(dlink.joinpath("file"))

        ups = UninstallPathSet(dist=Mock())
        ups.add(path1)
        ups.add(path2)
        assert ups._paths == {path1}
示例#28
0
def script_with_launchers(
    tmpdir_factory: pytest.TempdirFactory,
    script_factory: ScriptFactory,
    common_wheels: Path,
    pip_src: Path,
) -> PipTestEnvironment:
    tmpdir = Path(str(tmpdir_factory.mktemp("script_with_launchers")))
    script = script_factory(tmpdir.joinpath("workspace"))
    # Re-install pip so we get the launchers.
    script.pip_install_local("-f", common_wheels, pip_src)
    return script
示例#29
0
def simple_script(tmpdir_factory, script_factory, shared_data):
    tmpdir = Path(str(tmpdir_factory.mktemp("pip_test_package")))
    script = script_factory(tmpdir.joinpath("workspace"))
    script.pip(
        'install',
        '-f',
        shared_data.find_links,
        '--no-index',
        'simple==1.0',
        'simple2==3.0',
    )
    return script
示例#30
0
def script(
    tmpdir: Path,
    virtualenv: VirtualEnvironment,
    script_factory: Callable[[Path, Optional[VirtualEnvironment]], PipTestEnvironment],
) -> PipTestEnvironment:
    """
    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.PipTestEnvironment``.
    """
    return script_factory(tmpdir.joinpath("workspace"), virtualenv)