test-lib: Infrastructure to test and check for prerequisites
[git/git.git] / t / t3600-rm.sh
CommitLineData
d4a1cab5
CW
1#!/bin/sh
2#
3# Copyright (c) 2006 Carl D. Worth
4#
5
5be60078 6test_description='Test of the various options to git rm.'
d4a1cab5
CW
7
8. ./test-lib.sh
9
3844cdc8 10# Setup some files to be removed, some with funny characters
22669a04
SP
11test_expect_success \
12 'Initialize test directory' \
13 "touch -- foo bar baz 'space embedded' -q &&
5be60078 14 git add -- foo bar baz 'space embedded' -q &&
3604e7c5 15 git commit -m 'add normal files' &&
22669a04
SP
16 test_tabs=y &&
17 if touch -- 'tab embedded' 'newline
d51fac53 18embedded'
22669a04 19 then
5be60078 20 git add -- 'tab embedded' 'newline
22669a04 21embedded' &&
3604e7c5 22 git commit -m 'add files with tabs and newlines'
22669a04 23 else
22669a04
SP
24 test_tabs=n
25 fi"
d4a1cab5 26
5b46a428
JS
27test "$test_tabs" = n && say 'Your filesystem does not allow tabs in filenames.'
28
2283645b 29# Later we will try removing an unremovable path to make sure
5be60078 30# git rm barfs, but if the test is run as root that cannot be
2283645b 31# arranged.
22669a04
SP
32test_expect_success \
33 'Determine rm behavior' \
34 ': >test-file
35 chmod a-w .
36 rm -f test-file
37 test -f test-file && test_failed_remove=y
38 chmod 775 .
39 rm -f test-file'
2283645b 40
d4a1cab5 41test_expect_success \
5be60078
JH
42 'Pre-check that foo exists and is in index before git rm foo' \
43 '[ -f foo ] && git ls-files --error-unmatch foo'
d4a1cab5
CW
44
45test_expect_success \
5be60078
JH
46 'Test that git rm foo succeeds' \
47 'git rm --cached foo'
d4a1cab5 48
bdecd9d4
MM
49test_expect_success \
50 'Test that git rm --cached foo succeeds if the index matches the file' \
51 'echo content > foo
52 git add foo
53 git rm --cached foo'
54
55test_expect_success \
56 'Test that git rm --cached foo succeeds if the index matches the file' \
57 'echo content > foo
58 git add foo
59 git commit -m foo
60 echo "other content" > foo
61 git rm --cached foo'
62
41ac414e
JH
63test_expect_success \
64 'Test that git rm --cached foo fails if the index matches neither the file nor HEAD' '
65 echo content > foo
bdecd9d4
MM
66 git add foo
67 git commit -m foo
68 echo "other content" > foo
69 git add foo
70 echo "yet another content" > foo
d492b31c 71 test_must_fail git rm --cached foo
41ac414e 72'
bdecd9d4
MM
73
74test_expect_success \
75 'Test that git rm --cached -f foo works in case where --cached only did not' \
76 'echo content > foo
77 git add foo
78 git commit -m foo
79 echo "other content" > foo
80 git add foo
81 echo "yet another content" > foo
82 git rm --cached -f foo'
83
3844cdc8 84test_expect_success \
5be60078 85 'Post-check that foo exists but is not in index after git rm foo' \
d492b31c 86 '[ -f foo ] && test_must_fail git ls-files --error-unmatch foo'
3844cdc8
CW
87
88test_expect_success \
5be60078
JH
89 'Pre-check that bar exists and is in index before "git rm bar"' \
90 '[ -f bar ] && git ls-files --error-unmatch bar'
d4a1cab5
CW
91
92test_expect_success \
5be60078
JH
93 'Test that "git rm bar" succeeds' \
94 'git rm bar'
d4a1cab5 95
3844cdc8 96test_expect_success \
5be60078 97 'Post-check that bar does not exist and is not in index after "git rm -f bar"' \
d492b31c 98 '! [ -f bar ] && test_must_fail git ls-files --error-unmatch bar'
d4a1cab5
CW
99
100test_expect_success \
5be60078
JH
101 'Test that "git rm -- -q" succeeds (remove a file that looks like an option)' \
102 'git rm -- -q'
d4a1cab5 103
d51fac53 104test "$test_tabs" = y && test_expect_success \
5be60078
JH
105 "Test that \"git rm -f\" succeeds with embedded space, tab, or newline characters." \
106 "git rm -f 'space embedded' 'tab embedded' 'newline
3844cdc8
CW
107embedded'"
108
2283645b
JH
109if test "$test_failed_remove" = y; then
110chmod a-w .
41ac414e 111test_expect_success \
5be60078 112 'Test that "git rm -f" fails if its rm fails' \
d492b31c 113 'test_must_fail git rm -f baz'
2283645b
JH
114chmod 775 .
115else
fae74a04 116 say 'skipping removal failure test (perhaps running as root?)'
d51fac53 117fi
3844cdc8
CW
118
119test_expect_success \
5be60078
JH
120 'When the rm in "git rm -f" fails, it should not remove the file from the index' \
121 'git ls-files --error-unmatch baz'
3844cdc8 122
bb1faf0d
SG
123test_expect_success 'Remove nonexistent file with --ignore-unmatch' '
124 git rm --ignore-unmatch nonexistent
125'
126
b48caa20
SG
127test_expect_success '"rm" command printed' '
128 echo frotz > test-file &&
129 git add test-file &&
130 git commit -m "add file for rm test" &&
131 git rm test-file > rm-output &&
0feb4d1c 132 test `grep "^rm " rm-output | wc -l` = 1 &&
b48caa20
SG
133 rm -f test-file rm-output &&
134 git commit -m "remove file from rm test"
135'
136
137test_expect_success '"rm" command suppressed with --quiet' '
138 echo frotz > test-file &&
139 git add test-file &&
140 git commit -m "add file for rm --quiet test" &&
141 git rm --quiet test-file > rm-output &&
142 test `wc -l < rm-output` = 0 &&
143 rm -f test-file rm-output &&
144 git commit -m "remove file from rm --quiet test"
145'
146
467e1b53
JH
147# Now, failure cases.
148test_expect_success 'Re-add foo and baz' '
149 git add foo baz &&
150 git ls-files --error-unmatch foo baz
151'
152
153test_expect_success 'Modify foo -- rm should refuse' '
154 echo >>foo &&
d492b31c 155 test_must_fail git rm foo baz &&
467e1b53
JH
156 test -f foo &&
157 test -f baz &&
158 git ls-files --error-unmatch foo baz
159'
160
161test_expect_success 'Modified foo -- rm -f should work' '
162 git rm -f foo baz &&
163 test ! -f foo &&
164 test ! -f baz &&
d492b31c
SB
165 test_must_fail git ls-files --error-unmatch foo &&
166 test_must_fail git ls-files --error-unmatch bar
467e1b53
JH
167'
168
169test_expect_success 'Re-add foo and baz for HEAD tests' '
170 echo frotz >foo &&
171 git checkout HEAD -- baz &&
172 git add foo baz &&
173 git ls-files --error-unmatch foo baz
174'
175
176test_expect_success 'foo is different in index from HEAD -- rm should refuse' '
d492b31c 177 test_must_fail git rm foo baz &&
467e1b53
JH
178 test -f foo &&
179 test -f baz &&
180 git ls-files --error-unmatch foo baz
181'
182
183test_expect_success 'but with -f it should work.' '
184 git rm -f foo baz &&
185 test ! -f foo &&
186 test ! -f baz &&
d492b31c
SB
187 test_must_fail git ls-files --error-unmatch foo
188 test_must_fail git ls-files --error-unmatch baz
467e1b53
JH
189'
190
388b2acd
JH
191test_expect_success 'refuse to remove cached empty file with modifications' '
192 >empty &&
f55527f8
JK
193 git add empty &&
194 echo content >empty &&
195 test_must_fail git rm --cached empty
196'
197
198test_expect_success 'remove intent-to-add file without --force' '
199 echo content >intent-to-add &&
200 git add -N intent-to-add
201 git rm --cached intent-to-add
202'
203
467e1b53
JH
204test_expect_success 'Recursive test setup' '
205 mkdir -p frotz &&
206 echo qfwfq >frotz/nitfol &&
207 git add frotz &&
208 git commit -m "subdir test"
209'
210
211test_expect_success 'Recursive without -r fails' '
d492b31c 212 test_must_fail git rm frotz &&
467e1b53
JH
213 test -d frotz &&
214 test -f frotz/nitfol
215'
216
217test_expect_success 'Recursive with -r but dirty' '
218 echo qfwfq >>frotz/nitfol
d492b31c 219 test_must_fail git rm -r frotz &&
467e1b53
JH
220 test -d frotz &&
221 test -f frotz/nitfol
222'
223
224test_expect_success 'Recursive with -r -f' '
225 git rm -f -r frotz &&
226 ! test -f frotz/nitfol &&
227 ! test -d frotz
228'
229
41ac414e 230test_expect_success 'Remove nonexistent file returns nonzero exit status' '
d492b31c 231 test_must_fail git rm nonexistent
b48caa20
SG
232'
233
4d264672
OM
234test_expect_success 'Call "rm" from outside the work tree' '
235 mkdir repo &&
cced48a8
JS
236 (cd repo &&
237 git init &&
238 echo something > somefile &&
239 git add somefile &&
240 git commit -m "add a file" &&
241 (cd .. &&
242 git --git-dir=repo/.git --work-tree=repo rm somefile) &&
243 test_must_fail git ls-files --error-unmatch somefile)
244'
245
246test_expect_success 'refresh index before checking if it is up-to-date' '
247
248 git reset --hard &&
249 test-chmtime -86400 frotz/nitfol &&
250 git rm frotz/nitfol &&
251 test ! -f frotz/nitfol
252
4d264672
OM
253'
254
0693f9dd
JH
255test_expect_success 'choking "git rm" should not let it die with cruft' '
256 git reset -q --hard &&
257 H=0000000000000000000000000000000000000000 &&
258 i=0 &&
259 while test $i -lt 12000
260 do
261 echo "100644 $H 0 some-file-$i"
262 i=$(( $i + 1 ))
263 done | git update-index --index-info &&
264 git rm -n "some-file-*" | :;
265 test -f .git/index.lock
266 status=$?
267 rm -f .git/index.lock
268 git reset -q --hard
269 test "$status" != 0
270'
271
d4a1cab5 272test_done