3 # Copyright (c) 2006 Carl D. Worth
6 test_description='Test of the various options to git rm.'
10 # Setup some files to be removed, some with funny characters
12 'Initialize test directory' \
13 "touch -- foo bar baz 'space embedded' -q &&
14 git add -- foo bar baz 'space embedded' -q &&
15 git commit -m 'add normal files' &&
17 if touch -- 'tab embedded' 'newline
20 git add -- 'tab embedded' 'newline
22 git commit -m 'add files with tabs and newlines'
24 say 'Your filesystem does not allow tabs in filenames.'
28 # Later we will try removing an unremovable path to make sure
29 # git rm barfs, but if the test is run as root that cannot be
32 'Determine rm behavior' \
36 test -f test-file && test_failed_remove=y
41 'Pre-check that foo exists and is in index before git rm foo' \
42 '[ -f foo ] && git ls-files --error-unmatch foo'
45 'Test that git rm foo succeeds' \
49 'Test that git rm --cached foo succeeds if the index matches the file' \
55 'Test that git rm --cached foo succeeds if the index matches the file' \
59 echo "other content" > foo
63 'Test that git rm --cached foo fails if the index matches neither the file nor HEAD' '
67 echo "other content" > foo
69 echo "yet another content" > foo
70 test_must_fail git rm --cached foo
74 'Test that git rm --cached -f foo works in case where --cached only did not' \
78 echo "other content" > foo
80 echo "yet another content" > foo
81 git rm --cached -f foo'
84 'Post-check that foo exists but is not in index after git rm foo' \
85 '[ -f foo ] && test_must_fail git ls-files --error-unmatch foo'
88 'Pre-check that bar exists and is in index before "git rm bar"' \
89 '[ -f bar ] && git ls-files --error-unmatch bar'
92 'Test that "git rm bar" succeeds' \
96 'Post-check that bar does not exist and is not in index after "git rm -f bar"' \
97 '! [ -f bar ] && test_must_fail git ls-files --error-unmatch bar'
100 'Test that "git rm -- -q" succeeds (remove a file that looks like an option)' \
103 test "$test_tabs" = y && test_expect_success \
104 "Test that \"git rm -f\" succeeds with embedded space, tab, or newline characters." \
105 "git rm -f 'space embedded' 'tab embedded' 'newline
108 if test "$test_failed_remove" = y; then
110 test_expect_success \
111 'Test that "git rm -f" fails if its rm fails' \
112 'test_must_fail git rm -f baz'
115 test_expect_success 'skipping removal failure (perhaps running as root?)' :
118 test_expect_success \
119 'When the rm in "git rm -f" fails, it should not remove the file from the index' \
120 'git ls-files --error-unmatch baz'
122 test_expect_success 'Remove nonexistent file with --ignore-unmatch' '
123 git rm --ignore-unmatch nonexistent
126 test_expect_success '"rm" command printed' '
127 echo frotz > test-file &&
129 git commit -m "add file for rm test" &&
130 git rm test-file > rm-output &&
131 test `grep "^rm " rm-output | wc -l` = 1 &&
132 rm -f test-file rm-output &&
133 git commit -m "remove file from rm test"
136 test_expect_success '"rm" command suppressed with --quiet' '
137 echo frotz > test-file &&
139 git commit -m "add file for rm --quiet test" &&
140 git rm --quiet test-file > rm-output &&
141 test `wc -l < rm-output` = 0 &&
142 rm -f test-file rm-output &&
143 git commit -m "remove file from rm --quiet test"
146 # Now, failure cases.
147 test_expect_success 'Re-add foo and baz' '
149 git ls-files --error-unmatch foo baz
152 test_expect_success 'Modify foo -- rm should refuse' '
154 test_must_fail git rm foo baz &&
157 git ls-files --error-unmatch foo baz
160 test_expect_success 'Modified foo -- rm -f should work' '
164 test_must_fail git ls-files --error-unmatch foo &&
165 test_must_fail git ls-files --error-unmatch bar
168 test_expect_success 'Re-add foo and baz for HEAD tests' '
170 git checkout HEAD -- baz &&
172 git ls-files --error-unmatch foo baz
175 test_expect_success 'foo is different in index from HEAD -- rm should refuse' '
176 test_must_fail git rm foo baz &&
179 git ls-files --error-unmatch foo baz
182 test_expect_success 'but with -f it should work.' '
186 test_must_fail git ls-files --error-unmatch foo
187 test_must_fail git ls-files --error-unmatch baz
190 test_expect_failure 'refuse to remove cached empty file with modifications' '
193 echo content >empty &&
194 test_must_fail git rm --cached empty
197 test_expect_success 'remove intent-to-add file without --force' '
198 echo content >intent-to-add &&
199 git add -N intent-to-add
200 git rm --cached intent-to-add
203 test_expect_success 'Recursive test setup' '
205 echo qfwfq >frotz/nitfol &&
207 git commit -m "subdir test"
210 test_expect_success 'Recursive without -r fails' '
211 test_must_fail git rm frotz &&
216 test_expect_success 'Recursive with -r but dirty' '
217 echo qfwfq >>frotz/nitfol
218 test_must_fail git rm -r frotz &&
223 test_expect_success 'Recursive with -r -f' '
224 git rm -f -r frotz &&
225 ! test -f frotz/nitfol &&
229 test_expect_success 'Remove nonexistent file returns nonzero exit status' '
230 test_must_fail git rm nonexistent
233 test_expect_success 'Call "rm" from outside the work tree' '
237 echo something > somefile &&
239 git commit -m "add a file" &&
241 git --git-dir=repo/.git --work-tree=repo rm somefile) &&
242 test_must_fail git ls-files --error-unmatch somefile)
245 test_expect_success 'refresh index before checking if it is up-to-date' '
248 test-chmtime -86400 frotz/nitfol &&
249 git rm frotz/nitfol &&
250 test ! -f frotz/nitfol