/usr/share/perl5/Test/Requires/Git.pm is in libtest-requires-git-perl 1.008-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 | package Test::Requires::Git;
$Test::Requires::Git::VERSION = '1.008';
use strict;
use warnings;
use Carp;
use Git::Version::Compare ();
use base 'Test::Builder::Module';
our $GIT = 'git';
my %check = (
version_eq => \&Git::Version::Compare::eq_git,
version_ne => \&Git::Version::Compare::ne_git,
version_gt => \&Git::Version::Compare::gt_git,
version_le => \&Git::Version::Compare::le_git,
version_lt => \&Git::Version::Compare::lt_git,
version_ge => \&Git::Version::Compare::ge_git,
);
# aliases
$check{'=='} = $check{eq} = $check{version} = $check{version_eq};
$check{'!='} = $check{ne} = $check{version_ne};
$check{'>'} = $check{gt} = $check{version_gt};
$check{'<='} = $check{le} = $check{version_le};
$check{'<'} = $check{lt} = $check{version_lt};
$check{'>='} = $check{ge} = $check{version_ge};
my $quiet = 0;
my $lock = '.test.requires.git.lock';
sub import {
my $class = shift;
my $caller = caller(0);
# export methods
{
no strict 'refs';
*{"$caller\::test_requires_git"} = \&test_requires_git;
}
return if @_ == 1 && $_[0] eq '-nocheck';
# reset the global $GIT value
my $args = _extract_arguments(@_);
$GIT = $args->{git} if exists $args->{git};
# test arguments
test_requires_git(@_);
}
sub _extract_arguments {
my (@args) = @_;
my ( %args, @spec );
while (@args) {
# assume a lone parameter is a minimum git version
unshift @args, 'version_ge' if @args == 1;
my ( $key, $val ) = splice @args, 0, 2;
if ( $key =~ /^(?:git|skip)/ ) {
croak "Duplicate '$key' argument" if exists $args{$key};
$args{$key} = $val;
}
elsif ( !exists $check{$key} ) {
if ( @args % 2 ) { # odd number of arguments (see above)
unshift @args, version_ge => $key, $val;
redo;
}
croak "Unknown git specification '$key'";
}
else {
push @spec, $key, $val;
}
}
return wantarray ? ( \%args, @spec ) : \%args;
}
sub _git_version { qx{$GIT --version} }
sub test_requires_git {
my ( $args, @spec ) = _extract_arguments(@_);
my $skip = $args->{skip};
local $GIT = $args->{git} if exists $args->{git};
# get the git version
my ($version) = do {
no warnings 'uninitialized';
__PACKAGE__->_git_version(); # tests may override this
};
my $builder = __PACKAGE__->builder;
if ( !$quiet && time - ( ( stat $lock )[9] || 0 ) > 60 ) {
$builder->diag($version);
$quiet++;
open my $fh, '>', $lock if !-e $lock;
utime( undef, undef, $lock );
}
# perform the check
my ( $ok, $why ) = ( 1, '' );
if ( defined $version && Git::Version::Compare::looks_like_git($version) ) {
while ( my ( $spec, $arg ) = splice @spec, 0, 2 ) {
if ( !$check{$spec}->( $version, $arg ) ) {
$ok = 0;
$version =~ s/^git version|[\012\015]+$//g;
$why = "$version $spec $arg";
last;
}
}
}
else {
$ok = 0;
$why = "`$GIT` binary not available or broken";
}
# skip if needed
if ( !$ok ) {
# skip a specified number of tests
if ( $skip ) {
$builder->skip($why) for 1 .. $skip;
no warnings 'exiting';
last SKIP;
}
# no plan declared yet
elsif ( !defined $builder->has_plan ) {
if ( $builder->summary ) {
$builder->skip($why);
$builder->done_testing;
exit 0;
}
else {
$builder->skip_all($why);
}
}
# the plan is no_plan
elsif ( $builder->has_plan eq 'no_plan' ) {
$builder->skip($why);
exit 0;
}
# some plan was declared, skip all tests one by one
else {
$builder->skip($why) for 1 + $builder->summary .. $builder->has_plan;
exit 0;
}
}
}
'git';
__END__
=encoding utf-8
=head1 NAME
Test::Requires::Git - Check your test requirements against the available version of Git
=head1 SYNOPSIS
# will skip all if git is not available
use Test::Requires::Git;
# needs some git that supports `git init $dir`
test_requires_git version_ge => '1.6.5';
=head1 DESCRIPTION
Test::Requires::Git checks if the version of Git available for testing
meets the given requirements. If the checks fail, then all tests will
be I<skipped>.
C<use Test::Requires::Git> always calls C<test_requires_git> with the
given arguments. If you don't want C<test_requires_git> to be called
at import time, write this instead:
use Test::Requires::Git -nocheck;
Passing the C<git> parameter (see L</test_requires_git> below) to
C<use Test::Requires::Git> will override it for the rest of the program run.
=head1 EXPORTED FUNCTIONS
=head2 test_requires_git
# skip all unless git is available as required
test_requires_git version_ge => '1.6.5';
# giving no operator implies 'version_ge'
test_requires_git '1.6.5';
# skip 2 if git is not available
SKIP: {
test_requires_git skip => 2;
...;
}
# skip 2 unless git is available as required
SKIP: {
test_requires_git
skip => 2,
version_ge => '1.7.12';
...;
}
# skip all remaining tests if git is not available
test_requires_git;
# force which git binary to use
test_requires_git
git => '/usr/local/bin/git',
version_ge => '1.6.5';
Takes a list of version requirements (see L</GIT VERSION CHECKING>
below), and if one of them does not pass, I<skips> all remaining tests.
All conditions must be satisfied for the check to pass.
When the C<skip> parameter is given, only the specified number of tests
will be skipped.
The "current git" is obtained by running C<git --version>.
I.e. the first C<git> binary found in the current environment will
be tested. This is of course sensitive to local changes to C<PATH>,
so this will behave as expected:
# skip 4 tests if there's no git available in the alternative PATH
SKIP: {
local $ENV{PATH} = $alternative_PATH;
test_requires_git skip => 4;
...;
}
When the C<git> parameter is given, C<test_requires_git> will run that
program instead of C<git>.
If no condition is given, C<test_requires_git> will simply check if C<git>
is available.
The first time it's called, C<test_require_git> will print a test diagnostic
with the output of C<git --version> (if C<git> is available, of course).
To prevent this behaviour, load the module with:
use Test::Requires::Git -quiet;
=head1 GIT VERSION CHECKING
The actual comparison is handled by L<Git::Version::Compare>, so the
strings can be version numbers, tags from C<git.git> or the output of
C<git version> or C<git describe>.
The following version checks are currently supported:
=head2 version_eq
Aliases: C<version_eq>, C<eq>, C<==>, C<version>.
test_requires_git version_eq => $version;
Passes if the current B<git> version is I<equal> to C<$version>.
=head2 version_ne
Aliases: C<version_ne>, C<ne>, C<!=>.
test_requires_git version_eq => $version;
Passes if the current B<git> version is I<not equal> to C<$version>.
=head2 version_lt
Aliases: C<version_lt>, C<lt>, C<E<lt>>.
test_requires_git version_lt => $version;
Passes if the current B<git> version is I<less than> C<$version>.
=head2 version_gt
Aliases: C<version_gt>, C<gt>, C<E<gt>>.
test_requires_git version_gt => $version;
Passes if the current B<git> version is I<greater than> C<$version>.
=head2 version_le
Aliases: C<version_le>, C<le>, C<E<lt>=>.
test_requires_git version_le => $version;
Passes if the current B<git> version is I<less than or equal> C<$version>.
=head2 version_ge
Aliases: C<version_ge>, C<ge>, C<E<gt>=>.
test_requires_git version_ge => $version;
Passes if the current B<git> version is I<greater than or equal > C<$version>.
As a special shortcut for the most common case, a lone version number
is turned into a C<version_ge> check, so the following two lines are
exactly equivalent:
test_requires_git version_ge => '1.6.5';
# version_ge implied
test_requires_git '1.6.5';
=head1 SEE ALSO
L<Test::Requires>, L<Git::Version::Compare>.
=head1 ACKNOWLEDGEMENTS
Thanks to Oliver Mengué (DOLMEN), who gave me the idea for this module
at the Perl QA Hackathon 2015 in Berlin, and suggested to give a look
at L<Test::Requires> for inspiration.
=head1 AUTHOR
Philippe Bruhat (BooK), <book@cpan.org>.
=head1 COPYRIGHT
Copyright 2015-2016 Philippe Bruhat (BooK), all rights reserved.
=head1 LICENSE
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut
|