def _create_env_prep(self, ui: UI) -> EnvironmentPreparation: env_prep = EnvironmentPreparation( self._factory.create_local_filesystem(), self._factory.create_ssh_filesystem(), ui) env_prep.files_to_copy(self._files) return env_prep
def test__given_files_to_copy__but_not_preparing__should_not_do_anything(): source_fs = new_mock_filesystem(["file1.txt"]) target_fs = new_mock_filesystem() sut = EnvironmentPreparation(source_fs, target_fs) sut.files_to_copy([CopyInstruction("file1.txt", "file2.txt")]) assert target_fs.exists("file2.txt") is False
def test__given_files_to_clean__but_not_cleaning__should_not_do_anything(): source_fs = new_mock_filesystem() target_fs_spy = new_mock_filesystem(["file1.txt"]) sut = EnvironmentPreparation(source_fs, target_fs_spy) sut.files_to_clean(["file1.txt"]) assert target_fs_spy.exists("file1.txt")
def _try_prepare(self, env_prep: EnvironmentPreparation, ui: UI) -> bool: try: ui.info("Copying files...") env_prep.prepare() ui.success("Done") except (FileExistsError, FileNotFoundError) as err: self._do_rollback(env_prep, err, ui) return False return True
def test__given_rollback_done__when_rolling_back_again__should_not_do_anything( ): source_fs_spy = new_mock_filesystem(["file.txt", "funny.gif"]) target_fs = MagicMock(wraps=new_mock_filesystem()) sut = EnvironmentPreparation(source_fs_spy, target_fs) sut.files_to_copy([ CopyInstruction("file.txt", "filecopy.txt"), CopyInstruction("funny.gif", "evenfunnier.gif") ]) sut.prepare() sut.rollback() target_fs.reset_mock() sut.rollback() target_fs.delete.assert_not_called()
def __call__(self, ui: UI) -> bool: env_prep = EnvironmentPreparation( self._factory.create_local_filesystem(), self._factory.create_ssh_filesystem(), ui) self._collect_files(env_prep, ui) self._clean_files(env_prep, ui) return True
def test__given_rollback_done_with_file_not_found__when_rolling_back_again__should_try_to_delete_remaining_files( ): source_fs_spy = new_mock_filesystem(["file.txt", "funny.gif"]) target_fs_spy = MagicMock(wraps=new_mock_filesystem()) sut = EnvironmentPreparation(source_fs_spy, target_fs_spy) sut.files_to_copy([ CopyInstruction("file.txt", "filecopy.txt"), CopyInstruction("funny.gif", "evenfunnier.gif") ]) sut.prepare() target_fs_spy.delete("filecopy.txt") sut.rollback() target_fs_spy.reset_mock() sut.rollback() target_fs_spy.delete.assert_has_calls([call("filecopy.txt")])
def test__given_copied_file_not_on_target_fs__when_rolling_back__should_remove_remaining_copied_files_from_target_fs( ): source_fs_spy = new_mock_filesystem(["file.txt", "funny.gif"]) target_fs = new_mock_filesystem() sut = EnvironmentPreparation(source_fs_spy, target_fs) sut.files_to_copy([ CopyInstruction("file.txt", "filecopy.txt"), CopyInstruction("funny.gif", "evenfunnier.gif") ]) sut.prepare() target_fs.delete("filecopy.txt") sut.rollback() assert target_fs.exists("evenfunnier.gif") is False
def test__given_files_to_copy_with_non_existing_file__when_preparing_then_rollback__should_remove_copied_files_from_target_fs( ): source_fs_spy = new_mock_filesystem(["funny.gif"]) target_fs = new_mock_filesystem() sut = EnvironmentPreparation(source_fs_spy, target_fs) sut.files_to_copy([ CopyInstruction("file.txt", "filecopy.txt"), CopyInstruction("funny.gif", "evenfunnier.gif") ]) with pytest.raises(FileNotFoundError): sut.prepare() sut.rollback() assert target_fs.exists("filecopy.txt") is False
def test__given_files_to_clean_with_non_existing_files__when_cleaning__should_still_clean_remaining_files( ): source_fs_spy = new_mock_filesystem() target_fs = new_mock_filesystem(["funny.gif"]) sut = EnvironmentPreparation(source_fs_spy, target_fs) sut.files_to_clean([ "file.txt", "funny.gif", ]) sut.clean() assert target_fs.exists("funny.gif") is False
def test__given_files_to_collect__when_collect__should_copy_to_source_fs(): source_fs_spy = new_mock_filesystem(["copy_file.txt"]) target_fs = new_mock_filesystem(["file.txt", "funny.gif"]) sut = EnvironmentPreparation(source_fs_spy, target_fs) sut.files_to_collect([ CopyInstruction("file.txt", "copy_file.txt", True), CopyInstruction("funny.gif", "copy_funny.gif", False), ]) sut.collect() assert source_fs_spy.exists("copy_file.txt") assert source_fs_spy.exists("copy_funny.gif")
def test__given_files_to_collect_with_file_already_existing_on_source_fs__when_collecting__should_still_collect_remaining_files( ): source_fs_spy = new_mock_filesystem(["copy_file.txt"]) target_fs = new_mock_filesystem(["file.txt", "funny.gif"]) sut = EnvironmentPreparation(source_fs_spy, target_fs) sut.files_to_collect([ CopyInstruction("file.txt", "copy_file.txt", False), CopyInstruction("funny.gif", "copy_funny.gif", False), ]) sut.collect() assert source_fs_spy.exists("copy_funny.gif")
def test__given_files_to_copy__when_preparing__should_copy_files(): source_fs = new_mock_filesystem(["file.txt", "funny.gif"]) target_fs = new_mock_filesystem(["evenfunnier.gif"]) sut = EnvironmentPreparation(source_fs, target_fs) sut.files_to_copy([ CopyInstruction("file.txt", "filecopy.txt"), CopyInstruction("funny.gif", "evenfunnier.gif", overwrite=True) ]) sut.prepare() assert target_fs.exists("filecopy.txt") assert target_fs.exists("evenfunnier.gif")
def test__given_files_to_clean__when_cleaning__should_delete_files(): source_fs = new_mock_filesystem() target_fs_spy = new_mock_filesystem(["file.txt", "funny.gif"]) sut = EnvironmentPreparation(source_fs, target_fs_spy) sut.files_to_clean([ "file.txt", "funny.gif", ]) sut.clean() assert target_fs_spy.exists("file.txt") is False assert target_fs_spy.exists("funny.gif") is False
def test__given_files_to_clean_with_non_existing_files__when_cleaning__should_log_error_to_ui( ): source_fs_spy = new_mock_filesystem() target_fs = new_mock_filesystem(["funny.gif"]) ui_spy = MagicMock() sut = EnvironmentPreparation(source_fs_spy, target_fs, ui_spy) sut.files_to_clean([ "file.txt", "funny.gif", ]) sut.clean() ui_spy.error.assert_called_with( "FileNotFoundError: Cannot delete file 'file.txt'")
def test__given_files_to_collect_with_file_already_existing_on_source_fs__when_collecting__should_log_error_to_ui( ): source_fs_spy = new_mock_filesystem(["copy_file.txt"]) target_fs = new_mock_filesystem(["file.txt", "funny.gif"]) ui_spy = MagicMock() sut = EnvironmentPreparation(source_fs_spy, target_fs, ui_spy) sut.files_to_collect([ CopyInstruction("file.txt", "copy_file.txt", False), CopyInstruction("funny.gif", "copy_funny.gif", False), ]) sut.collect() ui_spy.error.assert_called_with( "FileExistsError: Cannot copy file 'file.txt'")
def _clean_files(self, env_prep: EnvironmentPreparation, ui: UI) -> None: env_prep.files_to_clean(self._clean) ui.info("Cleaning files...") env_prep.clean() ui.success("Done")
def _collect_files(self, env_prep: EnvironmentPreparation, ui: UI) -> None: env_prep.files_to_collect(self._collect) ui.info("Collecting files...") env_prep.collect() ui.success("Done")
def _do_rollback(env_prep: EnvironmentPreparation, err: Exception, ui: UI) -> None: ui.error(get_error_message(err)) ui.info("Performing rollback") env_prep.rollback() ui.success("Done")