Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

478 lines
13 KiB

  1. package Test::Harness;
  2. BEGIN {require 5.002;}
  3. use Exporter;
  4. use Benchmark;
  5. use Config;
  6. use FileHandle;
  7. use strict;
  8. use vars qw($VERSION $verbose $switches $have_devel_corestack $curtest
  9. @ISA @EXPORT @EXPORT_OK);
  10. $have_devel_corestack = 0;
  11. $VERSION = "1.1602";
  12. # Some experimental versions of OS/2 build have broken $?
  13. my $ignore_exitcode = $ENV{HARNESS_IGNORE_EXITCODE};
  14. my $files_in_dir = $ENV{HARNESS_FILELEAK_IN_DIR};
  15. my $tests_skipped = 0;
  16. my $subtests_skipped = 0;
  17. @ISA=('Exporter');
  18. @EXPORT= qw(&runtests);
  19. @EXPORT_OK= qw($verbose $switches);
  20. format STDOUT_TOP =
  21. Failed Test Status Wstat Total Fail Failed List of failed
  22. -------------------------------------------------------------------------------
  23. .
  24. format STDOUT =
  25. @<<<<<<<<<<<<<< @>> @>>>> @>>>> @>>> ^##.##% ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  26. { $curtest->{name},
  27. $curtest->{estat},
  28. $curtest->{wstat},
  29. $curtest->{max},
  30. $curtest->{failed},
  31. $curtest->{percent},
  32. $curtest->{canon}
  33. }
  34. ~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  35. $curtest->{canon}
  36. .
  37. $verbose = 0;
  38. $switches = "-w";
  39. sub globdir { opendir DIRH, shift; my @f = readdir DIRH; closedir DIRH; @f }
  40. sub runtests {
  41. my(@tests) = @_;
  42. local($|) = 1;
  43. my($test,$te,$ok,$next,$max,$pct,$totok,$totbonus,@failed,%failedtests);
  44. my $totmax = 0;
  45. my $files = 0;
  46. my $bad = 0;
  47. my $good = 0;
  48. my $total = @tests;
  49. # pass -I flags to children
  50. my $old5lib = $ENV{PERL5LIB};
  51. local($ENV{'PERL5LIB'}) = join($Config{path_sep}, @INC);
  52. if ($^O eq 'VMS') { $switches =~ s/-(\S*[A-Z]\S*)/"-$1"/g }
  53. my @dir_files = globdir $files_in_dir if defined $files_in_dir;
  54. my $t_start = new Benchmark;
  55. while ($test = shift(@tests)) {
  56. $te = $test;
  57. chop($te);
  58. if ($^O eq 'VMS') { $te =~ s/^.*\.t\./[.t./; }
  59. print "$te" . '.' x (20 - length($te));
  60. my $fh = new FileHandle;
  61. $fh->open($test) or print "can't open $test. $!\n";
  62. my $first = <$fh>;
  63. my $s = $switches;
  64. $s .= q[ "-T"] if $first =~ /^#!.*\bperl.*-\w*T/;
  65. $fh->close or print "can't close $test. $!\n";
  66. my $cmd = ($ENV{'COMPILE_TEST'})?
  67. "./perl -I../lib ../utils/perlcc $test -run -verbose dcf -log ./compilelog |"
  68. : "$^X $s $test|";
  69. $cmd = "MCR $cmd" if $^O eq 'VMS';
  70. $fh->open($cmd) or print "can't run $test. $!\n";
  71. $ok = $next = $max = 0;
  72. @failed = ();
  73. my %todo = ();
  74. my $bonus = 0;
  75. my $skipped = 0;
  76. while (<$fh>) {
  77. if( $verbose ){
  78. print $_;
  79. }
  80. if (/^1\.\.([0-9]+) todo([\d\s]+)\;/) {
  81. $max = $1;
  82. for (split(/\s+/, $2)) { $todo{$_} = 1; }
  83. $totmax += $max;
  84. $files++;
  85. $next = 1;
  86. } elsif (/^1\.\.([0-9]+)/) {
  87. $max = $1;
  88. $totmax += $max;
  89. $files++;
  90. $next = 1;
  91. } elsif ($max && /^(not\s+)?ok\b/) {
  92. my $this = $next;
  93. if (/^not ok\s*(\d*)/){
  94. $this = $1 if $1 > 0;
  95. if (!$todo{$this}) {
  96. push @failed, $this;
  97. } else {
  98. $ok++;
  99. $totok++;
  100. }
  101. } elsif (/^ok\s*(\d*)(\s*\#\s*[Ss]kip)?/) {
  102. $this = $1 if $1 > 0;
  103. $ok++;
  104. $totok++;
  105. $skipped++ if defined $2;
  106. $bonus++, $totbonus++ if $todo{$this};
  107. }
  108. if ($this > $next) {
  109. # warn "Test output counter mismatch [test $this]\n";
  110. # no need to warn probably
  111. push @failed, $next..$this-1;
  112. } elsif ($this < $next) {
  113. #we have seen more "ok" lines than the number suggests
  114. warn "Confused test output: test $this answered after test ", $next-1, "\n";
  115. $next = $this;
  116. }
  117. $next = $this + 1;
  118. }
  119. }
  120. $fh->close; # must close to reap child resource values
  121. my $wstatus = $ignore_exitcode ? 0 : $?; # Can trust $? ?
  122. my $estatus;
  123. $estatus = ($^O eq 'VMS'
  124. ? eval 'use vmsish "status"; $estatus = $?'
  125. : $wstatus >> 8);
  126. if ($wstatus) {
  127. my ($failed, $canon, $percent) = ('??', '??');
  128. printf "dubious\n\tTest returned status $estatus (wstat %d, 0x%x)\n",
  129. $wstatus,$wstatus;
  130. print "\t\t(VMS status is $estatus)\n" if $^O eq 'VMS';
  131. if (corestatus($wstatus)) { # until we have a wait module
  132. if ($have_devel_corestack) {
  133. Devel::CoreStack::stack($^X);
  134. } else {
  135. print "\ttest program seems to have generated a core\n";
  136. }
  137. }
  138. $bad++;
  139. if ($max) {
  140. if ($next == $max + 1 and not @failed) {
  141. print "\tafter all the subtests completed successfully\n";
  142. $percent = 0;
  143. $failed = 0; # But we do not set $canon!
  144. } else {
  145. push @failed, $next..$max;
  146. $failed = @failed;
  147. (my $txt, $canon) = canonfailed($max,$skipped,@failed);
  148. $percent = 100*(scalar @failed)/$max;
  149. print "DIED. ",$txt;
  150. }
  151. }
  152. $failedtests{$test} = { canon => $canon, max => $max || '??',
  153. failed => $failed,
  154. name => $test, percent => $percent,
  155. estat => $estatus, wstat => $wstatus,
  156. };
  157. } elsif ($ok == $max && $next == $max+1) {
  158. if ($max and $skipped + $bonus) {
  159. my @msg;
  160. push(@msg, "$skipped/$max subtest".($skipped>1?'s':'')." skipped")
  161. if $skipped;
  162. push(@msg, "$bonus subtest".($bonus>1?'s':'').
  163. " unexpectedly succeeded")
  164. if $bonus;
  165. print "ok, ".join(', ', @msg)."\n";
  166. } elsif ($max) {
  167. print "ok\n";
  168. } else {
  169. print "skipping test on this platform\n";
  170. $tests_skipped++;
  171. }
  172. $good++;
  173. } elsif ($max) {
  174. if ($next <= $max) {
  175. push @failed, $next..$max;
  176. }
  177. if (@failed) {
  178. my ($txt, $canon) = canonfailed($max,$skipped,@failed);
  179. print $txt;
  180. $failedtests{$test} = { canon => $canon, max => $max,
  181. failed => scalar @failed,
  182. name => $test, percent => 100*(scalar @failed)/$max,
  183. estat => '', wstat => '',
  184. };
  185. } else {
  186. print "Don't know which tests failed: got $ok ok, expected $max\n";
  187. $failedtests{$test} = { canon => '??', max => $max,
  188. failed => '??',
  189. name => $test, percent => undef,
  190. estat => '', wstat => '',
  191. };
  192. }
  193. $bad++;
  194. } elsif ($next == 0) {
  195. print "FAILED before any test output arrived\n";
  196. $bad++;
  197. $failedtests{$test} = { canon => '??', max => '??',
  198. failed => '??',
  199. name => $test, percent => undef,
  200. estat => '', wstat => '',
  201. };
  202. }
  203. $subtests_skipped += $skipped;
  204. if (defined $files_in_dir) {
  205. my @new_dir_files = globdir $files_in_dir;
  206. if (@new_dir_files != @dir_files) {
  207. my %f;
  208. @f{@new_dir_files} = (1) x @new_dir_files;
  209. delete @f{@dir_files};
  210. my @f = sort keys %f;
  211. print "LEAKED FILES: @f\n";
  212. @dir_files = @new_dir_files;
  213. }
  214. }
  215. }
  216. my $t_total = timediff(new Benchmark, $t_start);
  217. if ($^O eq 'VMS') {
  218. if (defined $old5lib) {
  219. $ENV{PERL5LIB} = $old5lib;
  220. } else {
  221. delete $ENV{PERL5LIB};
  222. }
  223. }
  224. my $bonusmsg = '';
  225. $bonusmsg = (" ($totbonus subtest".($totbonus>1?'s':'').
  226. " UNEXPECTEDLY SUCCEEDED)")
  227. if $totbonus;
  228. if ($tests_skipped) {
  229. $bonusmsg .= ", $tests_skipped test" . ($tests_skipped != 1 ? 's' : '') .
  230. ' skipped';
  231. }
  232. if ($subtests_skipped) {
  233. $bonusmsg .= ($tests_skipped ? ', plus ' : ', ').
  234. "$subtests_skipped subtest"
  235. . ($subtests_skipped != 1 ? 's' : '') .
  236. " skipped";
  237. }
  238. if ($bad == 0 && $totmax) {
  239. print "All tests successful$bonusmsg.\n";
  240. } elsif ($total==0){
  241. die "FAILED--no tests were run for some reason.\n";
  242. } elsif ($totmax==0) {
  243. my $blurb = $total==1 ? "script" : "scripts";
  244. die "FAILED--$total test $blurb could be run, alas--no output ever seen\n";
  245. } else {
  246. $pct = sprintf("%.2f", $good / $total * 100);
  247. my $subpct = sprintf " %d/%d subtests failed, %.2f%% okay.",
  248. $totmax - $totok, $totmax, 100*$totok/$totmax;
  249. my $script;
  250. for $script (sort keys %failedtests) {
  251. $curtest = $failedtests{$script};
  252. write;
  253. }
  254. if ($bad) {
  255. $bonusmsg =~ s/^,\s*//;
  256. print "$bonusmsg.\n" if $bonusmsg;
  257. die "Failed $bad/$total test scripts, $pct% okay.$subpct\n";
  258. }
  259. }
  260. printf("Files=%d, Tests=%d, %s\n", $files, $totmax, timestr($t_total, 'nop'));
  261. return ($bad == 0 && $totmax) ;
  262. }
  263. my $tried_devel_corestack;
  264. sub corestatus {
  265. my($st) = @_;
  266. my($ret);
  267. eval {require 'wait.ph'};
  268. if ($@) {
  269. SWITCH: {
  270. $ret = ($st & 0200); # Tim says, this is for 90%
  271. }
  272. } else {
  273. $ret = WCOREDUMP($st);
  274. }
  275. eval { require Devel::CoreStack; $have_devel_corestack++ }
  276. unless $tried_devel_corestack++;
  277. $ret;
  278. }
  279. sub canonfailed ($@) {
  280. my($max,$skipped,@failed) = @_;
  281. my %seen;
  282. @failed = sort {$a <=> $b} grep !$seen{$_}++, @failed;
  283. my $failed = @failed;
  284. my @result = ();
  285. my @canon = ();
  286. my $min;
  287. my $last = $min = shift @failed;
  288. my $canon;
  289. if (@failed) {
  290. for (@failed, $failed[-1]) { # don't forget the last one
  291. if ($_ > $last+1 || $_ == $last) {
  292. if ($min == $last) {
  293. push @canon, $last;
  294. } else {
  295. push @canon, "$min-$last";
  296. }
  297. $min = $_;
  298. }
  299. $last = $_;
  300. }
  301. local $" = ", ";
  302. push @result, "FAILED tests @canon\n";
  303. $canon = "@canon";
  304. } else {
  305. push @result, "FAILED test $last\n";
  306. $canon = $last;
  307. }
  308. push @result, "\tFailed $failed/$max tests, ";
  309. push @result, sprintf("%.2f",100*(1-$failed/$max)), "% okay";
  310. my $ender = 's' x ($skipped > 1);
  311. my $good = $max - $failed - $skipped;
  312. my $goodper = sprintf("%.2f",100*($good/$max));
  313. push @result, " (-$skipped skipped test$ender: $good okay, $goodper%)" if $skipped;
  314. push @result, "\n";
  315. my $txt = join "", @result;
  316. ($txt, $canon);
  317. }
  318. 1;
  319. __END__
  320. =head1 NAME
  321. Test::Harness - run perl standard test scripts with statistics
  322. =head1 SYNOPSIS
  323. use Test::Harness;
  324. runtests(@tests);
  325. =head1 DESCRIPTION
  326. (By using the L<Test> module, you can write test scripts without
  327. knowing the exact output this module expects. However, if you need to
  328. know the specifics, read on!)
  329. Perl test scripts print to standard output C<"ok N"> for each single
  330. test, where C<N> is an increasing sequence of integers. The first line
  331. output by a standard test script is C<"1..M"> with C<M> being the
  332. number of tests that should be run within the test
  333. script. Test::Harness::runtests(@tests) runs all the testscripts
  334. named as arguments and checks standard output for the expected
  335. C<"ok N"> strings.
  336. After all tests have been performed, runtests() prints some
  337. performance statistics that are computed by the Benchmark module.
  338. =head2 The test script output
  339. Any output from the testscript to standard error is ignored and
  340. bypassed, thus will be seen by the user. Lines written to standard
  341. output containing C</^(not\s+)?ok\b/> are interpreted as feedback for
  342. runtests(). All other lines are discarded.
  343. It is tolerated if the test numbers after C<ok> are omitted. In this
  344. case Test::Harness maintains temporarily its own counter until the
  345. script supplies test numbers again. So the following test script
  346. print <<END;
  347. 1..6
  348. not ok
  349. ok
  350. not ok
  351. ok
  352. ok
  353. END
  354. will generate
  355. FAILED tests 1, 3, 6
  356. Failed 3/6 tests, 50.00% okay
  357. The global variable $Test::Harness::verbose is exportable and can be
  358. used to let runtests() display the standard output of the script
  359. without altering the behavior otherwise.
  360. The global variable $Test::Harness::switches is exportable and can be
  361. used to set perl command line options used for running the test
  362. script(s). The default value is C<-w>.
  363. If the standard output line contains substring C< # Skip> (with
  364. variations in spacing and case) after C<ok> or C<ok NUMBER>, it is
  365. counted as a skipped test. If the whole testscript succeeds, the
  366. count of skipped tests is included in the generated output.
  367. =head1 EXPORT
  368. C<&runtests> is exported by Test::Harness per default.
  369. =head1 DIAGNOSTICS
  370. =over 4
  371. =item C<All tests successful.\nFiles=%d, Tests=%d, %s>
  372. If all tests are successful some statistics about the performance are
  373. printed.
  374. =item C<FAILED tests %s\n\tFailed %d/%d tests, %.2f%% okay.>
  375. For any single script that has failing subtests statistics like the
  376. above are printed.
  377. =item C<Test returned status %d (wstat %d)>
  378. Scripts that return a non-zero exit status, both C<$? E<gt>E<gt> 8> and C<$?> are
  379. printed in a message similar to the above.
  380. =item C<Failed 1 test, %.2f%% okay. %s>
  381. =item C<Failed %d/%d tests, %.2f%% okay. %s>
  382. If not all tests were successful, the script dies with one of the
  383. above messages.
  384. =back
  385. =head1 ENVIRONMENT
  386. Setting C<HARNESS_IGNORE_EXITCODE> makes harness ignore the exit status
  387. of child processes.
  388. If C<HARNESS_FILELEAK_IN_DIR> is set to the name of a directory, harness
  389. will check after each test whether new files appeared in that directory,
  390. and report them as
  391. LEAKED FILES: scr.tmp 0 my.db
  392. If relative, directory name is with respect to the current directory at
  393. the moment runtests() was called. Putting absolute path into
  394. C<HARNESS_FILELEAK_IN_DIR> may give more predicatable results.
  395. =head1 SEE ALSO
  396. L<Test> for writing test scripts and also L<Benchmark> for the
  397. underlying timing routines.
  398. =head1 AUTHORS
  399. Either Tim Bunce or Andreas Koenig, we don't know. What we know for
  400. sure is, that it was inspired by Larry Wall's TEST script that came
  401. with perl distributions for ages. Numerous anonymous contributors
  402. exist. Current maintainer is Andreas Koenig.
  403. =head1 BUGS
  404. Test::Harness uses $^X to determine the perl binary to run the tests
  405. with. Test scripts running via the shebang (C<#!>) line may not be
  406. portable because $^X is not consistent for shebang scripts across
  407. platforms. This is no problem when Test::Harness is run with an
  408. absolute path to the perl binary or when $^X can be found in the path.
  409. =cut