Skip to content

Commit 3c7641e

Browse files
committed
Add tests for ff0ae3e
Now keeps the restore errors
1 parent ff0ae3e commit 3c7641e

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

qgitc/commitwindow.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ def _doUnstage(self, submodule: str, files: List[str], cancelEvent: CancelEvent)
794794
repoDir = fullRepoDir(submodule)
795795
if files:
796796
repoFiles = [toSubmodulePath(submodule, file) for file in files]
797-
error = Git.restoreStagedFiles(repoDir, repoFiles)
797+
error, _ = Git.restoreStagedFiles(repoDir, repoFiles)
798798
if error:
799799
ApplicationBase.instance().postEvent(self, GitErrorEvent(error))
800800
self._statusFetcher.fetchStatus(submodule, cancelEvent)
@@ -1546,8 +1546,6 @@ def _doRestoreFiles(self, submodule: str, files: List[str], cancelEvent: CancelE
15461546

15471547
self._statusFetcher.fetchStatus(submodule, cancelEvent)
15481548

1549-
if error and error.startswith("error: pathspec "):
1550-
error = None
15511549
ApplicationBase.instance().postEvent(
15521550
self, FileRestoreEvent(submodule, files, error))
15531551

tests/test_gitutils.py

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,74 @@ def testRawDiff(self):
4444
self.assertIn("diff --git a/test.dot b/test.dot", lines)
4545

4646
def testRestoreFiles(self):
47+
"""Test restoreFiles with newly added file - should not fail"""
4748
with open("newfile.txt", "w", encoding="utf-8") as f:
4849
f.write("test")
4950
self.assertIsNone(Git.addFiles(None, ["newfile.txt"]))
5051

52+
# Should successfully unstage without error (newly added files won't be restored)
5153
error = Git.restoreFiles(None, ["newfile.txt"], staged=True)
52-
self.assertTrue(error.startswith("error: pathspec "))
54+
self.assertIsNone(error)
55+
56+
def testRestoreStagedFilesNewlyAdded(self):
57+
"""Test restoreStagedFiles with newly added file"""
58+
with open("newfile_staged.txt", "w", encoding="utf-8") as f:
59+
f.write("test content")
60+
self.assertIsNone(Git.addFiles(None, ["newfile_staged.txt"]))
61+
62+
# restoreStagedFiles should successfully unstage without error
63+
error, filesToRestore = Git.restoreStagedFiles(
64+
None, ["newfile_staged.txt"])
65+
self.assertIsNone(error)
66+
# Newly added file should not be in filesToRestore (no unstaged changes)
67+
self.assertEqual(filesToRestore, [])
68+
69+
def testRestoreStagedFilesModified(self):
70+
"""Test restoreStagedFiles with modified file"""
71+
# First commit a file
72+
with open("existing.txt", "w", encoding="utf-8") as f:
73+
f.write("original content")
74+
self.assertIsNone(Git.addFiles(None, ["existing.txt"]))
75+
Git.commit("Add existing.txt")
76+
77+
# Modify and stage it
78+
with open("existing.txt", "w", encoding="utf-8") as f:
79+
f.write("modified content")
80+
self.assertIsNone(Git.addFiles(None, ["existing.txt"]))
81+
82+
# restoreStagedFiles should unstage and return it in filesToRestore
83+
error, filesToRestore = Git.restoreStagedFiles(None, ["existing.txt"])
84+
self.assertIsNone(error)
85+
# Modified file should be in filesToRestore (has unstaged changes after reset)
86+
self.assertIn("existing.txt", filesToRestore)
87+
88+
def testRestoreFilesMixed(self):
89+
"""Test restoreFiles with mix of newly added and modified files"""
90+
# Create and commit a file
91+
with open("committed.txt", "w", encoding="utf-8") as f:
92+
f.write("original")
93+
self.assertIsNone(Git.addFiles(None, ["committed.txt"]))
94+
Git.commit("Add committed.txt")
95+
96+
# Modify the existing file
97+
with open("committed.txt", "w", encoding="utf-8") as f:
98+
f.write("modified")
99+
self.assertIsNone(Git.addFiles(None, ["committed.txt"]))
100+
101+
# Add a new file
102+
with open("newfile_mixed.txt", "w", encoding="utf-8") as f:
103+
f.write("new content")
104+
self.assertIsNone(Git.addFiles(None, ["newfile_mixed.txt"]))
105+
106+
# Restore both files - should not fail
107+
error = Git.restoreFiles(
108+
None, ["committed.txt", "newfile_mixed.txt"], staged=True)
109+
self.assertIsNone(error)
110+
111+
# Check that the modified file was restored to original content
112+
with open("committed.txt", "r", encoding="utf-8") as f:
113+
content = f.read()
114+
self.assertEqual(content, "original")
53115

54116
def testChangeCommitAuthorHead(self):
55117
"""Test changing the author of the HEAD commit"""

0 commit comments

Comments
 (0)