Add --quiet option to suppress output of "rm" commands for removed files.
[git] / t / t3600-rm.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2006 Carl D. Worth
4 #
5
6 test_description='Test of the various options to git-rm.'
7
8 . ./test-lib.sh
9
10 # Setup some files to be removed, some with funny characters
11 test_expect_success \
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' &&
16      test_tabs=y &&
17      if touch -- 'tab   embedded' 'newline
18 embedded'
19      then
20      git-add -- 'tab    embedded' 'newline
21 embedded' &&
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"
27
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
30 # arranged.
31 test_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'
39
40 test_expect_success \
41     'Pre-check that foo exists and is in index before git-rm foo' \
42     '[ -f foo ] && git-ls-files --error-unmatch foo'
43
44 test_expect_success \
45     'Test that git-rm foo succeeds' \
46     'git-rm --cached foo'
47
48 test_expect_success \
49     'Post-check that foo exists but is not in index after git-rm foo' \
50     '[ -f foo ] && ! git-ls-files --error-unmatch foo'
51
52 test_expect_success \
53     'Pre-check that bar exists and is in index before "git-rm bar"' \
54     '[ -f bar ] && git-ls-files --error-unmatch bar'
55
56 test_expect_success \
57     'Test that "git-rm bar" succeeds' \
58     'git-rm bar'
59
60 test_expect_success \
61     'Post-check that bar does not exist and is not in index after "git-rm -f bar"' \
62     '! [ -f bar ] && ! git-ls-files --error-unmatch bar'
63
64 test_expect_success \
65     'Test that "git-rm -- -q" succeeds (remove a file that looks like an option)' \
66     'git-rm -- -q'
67
68 test "$test_tabs" = y && test_expect_success \
69     "Test that \"git-rm -f\" succeeds with embedded space, tab, or newline characters." \
70     "git-rm -f 'space embedded' 'tab    embedded' 'newline
71 embedded'"
72
73 if test "$test_failed_remove" = y; then
74 chmod a-w .
75 test_expect_failure \
76     'Test that "git-rm -f" fails if its rm fails' \
77     'git-rm -f baz'
78 chmod 775 .
79 else
80     test_expect_success 'skipping removal failure (perhaps running as root?)' :
81 fi
82
83 test_expect_success \
84     'When the rm in "git-rm -f" fails, it should not remove the file from the index' \
85     'git-ls-files --error-unmatch baz'
86
87 test_expect_success '"rm" command printed' '
88         echo frotz > test-file &&
89         git add test-file &&
90         git commit -m "add file for rm test" &&
91         git rm test-file > rm-output &&
92         test `egrep "^rm " rm-output | wc -l` = 1 &&
93         rm -f test-file rm-output &&
94         git commit -m "remove file from rm test"
95 '
96
97 test_expect_success '"rm" command suppressed with --quiet' '
98         echo frotz > test-file &&
99         git add test-file &&
100         git commit -m "add file for rm --quiet test" &&
101         git rm --quiet test-file > rm-output &&
102         test `wc -l < rm-output` = 0 &&
103         rm -f test-file rm-output &&
104         git commit -m "remove file from rm --quiet test"
105 '
106
107 # Now, failure cases.
108 test_expect_success 'Re-add foo and baz' '
109         git add foo baz &&
110         git ls-files --error-unmatch foo baz
111 '
112
113 test_expect_success 'Modify foo -- rm should refuse' '
114         echo >>foo &&
115         ! git rm foo baz &&
116         test -f foo &&
117         test -f baz &&
118         git ls-files --error-unmatch foo baz
119 '
120
121 test_expect_success 'Modified foo -- rm -f should work' '
122         git rm -f foo baz &&
123         test ! -f foo &&
124         test ! -f baz &&
125         ! git ls-files --error-unmatch foo &&
126         ! git ls-files --error-unmatch bar
127 '
128
129 test_expect_success 'Re-add foo and baz for HEAD tests' '
130         echo frotz >foo &&
131         git checkout HEAD -- baz &&
132         git add foo baz &&
133         git ls-files --error-unmatch foo baz
134 '
135
136 test_expect_success 'foo is different in index from HEAD -- rm should refuse' '
137         ! git rm foo baz &&
138         test -f foo &&
139         test -f baz &&
140         git ls-files --error-unmatch foo baz
141 '
142
143 test_expect_success 'but with -f it should work.' '
144         git rm -f foo baz &&
145         test ! -f foo &&
146         test ! -f baz &&
147         ! git ls-files --error-unmatch foo
148         ! git ls-files --error-unmatch baz
149 '
150
151 test_expect_success 'Recursive test setup' '
152         mkdir -p frotz &&
153         echo qfwfq >frotz/nitfol &&
154         git add frotz &&
155         git commit -m "subdir test"
156 '
157
158 test_expect_success 'Recursive without -r fails' '
159         ! git rm frotz &&
160         test -d frotz &&
161         test -f frotz/nitfol
162 '
163
164 test_expect_success 'Recursive with -r but dirty' '
165         echo qfwfq >>frotz/nitfol
166         ! git rm -r frotz &&
167         test -d frotz &&
168         test -f frotz/nitfol
169 '
170
171 test_expect_success 'Recursive with -r -f' '
172         git rm -f -r frotz &&
173         ! test -f frotz/nitfol &&
174         ! test -d frotz
175 '
176
177 test_expect_failure 'Remove nonexistent file returns nonzero exit status' '
178         git rm nonexistent
179 '
180
181 test_done