File Coverage

blib/lib/Devel/Git/MultiBisect/Opts.pm
Criterion Covered Total %
statement 62 62 100.0
branch 23 26 88.4
condition 5 6 83.3
subroutine 11 11 100.0
pod 0 1 0.0
total 101 106 95.2


line stmt bran cond sub pod time code
1             package Devel::Git::MultiBisect::Opts;
2 9     9   8047 use v5.14.0;
  9         29  
3 9     9   67 use warnings;
  9         15  
  9         750  
4             our $VERSION = '0.21';
5             $VERSION = eval $VERSION;
6 9     9   48 use base qw( Exporter );
  9         12  
  9         3967  
7             our @EXPORT_OK = qw(
8             process_options
9             );
10 9     9   57 use Carp;
  9         18  
  9         631  
11 9     9   49 use Config;
  9         15  
  9         368  
12 9     9   49 use Cwd;
  9         19  
  9         553  
13 9     9   754 use Data::Dumper;
  9         11984  
  9         505  
14 9     9   49 use File::Path qw( mkpath );
  9         19  
  9         507  
15 9     9   1142 use File::Temp qw( tempdir );
  9         32641  
  9         495  
16 9     9   6685 use Getopt::Long;
  9         127907  
  9         47  
17              
18             =head1 NAME
19              
20             Devel::Git::MultiBisect::Opts - Prepare parameters for Devel::Git::MultiBisect
21              
22             =head1 SYNOPSIS
23              
24             use Devel::Git::MultiBisect::Opts qw( process_options );
25              
26             my $params = process_options();
27              
28             =head1 DESCRIPTION
29              
30             This package exports on demand only one subroutine, C, used
31             to prepare parameters for Devel::Git::MultiBisect.
32              
33             C takes as arguments an optional list of key-value pairs.
34             This approach is useful in testing the subroutine but is not expected to be
35             used otherwise.
36              
37             C is a wrapper around C. It
38             returns a reference to a hash populated with values in the following order:
39              
40             =over 4
41              
42             =item 1 Default values hard-coded within the subroutine.
43              
44             =item 2 Command-line options.
45              
46             =item 3 Key-value pairs provided as arguments to the function.
47              
48             =back
49              
50             =cut
51              
52             sub process_options {
53 10 100   10 0 371048 croak "Must provide even list of key-value pairs to process_options()"
54             unless (@_ % 2 == 0);
55 9         38 my %args = @_;
56 9 100       34 if (defined $args{targets}) {
57             croak "Value of 'targets' must be an array reference"
58 6 100       125 unless ref($args{targets}) eq 'ARRAY';
59             }
60 8         219 my $found_make = $Config{make};
61 8 100       39 if ($args{verbose}) {
62 1         53 say "Arguments provided to process_options():";
63 1         10 say Dumper \%args;
64 1         141 say "";
65 1         9 say q|For 'make', %Config has: |, $found_make;
66             }
67              
68 8         72 my %defaults = (
69             'short' => 7,
70             'repository' => 'origin',
71             'branch' => 'master',
72             'verbose' => 0,
73             'configure_command' => 'perl Makefile.PL 1>/dev/null',
74             'make_command' => "$found_make 1>/dev/null",
75             'test_command' => 'prove -vb',
76             'probe' => 'error',
77             );
78              
79 8         24 my %opts;
80             GetOptions(
81             "gitdir=s" => \$opts{gitdir},
82             "target=s@" => \$opts{targets},
83             "last_before=s" => \$opts{last_before},
84             "last-before=s" => \$opts{last_before},
85             "first=s" => \$opts{first},
86             "last=s" => \$opts{last},
87             "compiler=s" => \$opts{compiler},
88             "configure_command=s" => \$opts{configure_command},
89             "make_command=s" => \$opts{make_command},
90             "test_command=s" => \$opts{test_command},
91             "outputdir=s" => \$opts{outputdir},
92             "short=i" => \$opts{short},
93             "repository=s" => \$opts{repository},
94             "branch=s" => \$opts{branch},
95             "probe=s" => \$opts{probe},
96             "verbose" => \$opts{verbose}, # flag
97 8 50       133 ) or croak("Error in command line arguments\n");
98              
99 8 100       8953 if ($opts{verbose}) {
100 1         59 say "Command-line arguments:";
101 1         4 my %defined_opts;
102 1         5 for my $k (keys %opts) {
103 15 100       25 $defined_opts{$k} = $opts{$k} if defined $opts{$k};
104             }
105 1         7 say Dumper \%defined_opts;
106             }
107              
108             # Final selection of params starts with defaults.
109 8         170 my %params = map { $_ => $defaults{$_} } keys %defaults;
  64         134  
110              
111             # Override with command-line arguments.
112 8         38 for my $o (keys %opts) {
113 120 100       187 if (defined $opts{$o}) {
114 4         8 $params{$o} = $opts{$o};
115             }
116             }
117             # Arguments provided directly to process_options() supersede command-line
118             # arguments. (Mainly used in testing of this module.)
119 8         24 for my $o (keys %args) {
120 25         45 $params{$o} = $args{$o};
121             }
122              
123             # If user has not supplied a value for 'outputdir' by this point, then we
124             # have to use a tempdir.
125              
126 8 50       25 if (! exists $params{outputdir}) {
127 8 50       40 $params{outputdir} = tempdir
128             or croak "Unable to create tempdir";
129             }
130              
131             croak "Must define only one of 'last_before' and 'first'"
132 8 100 100     4516 if (defined $params{last_before} and defined $params{first});
133              
134             croak "Must define one of 'last_before' and 'first'"
135 7 100 66     174 unless (defined $params{last_before} or defined $params{first});
136              
137 6         19 for my $p ( qw|
138             short
139             repository
140             branch
141             configure_command
142             make_command
143             test_command
144             outputdir
145              
146             gitdir
147             last
148             | ) {
149 53 100       530 croak "Undefined parameter: $p" unless defined $params{$p};
150             }
151              
152 4         31 return \%params;
153             }
154              
155             1;
156