Update stale documentation links from the main documentation.
[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 &&
22669a04
SP
15 git-commit -m 'add normal files' &&
16 test_tabs=y &&
17 if touch -- 'tab embedded' 'newline
d51fac53 18embedded'
22669a04 19 then
5be60078 20 git add -- 'tab embedded' 'newline
22669a04
SP
21embedded' &&
22 git-commit -m 'add files with tabs and newlines'
23 else
24 say 'Your filesystem does not allow tabs in filenames.'
25 test_tabs=n
26 fi"
d4a1cab5 27
2283645b 28# Later we will try removing an unremovable path to make sure
5be60078 29# git rm barfs, but if the test is run as root that cannot be
2283645b 30# arranged.
22669a04
SP
31test_expect_success \
32 'Determine rm behavior' \
33 ': >test-file
34 chmod a-w .
35 rm -f test-file
36 test -f test-file && test_failed_remove=y
37 chmod 775 .
38 rm -f test-file'
2283645b 39
d4a1cab5 40test_expect_success \
5be60078
JH
41 'Pre-check that foo exists and is in index before git rm foo' \
42 '[ -f foo ] && git ls-files --error-unmatch foo'
d4a1cab5
CW
43
44test_expect_success \
5be60078
JH
45 'Test that git rm foo succeeds' \
46 'git rm --cached foo'
d4a1cab5 47
bdecd9d4
MM
48test_expect_success \
49 'Test that git rm --cached foo succeeds if the index matches the file' \
50 'echo content > foo
51 git add foo
52 git rm --cached foo'
53
54test_expect_success \
55 'Test that git rm --cached foo succeeds if the index matches the file' \
56 'echo content > foo
57 git add foo
58 git commit -m foo
59 echo "other content" > foo
60 git rm --cached foo'
61
62test_expect_failure \
63 'Test that git rm --cached foo fails if the index matches neither the file nor HEAD' \
64 'echo content > foo
65 git add foo
66 git commit -m foo
67 echo "other content" > foo
68 git add foo
69 echo "yet another content" > foo
70 git rm --cached foo'
71
72test_expect_success \
73 'Test that git rm --cached -f foo works in case where --cached only did not' \
74 'echo content > foo
75 git add foo
76 git commit -m foo
77 echo "other content" > foo
78 git add foo
79 echo "yet another content" > foo
80 git rm --cached -f foo'
81
3844cdc8 82test_expect_success \
5be60078
JH
83 'Post-check that foo exists but is not in index after git rm foo' \
84 '[ -f foo ] && ! git ls-files --error-unmatch foo'
3844cdc8
CW
85
86test_expect_success \
5be60078
JH
87 'Pre-check that bar exists and is in index before "git rm bar"' \
88 '[ -f bar ] && git ls-files --error-unmatch bar'
d4a1cab5
CW
89
90test_expect_success \
5be60078
JH
91 'Test that "git rm bar" succeeds' \
92 'git rm bar'
d4a1cab5 93
3844cdc8 94test_expect_success \
5be60078
JH
95 'Post-check that bar does not exist and is not in index after "git rm -f bar"' \
96 '! [ -f bar ] && ! git ls-files --error-unmatch bar'
d4a1cab5
CW
97
98test_expect_success \
5be60078
JH
99 'Test that "git rm -- -q" succeeds (remove a file that looks like an option)' \
100 'git rm -- -q'
d4a1cab5 101
d51fac53 102test "$test_tabs" = y && test_expect_success \
5be60078
JH
103 "Test that \"git rm -f\" succeeds with embedded space, tab, or newline characters." \
104 "git rm -f 'space embedded' 'tab embedded' 'newline
3844cdc8
CW
105embedded'"
106
2283645b
JH
107if test "$test_failed_remove" = y; then
108chmod a-w .
3844cdc8 109test_expect_failure \
5be60078
JH
110 'Test that "git rm -f" fails if its rm fails' \
111 'git rm -f baz'
2283645b
JH
112chmod 775 .
113else
114 test_expect_success 'skipping removal failure (perhaps running as root?)' :
d51fac53 115fi
3844cdc8
CW
116
117test_expect_success \
5be60078
JH
118 'When the rm in "git rm -f" fails, it should not remove the file from the index' \
119 'git ls-files --error-unmatch baz'
3844cdc8 120
bb1faf0d
SG
121test_expect_success 'Remove nonexistent file with --ignore-unmatch' '
122 git rm --ignore-unmatch nonexistent
123'
124
b48caa20
SG
125test_expect_success '"rm" command printed' '
126 echo frotz > test-file &&
127 git add test-file &&
128 git commit -m "add file for rm test" &&
129 git rm test-file > rm-output &&
0feb4d1c 130 test `grep "^rm " rm-output | wc -l` = 1 &&
b48caa20
SG
131 rm -f test-file rm-output &&
132 git commit -m "remove file from rm test"
133'
134
135test_expect_success '"rm" command suppressed with --quiet' '
136 echo frotz > test-file &&
137 git add test-file &&
138 git commit -m "add file for rm --quiet test" &&
139 git rm --quiet test-file > rm-output &&
140 test `wc -l < rm-output` = 0 &&
141 rm -f test-file rm-output &&
142 git commit -m "remove file from rm --quiet test"
143'
144
467e1b53
JH
145# Now, failure cases.
146test_expect_success 'Re-add foo and baz' '
147 git add foo baz &&
148 git ls-files --error-unmatch foo baz
149'
150
151test_expect_success 'Modify foo -- rm should refuse' '
152 echo >>foo &&
153 ! git rm foo baz &&
154 test -f foo &&
155 test -f baz &&
156 git ls-files --error-unmatch foo baz
157'
158
159test_expect_success 'Modified foo -- rm -f should work' '
160 git rm -f foo baz &&
161 test ! -f foo &&
162 test ! -f baz &&
163 ! git ls-files --error-unmatch foo &&
164 ! git ls-files --error-unmatch bar
165'
166
167test_expect_success 'Re-add foo and baz for HEAD tests' '
168 echo frotz >foo &&
169 git checkout HEAD -- baz &&
170 git add foo baz &&
171 git ls-files --error-unmatch foo baz
172'
173
174test_expect_success 'foo is different in index from HEAD -- rm should refuse' '
175 ! git rm foo baz &&
176 test -f foo &&
177 test -f baz &&
178 git ls-files --error-unmatch foo baz
179'
180
181test_expect_success 'but with -f it should work.' '
182 git rm -f foo baz &&
183 test ! -f foo &&
184 test ! -f baz &&
185 ! git ls-files --error-unmatch foo
186 ! git ls-files --error-unmatch baz
187'
188
189test_expect_success 'Recursive test setup' '
190 mkdir -p frotz &&
191 echo qfwfq >frotz/nitfol &&
192 git add frotz &&
193 git commit -m "subdir test"
194'
195
196test_expect_success 'Recursive without -r fails' '
197 ! git rm frotz &&
198 test -d frotz &&
199 test -f frotz/nitfol
200'
201
202test_expect_success 'Recursive with -r but dirty' '
203 echo qfwfq >>frotz/nitfol
204 ! git rm -r frotz &&
205 test -d frotz &&
206 test -f frotz/nitfol
207'
208
209test_expect_success 'Recursive with -r -f' '
210 git rm -f -r frotz &&
211 ! test -f frotz/nitfol &&
212 ! test -d frotz
213'
214
b48caa20
SG
215test_expect_failure 'Remove nonexistent file returns nonzero exit status' '
216 git rm nonexistent
217'
218
d4a1cab5 219test_done