blib/lib/App/GitHooks/Plugin/PerlCompile.pm

filter: Statementstmt 97.5% 40 / 41 Branchbran 75.0% 3 / 4 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 9 / 9 Podpod 100% 3 / 3 totaltotal 93.3% 56 / 60 CC 4 SCAR 7.0
CC
4
Cov
94%
run_pre_commit_file
4.0
BEGIN
1.0
BEGIN
1.0
CRAP 2.0
↑ Summary
1 package App::GitHooks::Plugin::PerlCompile;
2  
3 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 17 ▶use base 'App::GitHooks::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # External dependencies.
9 17 ▶use File::Spec qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 17 ▶use System::Command;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # Internal dependencies.
13 17 ▶use App::GitHooks::Constants qw( :PLUGIN_RETURN_CODES );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 =head1 NAME
17  
18 App::GitHooks::Plugin::PerlCompile - Verify that Perl files compile without errors.
19  
20  
21 =head1 DESCRIPTION
22  
23 This plugin verifies that staged Perl files compile without errors before
24 allowing the commit to be completed.
25  
26  
27 =head1 VERSION
28  
29 Version 1.1.1
30  
31 =cut
32  
33 17 our $VERSION = '1.1.1';
34  
35  
36 =head1 CONFIGURATION OPTIONS
37  
38 This plugin supports the following options in the C<[PerlCompile]> section of
39 your C<.githooksrc> file.
40  
41 [PerlCompile]
42 lib_paths = ./lib, ./t/lib
43  
44  
45 =head2 lib_paths
46  
47 This option gives an opportunity to include other paths to Perl libraries, and
48 in particular paths that are local to the current repository. It allows testing
49 that the Perl files compile without having to amend PERL5LIB to include the
50 repository-specific libraries.
51  
52 lib_paths = ./lib, ./t/lib
53  
54  
55 =head1 METHODS
56  
57 =head2 get_file_pattern()
58  
59 Return a pattern to filter the files this plugin should analyze.
60  
61 my $file_pattern = App::GitHooks::Plugin::PerlCompile->get_file_pattern(
62 app => $app,
63 );
64  
65 =cut
66  
67 sub get_file_pattern
68 15 ▶{
Subroutine get_file_pattern: called CC=1 SCAR=0.0
Pod documented
69 15 return qr/\.(?:pl|pm|t|cgi)$/x;
70 }
71  
72  
73 =head2 get_file_check_description()
74  
75 Return a description of the check performed on files by the plugin and that
76 will be displayed to the user, if applicable, along with an indication of the
77 success or failure of the plugin.
78  
79 my $description = App::GitHooks::Plugin::PerlCompile->get_file_check_description();
80  
81 =cut
82  
83 sub get_file_check_description
84 13 ▶{
Subroutine get_file_check_description: called CC=1 SCAR=0.0
Pod documented
85 13 return 'The file passes perl -c';
86 }
87  
88  
89 =head2 run_pre_commit_file()
90  
91 Code to execute for each file as part of the pre-commit hook.
92  
93 my $success = App::GitHooks::Plugin::PerlCompile->run_pre_commit_file();
94  
95 =cut
96  
97 sub run_pre_commit_file
98 6 ▶{
Subroutine run_pre_commit_file: called CC=4 SCAR=13.9
Pod documented
99 6 my ( $class, %args ) = @_;
100 6 my $file = delete( $args{'file'} );
101 6 my $git_action = delete( $args{'git_action'} );
102 6 my $app = delete( $args{'app'} );
103 6 my $repository = $app->get_repository();
104 6 my $config = $app->get_config();
105  
106 # Ignore deleted files.
107 6 ▶ return $PLUGIN_RETURN_SKIPPED
Branch: 1 branch
BranchTrueFalseTotal
if $git_action eq "D" 0 6 6
108 if $git_action eq 'D';
109  
110 # Prepare extra libs specified in .githooksrc.
111 6 my $lib_paths = $config->get( 'PerlCompile', 'lib_paths' );
112 0 ▶ my @lib = map { ( '-I', $_ ) } split( /\s*,\s*/, $lib_paths // '' );
Condition50%1/2
$lib_paths // ""
l!l
06
MC/DC0%0/1
$lib_paths // ""
$lib_paths
Truth table50%1/2
A
A= $lib_paths
Aresult
11
00
113  
114 # Execute perl -cw.
115 6 my $path = File::Spec->catfile( $repository->work_tree(), $file );
116 6 my ( $pid, $stdin, $stdout, $stderr ) = System::Command->spawn( $^X, '-cw', @lib, $path );
117  
118 # Retrieve the output.
119 6 my $output;
120 {
121 6 local $/ = undef;
122 6 $output = <$stderr>;
123 6 chomp( $output );
124 }
125  
126 # Raise an exception if we didn't get "syntax OK".
127 6 ▶ die "$output\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $output =~ /\Q$file syntax OK\E$/x 5 1 6
128 if $output !~ /\Q$file syntax OK\E$/x;
129  
130 1 return $PLUGIN_RETURN_PASSED;
131 }
132  
133  
134 =head1 BUGS
135  
136 Please report any bugs or feature requests through the web interface at
137 L<https://github.com/guillaumeaubert/App-GitHooks-Plugin-PerlCompile/issues/new>.
138 I will be notified, and then you'll automatically be notified of progress on
139 your bug as I make changes.
140  
141  
142 =head1 SUPPORT
143  
144 You can find documentation for this module with the perldoc command.
145  
146 perldoc App::GitHooks::Plugin::PerlCompile
147  
148  
149 You can also look for information at:
150  
151 =over
152  
153 =item * GitHub's request tracker
154  
155 L<https://github.com/guillaumeaubert/App-GitHooks-Plugin-PerlCompile/issues>
156  
157 =item * AnnoCPAN: Annotated CPAN documentation
158  
159 L<http://annocpan.org/dist/app-githooks-plugin-perlcompile>
160  
161 =item * CPAN Ratings
162  
163 L<http://cpanratings.perl.org/d/app-githooks-plugin-perlcompile>
164  
165 =item * MetaCPAN
166  
167 L<https://metacpan.org/release/App-GitHooks-Plugin-PerlCompile>
168  
169 =back
170  
171  
172 =head1 AUTHOR
173  
174 L<Guillaume Aubert|https://metacpan.org/author/AUBERTG>,
175 C<< <aubertg at cpan.org> >>.
176  
177  
178 =head1 COPYRIGHT & LICENSE
179  
180 Copyright 2013-2016 Guillaume Aubert.
181  
182 This code is free software; you can redistribute it and/or modify it under the
183 same terms as Perl 5 itself.
184  
185 This program is distributed in the hope that it will be useful, but WITHOUT ANY
186 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
187 PARTICULAR PURPOSE. See the LICENSE file for more details.
188  
189 =cut
190  
191 17 1;
↑ Summary