[PATCH 1/5] test: Better handling of stdout and stderr
[notmuch-archives.git] / 45 / c08fd832e4ba2eca47ce8e967ff7ddd98e0cbd
1 Return-Path: <sojkam1@fel.cvut.cz>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id 9720840DDC9\r
6         for <notmuch@notmuchmail.org>; Sun, 14 Nov 2010 13:54:54 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -1.9\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-1.9 tagged_above=-999 required=5\r
12         tests=[BAYES_00=-1.9] autolearn=ham\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id B8M0rVVqMv-0 for <notmuch@notmuchmail.org>;\r
16         Sun, 14 Nov 2010 13:54:42 -0800 (PST)\r
17 Received: from max.feld.cvut.cz (max.feld.cvut.cz [147.32.192.36])\r
18         by olra.theworths.org (Postfix) with ESMTP id B81C440DDF7\r
19         for <notmuch@notmuchmail.org>; Sun, 14 Nov 2010 13:54:41 -0800 (PST)\r
20 Received: from localhost (unknown [192.168.200.4])\r
21         by max.feld.cvut.cz (Postfix) with ESMTP id 175533CFE74;\r
22         Sun, 14 Nov 2010 22:54:41 +0100 (CET)\r
23 X-Virus-Scanned: IMAP AMAVIS\r
24 Received: from max.feld.cvut.cz ([192.168.200.1])\r
25         by localhost (styx.feld.cvut.cz [192.168.200.4]) (amavisd-new,\r
26         port 10044)\r
27         with ESMTP id o6lj5FZRmmI4; Sun, 14 Nov 2010 22:54:39 +0100 (CET)\r
28 Received: from imap.feld.cvut.cz (imap.feld.cvut.cz [147.32.192.34])\r
29         by max.feld.cvut.cz (Postfix) with ESMTP id 6FA0F19F331D;\r
30         Sun, 14 Nov 2010 22:54:39 +0100 (CET)\r
31 Received: from steelpick.2x.cz (unknown [213.29.198.144])\r
32         (Authenticated sender: sojkam1)\r
33         by imap.feld.cvut.cz (Postfix) with ESMTPSA id 51E4DFA004;\r
34         Sun, 14 Nov 2010 22:54:39 +0100 (CET)\r
35 Received: from wsh by steelpick.2x.cz with local (Exim 4.72)\r
36         (envelope-from <sojkam1@fel.cvut.cz>)\r
37         id 1PHkX0-0002Oy-W7; Sun, 14 Nov 2010 22:54:39 +0100\r
38 From: Michal Sojka <sojkam1@fel.cvut.cz>\r
39 To: notmuch@notmuchmail.org\r
40 Subject: [PATCH 1/5] test: Better handling of stdout and stderr\r
41 Date: Sun, 14 Nov 2010 22:54:28 +0100\r
42 Message-Id: <1289771672-9204-1-git-send-email-sojkam1@fel.cvut.cz>\r
43 X-Mailer: git-send-email 1.7.2.3\r
44 In-Reply-To: <87hbfjr2sr.fsf@steelpick.2x.cz>\r
45 References: <87hbfjr2sr.fsf@steelpick.2x.cz>\r
46 X-BeenThere: notmuch@notmuchmail.org\r
47 X-Mailman-Version: 2.1.13\r
48 Precedence: list\r
49 List-Id: "Use and development of the notmuch mail system."\r
50         <notmuch.notmuchmail.org>\r
51 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
52         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
53 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
54 List-Post: <mailto:notmuch@notmuchmail.org>\r
55 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
56 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
57         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
58 X-List-Received-Date: Sun, 14 Nov 2010 21:54:54 -0000\r
59 \r
60 Git-style tests (test_expect_success etc.) suppress stdout and stderr\r
61 unless -v is given. Notmuch-style tests (created by test_begin_subtest\r
62 and test_expect_equal) do not have this behavior so implement it the\r
63 same.\r
64 \r
65 Additionally, for both test styles, the test-lib.sh is changed so that\r
66 the content of suppressed stdout and stderr is shown in case of failed\r
67 test.\r
68 \r
69 Finally a test for this functionality is added to basic tests.\r
70 ---\r
71  test/basic                                 |   21 ++++++++++++++++++++-\r
72  test/test-lib.sh                           |   21 +++++++++++++++------\r
73  test/test-verbose                          |   27 +++++++++++++++++++++++++++\r
74  test/test.expected-output/test-verbose-no  |   20 ++++++++++++++++++++\r
75  test/test.expected-output/test-verbose-yes |   24 ++++++++++++++++++++++++\r
76  5 files changed, 106 insertions(+), 7 deletions(-)\r
77  create mode 100755 test/test-verbose\r
78  create mode 100644 test/test.expected-output/test-verbose-no\r
79  create mode 100644 test/test.expected-output/test-verbose-yes\r
80 \r
81 diff --git a/test/basic b/test/basic\r
82 index 725c753..309779c 100755\r
83 --- a/test/basic\r
84 +++ b/test/basic\r
85 @@ -52,9 +52,28 @@ test_expect_code 2 'failure to clean up causes the test to fail' '\r
86  # Ensure that all tests are being run\r
87  test_begin_subtest 'Ensure that all available tests will be run by notmuch-test'\r
88  tests_in_suite=$(grep TESTS= ../notmuch-test | sed -e "s/TESTS=\"\(.*\)\"/\1/" | tr " " "\n" | sort)\r
89 -available=$(ls -1 ../ | grep -v -E "^(aggregate-results.sh|Makefile|Makefile.local|notmuch-test|README|test-lib.sh|test-results|tmp.*|valgrind|corpus*|emacs.expected-output|smtp-dummy|smtp-dummy.c)" | sort)\r
90 +available=$(ls -1 ../ | grep -v -E "^(aggregate-results.sh|Makefile|Makefile.local|notmuch-test|README|test-lib.sh|test-results|tmp.*|valgrind|corpus*|emacs.expected-output|smtp-dummy|smtp-dummy.c|test-verbose|test.expected-output)" | sort)\r
91  test_expect_equal "$tests_in_suite" "$available"\r
92  \r
93 +EXPECTED=../test.expected-output\r
94 +suppress_diff_date() {\r
95 +    sed -e 's/\(.*\-\-\- test-verbose\.4\.\expected\).*/\1/' \\r
96 +       -e 's/\(.*\+\+\+ test-verbose\.4\.\output\).*/\1/'\r
97 +}\r
98 +\r
99 +test_begin_subtest "Ensure that test output is suppressed unless the test fails"\r
100 +output=$(cd ..; ./test-verbose 2>&1 | suppress_diff_date)\r
101 +expected=$(cat $EXPECTED/test-verbose-no | suppress_diff_date)\r
102 +test_expect_equal "$output" "$expected"\r
103 +\r
104 +test_begin_subtest "Ensure that -v does not suppress test output"\r
105 +output=$(cd ..; ./test-verbose -v 2>&1 | suppress_diff_date)\r
106 +expected=$(cat $EXPECTED/test-verbose-yes | suppress_diff_date)\r
107 +# Do not include the results of test-verbose in totals\r
108 +rm $TEST_DIRECTORY/test-results/test-verbose-*\r
109 +test_expect_equal "$output" "$expected"\r
110 +\r
111 +\r
112  ################################################################\r
113  # Test mail store prepared in test-lib.sh\r
114  \r
115 diff --git a/test/test-lib.sh b/test/test-lib.sh\r
116 index 418eaa7..68c9cf8 100644\r
117 --- a/test/test-lib.sh\r
118 +++ b/test/test-lib.sh\r
119 @@ -165,12 +165,6 @@ fi\r
120  echo $(basename "$0"): "Testing ${test_description}"\r
121  \r
122  exec 5>&1\r
123 -if test "$verbose" = "t"\r
124 -then\r
125 -       exec 4>&2 3>&1\r
126 -else\r
127 -       exec 4>/dev/null 3>/dev/null\r
128 -fi\r
129  \r
130  test_failure=0\r
131  test_count=0\r
132 @@ -403,6 +397,11 @@ add_email_corpus ()\r
133  test_begin_subtest ()\r
134  {\r
135      test_subtest_name="$1"\r
136 +    # Remember stdout and stderr file descriptios and redirect test\r
137 +    # output to the previously prepared file descriptors 3 and 4 (see\r
138 +    # bellow)\r
139 +    if test "$verbose" != "t"; then exec 4>test.output 3>&4; fi\r
140 +    exec 6>&1 7>&2 >&3 2>&4\r
141  }\r
142  \r
143  # Pass test if two arguments match\r
144 @@ -413,6 +412,7 @@ test_begin_subtest ()\r
145  # name.\r
146  test_expect_equal ()\r
147  {\r
148 +       exec 1>&6 2>&7          # Restore stdout and stderr\r
149         test "$#" = 3 && { prereq=$1; shift; } || prereq=\r
150         test "$#" = 2 ||\r
151         error "bug in the test script: not 2 or 3 parameters to test_expect_equal"\r
152 @@ -508,6 +508,7 @@ test_failure_ () {\r
153         echo " $1"\r
154         shift\r
155         echo "$@" | sed -e 's/^/        /'\r
156 +       if test "$verbose" != "t"; then cat test.output; fi\r
157         test "$immediate" = "" || { GIT_EXIT_OK=t; exit 1; }\r
158  }\r
159  \r
160 @@ -529,6 +530,7 @@ test_debug () {\r
161  \r
162  test_run_ () {\r
163         test_cleanup=:\r
164 +       if test "$verbose" != "t"; then exec 4>test.output 3>&4; fi\r
165         eval >&3 2>&4 "$1"\r
166         eval_ret=$?\r
167         eval >&3 2>&4 "$test_cleanup"\r
168 @@ -918,6 +920,13 @@ EOF\r
169  # in subprocesses like git equals our $PWD (for pathname comparisons).\r
170  cd -P "$test" || error "Cannot setup test environment"\r
171  \r
172 +if test "$verbose" = "t"\r
173 +then\r
174 +       exec 4>&2 3>&1\r
175 +else\r
176 +       exec 4>test.output 3>&4\r
177 +fi\r
178 +\r
179  this_test=${0##*/}\r
180  for skp in $NOTMUCH_SKIP_TESTS\r
181  do\r
182 diff --git a/test/test-verbose b/test/test-verbose\r
183 new file mode 100755\r
184 index 0000000..f29a9c7\r
185 --- /dev/null\r
186 +++ b/test/test-verbose\r
187 @@ -0,0 +1,27 @@\r
188 +#!/bin/bash\r
189 +\r
190 +test_description='the verbosity options of the test framework itself.'\r
191 +\r
192 +. ./test-lib.sh\r
193 +\r
194 +test_expect_success 'print something in test_expect_success and pass' '\r
195 +  echo "hello stdout" &&\r
196 +  echo "hello stderr" >&2 &&\r
197 +  true\r
198 +'\r
199 +test_expect_success 'print something in test_expect_success and fail' '\r
200 +  echo "hello stdout" &&\r
201 +  echo "hello stderr" >&2 &&\r
202 +  false\r
203 +'\r
204 +test_begin_subtest 'print something between test_begin_subtest and test_expect_equal and pass'\r
205 +echo "hello stdout"\r
206 +echo "hello stderr" >&2\r
207 +test_expect_equal "a" "a"\r
208 +\r
209 +test_begin_subtest 'print something test_begin_subtest and test_expect_equal and fail'\r
210 +echo "hello stdout"\r
211 +echo "hello stderr" >&2\r
212 +test_expect_equal "a" "b"\r
213 +\r
214 +test_done\r
215 diff --git a/test/test.expected-output/test-verbose-no b/test/test.expected-output/test-verbose-no\r
216 new file mode 100644\r
217 index 0000000..0bca754\r
218 --- /dev/null\r
219 +++ b/test/test.expected-output/test-verbose-no\r
220 @@ -0,0 +1,20 @@\r
221 +test-verbose: Testing the verbosity options of the test framework itself.\r
222 + PASS   print something in test_expect_success and pass\r
223 + FAIL   print something in test_expect_success and fail\r
224 +       \r
225 +         echo "hello stdout" &&\r
226 +         echo "hello stderr" >&2 &&\r
227 +         false\r
228 +       \r
229 +hello stdout\r
230 +hello stderr\r
231 + PASS   print something between test_begin_subtest and test_expect_equal and pass\r
232 + FAIL   print something test_begin_subtest and test_expect_equal and fail\r
233 +       --- test-verbose.4.expected     2010-11-14 21:41:12.738189710 +0000\r
234 +       +++ test-verbose.4.output       2010-11-14 21:41:12.738189710 +0000\r
235 +       @@ -1 +1 @@\r
236 +       -b\r
237 +       +a\r
238 +hello stdout\r
239 +hello stderr\r
240 +\r
241 diff --git a/test/test.expected-output/test-verbose-yes b/test/test.expected-output/test-verbose-yes\r
242 new file mode 100644\r
243 index 0000000..ebe5187\r
244 --- /dev/null\r
245 +++ b/test/test.expected-output/test-verbose-yes\r
246 @@ -0,0 +1,24 @@\r
247 +test-verbose: Testing the verbosity options of the test framework itself.\r
248 +hello stdout\r
249 +hello stderr\r
250 + PASS   print something in test_expect_success and pass\r
251 +hello stdout\r
252 +hello stderr\r
253 + FAIL   print something in test_expect_success and fail\r
254 +       \r
255 +         echo "hello stdout" &&\r
256 +         echo "hello stderr" >&2 &&\r
257 +         false\r
258 +       \r
259 +hello stdout\r
260 +hello stderr\r
261 + PASS   print something between test_begin_subtest and test_expect_equal and pass\r
262 +hello stdout\r
263 +hello stderr\r
264 + FAIL   print something test_begin_subtest and test_expect_equal and fail\r
265 +       --- test-verbose.4.expected     2010-11-14 21:41:06.650023289 +0000\r
266 +       +++ test-verbose.4.output       2010-11-14 21:41:06.650023289 +0000\r
267 +       @@ -1 +1 @@\r
268 +       -b\r
269 +       +a\r
270 +\r
271 -- \r
272 1.7.2.3\r
273 \r