コード例 #1
0
def _validate_directory_with_contents(
        path: str, symlinks_replaced_with_files: bool = False) -> bool:
    """Check that the contents of a directory\
    created with `_create_directory_with_contents()` is OK."""
    assert file_utils.dir_exists_ne(path).unwrap()
    subdirs = ["subdir2", "subdir3", "subdir4"]
    for s in subdirs:
        s_path = join(path, s)
        assert file_utils.dir_exists_ne(s_path).unwrap()
        f_path = join(s_path, "dummy.txt")
        assert file_utils.file_exists_ne(f_path).unwrap()

    subsubdir = join(path, "subdir", "sub-subdir")
    assert file_utils.dir_exists_ne(subsubdir).unwrap()

    assert file_utils.file_exists_ne(join(path,
                                          "dummy_text_file.txt")).unwrap()

    hidden_text_file = join(path, ".hidden_text_file")
    assert file_utils.file_exists_ne(hidden_text_file).unwrap()

    assert file_utils.file_exists_ne(join(subsubdir,
                                          ".hidden_text_file2")).unwrap()

    symlink1 = join(subsubdir, ".hidden_text_file")
    if not symlinks_replaced_with_files:
        assert file_utils.symlink_exists_ne(symlink1).unwrap()
    else:
        assert file_utils.file_exists_ne(symlink1).unwrap()
    return True
コード例 #2
0
def test_get_subdir_list_recursively_ne(existing_dir: str,
                                        existing_text_file: str) -> None:
    root_dir = join(existing_dir, "root_for_get_subdir_list_recursively_ne")
    _create_directory_with_contents(root_dir, existing_text_file)
    assert file_utils.dir_exists_ne(root_dir).unwrap()

    # Ok(List[str]) when getting sorted list of subdirs
    subdirs = file_utils.get_subdir_list_recursively_ne(root_dir).unwrap()
    assert subdirs == sorted([
        "subdir", "subdir2", "subdir3", "subdir4",
        join("subdir", "sub-subdir")
    ])

    # Ok(List[str]) when getting unsorted list of subdirs
    subdirs = file_utils.get_subdir_list_recursively_ne(root_dir,
                                                        sort=False).unwrap()
    assert len(subdirs) == 5

    # Ignored sub-directories are excluded from the result
    ignore = ["subdir3", join("subdir", "sub-subdir"), ""]
    subdirs = file_utils.get_subdir_list_recursively_ne(
        root_dir, ignore_list=ignore).unwrap()
    assert subdirs == sorted(["subdir", "subdir2", "subdir4"])

    # negative path

    # TypeError when path is not a directory
    assert (file_utils.get_subdir_list_recursively_ne(
        existing_text_file).unwrap_err().kind_is(ErrorKind.TypeError))

    # FileNotFoundError when path not exists
    assert (file_utils.get_subdir_list_recursively_ne(
        NOT_EXISTING_DIR).unwrap_err().kind_is(ErrorKind.FileNotFoundError))
コード例 #3
0
def test_get_subdir_list_ne(existing_dir: str,
                            existing_text_file: str) -> None:
    root_dir = join(existing_dir, "root_for_get_subdir_list_ne")
    _create_directory_with_contents(root_dir, existing_text_file)
    assert file_utils.dir_exists_ne(root_dir).unwrap()

    # Ok(List[str]) when getting sorted list of subdirs
    first_level_subdirs = file_utils.get_subdir_list_ne(root_dir).unwrap()
    assert first_level_subdirs == ["subdir", "subdir2", "subdir3", "subdir4"]

    # Ok(List[str]) when getting unsorted list of subdirs
    first_level_subdirs = file_utils.get_subdir_list_ne(root_dir,
                                                        sort=False).unwrap()
    assert len(first_level_subdirs) == 4

    # negative path

    # TypeError when path is not a directory
    assert (
        file_utils.get_subdir_list_ne(existing_text_file).unwrap_err().kind_is(
            ErrorKind.TypeError))

    # FileNotFoundError when path not exists
    assert (
        file_utils.get_subdir_list_ne(NOT_EXISTING_DIR).unwrap_err().kind_is(
            ErrorKind.FileNotFoundError))
コード例 #4
0
def test_get_item_count_ne(existing_dir: str, existing_text_file: str) -> None:
    root_dir = join(existing_dir, "root_for_test_get_item_count_ne")
    _create_directory_with_contents(root_dir, existing_text_file)
    assert file_utils.dir_exists_ne(root_dir).unwrap()

    # Ok(int) when getting item count of an existing directory
    count = file_utils.get_item_count_ne(root_dir).unwrap()
    assert count == 6  # 4 directories and 2 files
コード例 #5
0
def test_dir_exists_ne(existing_text_file: str, existing_dir: str,
                       existing_text_file_symlink: str) -> None:
    # Ok(True) if dir exists
    assert file_utils.dir_exists_ne(existing_dir).expect(
        "error while checking for dir existence")
    # Ok(False) if dir does not exist
    assert not file_utils.dir_exists_ne(NOT_EXISTING_DIR).expect(
        "error while checking for existence of non-existent dir")

    # negative path

    # fail if it is a file
    assert (file_utils.dir_exists_ne(existing_text_file).unwrap_err().kind_is(
        ErrorKind.TypeError))

    # fail if it is a symlink
    assert (file_utils.dir_exists_ne(
        existing_text_file_symlink).unwrap_err().kind_is(ErrorKind.TypeError))
コード例 #6
0
def test_gzip_tree_ne(existing_dir: str, existing_text_file: str) -> None:

    dummy_dir = join(existing_dir, "root_for_test_gzip_tree_ne")
    _create_directory_with_contents(dummy_dir, existing_text_file)
    assert file_utils.dir_exists_ne(dummy_dir).unwrap()

    dummy_dir_gz = join(existing_dir, "dummy_dir.tar.gz")
    dummy_dir_bz2 = join(existing_dir, "dummy_dir.tar.bz2")
    assert file_utils.gzip_tree_ne(dummy_dir,
                                   dest=dummy_dir_gz,
                                   arch_type="gz").is_ok()

    assert file_utils.gzip_tree_ne(dummy_dir,
                                   dest=dummy_dir_bz2,
                                   arch_type="bz2",
                                   remove_src=True).is_ok()

    assert not file_utils.dir_exists_ne(dummy_dir).unwrap()

    # Ok(None) when extracting existing archive into not-existing directory
    gz_dir_extract_root = join(existing_dir, "gz_dir_extract_root")
    assert file_utils.extract_gzip_archive_ne(
        dummy_dir_gz, dest=gz_dir_extract_root).is_ok()

    # FileExistsError when extracting existing archive into existing directory
    # and overwrite=False
    assert (file_utils.extract_gzip_archive_ne(
        dummy_dir_gz, dest=gz_dir_extract_root).unwrap_err().kind_is(
            ErrorKind.FileExistsError))

    # Ok(None) when extracting existing archive into existing directory
    # and overwrite=True
    assert file_utils.extract_gzip_archive_ne(dummy_dir_gz,
                                              dest=gz_dir_extract_root,
                                              overwrite=True,
                                              remove_src=True).is_ok()

    # ReadError when src has wrong format
    assert (file_utils.extract_gzip_archive_ne(
        existing_text_file, dest=gz_dir_extract_root,
        overwrite=True).unwrap_err().kind_is("ReadError"))
コード例 #7
0
def test_get_file_list_ne(existing_dir: str, existing_text_file: str) -> None:
    root_dir = join(existing_dir, "root_for_get_file_list_ne")
    _create_directory_with_contents(root_dir, existing_text_file)
    assert file_utils.dir_exists_ne(root_dir).unwrap()
    # Ok(List[str]) when getting sorted list of files
    first_level_files = file_utils.get_file_list_ne(root_dir).unwrap()
    assert first_level_files == [".hidden_text_file", "dummy_text_file.txt"]

    # Ok(List[str]) when getting unsorted list of files
    first_level_files = file_utils.get_file_list_ne(root_dir,
                                                    sort=False).unwrap()
    assert len(first_level_files) == 2
コード例 #8
0
def test_remove_dir_contents_ne(existing_dir: str,
                                existing_text_file: str) -> None:
    root_dir = join(existing_dir, "root_for_remove_dir_contents")
    _create_directory_with_contents(root_dir, existing_text_file)

    assert not file_utils.dir_empty_ne(root_dir).unwrap()
    assert file_utils.remove_dir_contents_ne(root_dir).is_ok()
    assert file_utils.dir_exists_ne(root_dir).unwrap()
    assert file_utils.dir_empty_ne(root_dir).unwrap()

    # negative tests

    # `FileNotFoundError` if path not valid
    assert (
        file_utils.remove_dir_contents_ne(INVALID_PATH).unwrap_err().kind_is(
            ErrorKind.FileNotFoundError))
コード例 #9
0
def test_move_tree_ne(existing_dir: str, existing_text_file: str) -> None:
    root_dir = join(existing_dir, "root_for_move_tree")
    _create_directory_with_contents(root_dir, existing_text_file)
    moved_dir = join(existing_dir, "moved_tree")

    # Ok(None) when moving existing dir to not-existing dest
    assert file_utils.move_tree_ne(root_dir, moved_dir).is_ok()

    assert not file_utils.dir_exists_ne(root_dir).unwrap()
    assert _validate_directory_with_contents(moved_dir)

    # Ok(None) when destination directory already exists
    # and overwrite=True
    assert file_utils.copy_tree_ne(moved_dir, root_dir).is_ok()
    assert file_utils.move_tree_ne(root_dir, moved_dir, overwrite=True).is_ok()

    # negative path
    # FileNotFoundError when src does not exist
    assert (file_utils.move_tree_ne(NOT_EXISTING_DIR,
                                    join(existing_dir,
                                         "moved_tree_2")).unwrap_err().kind_is(
                                             ErrorKind.FileNotFoundError))

    # FileExistsError when destination directory already exists
    # and overwrite=False
    assert file_utils.copy_tree_ne(moved_dir, root_dir).is_ok()
    assert (file_utils.move_tree_ne(root_dir, moved_dir).unwrap_err().kind_is(
        ErrorKind.FileExistsError))

    # FileExistsError when destination directory already exists
    # and overwrite=False
    assert (file_utils.move_tree_ne(root_dir, moved_dir).unwrap_err().kind_is(
        ErrorKind.FileExistsError))

    # TypeError when src is not a directory
    assert (file_utils.move_tree_ne(existing_text_file,
                                    join(existing_dir,
                                         "moved_tree_3")).unwrap_err().kind_is(
                                             ErrorKind.TypeError))

    # TypeError when dest is not a directory
    assert (file_utils.move_tree_ne(root_dir,
                                    existing_text_file).unwrap_err().kind_is(
                                        ErrorKind.TypeError))
コード例 #10
0
def test_create_remove_dir_ne(existing_dir: str, existing_text_file: str,
                              existing_text_file_symlink: str) -> None:
    valid_path = join(existing_dir, "valid", "path")
    # Ok(None) when created a new path
    assert file_utils.create_path_ne(valid_path).is_ok()
    assert file_utils.dir_exists_ne(valid_path).unwrap()

    # Ok(None) when trying to create path and it already exists
    # and `overwrite` is `True`
    assert file_utils.create_path_ne(valid_path, overwrite=True).is_ok()

    # Ok(None) when removing existent directory
    assert file_utils.remove_dir_ne(valid_path).is_ok()

    # Ok(None) when removing non-existent directory and `fail_if_not_exists=False`
    assert file_utils.remove_dir_ne(valid_path).is_ok()

    # negative path

    # `FileNotFoundError` when removing non-existent directory
    # and `fail_if_not_exists=True`
    assert (file_utils.remove_dir_ne(
        valid_path, fail_if_not_exists=True).unwrap_err().kind_is(
            ErrorKind.FileNotFoundError))
    # `TypeError` when trying to remove a file
    assert (file_utils.remove_dir_ne(existing_text_file).unwrap_err().kind_is(
        ErrorKind.TypeError))

    # `TypeError` when trying to remove a symlink
    assert (file_utils.remove_dir_ne(
        existing_text_file_symlink).unwrap_err().kind_is(ErrorKind.TypeError))

    # `TypeError` when trying to create path and file with same name exists
    assert (file_utils.create_path_ne(existing_text_file).unwrap_err().kind_is(
        ErrorKind.TypeError))

    # `FileExistsError` when trying to create path and it already exists
    # and `overwrite` is `False`
    assert (file_utils.create_path_ne(existing_dir).unwrap_err().kind_is(
        ErrorKind.FileExistsError))