t7006: guard cleanup with test_expect_success
Most of these tests are removing files, environment variables, and configuration that might interfere outside the test. Putting these clean-up commands in the test (in the same spirit as v1.7.1-rc0~59, 2010-03-20) means that errors during setup will be caught quickly and non-error text will be suppressed without -v. While at it, apply some other minor fixes: - do not rely on the shell to export variables defined with the same command as a function call - avoid whitespace immediately after the > redirection operator, for consistency with the style of other tests Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jonathan Nieder committed
Apr 14, 2010 at 19:38 UTC
fdf1bc48caa008c7f285db3323498f25a7758879
2 files changed
+117
-48
t/t7006-pager.sh
+101
-48
@@ -4,17 +4,24 @@ test_description='Test automatic use of a pager.'
4
5
. ./test-lib.sh
6
7
-rm -f stdout_is_tty
7
+cleanup_fail() {
8
+ echo >&2 cleanup failed
9
+ (exit 1)
10
+}
11
+
12
test_expect_success 'set up terminal for tests' '
13
+ rm -f stdout_is_tty ||
14
+ cleanup_fail &&
15
+
16
if test -t 1
17
then
11
- : > stdout_is_tty
18
+ >stdout_is_tty
19
elif
20
test_have_prereq PERL &&
21
"$PERL_PATH" "$TEST_DIRECTORY"/t7006/test-terminal.perl \
22
sh -c "test -t 1"
23
then
17
- : > test_terminal_works
24
+ >test_terminal_works
25
fi
26
'
27
@@ -32,53 +39,68 @@ else
39
say no usable terminal, so skipping some tests
40
fi
41
35
-unset GIT_PAGER GIT_PAGER_IN_USE
36
-git config --unset core.pager
37
-PAGER='cat > paginated.out'
38
-export PAGER
39
-
42
test_expect_success 'setup' '
43
+ unset GIT_PAGER GIT_PAGER_IN_USE &&
44
+ test_might_fail git config --unset core.pager &&
45
+
46
+ PAGER="cat >paginated.out" &&
47
+ export PAGER &&
48
+
49
test_commit initial
50
'
51
44
-rm -f paginated.out
52
test_expect_success TTY 'some commands use a pager' '
53
+ rm -f paginated.out ||
54
+ cleanup_fail &&
55
+
56
test_terminal git log &&
57
test -e paginated.out
58
'
59
50
-rm -f paginated.out
60
test_expect_success TTY 'some commands do not use a pager' '
61
+ rm -f paginated.out ||
62
+ cleanup_fail &&
63
+
64
test_terminal git rev-list HEAD &&
65
! test -e paginated.out
66
'
67
56
-rm -f paginated.out
68
test_expect_success 'no pager when stdout is a pipe' '
69
+ rm -f paginated.out ||
70
+ cleanup_fail &&
71
+
72
git log | cat &&
73
! test -e paginated.out
74
'
75
62
-rm -f paginated.out
76
test_expect_success 'no pager when stdout is a regular file' '
64
- git log > file &&
77
+ rm -f paginated.out ||
78
+ cleanup_fail &&
79
+
80
+ git log >file &&
81
! test -e paginated.out
82
'
83
68
-rm -f paginated.out
84
test_expect_success TTY 'git --paginate rev-list uses a pager' '
85
+ rm -f paginated.out ||
86
+ cleanup_fail &&
87
+
88
test_terminal git --paginate rev-list HEAD &&
89
test -e paginated.out
90
'
91
74
-rm -f file paginated.out
92
test_expect_success 'no pager even with --paginate when stdout is a pipe' '
93
+ rm -f file paginated.out ||
94
+ cleanup_fail &&
95
+
96
git --paginate log | cat &&
97
! test -e paginated.out
98
'
99
80
-rm -f paginated.out
100
test_expect_success TTY 'no pager with --no-pager' '
101
+ rm -f paginated.out ||
102
+ cleanup_fail &&
103
+
104
test_terminal git --no-pager log &&
105
! test -e paginated.out
106
'
@@ -86,88 +108,119 @@ test_expect_success TTY 'no pager with --no-pager' '
108
# A colored commit log will begin with an appropriate ANSI escape
109
# for the first color; the text "commit" comes later.
110
colorful() {
89
- read firstline < $1
111
+ read firstline <$1
112
! expr "$firstline" : "^[a-zA-Z]" >/dev/null
113
}
114
93
-rm -f colorful.log colorless.log
115
test_expect_success 'tests can detect color' '
95
- git log --no-color > colorless.log &&
96
- git log --color > colorful.log &&
116
+ rm -f colorful.log colorless.log ||
117
+ cleanup_fail &&
118
+
119
+ git log --no-color >colorless.log &&
120
+ git log --color >colorful.log &&
121
! colorful colorless.log &&
122
colorful colorful.log
123
'
124
101
-rm -f colorless.log
102
-git config color.ui auto
125
test_expect_success 'no color when stdout is a regular file' '
104
- git log > colorless.log &&
126
+ rm -f colorless.log &&
127
+ git config color.ui auto ||
128
+ cleanup_fail &&
129
+
130
+ git log >colorless.log &&
131
! colorful colorless.log
132
'
133
108
-rm -f paginated.out
109
-git config color.ui auto
134
test_expect_success TTY 'color when writing to a pager' '
111
- TERM=vt100 test_terminal git log &&
135
+ rm -f paginated.out &&
136
+ git config color.ui auto ||
137
+ cleanup_fail &&
138
+
139
+ (
140
+ TERM=vt100 &&
141
+ export TERM &&
142
+ test_terminal git log
143
+ ) &&
144
colorful paginated.out
145
'
146
115
-rm -f colorful.log
116
-git config color.ui auto
147
test_expect_success 'color when writing to a file intended for a pager' '
118
- TERM=vt100 GIT_PAGER_IN_USE=true git log > colorful.log &&
148
+ rm -f colorful.log &&
149
+ git config color.ui auto ||
150
+ cleanup_fail &&
151
+
152
+ (
153
+ TERM=vt100 &&
154
+ GIT_PAGER_IN_USE=true &&
155
+ export TERM GIT_PAGER_IN_USE &&
156
+ git log >colorful.log
157
+ ) &&
158
colorful colorful.log
159
'
160
122
-unset PAGER GIT_PAGER
123
-git config --unset core.pager
161
test_expect_success 'determine default pager' '
162
+ unset PAGER GIT_PAGER &&
163
+ test_might_fail git config --unset core.pager ||
164
+ cleanup_fail &&
165
+
166
less=$(git var GIT_PAGER) &&
167
test -n "$less"
168
'
169
129
-if expr "$less" : '^[a-z]*$' > /dev/null && test_have_prereq TTY
170
+if expr "$less" : '^[a-z][a-z]*$' >/dev/null && test_have_prereq TTY
171
then
172
test_set_prereq SIMPLEPAGER
173
fi
174
134
-unset PAGER GIT_PAGER
135
-git config --unset core.pager
136
-rm -f default_pager_used
175
test_expect_success SIMPLEPAGER 'default pager is used by default' '
138
- cat > $less <<-EOF &&
139
- #!$SHELL_PATH
140
- wc > default_pager_used
176
+ unset PAGER GIT_PAGER &&
177
+ test_might_fail git config --unset core.pager &&
178
+ rm -f default_pager_used ||
179
+ cleanup_fail &&
180
+
181
+ cat >$less <<-\EOF &&
182
+ #!/bin/sh
183
+ wc >default_pager_used
184
EOF
185
chmod +x $less &&
143
- PATH=.:$PATH test_terminal git log &&
186
+ (
187
+ PATH=.:$PATH &&
188
+ export PATH &&
189
+ test_terminal git log
190
+ ) &&
191
test -e default_pager_used
192
'
193
147
-unset GIT_PAGER
148
-git config --unset core.pager
149
-rm -f PAGER_used
194
test_expect_success TTY 'PAGER overrides default pager' '
151
- PAGER="wc > PAGER_used" &&
195
+ unset GIT_PAGER &&
196
+ test_might_fail git config --unset core.pager &&
197
+ rm -f PAGER_used ||
198
+ cleanup_fail &&
199
+
200
+ PAGER="wc >PAGER_used" &&
201
export PAGER &&
202
test_terminal git log &&
203
test -e PAGER_used
204
'
205
157
-unset GIT_PAGER
158
-rm -f core.pager_used
206
test_expect_success TTY 'core.pager overrides PAGER' '
207
+ unset GIT_PAGER &&
208
+ rm -f core.pager_used ||
209
+ cleanup_fail &&
210
+
211
PAGER=wc &&
212
export PAGER &&
162
- git config core.pager "wc > core.pager_used" &&
213
+ git config core.pager "wc >core.pager_used" &&
214
test_terminal git log &&
215
test -e core.pager_used
216
'
217
167
-rm -f GIT_PAGER_used
218
test_expect_success TTY 'GIT_PAGER overrides core.pager' '
219
+ rm -f GIT_PAGER_used ||
220
+ cleanup_fail &&
221
+
222
git config core.pager wc &&
170
- GIT_PAGER="wc > GIT_PAGER_used" &&
223
+ GIT_PAGER="wc >GIT_PAGER_used" &&
224
export GIT_PAGER &&
225
test_terminal git log &&
226
test -e GIT_PAGER_used
t/test-lib.sh
+16
@@ -512,6 +512,22 @@ test_must_fail () {
512
test $? -gt 0 -a $? -le 129 -o $? -gt 192
513
}
514
515
+# Similar to test_must_fail, but tolerates success, too. This is
516
+# meant to be used in contexts like:
517
+#
518
+# test_expect_success 'some command works without configuration' '
519
+# test_might_fail git config --unset all.configuration &&
520
+# do something
521
+# '
522
+#
523
+# Writing "git config --unset all.configuration || :" would be wrong,
524
+# because we want to notice if it fails due to segv.
525
+
526
+test_might_fail () {
527
+ "$@"
528
+ test $? -ge 0 -a $? -le 129 -o $? -gt 192
529
+}
530
+
531
# test_cmp is a helper function to compare actual and expected output.
532
# You can use it like:
533
#