From 8cd2e1c492d46b43f270f0d926bda9caf2357a34 Mon Sep 17 00:00:00 2001 From: kzaoaai <39029008+kzaoaai@users.noreply.github.com> Date: Sun, 19 Apr 2026 04:43:52 +0300 Subject: [PATCH] fix: handle restore permission errors when replacing files --- src/mackup/application.py | 11 +++++++++-- tests/test_application.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/mackup/application.py b/src/mackup/application.py index 5369b6445..299f8f567 100644 --- a/src/mackup/application.py +++ b/src/mackup/application.py @@ -168,8 +168,15 @@ def copy_files_from_mackup_folder(self) -> None: " home folder.\nAre you sure that you want to" " replace it?", ): - # If confirmed, delete the existing home file - utils.delete(home_filepath) + # If confirmed, delete the existing home file before restoring. + try: + utils.delete(home_filepath) + except PermissionError as e: + print( + f"Error: Unable to copy file from {mackup_filepath} to " + f"{home_filepath} due to permission issue: {e}", + ) + continue else: continue diff --git a/tests/test_application.py b/tests/test_application.py index a2373d845..c1b9f944c 100644 --- a/tests/test_application.py +++ b/tests/test_application.py @@ -394,6 +394,37 @@ def test_copy_files_from_mackup_folder_decline_replace_skips_copy(self): with open(home_filepath) as f: assert f.read() == "existing home" + def test_copy_files_from_mackup_folder_delete_permission_error(self): + """Test restore handles PermissionError when removing existing home file.""" + test_file = ".testfile" + home_filepath = os.path.join(self.temp_home, test_file) + mackup_filepath = os.path.join(self.mock_mackup.mackup_folder, test_file) + + with open(home_filepath, "w") as f: + f.write("existing home") + with open(mackup_filepath, "w") as f: + f.write("backup content") + + with patch("mackup.application.utils.confirm", return_value=True), \ + patch("mackup.application.utils.delete") as mock_delete, \ + patch("mackup.application.utils.copy") as mock_copy: + mock_delete.side_effect = PermissionError("Permission denied") + + captured_output = StringIO() + sys.stdout = captured_output + + self.app_profile.copy_files_from_mackup_folder() + + sys.stdout = sys.__stdout__ + + mock_delete.assert_called_once_with(home_filepath) + mock_copy.assert_not_called() + + output = captured_output.getvalue() + assert "Error: Unable to copy file" in output + assert "permission issue" in output + assert home_filepath in output + def test_link_uninstall_mackup_not_a_link(self): """Test link_uninstall skips when home file is not a symbolic link.""" # Create a test file in the mackup directory (regular file, not a link)