Upgrade to Test-Harness-3.17
[p5sagit/p5-mst-13.2.git] / ext / Test-Harness / t / file.t
CommitLineData
bdaf8c65 1#!/usr/bin/perl -w
2
3BEGIN {
4 if ( $ENV{PERL_CORE} ) {
5 chdir 't';
6 @INC = ( '../lib', '../ext/Test-Harness/t/lib' );
7 }
8 else {
9 unshift @INC, 't/lib';
10 }
11}
12
13use strict;
14
15use Test::More;
16
17use TAP::Harness;
18
19my $HARNESS = 'TAP::Harness';
20
21my $source_tests
22 = $ENV{PERL_CORE} ? '../ext/Test-Harness/t/source_tests' : 't/source_tests';
23my $sample_tests
24 = $ENV{PERL_CORE} ? '../ext/Test-Harness/t/sample-tests' : 't/sample-tests';
25
a39e16d8 26plan tests => 56;
bdaf8c65 27
28# note that this test will always pass when run through 'prove'
29ok $ENV{HARNESS_ACTIVE}, 'HARNESS_ACTIVE env variable should be set';
30ok $ENV{HARNESS_VERSION}, 'HARNESS_VERSION env variable should be set';
31
32{
33 my @output;
34 local $^W;
35 require TAP::Formatter::Base;
36 local *TAP::Formatter::Base::_output = sub {
37 my $self = shift;
38 push @output => grep { $_ ne '' }
39 map {
40 local $_ = $_;
41 chomp;
42 trim($_)
43 } map { split /\n/ } @_;
44 };
a39e16d8 45
46 # Make sure verbosity 1 overrides failures and comments.
47 my $harness = TAP::Harness->new(
48 { verbosity => 1,
49 failures => 1,
50 comments => 1,
51 }
52 );
bdaf8c65 53 my $harness_whisper = TAP::Harness->new( { verbosity => -1 } );
54 my $harness_mute = TAP::Harness->new( { verbosity => -2 } );
55 my $harness_directives = TAP::Harness->new( { directives => 1 } );
56 my $harness_failures = TAP::Harness->new( { failures => 1 } );
a39e16d8 57 my $harness_comments = TAP::Harness->new( { comments => 1 } );
58 my $harness_fandc = TAP::Harness->new(
59 { failures => 1,
60 comments => 1
61 }
62 );
bdaf8c65 63
64 can_ok $harness, 'runtests';
65
66 # normal tests in verbose mode
67
68 ok my $aggregate = _runtests( $harness, "$source_tests/harness" ),
69 '... runtests returns the aggregate';
70
71 isa_ok $aggregate, 'TAP::Parser::Aggregator';
72
73 chomp(@output);
74
75 my @expected = (
76 "$source_tests/harness ..",
77 '1..1',
78 'ok 1 - this is a test',
79 'ok',
80 'All tests successful.',
81 );
82 my $status = pop @output;
83 my $expected_status = qr{^Result: PASS$};
84 my $summary = pop @output;
85 my $expected_summary = qr{^Files=1, Tests=1, \d+ wallclock secs};
86
a39e16d8 87 is_deeply \@output, \@expected, '... the output should be correct';
bdaf8c65 88 like $status, $expected_status,
89 '... and the status line should be correct';
90 like $summary, $expected_summary,
91 '... and the report summary should look correct';
92
93 # use an alias for test name
94
95 @output = ();
96 ok $aggregate
97 = _runtests( $harness, [ "$source_tests/harness", 'My Nice Test' ] ),
a39e16d8 98 'runtests returns the aggregate';
bdaf8c65 99
100 isa_ok $aggregate, 'TAP::Parser::Aggregator';
101
102 chomp(@output);
103
104 @expected = (
105 'My Nice Test ..',
106 '1..1',
107 'ok 1 - this is a test',
108 'ok',
109 'All tests successful.',
110 );
111 $status = pop @output;
112 $expected_status = qr{^Result: PASS$};
113 $summary = pop @output;
114 $expected_summary = qr{^Files=1, Tests=1, \d+ wallclock secs};
115
a39e16d8 116 is_deeply \@output, \@expected, '... the output should be correct';
bdaf8c65 117 like $status, $expected_status,
118 '... and the status line should be correct';
119 like $summary, $expected_summary,
120 '... and the report summary should look correct';
121
122 # run same test twice
123
124 @output = ();
125 ok $aggregate = _runtests(
126 $harness, [ "$source_tests/harness", 'My Nice Test' ],
127 [ "$source_tests/harness", 'My Nice Test Again' ]
128 ),
a39e16d8 129 'runtests labels returns the aggregate';
bdaf8c65 130
131 isa_ok $aggregate, 'TAP::Parser::Aggregator';
132
133 chomp(@output);
134
135 @expected = (
136 'My Nice Test ........',
137 '1..1',
138 'ok 1 - this is a test',
139 'ok',
140 'My Nice Test Again ..',
141 '1..1',
142 'ok 1 - this is a test',
143 'ok',
144 'All tests successful.',
145 );
146 $status = pop @output;
147 $expected_status = qr{^Result: PASS$};
148 $summary = pop @output;
149 $expected_summary = qr{^Files=2, Tests=2, \d+ wallclock secs};
150
a39e16d8 151 is_deeply \@output, \@expected, '... the output should be correct';
bdaf8c65 152 like $status, $expected_status,
153 '... and the status line should be correct';
154 like $summary, $expected_summary,
155 '... and the report summary should look correct';
156
157 # normal tests in quiet mode
158
159 @output = ();
a39e16d8 160 ok _runtests( $harness_whisper, "$source_tests/harness" ),
161 'Run tests with whisper';
bdaf8c65 162
163 chomp(@output);
164 @expected = (
165 "$source_tests/harness .. ok",
166 'All tests successful.',
167 );
168
169 $status = pop @output;
170 $expected_status = qr{^Result: PASS$};
171 $summary = pop @output;
172 $expected_summary = qr/^Files=1, Tests=1, \d+ wallclock secs/;
173
a39e16d8 174 is_deeply \@output, \@expected, '... the output should be correct';
bdaf8c65 175 like $status, $expected_status,
176 '... and the status line should be correct';
177 like $summary, $expected_summary,
178 '... and the report summary should look correct';
179
180 # normal tests in really_quiet mode
181
182 @output = ();
a39e16d8 183 ok _runtests( $harness_mute, "$source_tests/harness" ), 'Run tests mute';
bdaf8c65 184
185 chomp(@output);
186 @expected = (
187 'All tests successful.',
188 );
189
190 $status = pop @output;
191 $expected_status = qr{^Result: PASS$};
192 $summary = pop @output;
193 $expected_summary = qr/^Files=1, Tests=1, \d+ wallclock secs/;
194
a39e16d8 195 is_deeply \@output, \@expected, '... the output should be correct';
bdaf8c65 196 like $status, $expected_status,
197 '... and the status line should be correct';
198 like $summary, $expected_summary,
199 '... and the report summary should look correct';
200
201 # normal tests with failures
202
203 @output = ();
a39e16d8 204 ok _runtests( $harness, "$source_tests/harness_failure" ),
205 'Run tests with failures';
bdaf8c65 206
207 $status = pop @output;
208 $summary = pop @output;
209
a39e16d8 210 like $status, qr{^Result: FAIL$}, '... the status line should be correct';
bdaf8c65 211
a39e16d8 212 my @summary = @output[ 9 .. $#output ];
213 @output = @output[ 0 .. 8 ];
bdaf8c65 214
215 @expected = (
216 "$source_tests/harness_failure ..",
217 '1..2',
218 'ok 1 - this is a test',
219 'not ok 2 - this is another test',
a39e16d8 220 q{# Failed test 'this is another test'},
221 '# in harness_failure.t at line 5.',
222 q{# got: 'waffle'},
223 q{# expected: 'yarblokos'},
bdaf8c65 224 'Failed 1/2 subtests',
225 );
226
227 is_deeply \@output, \@expected,
228 '... and failing test output should be correct';
229
230 my @expected_summary = (
231 'Test Summary Report',
232 '-------------------',
233 "$source_tests/harness_failure (Wstat: 0 Tests: 2 Failed: 1)",
234 'Failed test:',
235 '2',
236 );
237
238 is_deeply \@summary, \@expected_summary,
239 '... and the failure summary should also be correct';
240
241 # quiet tests with failures
242
243 @output = ();
a39e16d8 244 ok _runtests( $harness_whisper, "$source_tests/harness_failure" ),
245 'Run whisper tests with failures';
bdaf8c65 246
247 $status = pop @output;
248 $summary = pop @output;
249 @expected = (
250 "$source_tests/harness_failure ..",
251 'Failed 1/2 subtests',
252 'Test Summary Report',
253 '-------------------',
254 "$source_tests/harness_failure (Wstat: 0 Tests: 2 Failed: 1)",
255 'Failed test:',
256 '2',
257 );
258
a39e16d8 259 like $status, qr{^Result: FAIL$}, '... the status line should be correct';
bdaf8c65 260
261 is_deeply \@output, \@expected,
262 '... and failing test output should be correct';
263
264 # really quiet tests with failures
265
266 @output = ();
a39e16d8 267 ok _runtests( $harness_mute, "$source_tests/harness_failure" ),
268 'Run mute tests with failures';
bdaf8c65 269
270 $status = pop @output;
271 $summary = pop @output;
272 @expected = (
273 'Test Summary Report',
274 '-------------------',
275 "$source_tests/harness_failure (Wstat: 0 Tests: 2 Failed: 1)",
276 'Failed test:',
277 '2',
278 );
279
a39e16d8 280 like $status, qr{^Result: FAIL$}, '... the status line should be correct';
bdaf8c65 281
282 is_deeply \@output, \@expected,
283 '... and failing test output should be correct';
284
285 # only show directives
286
287 @output = ();
a39e16d8 288 ok _runtests(
bdaf8c65 289 $harness_directives,
290 "$source_tests/harness_directives"
a39e16d8 291 ),
292 'Run tests with directives';
bdaf8c65 293
294 chomp(@output);
295
296 @expected = (
297 "$source_tests/harness_directives ..",
298 'not ok 2 - we have a something # TODO some output',
299 "ok 3 houston, we don't have liftoff # SKIP no funding",
300 'ok',
301 'All tests successful.',
302
303 # ~TODO {{{ this should be an option
304 #'Test Summary Report',
305 #'-------------------',
306 #"$source_tests/harness_directives (Wstat: 0 Tests: 3 Failed: 0)",
307 #'Tests skipped:',
308 #'3',
309 # }}}
310 );
311
312 $status = pop @output;
313 $summary = pop @output;
314 $expected_summary = qr/^Files=1, Tests=3, \d+ wallclock secs/;
315
a39e16d8 316 is_deeply \@output, \@expected, '... the output should be correct';
bdaf8c65 317 like $summary, $expected_summary,
318 '... and the report summary should look correct';
319
320 like $status, qr{^Result: PASS$},
321 '... and the status line should be correct';
322
323 # normal tests with bad tap
324
325 @output = ();
a39e16d8 326 ok _runtests( $harness, "$source_tests/harness_badtap" ),
327 'Run tests with bad TAP';
bdaf8c65 328 chomp(@output);
329
330 @output = map { trim($_) } @output;
331 $status = pop @output;
332 @summary = @output[ 6 .. ( $#output - 1 ) ];
333 @output = @output[ 0 .. 5 ];
334 @expected = (
335 "$source_tests/harness_badtap ..",
336 '1..2',
337 'ok 1 - this is a test',
338 'not ok 2 - this is another test',
339 '1..2',
340 'Failed 1/2 subtests',
341 );
342 is_deeply \@output, \@expected,
a39e16d8 343 '... failing test output should be correct';
bdaf8c65 344 like $status, qr{^Result: FAIL$},
345 '... and the status line should be correct';
346 @expected_summary = (
347 'Test Summary Report',
348 '-------------------',
349 "$source_tests/harness_badtap (Wstat: 0 Tests: 2 Failed: 1)",
350 'Failed test:',
351 '2',
352 'Parse errors: More than one plan found in TAP output',
353 );
354 is_deeply \@summary, \@expected_summary,
355 '... and the badtap summary should also be correct';
356
357 # coverage testing for _should_show_failures
358 # only show failures
359
360 @output = ();
a39e16d8 361 ok _runtests( $harness_failures, "$source_tests/harness_failure" ),
362 'Run tests with failures only';
bdaf8c65 363
364 chomp(@output);
365
366 @expected = (
367 "$source_tests/harness_failure ..",
368 'not ok 2 - this is another test',
369 'Failed 1/2 subtests',
370 'Test Summary Report',
371 '-------------------',
372 "$source_tests/harness_failure (Wstat: 0 Tests: 2 Failed: 1)",
373 'Failed test:',
374 '2',
375 );
376
377 $status = pop @output;
378 $summary = pop @output;
379
a39e16d8 380 like $status, qr{^Result: FAIL$}, '... the status line should be correct';
bdaf8c65 381 $expected_summary = qr/^Files=1, Tests=2, \d+ wallclock secs/;
382 is_deeply \@output, \@expected, '... and the output should be correct';
383
384 # check the status output for no tests
385
386 @output = ();
a39e16d8 387 ok _runtests( $harness_failures, "$sample_tests/no_output" ),
388 'Run tests with failures';
bdaf8c65 389
390 chomp(@output);
391
392 @expected = (
393 "$sample_tests/no_output ..",
394 'No subtests run',
395 'Test Summary Report',
396 '-------------------',
397 "$sample_tests/no_output (Wstat: 0 Tests: 0 Failed: 0)",
398 'Parse errors: No plan found in TAP output',
399 );
400
401 $status = pop @output;
402 $summary = pop @output;
403
a39e16d8 404 like $status, qr{^Result: FAIL$}, '... the status line should be correct';
405 $expected_summary = qr/^Files=1, Tests=2, \d+ wallclock secs/;
406 is_deeply \@output, \@expected, '... and the output should be correct';
407
408 # coverage testing for _should_show_comments
409 # only show comments
410
411 @output = ();
412 ok _runtests( $harness_comments, "$source_tests/harness_failure" ),
413 'Run tests with comments';
414 chomp(@output);
415
416 @expected = (
417 "$source_tests/harness_failure ..",
418 q{# Failed test 'this is another test'},
419 '# in harness_failure.t at line 5.',
420 q{# got: 'waffle'},
421 q{# expected: 'yarblokos'},
422 'Failed 1/2 subtests',
423 'Test Summary Report',
424 '-------------------',
425 "$source_tests/harness_failure (Wstat: 0 Tests: 2 Failed: 1)",
426 'Failed test:',
427 '2',
428 );
429
430 $status = pop @output;
431 $summary = pop @output;
432
433 like $status, qr{^Result: FAIL$}, '... the status line should be correct';
434 $expected_summary = qr/^Files=1, Tests=2, \d+ wallclock secs/;
435 is_deeply \@output, \@expected, '... and the output should be correct';
436
437 # coverage testing for _should_show_comments and _should_show_failures
438 # only show comments and failures
439
440 @output = ();
441 $ENV{FOO} = 1;
442 ok _runtests( $harness_fandc, "$source_tests/harness_failure" ),
443 'Run tests with failures and comments';
444 delete $ENV{FOO};
445 chomp(@output);
446
447 @expected = (
448 "$source_tests/harness_failure ..",
449 'not ok 2 - this is another test',
450 q{# Failed test 'this is another test'},
451 '# in harness_failure.t at line 5.',
452 q{# got: 'waffle'},
453 q{# expected: 'yarblokos'},
454 'Failed 1/2 subtests',
455 'Test Summary Report',
456 '-------------------',
457 "$source_tests/harness_failure (Wstat: 0 Tests: 2 Failed: 1)",
458 'Failed test:',
459 '2',
460 );
461
462 $status = pop @output;
463 $summary = pop @output;
464
465 like $status, qr{^Result: FAIL$}, '... the status line should be correct';
bdaf8c65 466 $expected_summary = qr/^Files=1, Tests=2, \d+ wallclock secs/;
467 is_deeply \@output, \@expected, '... and the output should be correct';
468
469 #XXXX
470}
471
472sub trim {
473 $_[0] =~ s/^\s+|\s+$//g;
474 return $_[0];
475}
476
477sub _runtests {
478 my ( $harness, @tests ) = @_;
479 local $ENV{PERL_TEST_HARNESS_DUMP_TAP} = 0;
480 my $aggregate = $harness->runtests(@tests);
481 return $aggregate;
482}
483