lib/Smolder/Build.pm

filter: Statementstmt 49.3% 79 / 160 Branchbran 21.7% 10 / 46 Conditioncond 4.5% 1 / 22 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 75.0% 18 / 24 Podpod 0.0% 0 / 9 totaltotal 39.4% 108 / 274 CC 32 SCAR 36.2
CC
32
Cov
39%
ACTION_smoke
132.0
_wrap_test_action
49.0
__ANON__
12.0
CRAP 37.5
« lib/Smolder/DB.pm ↑ Summary blib/lib/Smolder/Control/Projects.pm »
1 package Smolder::Build;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use base 'Module::Build';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Cwd qw(cwd);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Spec::Functions qw(catdir catfile tmpdir curdir rel2abs abs2rel splitdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Find ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Copy qw(copy);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Path qw(mkpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 my $PORT = '112234';
13 1 my $HOSTNAME = '127.0.0.1';
14  
15 =head1 NAME
16  
17 Smolder::Build
18  
19 =head1 DESCRIPTION
20  
21 L<Module::Build> subclass for Smolder specific testing and installation.
22  
23 =head1 OVERRIDDEN ACTIONS
24  
25 =head2 test
26  
27 Make sure that we test against a new, empty SQLite db and that Smolder
28 is up and running before running the test files. Then make sure we shut
29 down Smolder when done.
30  
31 =cut
32  
33 1 ▶sub ACTION_test {
Subroutine ACTION_test: called CC=1 SCAR=0.0
Pod undocumented
34 1 my $self = shift;
35 1 $self->_wrap_test_action('test');
36 }
37  
38 # TODO - handle optional Module::Build::TAPArchive features if we have it installed
39 #sub ACTION_test_archive {
40 # my $self = shift;
41 # $self->_wrap_test_action('test_archive');
42 #}
43  
44 1 ▶sub _wrap_test_action {
Subroutine _wrap_test_action: called CC=9 SCAR=38.9
45 1 my ($self, $action) = @_;
46 1 my $cwd = cwd();
47  
48 # create a temporary database and conf file
49 1 my $tmp_dir = File::Temp->newdir(template => 'smolder-XXXXXX');
50 1 my $share_dir = $self->_share_blib_dir;
51 1 my $tmp_conf = File::Temp->new(template => 'smolder-XXXXXX', suffix => '.conf', dir => tmpdir);
52 1 my $log_dir = rel2abs(catdir(curdir, 'blib', 'logs'));
53 1 ▶ mkpath($log_dir) unless -d $log_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $log_dir 1 0 1
54  
55 1 my $conf =
56 "HostName '$HOSTNAME'\nPort '$PORT'\n"
57 . "TemplateDir '"
58 . catdir($share_dir, 'templates') . "'\n"
59 . "HtdocsDir '"
60 . catdir($share_dir, 'htdocs') . "'\n"
61 . "SQLDir '"
62 . catdir($share_dir, 'sql') . "'\n"
63 . "DataDir '"
64 . $tmp_dir->dirname . "'\n"
65 . "PidFile '"
66 . catfile($tmp_dir->dirname, 'smolder.pid') . "'\n"
67 . "LogFile '"
68 . catdir($log_dir, 'smolder.log') . "'\n";
69 1 print $tmp_conf $conf;
70 1 close $tmp_conf;
71 1 $ENV{SMOLDER_CONF} = $tmp_conf->filename;
72  
73 # make sure we create a DB first. Smolder will do this when it starts,
74 # but we still want to run some tests even if we fail to start smolder
75 1 $self->depends_on('db');
76  
77 # our code needs to know it's running under the test harness
78 0 $ENV{SMOLDER_TEST_HARNESS_ACTIVE} = 1;
79  
80 # start the smolder server
81 0 my ($in, $out, $err);
82 0 $ENV{PERL5LIB} = catdir($cwd, 'blib', 'lib');
83 0 my @cmd = (
84 $^X,
85 "-I$ENV{PERL5LIB}",
86 catfile($cwd, 'blib', 'script', 'smolder'),
87 '--conf' => $tmp_conf->filename,
88 );
89 0 eval { require IPC::Run };
90 0 ▶ die "IPC::Run needed to run Smolder test: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
91 0 warn "Starting Smolder server\n";
92 0 my $subprocess = IPC::Run::harness(\@cmd, \$in, \$out, \$err);
93 0 $subprocess->start();
94  
95 0 my $tries = 0;
96 0 ▶ while (!_is_smolder_running() && $tries < 5 && !$err) {
Branch: 1 branch
BranchTrueFalseTotal
if (not _is_smolder_running() and $tries < 5 and not $err) 0 0 0
Condition0%0/6
not _is_smolder_running() and $tries < 5 and not $err
!ll&&!rl&&r
000
not _is_smolder_running() and $tries < 5
!ll&&!rl&&r
000
MC/DC0%0/3
not _is_smolder_running() and $tries < 5 and not $err
not _is_smolder_running() $tries < 5 not $err
Truth table0%0/4
not(A) and B and not(C)
A= not _is_smolder_running()B= $tries < 5C= not $err
ABCresult
0XX0
10X0
1100
1111
97 0 ▶ $subprocess->pump_nb() if $subprocess->pumpable;
Branch: 1 branch
BranchTrueFalseTotal
if $subprocess->pumpable 0 0 0
98 0 sleep(2);
99 0 $tries++;
100 }
101 0 ▶ if(!_is_smolder_running() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_is_smolder_running()) 0 0 0
102 0 warn "Could not start Smolder server\n";
103 0 ▶ warn "$err\n" if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
104 0 warn "Trying tests anyway.\n";
105 }
106  
107 0 my $method = "SUPER::ACTION_$action";
108 0 eval {
109  
110 # make sure depends_on('code') doesn't get run since we've already taken care of it
111 # with depends_on('db')
112 0 ▶ local *Module::Build::TAPArchive::depends_on = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
113 0 my ($self, @args) = @_;
114 0 ▶ if ($args[0] && $args[0] ne 'code') {
Branch: 1 branch
BranchTrueFalseTotal
if ($args[0] and $args[0] ne "code") 0 0 0
Condition0%0/3
$args[0] and $args[0] ne "code"
!ll&&!rl&&r
000
MC/DC0%0/2
$args[0] and $args[0] ne "code"
$args[0] $args[0] ne "code"
Truth table0%0/3
A and B
A= $args[0]B= $args[0] ne "code"
ABresult
0X0
100
111
115 0 return $self->SUPER::depends_on(@args);
116 }
117 0 };
118 0 $self->$method(@_);
119 };
120  
121 0 $subprocess->kill_kill;
122 }
123  
124 0 ▶sub _is_smolder_running {
Subroutine _is_smolder_running: not called CC=2 SCAR=17.9
125 0 my $url = "http://$HOSTNAME:$PORT/app";
126  
127 # Create a user agent object
128 0 eval { require LWP::UserAgent };
129 0 ▶ die "LWP::UserAgent neede to run Smolder tests: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
130 0 my $ua = LWP::UserAgent->new;
131 0 $ua->timeout(4);
132 0 my $res = $ua->get($url);
133  
134 # Check the outcome of the response
135 0 return $res->is_success;
136 }
137  
138 =head1 EXTRA ACTIONS
139  
140 =head2 smoke
141  
142 Run the smoke tests and submit them to our Smolder server.
143  
144 =cut
145  
146 1 __PACKAGE__->add_property(no_update => 0);
147 1 __PACKAGE__->add_property(tags => '');
148 1 __PACKAGE__->add_property(server => 'http://smolder.plusthree.com');
149 1 __PACKAGE__->add_property(project_id => 2);
150  
151 0 ▶sub ACTION_smoke {
Subroutine ACTION_smoke: not called CC=11 SCAR=48.8
Pod undocumented
152 0 my $self = shift;
153 0 my $p = $self->{properties};
154 0 ▶ if ($p->{no_update} or `svn update` =~ /Updated to/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{'no_update'} or `svn update` =~ /Updated to/i) { } 0 0 0
Condition0%0/3
$p->{"no_update"} or `svn update` =~ /Updated to/i
l!l&&r!l&&!r
000
MC/DC0%0/2
$p->{"no_update"} or `svn update` =~ /Updated to/i
$p->{"no_update"} `svn update` =~ /Updated to/i
Truth table0%0/3
A or B
A= $p->{"no_update"}B= `svn update` =~ /Updated to/i
ABresult
1X1
011
000
155  
156 0 $self->ACTION_test_archive();
157  
158 # now send the results off to smolder
159 0 eval { require WWW::Mechanize };
160 0 ▶ die "WWW::Mechanize neede to run Smolder smoke: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
161 0 my $mech = WWW::Mechanize->new();
162 0 $mech->get("$p->{server}/app");
163 0 ▶ unless ($mech->status eq '200') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mech->status eq 200) 0 0 0
164 0 print "Could not reach $p->{server}/app successfully. Received status "
165 . $mech->status . "\n";
166 0 exit(1);
167 }
168  
169 # now go to the add-smoke-report page for this project
170 0 $mech->get("$p->{server}/app/public_projects/add_report/$p->{project_id}");
171 0 ▶ if ($mech->status ne '200' || $mech->content !~ /New Smoke Report/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mech->status ne 200 or not $mech->content =~ /New Smoke Report/) 0 0 0
Condition0%0/3
$mech->status ne 200 or not $mech->content =~ /New Smoke Report/
l!l&&r!l&&!r
000
MC/DC0%0/2
$mech->status ne 200 or not $mech->content =~ /New Smoke Report/
$mech->status ne 200 not $mech->content =~ /New Smoke Report/
Truth table0%0/3
A or not(B)
A= $mech->status ne 200B= not $mech->content =~ /New Smoke Report/
ABresult
1X1
011
000
172 0 print "Could not reach the Add Smoke Report form in Smolder!\n";
173 0 exit(1);
174 }
175 0 $mech->form_name('add_report');
176 my %fields = (
177 report_file => $p->{archive_file},
178 0 platform => `cat /etc/redhat-release`,
179 architecture => `uname -m`,
180 );
181 0 ▶ $fields{tags} = $p->{tags} if $p->{tags};
Branch: 1 branch
BranchTrueFalseTotal
if $p->{"tags"} 0 0 0
182  
183 # get the comments from svn
184 0 my @lines = `svn info`;
185 0 @lines = grep { $_ =~ /URL|Revision|LastChanged/ } @lines;
186 0 $fields{comments} = join("\n", @lines);
187 0 $mech->set_fields(%fields);
188 0 $mech->submit();
189  
190 0 my $content = $mech->content;
191 0 ▶ if ($mech->status ne '200' || $content !~ /Recent Smoke Reports/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mech->status ne 200 or not $content =~ /Recent Smoke Reports/) 0 0 0
Condition0%0/3
$mech->status ne 200 or not $content =~ /Recent Smoke Reports/
l!l&&r!l&&!r
000
MC/DC0%0/2
$mech->status ne 200 or not $content =~ /Recent Smoke Reports/
$mech->status ne 200 not $content =~ /Recent Smoke Reports/
Truth table0%0/3
A or not(B)
A= $mech->status ne 200B= not $content =~ /Recent Smoke Reports/
ABresult
1X1
011
000
192 0 print "Could not upload smoke report with the given information!\n";
193 0 exit(1);
194 }
195 0 $content =~ /#(\d+) Added/;
196 0 my $report_id = $1;
197  
198 0 print "\nReport successfully uploaded as #$report_id.\n";
199 0 ▶ unlink($p->{archive_file}) if -e $p->{archive_file};
Branch: 1 branch
BranchTrueFalseTotal
if -e $p->{"archive_file"} 0 0 0
200  
201 } else {
202 0 print "No updates to Smolder\n";
203 0 exit(0);
204 }
205 }
206  
207 =head2 db
208  
209 Create a new blank DB in the data/ directory (used for development)
210  
211 =cut
212  
213 1 ▶sub ACTION_db {
Subroutine ACTION_db: called CC=1 SCAR=0.2
Pod undocumented
214 1 my $self = shift;
215 1 $self->depends_on('build');
216 1 require Smolder::DB;
217 0 Smolder::DB->create_database();
218 }
219  
220 =head2 update_smoke_html
221  
222 Update all the HTML for the existing smoke reports. This is useful for development
223 and also upgrading when the report HTML template files have changed
224 and you want that change to propagate.
225  
226 =cut
227  
228 0 ▶sub ACTION_update_smoke_html {
Subroutine ACTION_update_smoke_html: not called CC=1 SCAR=6.9
Pod undocumented
229 0 my $self = shift;
230 0 require Smolder::DB::SmokeReport;
231 0 Smolder::DB::SmokeReport->update_all_report_html();
232 }
233  
234 =head2 tidy
235  
236 Run perltidy over all the Perl files in the codebase.
237  
238 =cut
239  
240 1 my $TIDY_ARGS =
241 "--backup-and-modify-in-place "
242 . "--indent-columns=4 "
243 . "--cuddled-else "
244 . "--maximum-line-length=100 "
245 . "--nooutdent-long-quotes "
246 . "--paren-tightness=2 "
247 . "--brace-tightness=2 "
248 . "--square-bracket-tightness=2 ";
249  
250 0 ▶sub ACTION_tidy {
Subroutine ACTION_tidy: not called CC=1 SCAR=6.9
Pod undocumented
251 0 my $self = shift;
252 0 system(qq(find lib/Smolder/ -name '*.pm' | xargs perltidy $TIDY_ARGS));
253 0 system(qq(find t/ -name '*.t' | xargs perltidy $TIDY_ARGS));
254 0 system(qq(perltidy bin/* $TIDY_ARGS));
255 }
256  
257 =head2 tidy_modified
258  
259 Run perltidy over all the Perl files that have changed and not been committed.
260  
261 =cut
262  
263 0 ▶sub ACTION_tidy_modified {
Subroutine ACTION_tidy_modified: not called CC=1 SCAR=6.9
Pod undocumented
264 0 my $self = shift;
265 0 system(
266 qq{svn -q status | grep '^M.*\.\(pm\|pl\|t\)\$\$' | cut -c 8- | xargs perltidy $TIDY_ARGS});
267 }
268  
269 # handle the extra file types that smolder needs (templates, sql, htdocs, etc)
270 1 ▶sub process_templates_files {
Subroutine process_templates_files: called CC=1 SCAR=0.0
Pod undocumented
271 1 my $self = shift;
272 1 $self->_copy_files('templates');
273 }
274  
275 1 ▶sub process_sql_files {
Subroutine process_sql_files: called CC=1 SCAR=0.0
Pod undocumented
276 1 my $self = shift;
277 1 $self->_copy_files('sql');
278 }
279  
280 1 ▶sub process_htdocs_files {
Subroutine process_htdocs_files: called CC=1 SCAR=0.0
Pod undocumented
281 1 my $self = shift;
282 1 $self->_copy_files('htdocs');
283 }
284  
285 3 ▶sub _copy_files {
Subroutine _copy_files: called CC=3 SCAR=11.5
286 3 my ($self, $type) = @_;
287 3 my $cwd = cwd();
288 3 my $start_dir = rel2abs(catdir(curdir, $type));
289 3 my $start_dir_length = scalar splitdir($start_dir);
290 3 my $dest_dir = catdir($self->_share_blib_dir, $type);
291  
292 3 ▶ mkpath($dest_dir) or die "Could not create directory $dest_dir: $!" unless -d $dest_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dest_dir 0 3 3
Condition25%1/4
-d $dest_dir || (mkpath($dest_dir) || die("Could not create directory ${dest_dir}: $!"))
l!l
30
mkpath($dest_dir) or die "Could not create directory ${dest_dir}: $!"
l!l
00
MC/DC0%0/2
-d $dest_dir || (mkpath($dest_dir) || die("Could not create directory ${dest_dir}: $!"))
-d $dest_dir mkpath($dest_dir)
Truth table33%1/3
A || B
A= -d $dest_dirB= mkpath($dest_dir)
ABresult
1X1
011
000
293  
294 File::Find::find(
295 124 ▶ sub {
Subroutine __ANON__: called CC=9 SCAR=23.3
296 124 ▶ return if /^\./; # skip special files
Branch: 1 branch
BranchTrueFalseTotal
if /^\./ 3 121 124
297 121 ▶ return if $File::Find::dir =~ /\.svn/; # skip svn droppings
Branch: 1 branch
BranchTrueFalseTotal
if $File::Find::dir =~ /\.svn/ 0 121 121
298 121 ▶ return if -d;
Branch: 1 branch
BranchTrueFalseTotal
if -d $_ 17 104 121
299 104 my $name = $_;
300 104 my @new_dirs = splitdir($File::Find::dir);
301 104 @new_dirs = @new_dirs[$start_dir_length .. $#new_dirs];
302 104 my $full_path = $dest_dir;
303 104 foreach my $new_dir (@new_dirs) {
304 116 $full_path = catdir($full_path, $new_dir);
305 116 ▶ unless (-d $full_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $full_path) 0 116 116
306 0 ▶ mkdir($full_path) or die "Could not create directory $full_path: $!";
Branch: 1 branch
BranchTrueFalseTotal
mkdir $full_path or die "Could not create directory ${full_path}: $!" 0 0 0
307 }
308 }
309 104 ▶ $full_path = $full_path ? catfile($full_path, $name) : catfile($dest_dir, $name);
Branch: 1 branch
BranchTrueFalseTotal
$full_path ? : 104 0 104
310 104 warn "Copying "
311 . abs2rel($File::Find::name, $cwd) . " -> "
312 . abs2rel($full_path, $cwd) . "\n";
313 104 ▶ copy($File::Find::name, $full_path)
Branch: 1 branch
BranchTrueFalseTotal
copy($File::Find::name, $full_path) or die "Could not copy file $File::Find::name to ${full_path}: $!" 104 0 104
314 or die "Could not copy file $File::Find::name to $full_path: $!";
315 },
316 3 $start_dir
317 );
318 }
319  
320 4 ▶sub _share_blib_dir {
Subroutine _share_blib_dir: called CC=1 SCAR=0.0
321 4 return rel2abs(catdir(curdir, 'blib', 'lib', 'auto', 'share', 'dist', 'Smolder'));
322 }
323  
324 1 1;
« lib/Smolder/DB.pm ↑ Summary blib/lib/Smolder/Control/Projects.pm »