Ejemplo n.º 1
0
def test_trash_store_dir(tmp_path: pathlib.Path) -> None:
    """Test moving whole directory to trash."""
    test_utils.ensure_trestle_config_dir(tmp_path)

    # trash whole directory
    data_dir: pathlib.Path = tmp_path / 'data'
    data_dir.mkdir(exist_ok=True, parents=True)
    readme_file: pathlib.Path = data_dir / 'readme.md'
    readme_file.touch()

    # trash with deleting original
    assert not trash.to_trash_dir_path(data_dir).exists()
    assert not trash.to_trash_file_path(readme_file).exists()
    trash.store_dir(data_dir, True)
    assert data_dir.exists() is False
    assert readme_file.exists() is False
    assert trash.to_trash_dir_path(data_dir).exists()
    assert trash.to_trash_file_path(readme_file).exists()

    # trash without deleting original
    data_dir.mkdir(exist_ok=True, parents=True)
    readme_file.touch()
    trash.store_dir(data_dir, False)
    assert data_dir.exists()
    assert readme_file.exists()
    assert trash.to_trash_dir_path(data_dir).exists()
    assert trash.to_trash_file_path(readme_file).exists()
Ejemplo n.º 2
0
def test_to_origin_dir_path(tmp_path: pathlib.Path) -> None:
    """Test to origin dir path function."""
    # invalid trestle project would error
    with pytest.raises(AssertionError):
        trash.to_origin_dir_path(tmp_path)

    test_utils.ensure_trestle_config_dir(tmp_path)
    trash_dir_path = trash.to_trash_dir_path(tmp_path)

    # missing trash path would error
    with pytest.raises(AssertionError):
        trash.to_origin_dir_path(trash_dir_path)

    (tmp_path / trash.TRESTLE_TRASH_DIR).mkdir(exist_ok=True, parents=True)
    origin_dir = trash.to_origin_dir_path(trash_dir_path)
    assert tmp_path.resolve() == origin_dir.resolve()

    data_dir = tmp_path / 'data'
    trash_dir_path = trash.to_trash_dir_path(data_dir)
    origin_dir = trash.to_origin_dir_path(trash_dir_path)
    assert data_dir.resolve() == origin_dir.resolve()

    # invalid trash path should error
    with pytest.raises(AssertionError):
        trash.to_origin_dir_path(data_dir)

    # trash file path should error
    tmp_file = tmp_path / 'temp_file.md'
    trash_file_path = trash.to_trash_file_path(tmp_file)
    with pytest.raises(AssertionError):
        trash.to_origin_dir_path(trash_file_path)
Ejemplo n.º 3
0
def test_remove_path_file(tmp_path: pathlib.Path) -> None:
    """Test remove path with content clear option."""
    tmp_data_dir = tmp_path.joinpath('data')
    tmp_data_file = tmp_data_dir.joinpath('readme.md')
    tmp_data_dir.mkdir(exist_ok=True, parents=True)

    # not a valid trestle project should error in constructor
    with pytest.raises(TrestleError):
        rpa = RemovePathAction(tmp_data_file)

    # create trestle project
    test_utils.ensure_trestle_config_dir(tmp_path)
    rpa = RemovePathAction(tmp_data_file)

    # missing file should error
    # with pytest.raises(TrestleError): # noqa: E800  remove path is not working properly
    #    rpa.execute()                  # noqa: E800

    # write some content in the file
    file_pos = 0
    dummy_data = 'DUMMY DATA'
    with open(tmp_data_file, 'a+', encoding=const.FILE_ENCODING) as fp:
        fp.write(dummy_data)
        file_pos = fp.tell()
    assert file_pos >= 0

    # remove file
    tmp_data_file_trash = trash.to_trash_file_path(tmp_data_file)
    assert tmp_data_file_trash.exists() is False
    rpa.execute()
    tmp_data_file_trash.exists()
    assert tmp_data_file.exists() is False

    # rollback file
    rpa.rollback()
    tmp_data_file_trash.exists()
    tmp_data_file.exists()
    with open(tmp_data_file, 'a+', encoding=const.FILE_ENCODING) as fp:
        assert file_pos == fp.tell()

    # remove dir
    rpa = RemovePathAction(tmp_data_dir)
    tmp_data_trash = trash.to_trash_dir_path(tmp_data_dir)
    tmp_data_file_trash = trash.to_trash_file_path(tmp_data_file)
    if tmp_data_trash.exists():
        tmp_data_trash.rmdir()
    rpa.execute()
    assert tmp_data_trash.exists()
    assert tmp_data_file_trash.exists()
    assert tmp_data_file.exists() is False
    assert tmp_data_dir.exists() is False

    # rollback dir
    rpa.rollback()
    assert tmp_data_trash.exists() is False
    assert tmp_data_file_trash.exists() is False
    assert tmp_data_file.exists()
    assert tmp_data_dir.exists()
Ejemplo n.º 4
0
def test_to_trash_path(tmp_path: pathlib.Path) -> None:
    """Test to trash path function."""
    data_dir = tmp_path / 'data'
    data_dir.mkdir(exist_ok=True, parents=True)
    readme_file = data_dir / 'readme.md'
    readme_file.touch()

    test_utils.ensure_trestle_config_dir(tmp_path)

    assert trash.to_trash_file_path(readme_file) == trash.to_trash_path(
        readme_file)
    assert trash.to_trash_dir_path(readme_file.parent) == trash.to_trash_path(
        readme_file.parent)
Ejemplo n.º 5
0
def test_trash_store(tmp_path: pathlib.Path) -> None:
    """Test trash store function."""
    test_utils.ensure_trestle_config_dir(tmp_path)
    data_dir: pathlib.Path = tmp_path / 'data'
    data_dir.mkdir(exist_ok=True, parents=True)
    readme_file: pathlib.Path = data_dir / 'readme.md'
    readme_file.touch()

    # trash using common trash method
    trash.store(readme_file, True)
    assert readme_file.exists() is False
    assert data_dir.exists()
    assert trash.to_trash_dir_path(data_dir).exists()
    assert trash.to_trash_file_path(readme_file).exists()

    # trash whole directory
    data_dir.mkdir(exist_ok=True, parents=True)
    readme_file.touch()
    trash.store(data_dir, True)
    assert data_dir.exists() is False
    assert readme_file.exists() is False
    assert trash.to_trash_dir_path(data_dir).exists()
    assert trash.to_trash_file_path(readme_file).exists()
Ejemplo n.º 6
0
def test_to_origin_path(tmp_path: pathlib.Path) -> None:
    """Test to origin path function."""
    test_utils.ensure_trestle_config_dir(tmp_path)
    (tmp_path / trash.TRESTLE_TRASH_DIR).mkdir(exist_ok=True, parents=True)

    tmp_file = tmp_path / 'temp_file.md'
    trash_file_path = trash.to_trash_file_path(tmp_file)
    origin_file_path = trash.to_origin_path(trash_file_path)
    assert tmp_file.resolve() == origin_file_path.resolve()

    data_dir = tmp_path / 'data'
    trash_dir_path = trash.to_trash_dir_path(data_dir)
    origin_dir = trash.to_origin_path(trash_dir_path)
    assert data_dir.resolve() == origin_dir.resolve()
Ejemplo n.º 7
0
def test_to_trash_dir_path(tmp_path: pathlib.Path) -> None:
    """Test to_trash_dir_path method."""
    tmp_file: pathlib.Path = tmp_path / 'tmp_file.md'
    tmp_file.touch()

    data_dir: pathlib.Path = tmp_path / 'data'
    data_dir.mkdir(exist_ok=True, parents=True)
    readme_file: pathlib.Path = data_dir / 'readme.md'
    readme_file.touch()

    with pytest.raises(AssertionError):
        trash.to_trash_file_path(readme_file)

    test_utils.ensure_trestle_config_dir(tmp_path)

    # trestle root will use the trash root
    assert trash.to_trash_dir_path(tmp_path).name == pathlib.Path(
        trash.TRESTLE_TRASH_DIR).name
    assert trash.to_trash_dir_path(tmp_path).parent.name == pathlib.Path(
        trash.TRESTLE_TRASH_DIR).parent.name

    # any directory under trestle rool will have the trash as the parent
    assert trash.to_trash_dir_path(data_dir).parent.name == pathlib.Path(
        trash.TRESTLE_TRASH_DIR).name
Ejemplo n.º 8
0
def test_to_trash_file_path(tmp_path: pathlib.Path) -> None:
    """Test to_trash_file_path method."""
    tmp_file: pathlib.Path = tmp_path / 'tmp_file.md'
    tmp_file.touch()

    data_dir: pathlib.Path = tmp_path / 'data'
    data_dir.mkdir(exist_ok=True, parents=True)
    readme_file: pathlib.Path = data_dir / 'readme.md'
    readme_file.touch()

    with pytest.raises(AssertionError):
        trash.to_trash_file_path(readme_file)

    test_utils.ensure_trestle_config_dir(tmp_path)

    assert trash.to_trash_file_path(tmp_file) is not None
    assert trash.to_trash_file_path(
        tmp_file).parent == trash.to_trash_dir_path(tmp_path)
    assert trash.to_trash_file_path(
        readme_file).parent.name == f'data{trash.TRESTLE_TRASH_DIR_EXT}'