File Coverage

blib/lib/Devel/Git/MultiBisect/Opts.pm
Criterion Covered Total %
statement 65 65 100.0
branch 23 26 88.4
condition 5 6 83.3
subroutine 12 12 100.0
pod 0 1 0.0
total 105 110 95.4


line stmt bran cond sub pod time code
1             package Devel::Git::MultiBisect::Opts;
2 5     5   585 use strict;
  5         12  
  5         175  
3 5     5   27 use warnings;
  5         11  
  5         158  
4 5     5   78 use v5.14.0;
  5         18  
5             our $VERSION = '0.16';
6 5     5   49 use base qw( Exporter );
  5         45  
  5         837  
7             our @EXPORT_OK = qw(
8             process_options
9             );
10 5     5   38 use Carp;
  5         10  
  5         400  
11 5     5   32 use Config;
  5         10  
  5         276  
12 5     5   33 use Cwd;
  5         10  
  5         324  
13 5     5   3621 use Data::Dumper;
  5         37478  
  5         512  
14 5     5   48 use File::Path qw( mkpath );
  5         14  
  5         373  
15 5     5   882 use File::Temp qw( tempdir );
  5         22312  
  5         349  
16 5     5   4000 use Getopt::Long;
  5         57353  
  5         28  
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. C is a wrapper around
36             Getopt::Long::GetOptions(), so is devoted to processing command-line arguments
37             provided, for example, to the command-line utility F (not yet
38             created, but to be included in a future version of this CPAN distribution).
39              
40             The subroutine returns a reference to a hash populated with values in the
41             following order:
42              
43             =over 4
44              
45             =item 1 Default values hard-coded within the subroutine.
46              
47             =item 2 Command-line options.
48              
49             =item 3 Key-value pairs provided as arguments to the function.
50              
51             =back
52              
53             =cut
54              
55             sub process_options {
56 9 100   9 0 15983 croak "Must provide even list of key-value pairs to process_options()"
57             unless (@_ % 2 == 0);
58 8         49 my %args = @_;
59 8 100       27 if (defined $args{targets}) {
60             croak "Value of 'targets' must be an array reference"
61 6 100       136 unless ref($args{targets}) eq 'ARRAY';
62             }
63 7         261 my $found_make = $Config{make};
64 7 100       40 if ($args{verbose}) {
65 1         51 print "Arguments provided to process_options():\n";
66 1         30 print Dumper \%args;
67 1         301 print "\n";
68 1         13 print q|For 'make', %Config has: |, $found_make, "\n";
69             }
70              
71 7         17170 my %defaults = (
72             'workdir' => cwd(),
73             'short' => 7,
74             'repository' => 'origin',
75             'branch' => 'master',
76             'verbose' => 0,
77             'configure_command' => 'perl Makefile.PL 1>/dev/null',
78             'make_command' => "$found_make 1>/dev/null",
79             'test_command' => 'prove -vb',
80             );
81              
82 7         97 my %opts;
83             GetOptions(
84             "gitdir=s" => \$opts{gitdir},
85             "target=s@" => \$opts{targets},
86             "last_before=s" => \$opts{last_before},
87             "last-before=s" => \$opts{last_before},
88             "first=s" => \$opts{first},
89             "last=s" => \$opts{last},
90             "configure_command=s" => \$opts{configure_command},
91             "make_command=s" => \$opts{make_command},
92             "test_command=s" => \$opts{test_command},
93             "workdir=s" => \$opts{workdir},
94             "outputdir=s" => \$opts{outputdir},
95             "short=i" => \$opts{short},
96             "repository=s" => \$opts{repository},
97             "branch=s" => \$opts{branch},
98             "verbose" => \$opts{verbose}, # flag
99 7 50       562 ) or croak("Error in command line arguments\n");
100              
101 7 100       9750 if ($opts{verbose}) {
102 1         55 print "Command-line arguments:\n";
103 1         4 my %defined_opts;
104 1         14 for my $k (keys %opts) {
105 14 100       43 $defined_opts{$k} = $opts{$k} if defined $opts{$k};
106             }
107 1         28 print Dumper \%defined_opts;
108             }
109              
110             # Final selection of params starts with defaults.
111 7         286 my %params = map { $_ => $defaults{$_} } keys %defaults;
  56         132  
112              
113             # Override with command-line arguments.
114 7         39 for my $o (keys %opts) {
115 98 100       186 if (defined $opts{$o}) {
116 4         14 $params{$o} = $opts{$o};
117             }
118             }
119             # Arguments provided directly to process_options() supersede command-line
120             # arguments. (Mainly used in testing of this module.)
121 7         53 for my $o (keys %args) {
122 22         93 $params{$o} = $args{$o};
123             }
124              
125             # If user has not supplied a value for 'outputdir' by this point, then we
126             # have to use a tempdir.
127              
128 7 50       30 if (! exists $params{outputdir}) {
129 7 50       118 $params{outputdir} = tempdir
130             or croak "Unable to create tempdir";
131             }
132              
133             croak "Must define only one of 'last_before' and 'first'"
134 7 100 100     4554 if (defined $params{last_before} and defined $params{first});
135              
136             croak "Must define one of 'last_before' and 'first'"
137 6 100 66     314 unless (defined $params{last_before} or defined $params{first});
138              
139 5         21 for my $p ( qw|
140             workdir
141             short
142             repository
143             branch
144             configure_command
145             make_command
146             test_command
147             outputdir
148              
149             gitdir
150             last
151             | ) {
152 49 100       1004 croak "Undefined parameter: $p" unless defined $params{$p};
153             }
154              
155 3         92 return \%params;
156             }
157              
158             1;
159