1+ import subprocess
2+
3+ import pytest
4+
5+
6+ def test_mv_basic (xtl_clone , git2cpp_path , tmp_path ):
7+ """Test basic mv operation to rename a file"""
8+ assert (tmp_path / "xtl" ).exists ()
9+ xtl_path = tmp_path / "xtl"
10+
11+ # Create a test file
12+ test_file = xtl_path / "test_file.txt"
13+ test_file .write_text ("test content" )
14+
15+ # Add the file to git
16+ add_cmd = [git2cpp_path , "add" , "test_file.txt" ]
17+ p_add = subprocess .run (add_cmd , capture_output = True , cwd = xtl_path , text = True )
18+ assert p_add .returncode == 0
19+
20+ # Move/rename the file
21+ mv_cmd = [git2cpp_path , "mv" , "test_file.txt" , "renamed_file.txt" ]
22+ p_mv = subprocess .run (mv_cmd , capture_output = True , cwd = xtl_path , text = True )
23+ assert p_mv .returncode == 0
24+
25+ # Verify the file was moved
26+ assert not test_file .exists ()
27+ assert (xtl_path / "renamed_file.txt" ).exists ()
28+
29+ # Check git status
30+ status_cmd = [git2cpp_path , "status" , "--long" ]
31+ p_status = subprocess .run (status_cmd , capture_output = True , cwd = xtl_path , text = True )
32+ assert p_status .returncode == 0
33+ assert "renamed:" in p_status .stdout or "renamed_file.txt" in p_status .stdout
34+
35+
36+ def test_mv_to_subdirectory (xtl_clone , git2cpp_path , tmp_path ):
37+ """Test moving a file to a subdirectory"""
38+ assert (tmp_path / "xtl" ).exists ()
39+ xtl_path = tmp_path / "xtl"
40+
41+ # Create a test file
42+ test_file = xtl_path / "move_me.txt"
43+ test_file .write_text ("content to move" )
44+
45+ # Add the file to git
46+ add_cmd = [git2cpp_path , "add" , "move_me.txt" ]
47+ p_add = subprocess .run (add_cmd , capture_output = True , cwd = xtl_path , text = True )
48+ assert p_add .returncode == 0
49+
50+ # Move the file to existing subdirectory
51+ mv_cmd = [git2cpp_path , "mv" , "move_me.txt" , "include/move_me.txt" ]
52+ p_mv = subprocess .run (mv_cmd , capture_output = True , cwd = xtl_path , text = True )
53+ assert p_mv .returncode == 0
54+
55+ # Verify the file was moved
56+ assert not test_file .exists ()
57+ assert (xtl_path / "include" / "move_me.txt" ).exists ()
58+
59+ # Check git status
60+ status_cmd = [git2cpp_path , "status" , "--long" ]
61+ p_status = subprocess .run (status_cmd , capture_output = True , cwd = xtl_path , text = True )
62+ assert p_status .returncode == 0
63+
64+
65+ def test_mv_destination_exists_without_force (xtl_clone , git2cpp_path , tmp_path ):
66+ """Test that mv fails when destination exists without --force flag"""
67+ assert (tmp_path / "xtl" ).exists ()
68+ xtl_path = tmp_path / "xtl"
69+
70+ # Create source file
71+ source_file = xtl_path / "source.txt"
72+ source_file .write_text ("source content" )
73+
74+ # Create destination file
75+ dest_file = xtl_path / "destination.txt"
76+ dest_file .write_text ("destination content" )
77+
78+ # Add both files to git
79+ add_cmd = [git2cpp_path , "add" , "source.txt" , "destination.txt" ]
80+ p_add = subprocess .run (add_cmd , capture_output = True , cwd = xtl_path , text = True )
81+ assert p_add .returncode == 0
82+
83+ # Try to move without force - should fail
84+ mv_cmd = [git2cpp_path , "mv" , "source.txt" , "destination.txt" ]
85+ p_mv = subprocess .run (mv_cmd , capture_output = True , cwd = xtl_path , text = True )
86+ assert p_mv .returncode != 0
87+ assert "destination already exists" in p_mv .stderr
88+
89+ # Verify source file still exists
90+ assert source_file .exists ()
91+ assert dest_file .exists ()
92+
93+
94+ @pytest .mark .parametrize ("force_flag" , ["-f" , "--force" ])
95+ def test_mv_destination_exists_with_force (xtl_clone , git2cpp_path , tmp_path , force_flag ):
96+ """Test that mv succeeds when destination exists with --force flag"""
97+ assert (tmp_path / "xtl" ).exists ()
98+ xtl_path = tmp_path / "xtl"
99+
100+ # Create source file
101+ source_file = xtl_path / "source.txt"
102+ source_file .write_text ("source content" )
103+
104+ # Create destination file
105+ dest_file = xtl_path / "destination.txt"
106+ dest_file .write_text ("destination content" )
107+
108+ # Add both files to git
109+ add_cmd = [git2cpp_path , "add" , "source.txt" , "destination.txt" ]
110+ p_add = subprocess .run (add_cmd , capture_output = True , cwd = xtl_path , text = True )
111+ assert p_add .returncode == 0
112+
113+ # Move with force - should succeed
114+ mv_cmd = [git2cpp_path , "mv" , force_flag , "source.txt" , "destination.txt" ]
115+ p_mv = subprocess .run (mv_cmd , capture_output = True , cwd = xtl_path , text = True )
116+ assert p_mv .returncode == 0
117+
118+ # Verify source file was moved
119+ assert not source_file .exists ()
120+ assert dest_file .exists ()
121+ assert dest_file .read_text () == "source content"
122+
123+
124+ def test_mv_nonexistent_source (xtl_clone , git2cpp_path , tmp_path ):
125+ """Test that mv fails when source file doesn't exist"""
126+ assert (tmp_path / "xtl" ).exists ()
127+ xtl_path = tmp_path / "xtl"
128+
129+ # Try to move a file that doesn't exist
130+ mv_cmd = [git2cpp_path , "mv" , "nonexistent.txt" , "destination.txt" ]
131+ p_mv = subprocess .run (mv_cmd , capture_output = True , cwd = xtl_path , text = True )
132+ assert p_mv .returncode != 0
133+
134+
135+ def test_mv_multiple_files (xtl_clone , commit_env_config , git2cpp_path , tmp_path ):
136+ """Test moving multiple files sequentially"""
137+ assert (tmp_path / "xtl" ).exists ()
138+ xtl_path = tmp_path / "xtl"
139+
140+ # Create test files
141+ file1 = xtl_path / "file1.txt"
142+ file1 .write_text ("content 1" )
143+ file2 = xtl_path / "file2.txt"
144+ file2 .write_text ("content 2" )
145+
146+ # Add files to git
147+ add_cmd = [git2cpp_path , "add" , "file1.txt" , "file2.txt" ]
148+ p_add = subprocess .run (add_cmd , capture_output = True , cwd = xtl_path , text = True )
149+ assert p_add .returncode == 0
150+
151+ # Commit the files
152+ commit_cmd = [git2cpp_path , "commit" , "-m" , "Add test files" ]
153+ p_commit = subprocess .run (commit_cmd , capture_output = True , cwd = xtl_path , text = True )
154+ assert p_commit .returncode == 0
155+
156+ # Move first file
157+ mv_cmd1 = [git2cpp_path , "mv" , "file1.txt" , "renamed1.txt" ]
158+ p_mv1 = subprocess .run (mv_cmd1 , capture_output = True , cwd = xtl_path , text = True )
159+ assert p_mv1 .returncode == 0
160+
161+ # Move second file
162+ mv_cmd2 = [git2cpp_path , "mv" , "file2.txt" , "renamed2.txt" ]
163+ p_mv2 = subprocess .run (mv_cmd2 , capture_output = True , cwd = xtl_path , text = True )
164+ assert p_mv2 .returncode == 0
165+
166+ # Verify both files were moved
167+ assert not file1 .exists ()
168+ assert not file2 .exists ()
169+ assert (xtl_path / "renamed1.txt" ).exists ()
170+ assert (xtl_path / "renamed2.txt" ).exists ()
171+
172+
173+ def test_mv_and_commit (xtl_clone , commit_env_config , git2cpp_path , tmp_path ):
174+ """Test moving a file and committing the change"""
175+ assert (tmp_path / "xtl" ).exists ()
176+ xtl_path = tmp_path / "xtl"
177+
178+ # Create a test file
179+ test_file = xtl_path / "original.txt"
180+ test_file .write_text ("original content" )
181+
182+ # Add and commit the file
183+ add_cmd = [git2cpp_path , "add" , "original.txt" ]
184+ p_add = subprocess .run (add_cmd , capture_output = True , cwd = xtl_path , text = True )
185+ assert p_add .returncode == 0
186+
187+ commit_cmd = [git2cpp_path , "commit" , "-m" , "Add original file" ]
188+ p_commit = subprocess .run (commit_cmd , capture_output = True , cwd = xtl_path , text = True )
189+ assert p_commit .returncode == 0
190+
191+ # Move the file
192+ mv_cmd = [git2cpp_path , "mv" , "original.txt" , "moved.txt" ]
193+ p_mv = subprocess .run (mv_cmd , capture_output = True , cwd = xtl_path , text = True )
194+ assert p_mv .returncode == 0
195+
196+ # Check status before commit
197+ status_cmd = [git2cpp_path , "status" , "--long" ]
198+ p_status = subprocess .run (status_cmd , capture_output = True , cwd = xtl_path , text = True )
199+ assert p_status .returncode == 0
200+ assert "Changes to be committed" in p_status .stdout
201+
202+ # Commit the move
203+ commit_cmd2 = [git2cpp_path , "commit" , "-m" , "Move file" ]
204+ p_commit2 = subprocess .run (commit_cmd2 , capture_output = True , cwd = xtl_path , text = True )
205+ assert p_commit2 .returncode == 0
206+
207+ # Verify the file is in the new location
208+ assert not (xtl_path / "original.txt" ).exists ()
209+ assert (xtl_path / "moved.txt" ).exists ()
210+
211+
212+ def test_mv_nogit (git2cpp_path , tmp_path ):
213+ """Test that mv fails when not in a git repository"""
214+ # Create a test file outside a git repo
215+ test_file = tmp_path / "test.txt"
216+ test_file .write_text ("test content" )
217+
218+ # Try to mv without being in a git repo
219+ mv_cmd = [git2cpp_path , "mv" , "test.txt" , "moved.txt" ]
220+ p_mv = subprocess .run (mv_cmd , capture_output = True , cwd = tmp_path , text = True )
221+ assert p_mv .returncode != 0
222+
223+
224+ def test_mv_preserve_content (xtl_clone , git2cpp_path , tmp_path ):
225+ """Test that file content is preserved after mv"""
226+ assert (tmp_path / "xtl" ).exists ()
227+ xtl_path = tmp_path / "xtl"
228+
229+ # Create a test file with specific content
230+ test_content = "This is important content that should be preserved"
231+ test_file = xtl_path / "important.txt"
232+ test_file .write_text (test_content )
233+
234+ # Add the file to git
235+ add_cmd = [git2cpp_path , "add" , "important.txt" ]
236+ p_add = subprocess .run (add_cmd , capture_output = True , cwd = xtl_path , text = True )
237+ assert p_add .returncode == 0
238+
239+ # Move the file
240+ mv_cmd = [git2cpp_path , "mv" , "important.txt" , "preserved.txt" ]
241+ p_mv = subprocess .run (mv_cmd , capture_output = True , cwd = xtl_path , text = True )
242+ assert p_mv .returncode == 0
243+
244+ # Verify content is preserved
245+ moved_file = xtl_path / "preserved.txt"
246+ assert moved_file .exists ()
247+ assert moved_file .read_text () == test_content
0 commit comments