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