blib/lib/Test/Smoke/App/ConfigSmoke/WriteSmokeScript.pm

filter: Statementstmt 58.1% 57 / 98 Branchbran 29.4% 10 / 34 Conditioncond 25.0% 1 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 84.6% 11 / 13 Podpod 100% 4 / 4 totaltotal 53.5% 83 / 155 CC 22 SCAR 33.7
CC
22
Cov
50%
write_as_cmd
90.0
write_as_shell
12.3
schedule_entry
12.0
CRAP 29.1
« blib/lib/Test/Smoke/FTPClient.pm ↑ Summary blib/lib/Test/Smoke/App/RepostFromArchive.pm »
1 package Test::Smoke::App::ConfigSmoke::WriteSmokeScript;
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '0.002';
6  
7 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 our @EXPORT = qw/ write_smoke_script write_as_shell write_as_cmd /;
9  
10 1 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use POSIX qw/ strftime /;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Test::Smoke::App::Options;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Test::Smoke::LogMixin;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Test::Smoke::Util::FindHelpers qw/ whereis /;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 =head1 NAME
18  
19 Test::Smoke::App::ConfigSmoke::WriteSmokeScript - Mixin for L<Test::Smoke::App::ConfigSmoke>
20  
21 =head1 DESCRIPTION
22  
23 These methods will be added to the L<Test::Smoke::App::ConfigSmoke> class.
24  
25 =head2 write_smoke_script
26  
27 General method to write the smoke-script for different "shells".
28  
29 =cut
30  
31 1 ▶sub write_smoke_script {
Subroutine write_smoke_script: called CC=3 SCAR=14.2
Pod documented
32 1 my $self = shift;
33 1 my ($cronbin, $crontime) = @_;
34  
35 1 ▶ if ($^O eq 'MSWin32') {
Branch: 2 branches
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 1 1
elsif ($^O eq 'VMS') { } 1 0 1
36 0 $self->{_smoke_script} = $self->prefix . '.cmd';
37 0 $self->{_smoke_script_abs} = Cwd::abs_path($self->smoke_script);
38 0 $self->write_as_cmd($cronbin, $crontime);
39 }
40 elsif ($^O eq 'VMS') {
41 1 $self->{_smoke_script} = $self->prefix . '.com';
42 1 $self->{_smoke_script_abs} = Cwd::abs_path($self->smoke_script);
43 1 $self->log_warn("%s not (fully) supported yet.", $^O);
44 }
45 else {
46 0 $self->{_smoke_script} = $self->prefix . '.sh';
47 0 $self->{_smoke_script_abs} = Cwd::abs_path($self->smoke_script);
48 0 $self->write_as_shell($cronbin, $crontime);
49 }
50 }
51  
52 =head2 write_as_shell
53  
54 Configure options C<makeopt>, C<testmake>, C<harnessonly>, C<hasharness3> and C<harness3opts>.
55  
56 =cut
57  
58 3 ▶sub write_as_shell {
Subroutine write_as_shell: called CC=10 SCAR=25.1
Pod documented
59 3 my $self = shift;
60 3 my ($cronbin, $crontime) = @_;
61  
62 3 $self->log_info("-- Write shell script --");
63  
64 3 ▶ $crontime ||= '22:25';
Condition50%1/2
$crontime ||= "22:25"
l!l
30
MC/DC0%0/1
$crontime ||= "22:25"
$crontime
Truth table50%1/2
A
A= $crontime
Aresult
11
00
65 3 my $cronline = $self->schedule_entry_crontab($cronbin, $crontime);
66  
67 3 my $p5env = '';
68 3 for my $env (qw/ PERL5LIB PERL5OPT /) {
69 6 ▶ if (my $value = $self->current_values->{lc($env)}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $value = $self->current_values->{lc $env}) 0 6 6
70 0 $p5env = "$env='$value'\nexport $env\n";
71 0 delete($self->current_values->{lc($env)});
72 }
73 }
74  
75 3 my $handle_lock = ' echo "We seem to be running (or remove $LOCKFILE)" >& 2'
76 . "\n exit 200";
77 3 ▶ if ($self->current_values->{killtime}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->current_values->{"killtime"}) 0 3 3
78 0 $handle_lock = " # Not sure about this, so I will keep the old behaviour\n"
79 . " # tssmokeperl.pl will exit(42) on timeout\n"
80 . " # continue='--continue'\n"
81 . $handle_lock;
82 }
83 my @template_vars = (
84 $self->smoke_script, $0, $self->VERSION,
85 strftime("%Y-%m-%dT%H:%M:%S%z", localtime),
86 $self->dollar_0,
87 $cronline,
88 ($self->current_values->{renice} ? "" : "# ") . "renice " . $self->current_values->{renice} . " \$\$",
89 Cwd::abs_path(File::Spec->curdir),
90 $self->configfile,
91 ($self->current_values->{qfile} ? $^X : "# $^X"),
92 File::Spec->catfile($FindBin::Bin, 'tshandlequeue.pl'),
93 $self->prefix. ".lck",
94 $handle_lock,
95 $p5env,
96 $self->current_values->{umask},
97 $FindBin::Bin, $ENV{PATH},
98 $^X, File::Spec->catfile($FindBin::Bin, 'tssmokeperl.pl'), $self->current_values->{lfile},
99 3 ▶ );
Branch: 2 branches
BranchTrueFalseTotal
$self->current_values->{'renice'} ? : 0 3 3
$self->current_values->{'qfile'} ? : 0 3 3
100  
101 3 my $smoke_script = sprintf(<<'EO_SH', @template_vars);
102 #! /bin/sh
103 #
104 # %s: written by %s v%s
105 # on %s
106 # NOTE: Changes made in this file will be *lost*
107 # after rerunning %s
108 #
109 # cron: %s
110 %s
111 cd %s
112 CFGNAME=${CFGNAME:-%s}
113 %s %s - "$CFGNAME"
114 LOCKFILE=${LOCKFILE:-%s}
115 continue=''
116 if test -f "$LOCKFILE" && test -s "$LOCKFILE" ; then
117 %s
118 fi
119 echo "$CFGNAME" > "$LOCKFILE"
120  
121 %s
122 umask %s
123 PATH=%s:%s
124 export PATH
125 %s %s -c "$CFGNAME" $continue $* > "%s" 2>&1
126  
127 rm "$LOCKFILE"
128 EO_SH
129  
130 3 my $jcl = $self->smoke_script;
131 3 ▶ if (open(my $fh, '>', $jcl)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, '>', $jcl) { } 2 1 3
132 2 print {$fh} $smoke_script;
133 2 ▶ close($fh) or do {
Branch: 1 branch
BranchTrueFalseTotal
close $fh or do { $self->log_warn("Problem: cannot close(%s): %s", $jcl, $!); die "Please, fix yourself.\n" } 2 0 2
134 0 $self->log_warn("Problem: cannot close(%s): %s", $jcl, $!);
135 0 die "Please, fix yourself.\n";
136 };
137  
138 2 ▶ chmod(0755, $jcl) or $self->log_warn("Cannot chmod 0755 %s: %s", $jcl, $!);
Branch: 1 branch
BranchTrueFalseTotal
chmod 0755, $jcl or $self->log_warn("Cannot chmod 0755 %s: %s", $jcl, $!) 2 0 2
139 2 $self->log_info(" >> Created '%s'", $jcl);
140 }
141 else {
142 1 $self->log_warn("Problem: cannot create(%s): %s", $jcl, $!);
143 1 die "Please, fix yourself.\n";
144 }
145 }
146  
147 =head2 write_as_cmd
148  
149 Write the smoke-script as a MS-cmd script.
150  
151 =cut
152  
153 0 ▶sub write_as_cmd {
Subroutine write_as_cmd: not called CC=9 SCAR=45.0
Pod documented
154 0 my $self = shift;
155 0 my ($cronbin, $crontime) = @_;
156  
157 0 ▶ $crontime ||= '22:25';
Condition0%0/2
$crontime ||= "22:25"
l!l
00
MC/DC0%0/1
$crontime ||= "22:25"
$crontime
Truth table0%0/2
A
A= $crontime
Aresult
11
00
158 0 ▶ my $cronline = $cronbin =~ m/schtasks/
Branch: 1 branch
BranchTrueFalseTotal
$cronbin =~ /schtasks/ ? : 0 0 0
159 ? $self->query_entry_ms_schtasks($cronbin)
160 : $self->schedule_entry_ms_at($cronbin, $crontime);
161  
162 0 my $p5env = '';
163 0 for my $env (qw/ PERL5LIB PERL5OPT /) {
164 0 ▶ if (my $value = $self->current_values->{lc($env)}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $value = $self->current_values->{lc $env}) 0 0 0
165 0 $p5env = "set $env=$value\n";
166 0 delete($self->current_values->{lc($env)});
167 }
168 }
169  
170 my @template_vars = (
171 $self->smoke_script, $0, $self->VERSION,
172 strftime("%Y-%m-%dT%H:%M:%S%z", localtime),
173 $self->dollar_0,
174 $p5env,
175 $cronline,
176 Cwd::abs_path(File::Spec->curdir),
177 $self->configfile,
178 ($self->current_values->{qfile} ? qq["$^X"] : qq[REM "$^X"]),
179 File::Spec->catfile($FindBin::Bin, 'tshandlequeue.pl'),
180 $self->prefix. ".lck",
181 File::Spec->canonpath($FindBin::Bin), $ENV{PATH},
182 $^X, File::Spec->catfile($FindBin::Bin, 'tssmokeperl.pl'), $self->current_values->{lfile},
183 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$self->current_values->{'qfile'} ? : 0 0 0
184  
185 0 my $smoke_script = sprintf(<<'EO_BAT', @template_vars);
186 @echo off
187 setlocal
188 REM .
189 REM %s: written by %s v%s
190 REM on %s
191 REM NOTE: Changes made in this file will be \*lost\*
192 REM after rerunning %s
193 REM .
194 REM If you find hanging XCOPY during smoking, uncomenting
195 REM the next line might fix it?
196 REM set COPYCMD=/Y %%COPYCMD%%
197 REM .
198 %s
199 REM query scheduler: %s
200  
201 set WD=%s\
202 REM Change drive-Letter, then directory
203 for %%%%L in ( "%%WD%%" ) do %%%%~dL
204 cd "%%WD%%"
205 if "%%CFGNAME%%" == "" set CFGNAME=%s
206 %s %s -c "%CFGNAME%"
207 if "%%LOCKFILE%%" == "" set LOCKFILE=%s
208 if NOT EXIST %%LOCKFILE%% goto START_SMOKE
209 FIND "%%CFGNAME%%" %%LOCKFILE%% > NUL:
210 if ERRORLEVEL 1 goto START_SMOKE
211 echo We seem to be running [or remove %%LOCKFILE%%]>&2
212 goto :EOF
213  
214 :START_SMOKE
215 echo %%CFGNAME%% > %%LOCKFILE%%
216 set OLD_PATH=%%PATH%%
217 set PATH=%s;%s
218 %s %s -c "%%CFGNAME%%" %%* > "%s" 2>&1
219 set PATH=%%OLD_PATH%%
220  
221 del %%LOCKFILE%%
222 EO_BAT
223  
224 0 my $jcl = $self->smoke_script;
225 0 ▶ if (open(my $fh, '>', $jcl)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, '>', $jcl) { } 0 0 0
226 0 print {$fh} $smoke_script;
227 0 ▶ close($fh) or do {
Branch: 1 branch
BranchTrueFalseTotal
close $fh or do { $self->log_warn("Problem: cannot close(%s): %s", $jcl, $!); die "Please, fix yourself.\n" } 0 0 0
228 0 $self->log_warn("Problem: cannot close(%s): %s", $jcl, $!);
229 0 die "Please, fix yourself.\n";
230 };
231  
232 0 ▶ chmod(0755, $jcl) or $self->log_warn("Cannot chmod 0755 %s: %s", $jcl, $!);
Branch: 1 branch
BranchTrueFalseTotal
chmod 0755, $jcl or $self->log_warn("Cannot chmod 0755 %s: %s", $jcl, $!) 0 0 0
233 0 $self->log_info(" >> Created '%s'", $jcl);
234 }
235 else {
236 0 $self->log_warn("Problem: cannot create(%s): %s", $jcl, $!);
237 0 die "Please, fix yourself.\n";
238 }
239 }
240  
241 =head2 schedule_entry
242  
243 Returns a string that can be used to add the entry to the scheduler.
244  
245 =cut
246  
247 0 ▶sub schedule_entry {
Subroutine schedule_entry: not called CC=3 SCAR=24.8
Pod documented
248 0 my( $script, $cron, $crontime ) = @_;
249  
250 0 ▶ return '' unless $crontime;
Branch: 1 branch
BranchTrueFalseTotal
unless $crontime 0 0 0
251 0 my ($hour, $min) = $crontime =~ /(\d+):(\d+)/;
252  
253 0 my $entry;
254 0 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 0 0
255 0 $entry = sprintf(
256 qq[$cron %02d:%02d /EVERY:M,T,W,Th,F,S,Su "%s"],
257 $hour, $min, $script
258 );
259 } else {
260 0 $entry = sprintf(qq[%02d %02d * * * '%s'], $min, $hour, $script);
261 }
262 0 return $entry;
263 }
264  
265 1 1;
266  
267 =head1 COPYRIGHT
268  
269 (c) 2020, All rights reserved.
270  
271 * Abe Timmerman <abeltje@cpan.org>
272  
273 This library is free software; you can redistribute it and/or modify
274 it under the same terms as Perl itself.
275  
276 See:
277  
278 =over 4
279  
280 =item * L<http://www.perl.com/perl/misc/Artistic.html>
281  
282 =item * L<http://www.gnu.org/copyleft/gpl.html>
283  
284 =back
285  
286 This program is distributed in the hope that it will be useful,
287 but WITHOUT ANY WARRANTY; without even the implied warranty of
288 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
289  
290 =cut
291  
« blib/lib/Test/Smoke/FTPClient.pm ↑ Summary blib/lib/Test/Smoke/App/RepostFromArchive.pm »