Ejemplo n.º 1
0
def _test_archive(
    tmp_path: Path,
    archive_file: Path,
    *sources: t.Union[Dir, File],
    iteratee: t.Callable[[Path], t.Union[str, Path, sh.Ls]] = lambda p: p,
    ext: str = "",
    skip_extraction: bool = False,
):
    src_dir = create_archive_source(tmp_path, *sources)
    sh.archive(archive_file,
               *(iteratee(source.path) for source in src_dir.items),
               ext=ext)
    assert archive_file.is_file()

    if skip_extraction:
        return

    dst_path = tmp_path / "dst"
    if len(src_dir.items) > 1:
        extracted_src_path = dst_path / src_dir.path.name
    else:
        extracted_src_path = dst_path

    extract_archive(archive_file, dst_path, ext=ext)

    assert dst_path.is_dir()
    assert extracted_src_path.is_dir()
    assert is_same_dir(src_dir.path, extracted_src_path)
Ejemplo n.º 2
0
def test_archive__raises_when_add_fails(tmp_path: Path, rep_ext: str):
    src_dir = create_archive_source(tmp_path, File("1.txt", text="1"))

    with ExitStack() as mock_stack:
        mock_stack.enter_context(
            mock.patch("tarfile.TarFile.add", side_effect=Exception))
        mock_stack.enter_context(
            mock.patch("zipfile.ZipFile.write", side_effect=Exception))

        with pytest.raises(sh.ArchiveError):
            sh.archive(tmp_path / f"archive{rep_ext}", src_dir.path)
Ejemplo n.º 3
0
def test_archive__uses_custom_root_path_inside_archive(
    tmp_path: Path,
    rep_ext: str,
    source: t.Union[File, Dir],
    root: Path,
    expected_listing: t.Set[Path],
):
    src_dir = create_archive_source(tmp_path, source)
    root = src_dir.path / root

    archive_file = tmp_path / f"archive{rep_ext}"
    sh.archive(archive_file, *(item.path for item in src_dir.items), root=root)
    assert archive_file.is_file()

    listing = set(sh.lsarchive(archive_file))
    assert listing == expected_listing
Ejemplo n.º 4
0
def test_archive__archives_relative_paths(
    tmp_path: Path,
    rep_ext: str,
    source: t.Union[File, Dir],
    root: t.Optional[Path],
    expected_listing: t.Set[Path],
):
    src_dir = create_archive_source(tmp_path, source)
    archive_file = tmp_path / f"archive{rep_ext}"

    with sh.cd(src_dir.path):
        items = [item.path.relative_to(src_dir.path) for item in src_dir.items]
        sh.archive(archive_file, *items, root=root)

    assert archive_file.is_file()

    listing = set(sh.lsarchive(archive_file))
    assert listing == expected_listing
Ejemplo n.º 5
0
def test_archive__repaths_paths_inside_archive(
    tmp_path: Path,
    rep_ext: str,
    sources: t.List[t.Union[File, Dir]],
    paths: t.List[t.Union[str, Path, sh.Ls]],
    root: t.Optional[Path],
    repath: t.Optional[t.Union[str, dict]],
    expected_listing: t.Set[Path],
):
    src_dir = create_archive_source(tmp_path, *sources)
    archive_file = tmp_path / f"archive{rep_ext}"

    with sh.cd(src_dir.path):
        sh.archive(archive_file, *paths, root=root, repath=repath)

    assert archive_file.is_file()

    listing = set(sh.lsarchive(archive_file))
    assert listing == expected_listing
Ejemplo n.º 6
0
def test_archive__raises_when_repath_is_bad_type(tmp_path: Path, paths: list,
                                                 repath: t.Any,
                                                 expected_error: str):
    with pytest.raises(TypeError) as exc_info:
        sh.archive(tmp_path / "archive.tar", *paths, repath=repath)
    assert expected_error in str(exc_info.value)
Ejemplo n.º 7
0
def test_archive__raises_when_file_extension_not_supported(tmp_path: Path):
    with pytest.raises(NotImplementedError) as exc_info:
        sh.archive(tmp_path / "test.txt")
    assert "format not supported" in str(exc_info.value)
Ejemplo n.º 8
0
def test_archive__raises_when_sources_are_not_subpaths_of_root_path(
        tmp_path: Path, rep_ext: str):
    archive_file = tmp_path / f"archive{rep_ext}"
    with pytest.raises(ValueError) as exc_info:
        sh.archive(archive_file, tmp_path, root="bad-root")
    assert "paths must be a subpath of the root" in str(exc_info.value)