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 8     8   2355 use v5.14.0;
  8         31  
3 8     8   44 use warnings;
  8         20  
  8         517  
4             our $VERSION = '0.19';
5             $VERSION = eval $VERSION;
6 8     8   48 use base qw( Exporter );
  8         15  
  8         1164  
7             our @EXPORT_OK = qw(
8             process_options
9             );
10 8     8   59 use Carp;
  8         16  
  8         577  
11 8     8   66 use Config;
  8         43  
  8         382  
12 8     8   48 use Cwd;
  8         16  
  8         560  
13 8     8   5475 use Data::Dumper;
  8         58468  
  8         618  
14 8     8   64 use File::Path qw( mkpath );
  8         16  
  8         516  
15 8     8   832 use File::Temp qw( tempdir );
  8         22132  
  8         420  
16 8     8   6422 use Getopt::Long;
  8         89580  
  8         35  
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 9 100   9 0 10730 croak "Must provide even list of key-value pairs to process_options()"
54             unless (@_ % 2 == 0);
55 8         31 my %args = @_;
56 8 100       24 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 7         132 my $found_make = $Config{make};
61 7 100       31 if ($args{verbose}) {
62 1         44 say "Arguments provided to process_options():";
63 1         9 say Dumper \%args;
64 1         131 say "";
65 1         13 say q|For 'make', %Config has: |, $found_make;
66             }
67              
68 7         46 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             );
77              
78 7         11 my %opts;
79             GetOptions(
80             "gitdir=s" => \$opts{gitdir},
81             "target=s@" => \$opts{targets},
82             "last_before=s" => \$opts{last_before},
83             "last-before=s" => \$opts{last_before},
84             "first=s" => \$opts{first},
85             "last=s" => \$opts{last},
86             "configure_command=s" => \$opts{configure_command},
87             "make_command=s" => \$opts{make_command},
88             "test_command=s" => \$opts{test_command},
89             "outputdir=s" => \$opts{outputdir},
90             "short=i" => \$opts{short},
91             "repository=s" => \$opts{repository},
92             "branch=s" => \$opts{branch},
93             "verbose" => \$opts{verbose}, # flag
94 7 50       57 ) or croak("Error in command line arguments\n");
95              
96 7 100       7258 if ($opts{verbose}) {
97 1         44 say "Command-line arguments:";
98 1         5 my %defined_opts;
99 1         6 for my $k (keys %opts) {
100 13 100       31 $defined_opts{$k} = $opts{$k} if defined $opts{$k};
101             }
102 1         7 say Dumper \%defined_opts;
103             }
104              
105             # Final selection of params starts with defaults.
106 7         135 my %params = map { $_ => $defaults{$_} } keys %defaults;
  49         120  
107              
108             # Override with command-line arguments.
109 7         31 for my $o (keys %opts) {
110 91 100       171 if (defined $opts{$o}) {
111 4         9 $params{$o} = $opts{$o};
112             }
113             }
114             # Arguments provided directly to process_options() supersede command-line
115             # arguments. (Mainly used in testing of this module.)
116 7         18 for my $o (keys %args) {
117 22         42 $params{$o} = $args{$o};
118             }
119              
120             # If user has not supplied a value for 'outputdir' by this point, then we
121             # have to use a tempdir.
122              
123 7 50       18 if (! exists $params{outputdir}) {
124 7 50       22 $params{outputdir} = tempdir
125             or croak "Unable to create tempdir";
126             }
127              
128             croak "Must define only one of 'last_before' and 'first'"
129 7 100 100     2685 if (defined $params{last_before} and defined $params{first});
130              
131             croak "Must define one of 'last_before' and 'first'"
132 6 100 66     117 unless (defined $params{last_before} or defined $params{first});
133              
134 5         13 for my $p ( qw|
135             short
136             repository
137             branch
138             configure_command
139             make_command
140             test_command
141             outputdir
142              
143             gitdir
144             last
145             | ) {
146 44 100       355 croak "Undefined parameter: $p" unless defined $params{$p};
147             }
148              
149 3         22 return \%params;
150             }
151              
152             1;
153