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

filter: Statementstmt 0.0% 0 / 294 Branchbran 0.0% 0 / 90 Conditioncond 0.0% 0 / 58 MC/DCmcdc 0.0% 0 / 33 Subroutinesub 0.0% 0 / 36 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 526 CC 46 SCAR 49.3
CC
46
Cov
0%
prompt
462.0
prompt_noecho
110.0
default_for_option
30.0
CRAP 138.9
1 package Test::Smoke::App::ConfigSmoke;
2 use warnings;
3 use strict;
4  
5 our $VERSION = '0.103';
6  
7 use base 'Test::Smoke::App::Base';
8  
9 use Cwd;
10 use File::Basename;
11 use File::Spec;
12 use File::Path;
13 use System::Info;
14 use Test::Smoke::App::Options;
15  
16 use Test::Smoke::App::ConfigSmoke::Files;
17 use Test::Smoke::App::ConfigSmoke::MakeOptions;
18 use Test::Smoke::App::ConfigSmoke::Reporter;
19 use Test::Smoke::App::ConfigSmoke::Scheduler;
20 use Test::Smoke::App::ConfigSmoke::SmokeDB;
21 use Test::Smoke::App::ConfigSmoke::Mail;
22 use Test::Smoke::App::ConfigSmoke::SmokeEnv;
23 use Test::Smoke::App::ConfigSmoke::Smokedir;
24 use Test::Smoke::App::ConfigSmoke::Sync;
25  
26 use Test::Smoke::App::ConfigSmoke::WriteSmokeScript;
27  
28 =head1 NAME
29  
30 Test::Smoke::App::ConfigSmoke - App for configuring L<Test::Smoke>.
31  
32 =head1 DESCRIPTION
33  
34 This app will replace the old C<configsmoke.pl>.
35  
36 =cut
37  
38 use Data::Dumper;
39 local $Data::Dumper::Indent = 1;
40 local $Data::Dumper::Sortkeys = \&_sort_configkeys;
41 local $Data::Dumper::Trailingcomma = 1; # perl-5.24.0 or DD-1.60
42  
43 =head2 Test::Smoke::App::ConfigSmoke->new()
44  
45 Adding attributes: C<usedft>, C<current_values>, C<sysinfo>, C<prefix>, C<configfile>.
46  
47 =cut
48  
49 sub new {
50 my $class = shift;
51 my $self = $class->SUPER::new(@_);
52  
53 $self->{_usedft} = $self->option('des');
54 $self->{_current_values} = { };
55 $self->{_sysinfo} = System::Info->new();
56  
57 my $prefix = $self->option('configfile') // 'smokecurrent';
58 $self->_set_prefix($prefix);
59  
60 # undo the 'v' => 'verbose' mangling
61 $self->from_configfile->{v} = delete($self->from_configfile->{verbose})
62 if exists($self->from_configfile->{verbose});
63  
64 return $self;
65 }
66  
67 sub _set_prefix {
68 my $self = shift;
69 my ($maybe_prefix) = @_;
70  
71 (my $prefix = basename($maybe_prefix)) =~ s{[_.]config$}{};
72  
73 my $configfile = $prefix eq $maybe_prefix
74 ? "${prefix}_config"
75 : $maybe_prefix;
76  
77 $self->configfile_error(undef);
78 $self->{_prefix} = $prefix;
79 $self->{_configfile} = $configfile;
80  
81 # try to get an absolute version of $0
82 $self->{_dollar_0} = Cwd::abs_path($0);
83  
84 return $self;
85 }
86  
87 =head2 run
88  
89 Configure the Test::Smoke suite and write the configfile.
90  
91 =cut
92  
93 sub run {
94 my $self = shift;
95  
96 $self->say_hi();
97  
98 $self->config_smokedir();
99 if ($^O eq 'MSWin32') {
100 require Test::Smoke::App::ConfigSmoke::MSWin32;
101 Test::Smoke::App::ConfigSmoke::MSWin32->import('config_mswin32');
102 $self->config_mswin32();
103 }
104 $self->config_sync();
105 $self->config_make_options();
106 $self->config_smoke_db();
107 $self->config_mail();
108 $self->config_files();
109 $self->config_reporter_options();
110 $self->config_scheduler();
111 $self->config_smoke_env();
112  
113 $self->current_values->{perl_version} = $self->default_for_option(
114 Test::Smoke::App::Options->perl_version
115 ) // 'blead';
116 $self->current_values->{is56x} = 0;
117  
118 $self->write_config();
119  
120 my ($cronbin, $crontime) = (
121 $self->current_values->{cronbin},
122 $self->current_values->{crontime}
123 );
124 $self->write_smoke_script($cronbin, $crontime);
125  
126 $self->say_bye();
127 }
128  
129 =head2 say_hi
130  
131 Show introductory text.
132  
133 =cut
134  
135 sub say_hi {
136 my $self = shift;
137  
138 printf <<"EOHI", __PACKAGE__, $VERSION, $self->prefix;
139  
140  
141 -->%s v%s with prefix '%s'<--
142  
143 Welcome to the Perl core smoke suite.
144  
145 This is a new version of the configure script, SOME THINGS ARE DIFFERENT!
146  
147 You will be asked some questions in order to configure this smoke suite.
148 Please make sure to read the documentation "perldoc configsmoke"
149 in case you do not understand a question.
150  
151 * Values in angled-brackets (<>) are alternatives (none other allowed)
152 * Values in square-brackets ([]) are default values (<Enter> confirms)
153 * Use single space to clear a value
154 * Answer '&-d' to continue with all default answers
155  
156  
157 EOHI
158 }
159  
160 =head2 say_bye
161  
162 Configuration has finshed, show some of the results and say goodbye.
163  
164 =cut
165  
166 sub say_bye {
167 my $self = shift;
168  
169 my $report = join("\n", map { "\t$_" } split(m/\n/, $self->report_build_configs));
170  
171 printf <<"EOBYE", $self->current_values->{cfg}, $report, Cwd::abs_path($self->smoke_script);
172 Finished configuration of Test::Smoke!
173  
174 * Please check '%s' for
175 the build-configurations you want to test:
176 %s
177  
178 * Run your Perl core smoker with:
179 \t%s
180  
181 * Have the appropriate amount of fun!
182  
183 The TestSmokeTeam.
184 EOBYE
185 }
186  
187 =head2 write_config
188  
189 This method writes all the relevant values to the config-file.
190  
191 =cut
192  
193 sub write_config {
194 my $self = shift;
195  
196 # Filter some values we don't want:
197 my @donot_save = qw( cronbin docron add2cron );
198 my %current_config = %{ $self->current_values };
199 delete($current_config{$_}) for @donot_save;
200  
201 # Write the actual config-file
202 local $Data::Dumper::Terse = 1;
203 local $Data::Dumper::Sortkeys = \&_sort_configkeys;
204 (my $current_config = Dumper(\%current_config)) =~ s{\n*$}{};
205 if ( open(my $fh, '>', $self->configfile) ) {
206 print {$fh} "\$conf = $current_config;\n";
207 close($fh);
208 printf " >> Created '%s'\n", $self->configfile;
209 }
210 else {
211 printf "!!!!!\nProblem: Could not create(%s): $!\n!!!!!\n",
212 $self->configfile;
213 print "Please, fix this yourself.\n";
214 print "\$conf = $current_config;\n";
215 }
216 }
217  
218 =head2 handle_option
219  
220 Shows the help-text for an option and handles input for it.
221  
222 =cut
223  
224 sub handle_option {
225 my $self = shift;
226 my $option = shift;
227  
228 my $prompt_type = $option->configtype || 'prompt';
229  
230 printf "** %s - %s\n", $option->name, $option->helptext;
231 my $value = $self->$prompt_type($option);
232 $self->current_values->{$option->name} = $value;
233 }
234  
235 =head2 default_for_option
236  
237 Default values can come from differnt sources:
238  
239 I<app-option>, I<config-file>, I<config-default> or I<option-default>.
240  
241 =cut
242  
243 sub default_for_option {
244 my $self = shift;
245 my $option = shift;
246 my ($debug) = @_;
247  
248 my $caller = (caller 1)[3];
249  
250 if ( exists({$self->options}->{$option->name})) {
251 my $value = {$self->options}->{$option->name};
252 $debug and printf " ^^$caller^^ '%s' from app-options: '$value'\n", $option->name;
253 return $value;
254 }
255 if ( exists($self->from_configfile->{$option->name}) ) {
256 my $value = $self->from_configfile->{$option->name};
257 $debug and printf " ^^$caller^^ '%s' from config-file: '$value'\n", $option->name;
258 return $value;
259 }
260 if ( defined(my $value = $option->configdft->($self)) ) {
261 $debug and printf " ^^$caller^^ '%s' from config-default: '$value'\n", $option->name;
262 return $value;
263 }
264 if ( defined(my $value = $option->default) ) {
265 $debug and printf " ^^$caller^^ '%s' from option-default: '$value'\n", $option->name;
266 return $value;
267 }
268 $debug and printf " ^^$caller^^ '%s' no default found\n", $option->name;
269 return;
270 }
271  
272 =head2 prompt
273  
274 Ask for a text answer.
275  
276 =cut
277  
278 sub prompt {
279 my $self = shift;
280 my $option = shift;
281  
282 my $message = $option->configtext;
283  
284 my $df_val = $self->default_for_option($option);
285  
286 if ($option->configtype eq 'prompt_yn') {
287 $df_val =~ tr{01}{ny};
288 }
289  
290 unless ( defined $message ) {
291 my $retval = defined $df_val ? $df_val : "undef";
292 (caller 1)[3] or print "Got [$retval]\n";
293 return $df_val;
294 }
295  
296 $message =~ s/\s+$//;
297  
298 my %ok_val;
299 my $alt = $option->configalt
300 ? $option->configalt->($self)
301 : [ $option->default ];
302  
303 %ok_val = map { (lc $_ => 1) } @$alt if @$alt;
304  
305 my $default = defined $df_val ? $df_val : 'undef';
306 if ( @$alt && defined $df_val ) {
307 $default = $df_val = $alt->[0] unless exists $ok_val{ lc $df_val };
308 }
309 my $alts = @$alt ? "<" . join( "|", @$alt ) . "> " : "";
310 print "\n$message\n";
311  
312 my( $input, $clear );
313 INPUT: {
314 if ( $self->usedft ) {
315 $input = defined $df_val ? $df_val : " ";
316 } else {
317 print "$alts\[$default] \$ ";
318 chomp( $input = <STDIN> );
319 }
320  
321 if ( $input eq " " ) {
322 $input = "";
323 $clear = 1;
324 } elsif ( $input eq '&-d' ) {
325 $self->usedft(1);
326 print "(OK, We'll run with --des from now on.)\n";
327 redo INPUT;
328 } else {
329 $input =~ s/^\s+//;
330 $input =~ s/\s+$//;
331 $input = $df_val unless length $input;
332 }
333  
334 my $_allow = do {
335 local ($Data::Dumper::Indent, $Data::Dumper::Terse) = (0, 1);
336 defined($option->allow)
337 ? Data::Dumper::Dumper($option->allow)
338 : '*';
339 };
340 printf "Input is not OK (%s)\n", $_allow and redo INPUT
341 if !$option->allowed($input, @_);
342  
343 last INPUT unless %ok_val;
344 printf "Expected one of: '%s'\n", join "', '", @$alt and redo INPUT
345 unless exists $ok_val{ lc $input };
346  
347 }
348  
349 my $retval = length $input ? $input : $clear ? "" : $df_val;
350 print "Got [@{[ defined($retval) ? $retval : 'undef' ]}]\n";
351 return $retval;
352 }
353  
354 =head2 prompt_yn
355  
356 Ask for a Yes/No answer.
357  
358 =cut
359  
360 sub prompt_yn {
361 my $self = shift;
362 my $option = shift;
363  
364 my $default = $self->default_for_option($option);
365 $default =~ tr{01}{ny};
366 $option->configdft( sub { $default } );
367  
368 my $yesno = lc($self->prompt($option, qr{^[ny]$}i)) || 0;
369 ( my $retval = $yesno ) =~ tr/ny/01/;
370 return 0 + $retval;
371 }
372  
373 =head2 prompt_noecho
374  
375 Ask for a password type of string
376  
377 =cut
378  
379 sub prompt_noecho {
380 my $self = shift;
381 my $option = shift;
382  
383 eval "use Term::ReadKey";
384 if ($@) {
385 print "\n\t!!! Please install Term::ReadKey !!!\n";
386 return;
387 }
388  
389 (my $message = $option->configtext || '') =~ s{\s+$}{};
390 print "\n$message\n";
391  
392 my $cur_value = $self->default_for_option($option);
393 my $show_default = defined($cur_value) ? '******' : 'undef';
394 my $new_value;
395 GETPWD: {
396 my $input;
397 if ( $self->usedft ) {
398 $input = defined $cur_value ? $cur_value : " ";
399 } else {
400 print "[$show_default] \$ ";
401 ReadMode('noecho');
402 chomp( $input = ReadLine(0) );
403 ReadMode('restore');
404 }
405  
406 if ($input eq "") {
407 $new_value = $cur_value;
408 }
409 elsif ($input eq " ") {
410 $new_value = '';
411 }
412 elsif ($input eq '&-d') { # WHY ???
413 $self->usedft(1);
414 $new_value = $cur_value;
415 }
416 else {
417 $new_value = $input;
418 }
419  
420 if (! $option->allowed($new_value) ) {
421 my $_allow = do {
422 local ($Data::Dumper::Indent, $Data::Dumper::Terse) = (0, 1);
423 defined($option->allow)
424 ? Data::Dumper::Dumper($option->allow)
425 : '*';
426 };
427 printf "Input is not OK (%s)\n", $_allow;
428 redo GETPWD;
429 }
430 }
431 return $new_value;
432 }
433  
434 =head2 prompt_file
435  
436 Ask for an existing filename.
437  
438 =cut
439  
440 sub prompt_file {
441 my $self = shift;
442 my $option = shift;
443  
444 GETFILE: {
445 my $file = $self->prompt( $option );
446  
447 # thaks to perlfaq5
448 $file =~ s{^ ~ ([^/]*)}
449 {$1 ? ( getpwnam $1 )[7] :
450 ( $ENV{HOME} || $ENV{LOGDIR} ||
451 "$ENV{HOMEDRIVE}$ENV{HOMEPATH}" )}ex;
452 $file = File::Spec->rel2abs( $file ) unless !$file && $option->configfnex;
453  
454 print "'$file' does not exist: $!\n" and redo GETFILE
455 unless -f $file || $option->configfnex;
456  
457 printf "Got [%s]\n", defined $file ? $file : 'undef';
458 return $file;
459 }
460 }
461  
462 =head2 prompt_dir
463  
464 Ask for a directory name.
465  
466 =cut
467  
468 sub prompt_dir {
469 my $self = shift;
470 my $option = shift;
471  
472 my $dft = exists($self->from_configfile->{ $option->name })
473 ? $self->from_configfile->{ $option->name }
474 : "";
475  
476 if ($dft and !File::Spec->file_name_is_absolute($dft)) {
477 $dft = File::Spec->rel2abs( $dft )
478 }
479  
480 GETDIR: {
481 my $dir = $self->prompt($option);
482  
483 if ( $dir eq "" and !@{ $option->configalt } and ! $option->allowed($dir) ) {
484 print "Got []\n";
485 return "";
486 }
487  
488 # thanks to perlfaq5
489 $dir =~ s{^ ~ ([^/]*)}
490 {$1 ? ( getpwnam $1 )[7] :
491 ( $ENV{HOME} || $ENV{LOGDIR} ||
492 "$ENV{HOMEDRIVE}$ENV{HOMEPATH}" )}ex;
493  
494 defined( $dir = _chk_dir( $dir ) ) or redo GETDIR;
495  
496 print "Got [$dir]\n";
497 return $dir;
498 }
499 }
500  
501 sub _chk_dir {
502 my( $dir ) = @_;
503 defined $dir or return;
504 my $cwd = cwd();
505 File::Path::mkpath( $dir, 1, 0755 ) unless -d $dir;
506  
507 if ( ! chdir $dir ) {
508 warn "Cannot chdir($dir): $!\n";
509 $dir = undef;
510 } else {
511 $dir = File::Spec->canonpath( cwd() );
512 }
513 chdir $cwd or die "Cannot chdir($cwd) back: $!";
514  
515 return $dir;
516 }
517  
518 sub _sort_configkeys {
519 my @order = (
520 # Test::Smoke (startup) related
521 qw( cfg v smartsmoke renice killtime umask ),
522  
523 # Perl dist related
524 qw( perl_version is56x ddir ),
525  
526 # Sync related
527 qw( sync_type fsync rsync opts source tar server sdir sfile
528 unzip patchbin cleanup cdir hdir pfile
529 gitbin gitdir gitorigin gitdfbranch gitbare gitbranchfile ),
530  
531 # OS specific make related
532 qw( w32cc w32make w32args ),
533  
534 # Test environment related
535 qw( force_c_locale locale defaultenv perlio_only skip_tests ),
536  
537 # SmokeDB
538 qw( smokedb_url poster send_log send_out ua_timeout curlbin ),
539  
540 # Report related
541 qw( mail mail_type mailbin mailxbin sendmailbin sendemailbin
542 mserver msport msuser mspass from to ccp5p_onfail
543 swcc cc swbcc bcc ),
544  
545 # Archive reports and logfile
546 qw( adir lfile ),
547  
548 # make fine-tuning
549 qw( makeopt testmake harnessonly hasharness3 harness3opts ),
550  
551 # user_notes
552 qw( hostname user_note un_file un_position ),
553  
554 # internal files
555 qw( outfile rptfile jsnfile ),
556  
557 # ENV stuff
558 qw( perl5lib perl5opt delay_report ),
559 );
560  
561 my $i = 0;
562 my %keyorder = map { $_ => $i++ } @order;
563  
564 my @keyord = sort {
565 $a <=> $b
566 } @keyorder{ grep exists $keyorder{ $_}, keys %{ $_[0] } };
567  
568 return [ @order[ @keyord ],
569 sort grep !exists $keyorder{ $_ }, keys %{ $_[0] } ];
570 }
571  
572 1;
573  
574 =head1 COPYRIGHT
575  
576 (c) 2020, All rights reserved.
577  
578 * Abe Timmerman <abeltje@cpan.org>
579  
580 This library is free software; you can redistribute it and/or modify
581 it under the same terms as Perl itself.
582  
583 See:
584  
585 =over 4
586  
587 =item * L<http://www.perl.com/perl/misc/Artistic.html>
588  
589 =item * L<http://www.gnu.org/copyleft/gpl.html>
590  
591 =back
592  
593 This program is distributed in the hope that it will be useful,
594 but WITHOUT ANY WARRANTY; without even the implied warranty of
595 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
596  
597 =cut