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/CheckLog.pm ↑ Summary blib/lib/Git/Hooks/CheckFile.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 = '4.0.0';
6 ## no critic (RequireExplicitPackage)
7 ## no critic (ErrorHandling::RequireCarping)
8 15 ▶use v5.30.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}: $!") 15 0 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}: $!") 15 0 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}: $!") 15 0 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 0 ▶sub newdir {
Subroutine newdir: not called CC=1 SCAR=6.9
Pod undocumented
69 0 my $num = 1 + Test::Builder->new()->current_test();
70 0 my $dir = $T->child($num);
71 0 mkdir $dir;
72 0 return $dir;
73 }
74  
75 32 ▶sub install_hooks {
Subroutine install_hooks: called CC=16 SCAR=33.2
Pod undocumented
76 32 my ($git, $extra_perl, @hooks) = @_;
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}: $!") 32 0 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 warnings;
87 use lib '$bliblib';
88 EOS
89  
90 32 ▶ state $pathsep = $^O eq 'MSWin32' ? ';' : ':';
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 13 13
91 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
92 32 foreach my $path (reverse split "$pathsep", $ENV{PERL5LIB}) {
93 32 ▶ say $fh "use lib '$path';" if $path;
Branch: 1 branch
BranchTrueFalseTotal
if $path 32 0 32
94 }
95 }
96  
97 32 print $fh <<'EOS';
98 use Git::Hooks;
99 EOS
100  
101 32 ▶ print $fh $extra_perl if defined $extra_perl;
Branch: 1 branch
BranchTrueFalseTotal
if defined $extra_perl 11 21 32
102  
103 # Not all hooks defined the GIT_DIR environment variable
104 # (e.g., pre-rebase doesn't).
105 32 print $fh <<'EOS';
106 $ENV{GIT_DIR} = '.git' unless exists $ENV{GIT_DIR};
107 $ENV{GIT_CONFIG} = "$ENV{GIT_DIR}/config";
108 EOS
109  
110 # Hooks on Windows are invoked indirectly.
111 32 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 32 32
112 0 print $fh <<'EOS';
113 my $hook = shift;
114 run_hook($hook, @ARGV);
115 EOS
116 } else {
117 32 print $fh <<'EOS';
118 run_hook($0, @ARGV);
119 EOS
120 }
121 }
122 32 chmod 0755 => $hook_pl;
123  
124 32 ▶ @hooks = qw/ applypatch-msg pre-applypatch post-applypatch
Branch: 1 branch
BranchTrueFalseTotal
unless @hooks 1 31 32
125 pre-commit prepare-commit-msg commit-msg
126 post-commit pre-rebasen post-checkout post-merge
127 pre-push pre-receive update post-receive post-update
128 push-to-checkout pre-auto-gc post-rewrite
129 / unless @hooks;
130  
131 32 foreach my $hook (@hooks) {
132 77 my $hookfile = $hooks_dir->child($hook);
133 77 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 77 77
134 0 (my $perl = $^X) =~ tr:\\:/:;
135 0 $hook_pl =~ tr:\\:/:;
136 0 ▶ my $d = $ENV{DBG} ? '-d' : '';
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'DBG'} ? : 0 0 0
137 0 my $script = <<"EOS";
138 #!/bin/sh
139 $perl $d $hook_pl $hook \"\$@\"
140 EOS
141 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
142 or BAIL_OUT("can't path('$hookfile')->spew('$script')\n");
143 0 chmod 0755 => $hookfile;
144 } else {
145 77 $hookfile->remove; # in case we're replacing the hooks
146 77 ▶ symlink 'hook.pl', $hookfile
Branch: 1 branch
BranchTrueFalseTotal
symlink "hook.pl", $hookfile or BAIL_OUT("can't symlink '${hooks_dir}', '${hook}': $!") 77 0 77
147 or BAIL_OUT("can't symlink '$hooks_dir', '$hook': $!");
148 }
149 }
150 32 return;
151 }
152  
153 22 ▶sub new_repos {
Subroutine new_repos: called CC=9 SCAR=25.5
Pod undocumented
154 22 my $repodir = $T->child('repo');
155 22 my $filename = $repodir->child('file.txt');
156 22 my $clonedir = $T->child('clone');
157  
158 # Remove the directories recursively to create new ones.
159 22 $repodir->remove_tree({safe => 0});
160 22 $clonedir->remove_tree({safe => 0});
161  
162 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}: $!") 22 0 22
163 {
164 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}: $!")) 22 0 22
165 22 say $fh "first line";
166 22 close $fh;
167 }
168  
169 22 my $stderr = $T->child('stderr');
170  
171 22 my @result = eval {
172 22 Git::Repository->run(qw/-c init.defaultBranch=master init -q/,
173 "--template=$tmpldir", $repodir, { env => \%git_test_env });
174  
175 22 my $repo = Git::Repository->new(work_tree => $repodir,
176 { env => \%git_test_env });
177  
178 22 $repo->run(qw/config user.email myself@example.com/);
179 22 $repo->run(qw/config user.name/, 'My Self');
180  
181 {
182 22 my $cmd = Git::Repository->command(
183 qw/clone -q --bare --no-hardlinks/, "--template=$tmpldir", $repodir, $clonedir,
184 );
185  
186 22 my $my_stderr = $cmd->stderr;
187  
188 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]) 22 0 22
189 0 or croak "Can't open '@{[$T->child('stderr')]}' for writing: $!\n";
190 22 ▶ while (<$my_stderr>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $my_stderr)) 22 22 44
191 22 $err_h->print($_);
192 }
193 22 close $err_h;
194  
195 22 $cmd->close();
196 22 ▶ croak "Can't git-clone $repodir into $clonedir" unless $cmd->exit() == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $cmd->exit == 0 0 22 22
197 }
198  
199 22 my $clone = Git::Repository->new(git_dir => $clonedir);
200  
201 22 $repo->run(qw/remote add clone/, $clonedir);
202  
203 22 return ($repo, $filename, $clone, $T);
204 };
205  
206 22 ▶ if (my $E = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $E = $@) 0 22 22
207 0 my $exception = "$E"; # stringify it
208 0 ▶ if (-s $stderr) {
Branch: 1 branch
BranchTrueFalseTotal
if (-s $stderr) 0 0 0
209 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
210 or croak "Can't open '$stderr' for reading: $!\n";
211 0 local $/ = undef; # slurp mode
212 0 $exception .= 'STDERR=';
213 0 $exception .= <$err_h>;
214 0 close $err_h;
215 }
216  
217 # The BAIL_OUT function can't show a message with newlines
218 # inside. So, we have to make sure to get rid of any.
219 0 $exception =~ s/\n/;/g;
220 0 local $, = ':';
221 0 BAIL_OUT("Error setting up repos for test: Exception='$exception'; CWD=$T; git-version=$git_version; \@INC=(@INC).\n");
222 0 @result = ();
223 };
224  
225 22 return @result;
226 }
227  
228 13 ▶sub new_commit {
Subroutine new_commit: called CC=3 SCAR=11.3
Pod undocumented
229 13 my ($git, $file, $msg) = @_;
230  
231 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
232  
233 13 $git->run(add => $file);
234 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
235  
236 13 return;
237 }
238  
239  
240 # Executes a git command with arguments and return a four-elements
241 # list containing: (a) a boolean indication of success, (b) the exit
242 # code, (c) the command's STDOUT, and (d) the command's STDERR.
243 207 ▶sub test_command {
Subroutine test_command: called CC=1 SCAR=0.0
Pod undocumented
244 207 my ($git, $command, @args) = @_;
245  
246 207 my $cmd = $git->command($command, @args);
247  
248 207 my $stdout = do { local $/ = undef; readline($cmd->stdout); };
249 207 my $stderr = do { local $/ = undef; readline($cmd->stderr); };
250  
251 207 $cmd->close;
252  
253 207 return ($cmd->exit() == 0, $cmd->exit(), $stdout, $stderr);
254 }
255  
256 108 ▶sub test_ok {
Subroutine test_ok: called CC=2 SCAR=7.6
Pod undocumented
257 108 my ($testname, @args) = @_;
258 108 my ($ok, $exit, $stdout, $stderr) = test_command(@args);
259 108 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 108 0 108
260 108 pass($testname);
261 } else {
262 0 fail($testname);
263 0 diag(" exit=$exit\n stdout=$stdout\n stderr=$stderr\n git-version=$git_version\n");
264 }
265 108 return $ok;
266 }
267  
268 3 ▶sub test_ok_match {
Subroutine test_ok_match: called CC=4 SCAR=16.3
Pod undocumented
269 3 my ($testname, $regex, @args) = @_;
270 3 my ($ok, $exit, $stdout, $stderr) = test_command(@args);
271 3 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 3 0 3
272 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
273 3 pass($testname);
274 } else {
275 0 fail($testname);
276 0 diag(" did not match regex ($regex)\n stdout=$stdout\n stderr=$stderr\n git-version=$git_version\n");
277 }
278 } else {
279 0 fail($testname);
280 0 diag(" exit=$exit\n stdout=$stdout\n stderr=$stderr\n git-version=$git_version\n");
281 }
282 3 return $ok;
283 }
284  
285 7 ▶sub test_nok {
Subroutine test_nok: called CC=2 SCAR=7.6
Pod undocumented
286 7 my ($testname, @args) = @_;
287 7 my ($ok, $exit, $stdout, $stderr) = test_command(@args);
288 7 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 0 7 7
289 0 fail($testname);
290 0 diag(" succeeded without intention\n stdout=$stdout\n stderr=$stderr\n git-version=$git_version\n");
291 } else {
292 7 pass($testname);
293 }
294 7 return !$ok;
295 }
296  
297 78 ▶sub test_nok_match {
Subroutine test_nok_match: called CC=4 SCAR=19.1
Pod undocumented
298 78 my ($testname, $regex, @args) = @_;
299 78 my ($ok, $exit, $stdout, $stderr) = test_command(@args);
300 78 ▶ if ($ok) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ok) { } 0 78 78
elsif ($stdout =~ /$regex/u or $stderr =~ /$regex/u) { } 78 0 78
301 0 fail($testname);
302 0 diag(" succeeded without intention\n exit=$exit\n stdout=$stdout\n stderr=$stderr\n git-version=$git_version\n");
303 0 return 0;
304 ▶ } 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
305 78 pass($testname);
306 78 return 1;
307 } else {
308 0 fail($testname);
309 0 diag(" did not match regex ($regex)\n exit=$exit\n stdout=$stdout\n stderr=$stderr\n git-version=$git_version\n");
310 0 return 0;
311 }
312 }
313  
314 15 1;
315  
316 __END__
« blib/lib/Git/Hooks/CheckLog.pm ↑ Summary blib/lib/Git/Hooks/CheckFile.pm »