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()
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.absolute() == origin_dir.absolute() 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.absolute() == origin_dir.absolute() # 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)
def test_to_origin_dir_path(tmp_dir: pathlib.Path): """Test to origin dir path function.""" # invalid trestle project would error with pytest.raises(AssertionError): trash.to_origin_dir_path(tmp_dir) test_utils.ensure_trestle_config_dir(tmp_dir) trash_dir_path = trash.to_trash_dir_path(tmp_dir) # missing trash path would error with pytest.raises(AssertionError): trash.to_origin_dir_path(trash_dir_path) fs.ensure_directory(tmp_dir / trash.TRESTLE_TRASH_DIR) origin_dir = trash.to_origin_dir_path(trash_dir_path) assert tmp_dir.absolute() == origin_dir.absolute() data_dir = tmp_dir / 'data' trash_dir_path = trash.to_trash_dir_path(data_dir) origin_dir = trash.to_origin_dir_path(trash_dir_path) assert data_dir.absolute() == origin_dir.absolute() # invalid trash path should error with pytest.raises(AssertionError): trash.to_origin_dir_path(data_dir) # trash file path should error tmp_file = tmp_dir / '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)
def test_remove_path_file(tmp_dir: pathlib.Path): """Test remove path with content clear option.""" tmp_data_dir = tmp_dir.joinpath('data') tmp_data_file = tmp_data_dir.joinpath('readme.md') fs.ensure_directory(tmp_data_dir) # 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_dir) rpa = RemovePathAction(tmp_data_file) # missing file should error with pytest.raises(FileNotFoundError): rpa.execute() # write some content in the file file_pos = 0 dummy_data = 'DUMMY DATA' with open(tmp_data_file, 'a+') 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+') 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()
def test_to_trash_path(tmp_dir: pathlib.Path): """Test to trash path function.""" data_dir: pathlib.Path = tmp_dir / 'data' fs.ensure_directory(data_dir) readme_file: pathlib.Path = data_dir / 'readme.md' readme_file.touch() test_utils.ensure_trestle_config_dir(tmp_dir) 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)
def test_to_trash_dir_path(tmp_dir: pathlib.Path): """Test to_trash_dir_path method.""" tmp_file: pathlib.Path = tmp_dir / 'tmp_file.md' tmp_file.touch() data_dir: pathlib.Path = tmp_dir / 'data' fs.ensure_directory(data_dir) 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_dir) # trestle root will use the trash root assert trash.to_trash_dir_path(tmp_dir).name == pathlib.Path(trash.TRESTLE_TRASH_DIR).name assert trash.to_trash_dir_path(tmp_dir).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
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)
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()
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.absolute() == origin_file_path.absolute() 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.absolute() == origin_dir.absolute()
def test_to_origin_path(tmp_dir: pathlib.Path): """Test to origin path function.""" test_utils.ensure_trestle_config_dir(tmp_dir) fs.ensure_directory(tmp_dir / trash.TRESTLE_TRASH_DIR) tmp_file = tmp_dir / '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.absolute() == origin_file_path.absolute() data_dir = tmp_dir / 'data' trash_dir_path = trash.to_trash_dir_path(data_dir) origin_dir = trash.to_origin_path(trash_dir_path) assert data_dir.absolute() == origin_dir.absolute()
def test_to_trash_file_path(tmp_dir: pathlib.Path): """Test to_trash_file_path method.""" tmp_file: pathlib.Path = tmp_dir / 'tmp_file.md' tmp_file.touch() data_dir: pathlib.Path = tmp_dir / 'data' fs.ensure_directory(data_dir) 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_dir) 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_dir) assert trash.to_trash_file_path(readme_file).parent.name == f'data{trash.TRESTLE_TRASH_DIR_EXT}'