4 if ( $ENV{PERL_CORE} ) {
6 @INC = ( '../lib', '../ext/Test/Harness/t/lib' );
20 my $HARNESS = 'TAP::Harness';
23 = $ENV{PERL_CORE} ? '../ext/Test/Harness/t/source_tests' : 't/source_tests';
25 = $ENV{PERL_CORE} ? '../ext/Test/Harness/t/sample-tests' : 't/sample-tests';
29 # note that this test will always pass when run through 'prove'
30 ok $ENV{HARNESS_ACTIVE}, 'HARNESS_ACTIVE env variable should be set';
31 ok $ENV{HARNESS_VERSION}, 'HARNESS_VERSION env variable should be set';
33 #### For color tests ####
37 sub new { bless {}, shift }
41 my ( $self, $output, $color ) = @_;
42 $output->("[[$color]]");
49 $harness->formatter->_colorizer( Colorizer->new );
52 can_ok $HARNESS, 'new';
54 eval { $HARNESS->new( { no_such_key => 1 } ) };
55 like $@, qr/\QUnknown arguments to TAP::Harness::new (no_such_key)/,
56 '... and calling it with bad keys should fail';
58 eval { $HARNESS->new( { lib => 'aint_no_such_lib' } ) };
59 is $@, '', '... and calling it with a non-existent lib is fine';
61 eval { $HARNESS->new( { lib => [qw/bad_lib_1 bad_lib_2/] } ) };
62 is $@, '', '... and calling it with non-existent libs is fine';
64 ok my $harness = $HARNESS->new,
65 'Calling new() without arguments should succeed';
67 foreach my $test_args ( get_arg_sets() ) {
68 my %args = %$test_args;
69 foreach my $key ( sort keys %args ) {
70 $args{$key} = $args{$key}{in};
72 ok my $harness = $HARNESS->new( {%args} ),
73 'Calling new() with valid arguments should succeed';
74 isa_ok $harness, $HARNESS, '... and the object it returns';
76 while ( my ( $property, $test ) = each %$test_args ) {
77 my $value = $test->{out};
78 can_ok $harness, $property;
79 is_deeply scalar $harness->$property(), $value, $test->{test_name};
86 local *TAP::Formatter::Console::_should_show_count = sub {0};
87 local *TAP::Formatter::Console::_output = sub {
89 push @output => grep { $_ ne '' }
96 my $harness = TAP::Harness->new( { verbosity => 1 } );
97 my $harness_whisper = TAP::Harness->new( { verbosity => -1 } );
98 my $harness_mute = TAP::Harness->new( { verbosity => -2 } );
99 my $harness_directives = TAP::Harness->new( { directives => 1 } );
100 my $harness_failures = TAP::Harness->new( { failures => 1 } );
104 can_ok $harness, 'runtests';
106 # normal tests in verbose mode
108 ok my $aggregate = _runtests( $harness, "$source_tests/harness" ),
109 '... runtests returns the aggregate';
111 isa_ok $aggregate, 'TAP::Parser::Aggregator';
116 "$source_tests/harness....",
119 'ok 1 - this is a test',
122 'All tests successful.',
124 my $status = pop @output;
125 my $expected_status = qr{^Result: PASS$};
126 my $summary = pop @output;
127 my $expected_summary = qr{^Files=1, Tests=1, \d+ wallclock secs};
129 is_deeply \@output, \@expected, '... and the output should be correct';
130 like $status, $expected_status,
131 '... and the status line should be correct';
132 like $summary, $expected_summary,
133 '... and the report summary should look correct';
135 # use an alias for test name
139 = _runtests( $harness, [ "$source_tests/harness", 'My Nice Test' ] ),
140 '... runtests returns the aggregate';
142 isa_ok $aggregate, 'TAP::Parser::Aggregator';
150 'ok 1 - this is a test',
153 'All tests successful.',
155 $status = pop @output;
156 $expected_status = qr{^Result: PASS$};
157 $summary = pop @output;
158 $expected_summary = qr{^Files=1, Tests=1, \d+ wallclock secs};
160 is_deeply \@output, \@expected, '... and the output should be correct';
161 like $status, $expected_status,
162 '... and the status line should be correct';
163 like $summary, $expected_summary,
164 '... and the report summary should look correct';
166 # run same test twice
169 ok $aggregate = _runtests(
170 $harness, [ "$source_tests/harness", 'My Nice Test' ],
171 [ "$source_tests/harness", 'My Nice Test Again' ]
173 '... runtests returns the aggregate';
175 isa_ok $aggregate, 'TAP::Parser::Aggregator';
180 'My Nice Test..........',
183 'ok 1 - this is a test',
186 'My Nice Test Again....',
189 'ok 1 - this is a test',
192 'All tests successful.',
194 $status = pop @output;
195 $expected_status = qr{^Result: PASS$};
196 $summary = pop @output;
197 $expected_summary = qr{^Files=2, Tests=2, \d+ wallclock secs};
199 is_deeply \@output, \@expected, '... and the output should be correct';
200 like $status, $expected_status,
201 '... and the status line should be correct';
202 like $summary, $expected_summary,
203 '... and the report summary should look correct';
205 # normal tests in quiet mode
208 _runtests( $harness_whisper, "$source_tests/harness" );
212 "$source_tests/harness....",
214 'All tests successful.',
217 $status = pop @output;
218 $expected_status = qr{^Result: PASS$};
219 $summary = pop @output;
220 $expected_summary = qr/^Files=1, Tests=1, \d+ wallclock secs/;
222 is_deeply \@output, \@expected, '... and the output should be correct';
223 like $status, $expected_status,
224 '... and the status line should be correct';
225 like $summary, $expected_summary,
226 '... and the report summary should look correct';
228 # normal tests in really_quiet mode
231 _runtests( $harness_mute, "$source_tests/harness" );
235 'All tests successful.',
238 $status = pop @output;
239 $expected_status = qr{^Result: PASS$};
240 $summary = pop @output;
241 $expected_summary = qr/^Files=1, Tests=1, \d+ wallclock secs/;
243 is_deeply \@output, \@expected, '... and the output should be correct';
244 like $status, $expected_status,
245 '... and the status line should be correct';
246 like $summary, $expected_summary,
247 '... and the report summary should look correct';
249 # normal tests with failures
252 _runtests( $harness, "$source_tests/harness_failure" );
254 $status = pop @output;
255 $summary = pop @output;
257 like $status, qr{^Result: FAIL$},
258 '... and the status line should be correct';
260 my @summary = @output[ 10 .. $#output ];
261 @output = @output[ 0 .. 9 ];
264 "$source_tests/harness_failure....",
267 'ok 1 - this is a test',
270 'not ok 2 - this is another test',
273 'Failed 1/2 subtests',
276 is_deeply \@output, \@expected,
277 '... and failing test output should be correct';
279 my @expected_summary = (
281 'Test Summary Report',
282 '-------------------',
284 "$source_tests/harness_failure (Wstat: 0 Tests: 2 Failed: 1)",
294 is_deeply \@summary, \@expected_summary,
295 '... and the failure summary should also be correct';
297 # quiet tests with failures
300 _runtests( $harness_whisper, "$source_tests/harness_failure" );
302 $status = pop @output;
303 $summary = pop @output;
305 "$source_tests/harness_failure....",
306 'Failed 1/2 subtests',
307 'Test Summary Report',
308 '-------------------',
309 "$source_tests/harness_failure (Wstat: 0 Tests: 2 Failed: 1)",
314 like $status, qr{^Result: FAIL$},
315 '... and the status line should be correct';
317 is_deeply \@output, \@expected,
318 '... and failing test output should be correct';
320 # really quiet tests with failures
323 _runtests( $harness_mute, "$source_tests/harness_failure" );
325 $status = pop @output;
326 $summary = pop @output;
328 'Test Summary Report',
329 '-------------------',
330 "$source_tests/harness_failure (Wstat: 0 Tests: 2 Failed: 1)",
335 like $status, qr{^Result: FAIL$},
336 '... and the status line should be correct';
338 is_deeply \@output, \@expected,
339 '... and failing test output should be correct';
341 # only show directives
346 "$source_tests/harness_directives"
352 "$source_tests/harness_directives....",
353 'not ok 2 - we have a something # TODO some output',
354 "ok 3 houston, we don't have liftoff # SKIP no funding",
356 'All tests successful.',
358 # ~TODO {{{ this should be an option
359 #'Test Summary Report',
360 #'-------------------',
361 #"$source_tests/harness_directives (Wstat: 0 Tests: 3 Failed: 0)",
367 $status = pop @output;
368 $summary = pop @output;
369 $expected_summary = qr/^Files=1, Tests=3, \d+ wallclock secs/;
371 is_deeply \@output, \@expected, '... and the output should be correct';
372 like $summary, $expected_summary,
373 '... and the report summary should look correct';
375 like $status, qr{^Result: PASS$},
376 '... and the status line should be correct';
378 # normal tests with bad tap
380 # install callback handler
382 my $callback_count = 0;
384 my @callback_log = ();
386 for my $evt (qw(parser_args made_parser before_runtests after_runtests)) {
389 push @callback_log, $evt;
402 _runtests( $harness, "$source_tests/harness_badtap" );
405 @output = map { trim($_) } @output;
406 $status = pop @output;
407 @summary = @output[ 12 .. ( $#output - 1 ) ];
408 @output = @output[ 0 .. 11 ];
410 "$source_tests/harness_badtap....",
413 'ok 1 - this is a test',
416 'not ok 2 - this is another test',
421 'Failed 1/2 subtests',
423 is_deeply \@output, \@expected,
424 '... and failing test output should be correct';
425 like $status, qr{^Result: FAIL$},
426 '... and the status line should be correct';
427 @expected_summary = (
429 'Test Summary Report',
430 '-------------------',
432 "$source_tests/harness_badtap (Wstat: 0 Tests: 2 Failed: 1)",
441 'Parse errors: More than one plan found in TAP output',
444 is_deeply \@summary, \@expected_summary,
445 '... and the badtap summary should also be correct';
447 cmp_ok( $callback_count, '==', 1, 'callback called once' );
450 [ 'before_runtests', 'parser_args', 'made_parser', 'after_runtests' ],
451 'callback log matches'
453 isa_ok $parser, 'TAP::Parser';
455 # coverage testing for _should_show_failures
459 _runtests( $harness_failures, "$source_tests/harness_failure" );
464 "$source_tests/harness_failure....",
465 'not ok 2 - this is another test',
466 'Failed 1/2 subtests',
467 'Test Summary Report',
468 '-------------------',
469 "$source_tests/harness_failure (Wstat: 0 Tests: 2 Failed: 1)",
474 $status = pop @output;
475 $summary = pop @output;
477 like $status, qr{^Result: FAIL$},
478 '... and the status line should be correct';
479 $expected_summary = qr/^Files=1, Tests=2, \d+ wallclock secs/;
480 is_deeply \@output, \@expected, '... and the output should be correct';
482 # check the status output for no tests
485 _runtests( $harness_failures, "$sample_tests/no_output" );
490 "$sample_tests/no_output....",
492 'Test Summary Report',
493 '-------------------',
494 "$sample_tests/no_output (Wstat: 0 Tests: 0 Failed: 0)",
495 'Parse errors: No plan found in TAP output',
498 $status = pop @output;
499 $summary = pop @output;
501 like $status, qr{^Result: FAIL$},
502 '... and the status line should be correct';
503 $expected_summary = qr/^Files=1, Tests=2, \d+ wallclock secs/;
504 is_deeply \@output, \@expected, '... and the output should be correct';
509 # make sure we can exec something ... anything!
512 my $cat = '/bin/cat';
517 my $capture = IO::c55Capture->new_handle;
518 my $harness = TAP::Harness->new(
529 ? '../ext/Test/Harness/t/data/catme.1'
534 my @output = tied($$capture)->dump;
535 my $status = pop @output;
536 like $status, qr{^Result: PASS$},
537 '... and the status line should be correct';
538 pop @output; # get rid of summary line
539 my $answer = pop @output;
540 is( $answer, "All tests successful.\n", 'cat meows' );
543 # make sure that we can exec with a code ref.
545 my $capture = IO::c55Capture->new_handle;
546 my $harness = TAP::Harness->new(
553 _runtests( $harness, "$source_tests/harness" );
555 my @output = tied($$capture)->dump;
556 my $status = pop @output;
557 like $status, qr{^Result: PASS$},
558 '... and the status line should be correct';
559 pop @output; # get rid of summary line
560 my $answer = pop @output;
561 is( $answer, "All tests successful.\n", 'cat meows' );
564 # catches "exec accumulates arguments" issue (r77)
566 my $capture = IO::c55Capture->new_handle;
567 my $harness = TAP::Harness->new(
576 "$source_tests/harness_complain"
577 , # will get mad if run with args
578 "$source_tests/harness",
581 my @output = tied($$capture)->dump;
582 my $status = pop @output;
583 like $status, qr{^Result: PASS$},
584 '... and the status line should be correct';
585 pop @output; # get rid of summary line
586 is( $output[-1], "All tests successful.\n",
587 'No exec accumulation'
592 $_[0] =~ s/^\s+|\s+$//g;
597 return [ map {"-I$_"} @_ ];
602 # keys are keys to new()
606 out => liblist('lib'),
607 test_name => '... a single lib switch should be correct'
612 test_name => '... and we should be able to set verbosity to 1'
618 # test_name => '... and we should be able to set verbose to true'
622 in => [ 'lib', 't' ],
623 out => liblist( 'lib', 't' ),
624 test_name => '... multiple lib dirs should be correct'
629 test_name => '... and we should be able to set verbosity to 0'
635 # test_name => '... and we should be able to set verbose to false'
639 in => [ '-T', '-w', '-T' ],
640 out => [ '-T', '-w', '-T' ],
641 test_name => '... duplicate switches should remain',
647 '... and we should be able to set failures to true',
652 test_name => '... and we should be able to set verbosity to -1'
658 # test_name => '... and we should be able to set quiet to false'
665 test_name => '... and we should be able to set verbosity to -2'
672 # '... and we should be able to set really_quiet to true',
678 '... and we should be able to set the executable',
685 '... leading dashes (-) on switches are not optional',
691 test_name => '... we should be able to set switches',
696 test_name => '... and we should be able to set failures to true'
702 my ( $harness, @tests ) = @_;
703 local $ENV{PERL_TEST_HARNESS_DUMP_TAP} = 0;
704 my $aggregate = $harness->runtests(@tests);
710 # coverage tests for ctor
712 my $harness = TAP::Harness->new(
721 is $harness->timer(), 0, 'timer getter';
722 is $harness->timer(10), 10, 'timer setter';
723 is $harness->errors(), 1, 'errors getter';
724 is $harness->errors(10), 10, 'errors setter';
725 is $harness->merge(), 2, 'merge getter';
726 is $harness->merge(10), 10, 'merge setter';
729 is $harness->jobs(), 1, 'jobs';
734 # coverage tests for the stdout key of VALIDATON_FOR, used by _initialize() in the ctor
736 # the coverage tests are
737 # 1. ref $ref => false
738 # 2. ref => ! GLOB and ref->can(print)
739 # 3. ref $ref => GLOB
746 local $SIG{__DIE__} = sub { push @die, @_ };
748 my $harness = TAP::Harness->new(
749 { stdout => bless {}, '0', # how evil is THAT !!!
754 is @die, 1, 'bad filehandle to stdout';
755 like pop @die, qr/option 'stdout' needs a filehandle/,
756 '... and we died as expected';
764 sub new { return bless {}, shift }
770 my $harness = TAP::Harness->new(
771 { stdout => Printable->new(),
775 isa_ok $harness, 'TAP::Harness';
781 $harness = TAP::Harness->new(
782 { stdout => bless {}, 'GLOB', # again with the evil
786 isa_ok $harness, 'TAP::Harness';
791 # coverage testing of lib/switches accessor
792 my $harness = TAP::Harness->new;
797 local $SIG{__DIE__} = sub { push @die, @_ };
799 $harness->switches(qw( too many arguments));
802 is @die, 1, 'too many arguments to accessor';
804 like pop @die, qr/Too many arguments to method 'switches'/,
805 '...and we died as expected';
807 $harness->switches('simple scalar');
809 my $arrref = $harness->switches;
810 is_deeply $arrref, ['simple scalar'], 'scalar wrapped in arr ref';
815 # coverage tests for the basically untested T::H::_open_spool
819 ? ( File::Spec->updir(), 'ext', 'Test', 'Harness' )
824 $ENV{PERL_TEST_HARNESS_DUMP_TAP} = File::Spec->catfile(@spool);
826 # now given that we're going to be writing stuff to the file system, make sure we have
832 # remove the tree if we made it this far
833 rmtree( $ENV{PERL_TEST_HARNESS_DUMP_TAP} )
834 if $ENV{PERL_TEST_HARNESS_DUMP_TAP};
837 my $harness = TAP::Harness->new( { verbosity => -2 } );
839 can_ok $harness, 'runtests';
841 # normal tests in verbose mode
844 = $harness->runtests( File::Spec->catfile( $source_tests, 'harness' ) );
846 isa_ok $parser, 'TAP::Parser::Aggregator',
847 '... runtests returns the aggregate';
849 ok -e File::Spec->catfile(
850 $ENV{PERL_TEST_HARNESS_DUMP_TAP},
851 $source_tests, 'harness'
859 { name => 'all the same',
860 input => [ 'foo.t', 'bar.t', 'fletz.t' ],
862 [ 'foo.t', 'foo' ], [ 'bar.t', 'bar' ],
863 [ 'fletz.t', 'fletz' ]
866 { name => 'all the same, already cooked',
867 input => [ 'foo.t', [ 'bar.t', 'brip' ], 'fletz.t' ],
869 [ 'foo.t', 'foo' ], [ 'bar.t', 'brip' ],
870 [ 'fletz.t', 'fletz' ]
873 { name => 'different exts',
874 input => [ 'foo.t', 'bar.u', 'fletz.v' ],
876 [ 'foo.t', 'foo.t' ], [ 'bar.u', 'bar.u' ],
877 [ 'fletz.v', 'fletz.v' ]
880 { name => 'different exts, one already cooked',
881 input => [ 'foo.t', [ 'bar.u', 'bam' ], 'fletz.v' ],
883 [ 'foo.t', 'foo.t' ], [ 'bar.u', 'bam' ],
884 [ 'fletz.v', 'fletz.v' ]
887 { name => 'different exts, two already cooked',
888 input => [ 'foo.t', [ 'bar.u', 'bam.q' ], [ 'fletz.v', 'boo' ] ],
890 [ 'foo.t', 'foo.t' ], [ 'bar.u', 'bam.q' ],
896 for my $case (@cases) {
897 is_deeply [ TAP::Harness->_add_descriptions( @{ $case->{input} } ) ],
898 $case->{output}, '_add_descriptions: ' . $case->{name};