blib/lib/Git/Hooks/Test.pm untested

filter: Statementstmt 0.0% 0 / 174 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 41 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 282 CC 19 SCAR 35.6
CC
19
Cov
0%
install_hooks
110.0
new_repos
20.0
test_ok_match
12.0
CRAP 35.2
1 use warnings;
2  
3 package Git::Hooks::Test;
4 # ABSTRACT: Git::Hooks testing utilities
5 $Git::Hooks::Test::VERSION = '3.5.0';
6 ## no critic (RequireExplicitPackage)
7 ## no critic (ErrorHandling::RequireCarping)
8 use v5.16.0;
9 use utf8;
10 use Carp;
11 use Config;
12 use Exporter qw/import/;
13 use Path::Tiny;
14 use Git::Repository 'GitHooks';
15 use Test::More;
16  
17 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 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 my %git_test_env = (
38 LC_ALL => 'C',
39 GIT_CONFIG_NOSYSTEM => 1,
40 XDG_CONFIG_HOME => undef,
41 HOME => undef,
42 );
43  
44 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 CLEANUP => exists $ENV{REPO_CLEANUP} ? $ENV{REPO_CLEANUP} : 1,
54 );
55  
56 chdir $T or croak "Can't chdir $T: $!";
57 END { chdir '/' }
58  
59 my $tmpldir = $T->child('templates');
60 mkdir $tmpldir, 0777 or BAIL_OUT("can't mkdir $tmpldir: $!");
61 {
62 my $hooksdir = $tmpldir->child('hooks');
63 mkdir $hooksdir, 0777 or BAIL_OUT("can't mkdir $hooksdir: $!");
64 }
65  
66 my $git_version = eval { Git::Repository->version } || 'unknown';
67  
68 sub newdir {
69 my $num = 1 + Test::Builder->new()->current_test();
70 my $dir = $T->child($num);
71 mkdir $dir;
72 return $dir;
73 }
74  
75 sub install_hooks {
76 my ($git, $extra_perl, @hooks) = @_;
77 my $hooks_dir = path($git->git_dir())->child('hooks');
78 my $hook_pl = $hooks_dir->child('hook.pl');
79 {
80 ## no critic (RequireBriefOpen)
81 open my $fh, '>', $hook_pl or BAIL_OUT("Can't create $hook_pl: $!");
82 state $debug = $ENV{DBG} ? '-d' : '';
83 state $bliblib = $cwd->child('blib', 'lib');
84 print $fh <<"EOS";
85 #!$Config{perlpath} $debug
86 use strict;
87 use warnings;
88 use lib '$bliblib';
89 EOS
90  
91 state $pathsep = $^O eq 'MSWin32' ? ';' : ':';
92 if (defined $ENV{PERL5LIB} and length $ENV{PERL5LIB}) {
93 foreach my $path (reverse split "$pathsep", $ENV{PERL5LIB}) {
94 say $fh "use lib '$path';" if $path;
95 }
96 }
97  
98 print $fh <<'EOS';
99 use Git::Hooks;
100 EOS
101  
102 print $fh $extra_perl if defined $extra_perl;
103  
104 # Not all hooks defined the GIT_DIR environment variable
105 # (e.g., pre-rebase doesn't).
106 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 if ($^O eq 'MSWin32') {
113 print $fh <<'EOS';
114 my $hook = shift;
115 run_hook($hook, @ARGV);
116 EOS
117 } else {
118 print $fh <<'EOS';
119 run_hook($0, @ARGV);
120 EOS
121 }
122 }
123 chmod 0755 => $hook_pl;
124  
125 @hooks = qw/ applypatch-msg pre-applypatch post-applypatch
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 foreach my $hook (@hooks) {
133 my $hookfile = $hooks_dir->child($hook);
134 if ($^O eq 'MSWin32') {
135 (my $perl = $^X) =~ tr:\\:/:;
136 $hook_pl =~ tr:\\:/:;
137 my $d = $ENV{DBG} ? '-d' : '';
138 my $script = <<"EOS";
139 #!/bin/sh
140 $perl $d $hook_pl $hook \"\$@\"
141 EOS
142 path($hookfile)->spew($script)
143 or BAIL_OUT("can't path('$hookfile')->spew('$script')\n");
144 chmod 0755 => $hookfile;
145 } else {
146 $hookfile->remove; # in case we're replacing the hooks
147 symlink 'hook.pl', $hookfile
148 or BAIL_OUT("can't symlink '$hooks_dir', '$hook': $!");
149 }
150 }
151 return;
152 }
153  
154 sub new_repos {
155 my $repodir = $T->child('repo');
156 my $filename = $repodir->child('file.txt');
157 my $clonedir = $T->child('clone');
158  
159 # Remove the directories recursively to create new ones.
160 $repodir->remove_tree({safe => 0});
161 $clonedir->remove_tree({safe => 0});
162  
163 mkdir $repodir, 0777 or BAIL_OUT("can't mkdir $repodir: $!");
164 {
165 open my $fh, '>', $filename or croak BAIL_OUT("can't open $filename: $!");
166 say $fh "first line";
167 close $fh;
168 }
169  
170 my $stderr = $T->child('stderr');
171  
172 my @result = eval {
173 Git::Repository->run(qw/-c init.defaultBranch=master init -q/,
174 "--template=$tmpldir", $repodir, { env => \%git_test_env });
175  
176 my $repo = Git::Repository->new(work_tree => $repodir,
177 { env => \%git_test_env });
178  
179 $repo->run(qw/config user.email myself@example.com/);
180 $repo->run(qw/config user.name/, 'My Self');
181  
182 {
183 my $cmd = Git::Repository->command(
184 qw/clone -q --bare --no-hardlinks/, "--template=$tmpldir", $repodir, $clonedir,
185 );
186  
187 my $my_stderr = $cmd->stderr;
188  
189 open my $err_h, '>', $T->child('stderr')
190 or croak "Can't open '@{[$T->child('stderr')]}' for writing: $!\n";
191 while (<$my_stderr>) {
192 $err_h->print($_);
193 }
194 close $err_h;
195  
196 $cmd->close();
197 croak "Can't git-clone $repodir into $clonedir" unless $cmd->exit() == 0;
198 }
199  
200 my $clone = Git::Repository->new(git_dir => $clonedir);
201  
202 $repo->run(qw/remote add clone/, $clonedir);
203  
204 return ($repo, $filename, $clone, $T);
205 };
206  
207 if (my $E = $@) {
208 my $exception = "$E"; # stringify it
209 if (-s $stderr) {
210 open my $err_h, '<', $stderr
211 or croak "Can't open '$stderr' for reading: $!\n";
212 local $/ = undef; # slurp mode
213 $exception .= 'STDERR=';
214 $exception .= <$err_h>;
215 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 $exception =~ s/\n/;/g;
221 local $, = ':';
222 BAIL_OUT("Error setting up repos for test: Exception='$exception'; CWD=$T; git-version=$git_version; \@INC=(@INC).\n");
223 @result = ();
224 };
225  
226 return @result;
227 }
228  
229 sub new_commit {
230 my ($git, $file, $msg) = @_;
231  
232 $file->append($msg || 'new commit');
233  
234 $git->run(add => $file);
235 $git->run(qw/commit -q -m/, $msg || 'commit');
236  
237 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 my ($git, $command, @args) = @_;
246  
247 my $cmd = $git->command($command, @args);
248  
249 my $stdout = do { local $/ = undef; readline($cmd->stdout); };
250 my $stderr = do { local $/ = undef; readline($cmd->stderr); };
251  
252 $cmd->close;
253  
254 return ($cmd->exit() == 0, $cmd->exit(), $stdout, $stderr);
255 }
256  
257 sub test_ok {
258 my ($testname, @args) = @_;
259 my ($ok, $exit, $stdout, $stderr) = test_command(@args);
260 if ($ok) {
261 pass($testname);
262 } else {
263 fail($testname);
264 diag(" exit=$exit\n stdout=$stdout\n stderr=$stderr\n git-version=$git_version\n");
265 }
266 return $ok;
267 }
268  
269 sub test_ok_match {
270 my ($testname, $regex, @args) = @_;
271 my ($ok, $exit, $stdout, $stderr) = test_command(@args);
272 if ($ok) {
273 if ($stdout =~ $regex || $stderr =~ $regex) {
274 pass($testname);
275 } else {
276 fail($testname);
277 diag(" did not match regex ($regex)\n stdout=$stdout\n stderr=$stderr\n git-version=$git_version\n");
278 }
279 } else {
280 fail($testname);
281 diag(" exit=$exit\n stdout=$stdout\n stderr=$stderr\n git-version=$git_version\n");
282 }
283 return $ok;
284 }
285  
286 sub test_nok {
287 my ($testname, @args) = @_;
288 my ($ok, $exit, $stdout, $stderr) = test_command(@args);
289 if ($ok) {
290 fail($testname);
291 diag(" succeeded without intention\n stdout=$stdout\n stderr=$stderr\n git-version=$git_version\n");
292 } else {
293 pass($testname);
294 }
295 return !$ok;
296 }
297  
298 sub test_nok_match {
299 my ($testname, $regex, @args) = @_;
300 my ($ok, $exit, $stdout, $stderr) = test_command(@args);
301 if ($ok) {
302 fail($testname);
303 diag(" succeeded without intention\n exit=$exit\n stdout=$stdout\n stderr=$stderr\n git-version=$git_version\n");
304 return 0;
305 } elsif ($stdout =~ $regex || $stderr =~ $regex) {
306 pass($testname);
307 return 1;
308 } else {
309 fail($testname);
310 diag(" did not match regex ($regex)\n exit=$exit\n stdout=$stdout\n stderr=$stderr\n git-version=$git_version\n");
311 return 0;
312 }
313 }
314  
315 1;
316  
317 __END__