modules.pm 5.1 KB

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