@@ -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