TestCensus.pm

filter: Statementstmt 100% 18 / 18 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 100% 24 / 24 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Indexer/Worker/JustShowDist.pm ↑ Summary
1 #!/usr/bin/perl
2  
3 package MyCPAN::Indexer::TestCensus;
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use subs qw(get_caller_info);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use vars qw($VERSION $logger);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use parent qw(MyCPAN::Indexer);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 $VERSION = '1.282';
14  
15 =encoding utf8
16  
17 =head1 NAME
18  
19 MyCPAN::Indexer::TestCensus - Count the Test modules used in test suites
20  
21 =head1 SYNOPSIS
22  
23 use MyCPAN::Indexer;
24  
25 =head1 DESCRIPTION
26  
27 This module implements the indexer_class and reporter_class components
28 to allow C<backpan_indexer.pl> to count the test modules used in the
29 indexed distributions.
30  
31 It runs through the indexing and prints a report at the end of the run.
32 You probably
33  
34 =cut
35  
36 use Carp qw(croak);
37 use Cwd qw(cwd);
38  
39 use Log::Log4perl;
40  
41 BEGIN {
42 $logger = Log::Log4perl->get_logger( __PACKAGE__ );
43 }
44  
45 __PACKAGE__->run( @ARGV ) unless caller;
46  
47 =head2 Indexer class
48  
49 =over 4
50  
51 =item examine_dist
52  
53 Given a distribution, unpack it, look at it, and report the findings.
54 It does everything except the looking right now, so it merely croaks.
55 Most of this needs to move out of run and into this method.
56  
57 =cut
58  
59 {
60 my @methods = (
61 # method error message fatal
62 [ 'unpack_dist', "Could not unpack distribtion!", 1 ],
63 [ 'find_dist_dir', "Did not find distro directory!", 1 ],
64 [ 'find_tests', "Could not find tests!", 0 ],
65 );
66  
67 sub examine_dist
68 {
69 # TRACE( sub { get_caller_info } );
70  
71 foreach my $tuple ( @methods )
72 {
73 my( $method, $error, $die_on_error ) = @$tuple;
74 $logger->debug( "examine_dist calling $method" );
75  
76 unless( $_[0]->$method() )
77 {
78 $logger->error( $error );
79 if( $die_on_error ) # only if failure is fatal
80 {
81 ERROR( "Stopping: $error" );
82 $_[0]->set_run_info( 'fatal_error', $error );
83 return;
84 }
85 }
86 }
87  
88 {
89 my @file_info = ();
90 foreach my $file ( @{ $_[0]->dist_info( 'tests' ) } )
91 {
92 $logger->debug( "Processing test $file" );
93 my $hash = $_[0]->get_test_info( $file );
94 push @file_info, $hash;
95 }
96  
97 $_[0]->set_dist_info( 'test_info', [ @file_info ] );
98 }
99  
100 return 1;
101 }
102 }
103  
104 =item setup_run_info
105  
106 Like C<setup_run_info> in C<MyCPAN::Indexer>, but it remembers fewer
107 things. The test census really just cares about statements in the test
108 files, so the details about the run aren't as interesting.
109  
110 =cut
111  
112 sub setup_run_info
113 {
114 # TRACE( sub { get_caller_info } );
115  
116 require Config;
117  
118 my $perl = Probe::Perl->new;
119  
120 $_[0]->set_run_info( 'root_working_dir', cwd() );
121 $_[0]->set_run_info( 'run_start_time', time );
122 $_[0]->set_run_info( 'completed', 0 );
123 $_[0]->set_run_info( 'pid', $$ );
124 $_[0]->set_run_info( 'ppid', $_[0]->getppid );
125  
126 $_[0]->set_run_info( 'indexer', ref $_[0] );
127 $_[0]->set_run_info( 'indexer_versions', $_[0]->VERSION );
128  
129 return 1;
130 }
131  
132  
133 =item setup_dist_info
134  
135 Like C<setup_dist_info> in C<MyCPAN::Indexer>, but it remembers fewer
136 things. The test census really just cares about statements in the test
137 files, so the details about the distribution aren't as interesting.
138  
139 =cut
140  
141 sub setup_dist_info
142 {
143 # TRACE( sub { get_caller_info } );
144  
145 my( $self, $dist ) = @_;
146  
147 $logger->debug( "Setting dist [$dist]\n" );
148 $self->set_dist_info( 'dist_file', $dist );
149  
150 return 1;
151 }
152  
153 =back
154  
155 =head2 Reporter class
156  
157 =over 4
158  
159 =item get_reporter( $Notes )
160  
161 C<get_reporter> sets the C<reporter> key in the C<$Notes> hash reference. The
162 value is a code reference that takes the information collected about a distribution
163 and counts the modules used in the test files.
164  
165 See L<MyCPAN::Indexer::Tutorial> for details about what C<get_reporter> expects
166 and should do.
167  
168 =cut
169  
170  
171 {
172 sub get_reporter
173 {
174 #TRACE( sub { get_caller_info } );
175  
176 my( $self ) = @_;
177  
178 unlink "/Users/brian/Desktop/test_use";
179  
180 my $reporter = sub {
181  
182 my( $info ) = @_;
183  
184 my $test_files = $info->{dist_info}{test_info};
185  
186 dbmopen my %DBM, "/Users/brian/Desktop/test_use", 0755 or die "$!";
187  
188 foreach my $test_file ( @$test_files )
189 {
190 my $uses = $test_file->{uses};
191 $logger->debug( "Found test modules @$uses" );
192  
193 foreach my $used_module ( @$uses )
194 {
195 $DBM{$used_module}++;
196 }
197 }
198  
199 dbmclose %DBM;
200  
201 };
202  
203 $self->set_note( 'reporter', $reporter );
204  
205 1;
206 }
207  
208 }
209  
210 sub final_words
211 {
212 $logger->debug( "Final words from the Reporter" );
213  
214 our %DBM;
215 dbmopen %DBM, "/Users/brian/Desktop/test_use", undef;
216  
217 print "Found modules:\n";
218  
219 foreach my $module (
220 sort { $DBM{$b} <=> $DBM{$a} || $a cmp $b } keys %DBM )
221 {
222 next unless $module =~ m/^Test\b/;
223 printf "%6d %s\n", $DBM{$module}, $module;
224 }
225  
226 dbmclose %DBM;
227 }
228  
229 =pod
230  
231 foreach my $file ( glob "*.yml" )
232 {
233 my $yaml = LoadFile( $file );
234  
235 my $test_files = $yaml->{dist_info}{test_info};
236  
237 foreach my $test_file ( @$test_files )
238 {
239 my $uses = $test_file->{uses};
240  
241 foreach my $used_module ( @$uses )
242 {
243 $Seen{$used_module}++;
244 }
245 }
246 }
247  
248 =cut
249  
250 =back
251  
252 =head1 TO DO
253  
254 =over 4
255  
256 =item Count the lines in the files
257  
258 =item Code stats? Lines of code, lines of pod, lines of comments
259  
260 =back
261  
262 =head1 SOURCE AVAILABILITY
263  
264 This code is in Github:
265  
266 git://github.com/briandfoy/mycpan-indexer.git
267  
268 =head1 AUTHOR
269  
270 brian d foy, C<< <bdfoy@cpan.org> >>
271  
272 =head1 COPYRIGHT AND LICENSE
273  
274 Copyright © 2008-2018, brian d foy <bdfoy@cpan.org>. All rights reserved.
275  
276 You may redistribute this under the terms of the Artistic License 2.0.
277  
278 =cut
279  
280 1;
« Indexer/Worker/JustShowDist.pm ↑ Summary