-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_rm.py
More file actions
372 lines (291 loc) · 14.2 KB
/
test_rm.py
File metadata and controls
372 lines (291 loc) · 14.2 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import subprocess
def test_rm_basic_file(commit_env_config, git2cpp_path, tmp_path):
"""Test basic rm operation to remove 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 and commit the file
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
commit_cmd = [git2cpp_path, "commit", "-m", "Add test file"]
p_commit = subprocess.run(commit_cmd, capture_output=True, cwd=tmp_path, text=True)
assert p_commit.returncode == 0
# Remove the file
rm_cmd = [git2cpp_path, "rm", "test_file.txt"]
p_rm = subprocess.run(rm_cmd, capture_output=True, cwd=tmp_path, text=True)
assert p_rm.returncode == 0
# Verify the file was removed from working tree
assert not test_file.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 "Changes to be committed" in p_status.stdout
assert "deleted" in p_status.stdout
def test_rm_multiple_files(commit_env_config, git2cpp_path, tmp_path):
"""Test removing multiple files at once"""
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 and commit files
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_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
# Remove both files
rm_cmd = [git2cpp_path, "rm", "file1.txt", "file2.txt"]
p_rm = subprocess.run(rm_cmd, capture_output=True, cwd=tmp_path, text=True)
assert p_rm.returncode == 0
# Verify both files were removed
assert not file1.exists()
assert not file2.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 "Changes to be committed" in p_status.stdout
assert "deleted" in p_status.stdout
def test_rm_directory_without_recursive_flag(commit_env_config, git2cpp_path, tmp_path):
"""Test that rm fails when trying to remove a directory without -r flag"""
cmd_init = [git2cpp_path, "init", "."]
p_init = subprocess.run(cmd_init, capture_output=True, cwd=tmp_path)
assert p_init.returncode == 0
# Create a directory with a file
test_dir = tmp_path / "test_dir"
test_dir.mkdir()
test_file = test_dir / "file.txt"
test_file.write_text("content")
# Add and commit the file
add_cmd = [git2cpp_path, "add", "test_dir/file.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 test directory"]
p_commit = subprocess.run(commit_cmd, capture_output=True, cwd=tmp_path, text=True)
assert p_commit.returncode == 0
# Try to remove directory without -r flag - should fail
rm_cmd = [git2cpp_path, "rm", "test_dir"]
p_rm = subprocess.run(rm_cmd, capture_output=True, cwd=tmp_path, text=True)
assert p_rm.returncode != 0
assert "not removing" in p_rm.stderr and "recursively without -r" in p_rm.stderr
# Verify directory still exists
assert test_dir.exists()
assert test_file.exists()
def test_rm_directory_with_recursive_flag(commit_env_config, git2cpp_path, tmp_path):
"""Test removing a directory with -r flag"""
cmd_init = [git2cpp_path, "init", "."]
p_init = subprocess.run(cmd_init, capture_output=True, cwd=tmp_path)
assert p_init.returncode == 0
# Create a directory with files
test_dir = tmp_path / "test_dir"
test_dir.mkdir()
file1 = test_dir / "file1.txt"
file1.write_text("content 1")
file2 = test_dir / "file2.txt"
file2.write_text("content 2")
# Add and commit the files
add_cmd = [git2cpp_path, "add", "test_dir/file1.txt", "test_dir/file2.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 test directory"]
p_commit = subprocess.run(commit_cmd, capture_output=True, cwd=tmp_path, text=True)
assert p_commit.returncode == 0
# Remove directory with -r flag - should succeed
rm_cmd = [git2cpp_path, "rm", "-r", "test_dir"]
p_rm = subprocess.run(rm_cmd, capture_output=True, cwd=tmp_path, text=True)
assert p_rm.returncode == 0
# 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 "Changes to be committed" in p_status.stdout
assert "deleted" in p_status.stdout
def test_rm_and_commit(commit_env_config, git2cpp_path, tmp_path):
"""Test removing 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 / "to_remove.txt"
test_file.write_text("content to remove")
# Add and commit the file
add_cmd = [git2cpp_path, "add", "to_remove.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 file to remove"]
p_commit = subprocess.run(commit_cmd, capture_output=True, cwd=tmp_path, text=True)
assert p_commit.returncode == 0
# Remove the file
rm_cmd = [git2cpp_path, "rm", "to_remove.txt"]
p_rm = subprocess.run(rm_cmd, capture_output=True, cwd=tmp_path, text=True)
assert p_rm.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
assert "deleted" in p_status.stdout
# Commit the removal
commit_cmd2 = [git2cpp_path, "commit", "-m", "Remove file"]
p_commit2 = subprocess.run(commit_cmd2, capture_output=True, cwd=tmp_path, text=True)
assert p_commit2.returncode == 0
# Verify the file is gone
assert not test_file.exists()
# Check status after commit
status_cmd2 = [git2cpp_path, "status", "--long"]
p_status2 = subprocess.run(status_cmd2, capture_output=True, cwd=tmp_path, text=True)
assert p_status2.returncode == 0
assert "to_remove.txt" not in p_status2.stdout
def test_rm_nonexistent_file(git2cpp_path, tmp_path):
"""Test that rm fails when 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 remove a file that doesn't exist
rm_cmd = [git2cpp_path, "rm", "nonexistent.txt"]
p_rm = subprocess.run(rm_cmd, capture_output=True, cwd=tmp_path, text=True)
assert p_rm.returncode != 0
def test_rm_untracked_file(git2cpp_path, tmp_path):
"""Test that rm fails when trying to remove an untracked file"""
cmd_init = [git2cpp_path, "init", "."]
p_init = subprocess.run(cmd_init, capture_output=True, cwd=tmp_path)
assert p_init.returncode == 0
# Create an untracked file
untracked_file = tmp_path / "untracked.txt"
untracked_file.write_text("untracked content")
# Try to remove untracked file - should fail
rm_cmd = [git2cpp_path, "rm", "untracked.txt"]
p_rm = subprocess.run(rm_cmd, capture_output=True, cwd=tmp_path, text=True)
assert p_rm.returncode != 0
def test_rm_staged_file(git2cpp_path, tmp_path):
"""Test removing a file that was added but not yet committed"""
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 / "staged.txt"
test_file.write_text("staged content")
# Add the file (but don't commit)
add_cmd = [git2cpp_path, "add", "staged.txt"]
p_add = subprocess.run(add_cmd, capture_output=True, cwd=tmp_path, text=True)
assert p_add.returncode == 0
# Remove the file
rm_cmd = [git2cpp_path, "rm", "staged.txt"]
p_rm = subprocess.run(rm_cmd, capture_output=True, cwd=tmp_path, text=True)
assert p_rm.returncode == 0
# Verify the file was removed
assert not test_file.exists()
# Check git status - should show nothing staged
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" not in p_status.stdout
assert "staged.txt" not in p_status.stdout
def test_rm_file_in_subdirectory(commit_env_config, git2cpp_path, tmp_path):
"""Test removing a file in 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 subdirectory
test_dir = tmp_path / "test"
test_dir.mkdir()
test_file = test_dir / "test.txt"
test_file.write_text("test content")
# Add and commit the file
add_cmd = [git2cpp_path, "add", "test/test.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 file in subdirectory"]
p_commit = subprocess.run(commit_cmd, capture_output=True, cwd=tmp_path, text=True)
assert p_commit.returncode == 0
# Remove the file
rm_cmd = [git2cpp_path, "rm", "test/test.txt"]
p_rm = subprocess.run(rm_cmd, capture_output=True, cwd=tmp_path, text=True)
assert p_rm.returncode == 0
# Verify the file was removed
assert not test_file.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 "Changes to be committed" in p_status.stdout
assert "deleted" in p_status.stdout
def test_rm_nogit(git2cpp_path, tmp_path):
"""Test that rm 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 rm without being in a git repo
rm_cmd = [git2cpp_path, "rm", "test.txt"]
p_rm = subprocess.run(rm_cmd, capture_output=True, cwd=tmp_path, text=True)
assert p_rm.returncode != 0
def test_rm_nested_directory_recursive(commit_env_config, git2cpp_path, tmp_path):
"""Test removing a nested directory structure with -r flag"""
cmd_init = [git2cpp_path, "init", "."]
p_init = subprocess.run(cmd_init, capture_output=True, cwd=tmp_path)
assert p_init.returncode == 0
# Create nested directory structure
nested_dir = tmp_path / "level1" / "level2"
nested_dir.mkdir(parents=True)
file1 = tmp_path / "level1" / "file1.txt"
file1.write_text("content 1")
file2 = nested_dir / "file2.txt"
file2.write_text("content 2")
# Add and commit the files
add_cmd = [git2cpp_path, "add", "level1/file1.txt", "level1/level2/file2.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 nested structure"]
p_commit = subprocess.run(commit_cmd, capture_output=True, cwd=tmp_path, text=True)
assert p_commit.returncode == 0
# Remove the directory tree with -r flag
rm_cmd = [git2cpp_path, "rm", "-r", "level1"]
p_rm = subprocess.run(rm_cmd, capture_output=True, cwd=tmp_path, text=True)
assert p_rm.returncode == 0
# 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 "Changes to be committed" in p_status.stdout
assert "deleted" in p_status.stdout
def test_rm_mixed_files_and_directory(commit_env_config, git2cpp_path, tmp_path):
"""Test removing both individual files and directories in one command"""
cmd_init = [git2cpp_path, "init", "."]
p_init = subprocess.run(cmd_init, capture_output=True, cwd=tmp_path)
assert p_init.returncode == 0
# Create a file and a directory with contents
single_file = tmp_path / "single.txt"
single_file.write_text("single file")
test_dir = tmp_path / "remove_dir"
test_dir.mkdir()
dir_file = test_dir / "file.txt"
dir_file.write_text("file in dir")
# Add and commit everything
add_cmd = [git2cpp_path, "add", "single.txt", "remove_dir/file.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 mixed content"]
p_commit = subprocess.run(commit_cmd, capture_output=True, cwd=tmp_path, text=True)
assert p_commit.returncode == 0
# Remove both file and directory
rm_cmd = [git2cpp_path, "rm", "-r", "single.txt", "remove_dir"]
p_rm = subprocess.run(rm_cmd, capture_output=True, cwd=tmp_path, text=True)
assert p_rm.returncode == 0
# Verify everything was removed
assert not single_file.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 "Changes to be committed" in p_status.stdout
assert "deleted" in p_status.stdout