modules.pm 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. # -*- cperl -*-
  2. use strict;
  3. BEGIN {
  4. unshift @INC, 't';
  5. }
  6. require "test.pl";
  7. use Test::More;
  8. use Config;
  9. use Cwd;
  10. use Exporter;
  11. our @ISA = qw(Exporter);
  12. our @EXPORT = qw(%modules $keep
  13. perlversion
  14. percent log_diag log_pass log_err get_module_list
  15. random_sublist is_subset
  16. );
  17. our (%modules);
  18. our $log = 0;
  19. our $keep = '';
  20. sub perlversion {
  21. my $DEBUGGING = ($Config{ccflags} =~ m/-DDEBUGGING/);
  22. return sprintf("%1.6f%s%s", $],
  23. ($DEBUGGING ? 'd' : ''),
  24. ($Config{useithreads} ? ''
  25. : $Config{usemultiplicity} ? '-m'
  26. : '-nt'));
  27. }
  28. sub percent {
  29. $_[1] ? sprintf("%0.1f%%", $_[0]*100/$_[1]) : '';
  30. }
  31. sub log_diag {
  32. my $message = shift;
  33. chomp $message;
  34. diag( $message );
  35. return unless $log;
  36. foreach ($log, "$log.err") {
  37. open(LOG, ">>", $_);
  38. $message =~ s/\n./\n# /xmsg;
  39. print LOG "# $message\n";
  40. close LOG;
  41. }
  42. }
  43. sub log_pass {
  44. my ($pass_msg, $module, $todo) = @_;
  45. return unless $log;
  46. if ($todo) {
  47. $todo = " #TODO $todo";
  48. } else {
  49. $todo = '';
  50. }
  51. diag( "$pass_msg $module$todo" );
  52. open(LOG, ">>", "$log");
  53. print LOG "$pass_msg $module$todo\n";
  54. close LOG;
  55. }
  56. sub log_err {
  57. my ($module, $out, $err) = @_;
  58. return if(!$log);
  59. # diag prints for TODO to a special todo fh, which does not end at the console
  60. # ignore diag the TODO empty STDERR test for now. we diag the ok test only
  61. # diag( "fail $module $out" );
  62. # Test::More->builder->_print_comment( Test::More->builder->failure_output, "fail $module $out" );
  63. $_ =~ s/\n/\n# /xmsg foreach($out, $err); # Format for comments
  64. open(ERR, ">>", "$log.err");
  65. print ERR "Failed $module\n";
  66. print ERR "# No output\n" if(!$out && !$err);
  67. print ERR "# STDOUT:\n# $out\n" if($out && $out ne 'ok');
  68. print ERR "# STDERR:\n# $err\n" if($err);
  69. close ERR;
  70. }
  71. sub is_subset {
  72. return 0 if grep /^-no-subset$/, @ARGV;
  73. return ! (-d '.svn' or -d'.git') || grep /^-subset$/, @ARGV;
  74. }
  75. sub get_module_list {
  76. # Parse for command line modules and use this if seen.
  77. my @modules = grep {$_ !~ /^-([\w-]+)$/} @ARGV; # ignore options
  78. # -no-subset defaults to all top100
  79. my $module_list = (grep /^-no-subset$/, @ARGV) ? 't/top100' : 't/test10';
  80. if (@modules and -e $modules[0] and ! -x $modules[0]) { # skip an executable compiled module
  81. $module_list = $modules[0];
  82. }
  83. elsif (@modules) {
  84. # cmdline overrides require check and keeps .c
  85. $modules{$_} = 1 for @modules;
  86. $keep = "-S";
  87. return @modules;
  88. }
  89. local $/;
  90. open F, "<", $module_list or die "$module_list not found";
  91. my $s = <F>;
  92. close F;
  93. @modules = grep {s/\s+//g;!/^#/} split /\n/, $s;
  94. diag "scanning installed modules";
  95. for my $m (@modules) {
  96. # redirect stderr
  97. open (SAVEOUT, ">&STDERR");
  98. close STDERR;
  99. open (STDERR, ">", \$modules::saveout);
  100. if (eval "require $m;" or $m eq 'if') {
  101. $modules{$m} = 1;
  102. }
  103. # restore stderr
  104. close STDERR;
  105. open (STDERR, ">&SAVEOUT");
  106. close SAVEOUT;
  107. }
  108. if (&is_subset and @modules > 10) {
  109. log_diag("testing a random subset of the $module_list modules");
  110. @modules = random_sublist(@modules);
  111. }
  112. @modules;
  113. }
  114. sub random_sublist {
  115. my @modules = @_;
  116. my %sublist;
  117. return 1 if scalar(@modules) < 2;
  118. while (keys %sublist < 10) {
  119. my $m = $modules[int(rand(scalar @modules))];
  120. next unless $modules{$m}; # Don't random test uninstalled modules
  121. $sublist{$m} = 1;
  122. }
  123. return keys %sublist;
  124. }
  125. # for t/testm.sh -s
  126. sub skip_modules {
  127. my @modules = get_module_list;
  128. my @skip = ();
  129. for my $m (@modules) {
  130. push @skip, ($m) unless $modules{$m};
  131. }
  132. @skip;
  133. }
  134. # preparing automatic module tests
  135. package CPAN::Shell;
  136. #{ # add testcc to the dispatching methods
  137. # no strict "refs";
  138. # my $command = 'testcc';
  139. # *$command = sub { shift->rematein($command, @_); };
  140. #}
  141. sub testcc { shift->rematein('testcc', @_); }
  142. package CPAN::Module;
  143. sub testcc {
  144. my $self = shift;
  145. my $inst_file = $self->inst_file or return;
  146. # only if its a not-deprecated CPAN module. perl core not
  147. if ($self->can('_in_priv_or_arch')) { # 1.9301 not, 1.94 yes
  148. return if $self->_in_priv_or_arch($inst_file);
  149. }
  150. if ($] >= 5.011){
  151. if ($self->can('deprecated_in_core')) {
  152. return if $self->deprecated_in_core;
  153. } else {
  154. # CPAN-1.9402 has no such method anymore
  155. # trying to support deprecated.pm by Nicholas 2009-02
  156. if (my $distribution = $self->distribution) {
  157. return if $distribution->isa_perl;
  158. }
  159. }
  160. }
  161. $self->rematein('testcc', @_);
  162. }
  163. package CPAN::Distribution;
  164. sub testcc {
  165. my $self = shift;
  166. # $CPAN::DEBUG++;
  167. my $cwd = Cwd::getcwd();
  168. # posix shell only, but we are using a posix shell here. XXX -Wb=-uTest::Builder
  169. my $X = $^X =~ m/\s/ ? qq{"$^X"} : $^X;
  170. $self->prefs->{test}->{commandline} =
  171. "for t in t/*.t; do "
  172. . "echo \"# \$t\"; $X -Iblib/arch -Iblib/lib -I\"$cwd/blib/arch\" -I\"$cwd/blib/lib\" \"$cwd/blib/script/perlcc\" -T -r \$t;"
  173. ."done";
  174. $self->prefs->{test_report} = ''; # XXX ignored!
  175. $self->{make_test} = 'NO'; # override YAML check "Has already been tested successfully"
  176. $self->test(@_);
  177. # done
  178. }
  179. 1;