lib/Git/Hooks/Test.pm

filter: Statementstmt 78.2% 133 / 170 Branchbran 48.3% 29 / 60 Conditioncond 46.6% 7 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 94.7% 18 / 19 Podpod 0.0% 0 / 9 totaltotal 66.3% 187 / 282 CC 34 SCAR 25.0
CC
34
Cov
69%
install_hooks
27.5
new_repos
12.8
test_nok_match
6.7
CRAP 12.2
« blib/lib/Git/Hooks/CheckRewrite.pm ↑ Summary blib/lib/Git/Hooks/GerritChangeId.pm »
1 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Git::Hooks::Test;
4 # ABSTRACT: Git::Hooks testing utilities
5 15 $Git::Hooks::Test::VERSION = '3.3.1';
6 ## no critic (RequireExplicitPackage)
7 ## no critic (ErrorHandling::RequireCarping)
8 15 ▶use v5.16.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use Exporter qw/import/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 15 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 15 ▶use Git::Repository 'GitHooks';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 15 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 15 our @EXPORT_OK = qw/
18 install_hooks
19 new_commit
20 newdir
21 new_repos
22 test_command
23 test_nok
24 test_nok_match
25 test_ok
26 test_ok_match
27 /;
28  
29 15 our %EXPORT_TAGS = (
30 all => \@EXPORT_OK
31 );
32  
33 # Make sure the git messages come in English. (LC_ALL)
34 # Eliminate the effects of system wide (GIT_CONFIG_NOSYSTEM)
35 # and global configuration (XDG_CONFIG_HOME and HOME).
36 # https://metacpan.org/dist/Git-Repository/view/lib/Git/Repository/Tutorial.pod#Ignore-the-system-and-global-configuration-files
37 15 my %git_test_env = (
38 LC_ALL => 'C',
39 GIT_CONFIG_NOSYSTEM => 1,
40 XDG_CONFIG_HOME => undef,
41 HOME => undef,
42 );
43  
44 15 my $cwd = Path::Tiny->cwd;
45  
46 # It's better to perform all tests in a temporary directory because
47 # otherwise the author runs the risk of messing with its local
48 # Git::Hooks git repository.
49  
50 my $T = Path::Tiny->tempdir(
51 TEMPLATE => 'githooks.XXXXX',
52 TMPDIR => 1,
53 15 ▶ CLEANUP => exists $ENV{REPO_CLEANUP} ? $ENV{REPO_CLEANUP} : 1,
Branch: 1 branch
BranchTrueFalseTotal
exists $ENV{'REPO_CLEANUP'} ? : 0 15 15
54 );
55  
56 15 ▶chdir $T or croak "Can't chdir $T: $!";
Branch: 1 branch
BranchTrueFalseTotal
chdir $T or croak("Can't chdir ${T}: $!") 0 15 15
57 14 ▶END { chdir '/' }
Subroutine END: called CC=1 SCAR=0.0
58  
59 15 my $tmpldir = $T->child('templates');
60 15 ▶mkdir $tmpldir, 0777 or BAIL_OUT("can't mkdir $tmpldir: $!");
Branch: 1 branch
BranchTrueFalseTotal
mkdir $tmpldir, 511 or BAIL_OUT("can't mkdir ${tmpldir}: $!") 0 15 15
61 {
62 15 my $hooksdir = $tmpldir->child('hooks');
63 15 ▶ mkdir $hooksdir, 0777 or BAIL_OUT("can't mkdir $hooksdir: $!");
Branch: 1 branch
BranchTrueFalseTotal
mkdir $hooksdir, 511 or BAIL_OUT("can't mkdir ${hooksdir}: $!") 0 15 15
64 }
65  
66 15 ▶my $git_version = eval { Git::Repository->version } || 'unknown';
Condition50%1/2
eval { do { 'Git::Repository'->version } } || 'unknown'
l!l
150
MC/DC0%0/1
eval { do { 'Git::Repository'->version } } || 'unknown'
eval { do { 'Git::Repository'->version } }
Truth table50%1/2
A
A= eval { do { 'Git::Repository'->version } }
Aresult
11
00
67  
68 sub newdir {
69 0 ▶ my $num = 1 + Test::Builder->new()->current_test();
Subroutine newdir: not called CC=1 SCAR=6.9
Pod undocumented
70 0 my $dir = $T->child($num);
71 0 mkdir $dir;
72 0 return $dir;
73 }
74  
75 sub install_hooks {
76 32 ▶ my ($git, $extra_perl, @hooks) = @_;
Subroutine install_hooks: called CC=16 SCAR=33.2
Pod undocumented
77 32 my $hooks_dir = path($git->git_dir())->child('hooks');
78 32 my $hook_pl = $hooks_dir->child('hook.pl');
79 {
80 ## no critic (RequireBriefOpen)
81 32 ▶ open my $fh, '>', $hook_pl or BAIL_OUT("Can't create $hook_pl: $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $hook_pl or BAIL_OUT("Can't create ${hook_pl}: $!") 0 32 32
82 32 ▶ state $debug = $ENV{DBG} ? '-d' : '';
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'DBG'} ? : 0 13 13
83 32 state $bliblib = $cwd->child('blib', 'lib');
84 32 print $fh <<"EOS";
85 #!$Config{perlpath} $debug
86 use strict;
87 use warnings;
88 use lib '$bliblib';
89 EOS
90  
91 32 ▶ state $pathsep = $^O eq 'MSWin32' ? ';' : ':';
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 13 13
92 32 ▶ if (defined $ENV{PERL5LIB} and length $ENV{PERL5LIB}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{"PERL5LIB"} and length $ENV{"PERL5LIB"}) 32 0 32
Condition33%1/3
defined $ENV{"PERL5LIB"} and length $ENV{"PERL5LIB"}
!ll&&!rl&&r
0032
MC/DC0%0/2
defined $ENV{"PERL5LIB"} and length $ENV{"PERL5LIB"}
defined $ENV{"PERL5LIB"} length $ENV{"PERL5LIB"}
Truth table33%1/3
A and B
A= defined $ENV{"PERL5LIB"}B= length $ENV{"PERL5LIB"}
ABresult
0X0
100
111
93 32 foreach my $path (reverse split "$pathsep", $ENV{PERL5LIB}) {
94 32 ▶ say $fh "use lib '$path';" if $path;
Branch: 1 branch
BranchTrueFalseTotal
if $path 32 0 32
95 }
96 }
97  
98 32 print $fh <<'EOS';
99 use Git::Hooks;
100 EOS
101  
102 32 ▶ print $fh $extra_perl if defined $extra_perl;
Branch: 1 branch
BranchTrueFalseTotal
if defined $extra_perl 11 21 32
103  
104 # Not all hooks defined the GIT_DIR environment variable
105 # (e.g., pre-rebase doesn't).
106 32 print $fh <<'EOS';
107 $ENV{GIT_DIR} = '.git' unless exists $ENV{GIT_DIR};
108 $ENV{GIT_CONFIG} = "$ENV{GIT_DIR}/config";
109 EOS
110  
111 # Hooks on Windows are invoked indirectly.
112 32 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 32 32
113 0 print $fh <<'EOS';
114 my $hook = shift;
115 run_hook($hook, @ARGV);
116 EOS
117 } else {
118 32 print $fh <<'EOS';
119 run_hook($0, @ARGV);
120 EOS
121 }
122 }
123 32 chmod 0755 => $hook_pl;
124  
125 32 ▶ @hooks = qw/ applypatch-msg pre-applypatch post-applypatch
Branch: 1 branch
BranchTrueFalseTotal
unless @hooks 1 31 32
126 pre-commit prepare-commit-msg commit-msg
127 post-commit pre-rebasen post-checkout post-merge
128 pre-push pre-receive update post-receive post-update
129 push-to-checkout pre-auto-gc post-rewrite
130 / unless @hooks;
131  
132 32 foreach my $hook (@hooks) {
133 77 my $hookfile = $hooks_dir->child($hook);
134 77 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 77 77
135 0 (my $perl = $^X) =~ tr:\\:/:;
136 0 $hook_pl =~ tr:\\:/:;
137 0 ▶ my $d = $ENV{DBG} ? '-d' : '';
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'DBG'} ? : 0 0 0
138 0 my $script = <<"EOS";
139 #!/bin/sh
140 $perl $d $hook_pl $hook \"\$@\"
141 EOS
142 0 ▶ path($hookfile)->spew($script)
Branch: 1 branch
BranchTrueFalseTotal
path($hookfile)->spew($script) or BAIL_OUT("can't path('${hookfile}')->spew('${script}')\n") 0 0 0
143 or BAIL_OUT("can't path('$hookfile')->spew('$script')\n");
144 0 chmod 0755 => $hookfile;
145 } else {
146 77 $hookfile->remove; # in case we're replacing the hooks
147 77 ▶ symlink 'hook.pl', $hookfile
Branch: 1 branch
BranchTrueFalseTotal
symlink "hook.pl", $hookfile or BAIL_OUT("can't symlink '${hooks_dir}', '${hook}': $!") 0 77 77
148 or BAIL_OUT("can't symlink '$hooks_dir', '$hook': $!");
149 }
150 }
151 32 return;
152 }
153  
154 sub new_repos {
155 22 ▶ my $repodir = $T->child('repo');
Subroutine new_repos: called CC=9 SCAR=25.5
Pod undocumented
156 22 my $filename = $repodir->child('file.txt');
157 22 my $clonedir = $T->child('clone');
158  
159 # Remove the directories recursively to create new ones.
160 22 $repodir->remove_tree({safe => 0});
161 22 $clonedir->remove_tree({safe => 0});
162  
163 22 ▶ mkdir $repodir, 0777 or BAIL_OUT("can't mkdir $repodir: $!");
Branch: 1 branch
BranchTrueFalseTotal
mkdir $repodir, 511 or BAIL_OUT("can't mkdir ${repodir}: $!") 0 22 22
164 {
165 22 ▶ open my $fh, '>', $filename or croak BAIL_OUT("can't open $filename: $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $filename or croak(BAIL_OUT("can't open ${filename}: $!")) 0 22 22
166 22 say $fh "first line";
167 22 close $fh;
168 }
169  
170 22 my $stderr = $T->child('stderr');
171  
172 22 my @result = eval {
173 22 Git::Repository->run(qw/-c init.defaultBranch=master init -q/,
174 "--template=$tmpldir", $repodir, { env => \%git_test_env });
175  
176 22 my $repo = Git::Repository->new(work_tree => $repodir,
177 { env => \%git_test_env });
178  
179 22 $repo->run(qw/config user.email myself@example.com/);
180 22 $repo->run(qw/config user.name/, 'My Self');
181  
182 {
183 22 my $cmd = Git::Repository->command(
184 qw/clone -q --bare --no-hardlinks/, "--template=$tmpldir", $repodir, $clonedir,
185 );
186  
187 22 my $my_stderr = $cmd->stderr;
188  
189 22 ▶ open my $err_h, '>', $T->child('stderr')
Branch: 1 branch
BranchTrueFalseTotal
open my $err_h, ">", $T->child("stderr") or croak(qq[Can't open '@{[$T->child("stderr")];}' for writing: $!\n]) 0 22 22
190 0 or croak "Can't open '@{[$T->child('stderr')]}' for writing: $!\n";
191 22 ▶ while (<$my_stderr>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $my_stderr)) 22 22 44
192 22 $err_h->print($_);
193 }
194 22 close $err_h;
195  
196 22 $cmd->close();
197 22 ▶ croak "Can't git-clone $repodir into $clonedir" unless $cmd->exit() == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $cmd->exit == 0 0 22 22
198 }
199  
200 22 my $clone = Git::Repository->new(git_dir => $clonedir);
201  
202 22 $repo->run(qw/remote add clone/, $clonedir);
203  
204 22 return ($repo, $filename, $clone, $T);
205 };
206  
207 22 ▶ if (my $E = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $E = $@) 0 22 22
208 0 my $exception = "$E"; # stringify it
209 0 ▶ if (-s $stderr) {
Branch: 1 branch
BranchTrueFalseTotal
if (-s $stderr) 0 0 0
210 0 ▶ open my $err_h, '<', $stderr
Branch: 1 branch
BranchTrueFalseTotal
open my $err_h, "<", $stderr or croak("Can't open '${stderr}' for reading: $!\n") 0 0 0
211 or croak "Can't open '$stderr' for reading: $!\n";
212 0 local $/ = undef; # slurp mode
213 0 $exception .= 'STDERR=';
214 0 $exception .= <$err_h>;
215 0 close $err_h;
216 }
217  
218 # The BAIL_OUT function can't show a message with newlines
219 # inside. So, we have to make sure to get rid of any.
220 0 $exception =~ s/\n/;/g;
221 0 local $, = ':';
222 0 BAIL_OUT("Error setting up repos for test: Exception='$exception'; CWD=$T; git-version=$git_version; \@INC=(@INC).\n");
223 0 @result = ();
224 };
225  
226 22 return @result;
227 }
228  
229 sub new_commit {
230 13 ▶ my ($git, $file, $msg) = @_;
Subroutine new_commit: called CC=3 SCAR=11.3
Pod undocumented
231  
232 13 ▶ $file->append($msg || 'new commit');
Condition50%1/2
$msg || 'new commit'
l!l
130
MC/DC0%0/1
$msg || 'new commit'
$msg
Truth table50%1/2
A
A= $msg
Aresult
11
00
233  
234 13 $git->run(add => $file);
235 13 ▶ $git->run(qw/commit -q -m/, $msg || 'commit');
Condition50%1/2
$msg || 'commit'
l!l
130
MC/DC0%0/1
$msg || 'commit'
$msg
Truth table50%1/2
A
A= $msg
Aresult
11
00
236  
237 13 return;
238 }
239  
240  
241 # Executes a git command with arguments and return a four-elements
242 # list containing: (a) a boolean indication of success, (b) the exit
243 # code, (c) the command's STDOUT, and (d) the command's STDERR.
244 sub test_command {
245 203 ▶ my ($git, $command, @args) = @_;
Subroutine test_command: called CC=1 SCAR=0.0
Pod undocumented
246  
247 203 my $cmd = $git->command($command, @args);
248  
249 203 my $stdout = do { local $/ = undef; readline($cmd->stdout); };
250 203 my $stderr = do { local $/ = undef; readline($cmd->stderr); };
251  
252 203 $cmd->close;
253  
254 203 return ($cmd->exit() == 0, $cmd->exit(), $stdout, $stderr);
255 }
256  
257 sub test_ok {
258 104 ▶ my ($testname, @args) = @_;
Subroutine test_ok: called CC=2 SCAR=7.6
Pod undocumented
259 104 my ($ok, $exit, $stdout, $stderr) = test_command(@args);
260 104 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 104 0 104
261 104 pass($testname);
262 } else {
263 0 fail($testname);
264 0 diag(" exit=$exit\n stdout=$stdout\n stderr=$stderr\n git-version=$git_version\n");
265 }
266 104 return $ok;
267 }
268  
269 sub test_ok_match {
270 3 ▶ my ($testname, $regex, @args) = @_;
Subroutine test_ok_match: called CC=4 SCAR=16.3
Pod undocumented
271 3 my ($ok, $exit, $stdout, $stderr) = test_command(@args);
272 3 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 3 0 3
273 3 ▶ if ($stdout =~ $regex || $stderr =~ $regex) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stdout =~ /$regex/u or $stderr =~ /$regex/u) { } 3 0 3
Condition66%2/3
$stdout =~ /$regex/u or $stderr =~ /$regex/u
l!l&&r!l&&!r
120
MC/DC0%0/2
$stdout =~ /$regex/u or $stderr =~ /$regex/u
$stdout =~ /$regex/u $stderr =~ /$regex/u
Truth table66%2/3
A or B
A= $stdout =~ /$regex/uB= $stderr =~ /$regex/u
ABresult
1X1
011
000
274 3 pass($testname);
275 } else {
276 0 fail($testname);
277 0 diag(" did not match regex ($regex)\n stdout=$stdout\n stderr=$stderr\n git-version=$git_version\n");
278 }
279 } else {
280 0 fail($testname);
281 0 diag(" exit=$exit\n stdout=$stdout\n stderr=$stderr\n git-version=$git_version\n");
282 }
283 3 return $ok;
284 }
285  
286 sub test_nok {
287 7 ▶ my ($testname, @args) = @_;
Subroutine test_nok: called CC=2 SCAR=7.6
Pod undocumented
288 7 my ($ok, $exit, $stdout, $stderr) = test_command(@args);
289 7 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 0 7 7
290 0 fail($testname);
291 0 diag(" succeeded without intention\n stdout=$stdout\n stderr=$stderr\n git-version=$git_version\n");
292 } else {
293 7 pass($testname);
294 }
295 7 return !$ok;
296 }
297  
298 sub test_nok_match {
299 78 ▶ my ($testname, $regex, @args) = @_;
Subroutine test_nok_match: called CC=4 SCAR=19.1
Pod undocumented
300 78 my ($ok, $exit, $stdout, $stderr) = test_command(@args);
301 78 ▶ if ($ok) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ok) { } 0 78 78
elsif ($stdout =~ /$regex/u or $stderr =~ /$regex/u) { } 78 0 78
302 0 fail($testname);
303 0 diag(" succeeded without intention\n exit=$exit\n stdout=$stdout\n stderr=$stderr\n git-version=$git_version\n");
304 0 return 0;
305 ▶ } elsif ($stdout =~ $regex || $stderr =~ $regex) {
Condition33%1/3
$stdout =~ /$regex/u or $stderr =~ /$regex/u
l!l&&r!l&&!r
0780
MC/DC0%0/2
$stdout =~ /$regex/u or $stderr =~ /$regex/u
$stdout =~ /$regex/u $stderr =~ /$regex/u
Truth table33%1/3
A or B
A= $stdout =~ /$regex/uB= $stderr =~ /$regex/u
ABresult
1X1
011
000
306 78 pass($testname);
307 78 return 1;
308 } else {
309 0 fail($testname);
310 0 diag(" did not match regex ($regex)\n exit=$exit\n stdout=$stdout\n stderr=$stderr\n git-version=$git_version\n");
311 0 return 0;
312 }
313 }
314  
315 15 1;
316  
317 __END__
« blib/lib/Git/Hooks/CheckRewrite.pm ↑ Summary blib/lib/Git/Hooks/GerritChangeId.pm »