File Coverage

blib/lib/Test/Git.pm
Criterion Covered Total %
statement 49 56 87.5
branch 9 20 45.0
condition 9 18 50.0
subroutine 11 12 91.6
pod 2 2 100.0
total 80 108 74.0


line stmt bran cond sub pod time code
1             package Test::Git;
2             $Test::Git::VERSION = '1.325';
3 14     14   1147112 use strict;
  14         542  
  14         697  
4 14     14   140 use warnings;
  14         71  
  14         970  
5              
6 14     14   133 use Exporter;
  14         42  
  14         1193  
7 14     14   131 use Test::Builder;
  14         40  
  14         530  
8 14     14   7957 use Git::Repository; # 1.15
  14         47  
  14         58  
9 14     14   11996 use File::Temp qw( tempdir );
  14         224186  
  14         1100  
10 14     14   6998 use File::Spec::Functions qw( catdir );
  14         13054  
  14         966  
11 14     14   107 use Git::Version::Compare qw( cmp_git );
  14         30  
  14         662  
12 14     14   82 use Cwd qw( cwd );
  14         29  
  14         565  
13 14     14   79 use Carp;
  14         30  
  14         8782  
14              
15             our @ISA = qw( Exporter );
16             our @EXPORT = qw( has_git test_repository );
17              
18             my $Test = Test::Builder->new();
19              
20             sub has_git {
21 0     0 1 0 my ( $version, @options ) = ( ( grep !ref, @_ )[0], grep ref, @_ );
22 0 0       0 carp sprintf 'has_git() is obsolete, write `use Test::Requires::Git; test_requires_git%s;` instead',
23             $version ? " '$version'" : '';
24              
25             # check some git is present
26 0 0       0 $Test->skip_all('Default git binary not found in PATH')
27             if !Git::Repository::Command::_is_git('git');
28              
29             # check it's at least some minimum version
30 0         0 my $git_version = Git::Repository->version(@options);
31 0 0 0     0 $Test->skip_all(
32             "Test script requires git >= $version (this is only $git_version)")
33             if $version && Git::Repository->version_lt( $version, @options );
34             }
35              
36             sub test_repository {
37 13     13 1 15217 my %args = @_;
38              
39             croak "Can't use both 'init' and 'clone' parameters"
40 13 50 66     275 if exists $args{init} && exists $args{clone};
41              
42             # setup some default values
43 13   50     215 my $temp = $args{temp} || [ CLEANUP => 1 ]; # File::Temp options
44 13   100     132 my $init = $args{init} || []; # git init options
45 13   100     204 my $opts = $args{git} || {}; # Git::Repository options
46 13         78 my $safe = { %$opts, fatal => [] }; # ignore 'fatal' settings
47 13         38 my $clone = $args{clone}; # git clone options
48              
49             # version check
50 13         32 my $bad_version;
51 13         178 my $git_version = Git::Repository->version($safe);
52 13 100       1580 if ($clone) {
    50          
53 2 50 33     55 if (
      33        
54             cmp_git( $git_version, '1.6.2.rc0' ) < 0
55             || ( cmp_git( '1.7.0.rc1', $git_version ) <= 0
56             && cmp_git( $git_version, '1.7.0.2' ) <= 0 )
57             )
58             {
59 0         0 $bad_version = "test_repository( clone => ... ) requires git >= 1.6.2.rc0 and 1.7.0.rc1 < git < 1.7.0.2 (this is $git_version)";
60             }
61             }
62             elsif ( cmp_git( $git_version, '1.4.0' ) <= 0 ) {
63 0         0 $bad_version = "test_repository( init => ... ) requires git >= 1.4.0 (this is only $git_version)";
64             }
65 13 50       1167 croak $bad_version if $bad_version;
66              
67             # create a temporary directory to host our repository
68 13         285 my $dir = tempdir(@$temp);
69 13         11678 my $cwd = { cwd => $dir }; # option to chdir there
70              
71             # create the git repository there
72 13 50       1367 my $init_cmd = Git::Repository->version_lt('1.5.0.rc1') ? 'init-db' : 'init';
73 13 100       3634 my @cmd = $clone ? ( clone => @$clone, $dir ) : ( $init_cmd => @$init, $cwd );
74 13         199 Git::Repository->run( @cmd, $safe );
75              
76             # create the Git::Repository object
77 13         1240 my $gitdir = Git::Repository->run( qw( rev-parse --git-dir ), $cwd );
78 13         1618 return Git::Repository->new( git_dir => catdir( $dir, $gitdir ), $opts );
79             }
80              
81             1;
82              
83             __END__