From: Vincent Pit Date: Wed, 24 Feb 2010 18:20:43 +0000 (+0100) Subject: Make App::Rgit::Config->new discover the root directory itself X-Git-Tag: v0.07~3 X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2Frgit.git;a=commitdiff_plain;h=569b85b70a20cac15bc2ff0bed6df11ac0e844ec Make App::Rgit::Config->new discover the root directory itself --- diff --git a/bin/rgit b/bin/rgit index 19558a3..040dae6 100755 --- a/bin/rgit +++ b/bin/rgit @@ -43,12 +43,9 @@ if (not defined $policy) { setpgrp 0, 0 if $Config{d_setpgrp}; -my $root = $ENV{GIT_DIR}; -$root = cwd unless defined $root; - my $ar = App::Rgit->new( git => undef, # Autodiscovery - root => $root, + root => undef, # Autodiscovery cmd => $cmd, args => \@ARGV, policy => $policy, diff --git a/lib/App/Rgit/Config.pm b/lib/App/Rgit/Config.pm index 94a3359..903b3e4 100644 --- a/lib/App/Rgit/Config.pm +++ b/lib/App/Rgit/Config.pm @@ -4,7 +4,7 @@ use strict; use warnings; use Carp (); # confess -use Cwd (); # abs_path +use Cwd (); # cwd, abs_path use File::Spec (); # canonpath, catfile, path use App::Rgit::Repository; @@ -44,8 +44,12 @@ sub new { my %args = @_; - my $root = $args{root}; - return unless defined $root and -d $root; + my $root = defined $args{root} + ? $args{root} + : defined $ENV{GIT_DIR} + ? $ENV{GIT_DIR} + : Cwd::cwd; + Carp::confess("Invalid root directory") unless -d $root; $root = File::Spec->canonpath(Cwd::abs_path($root)); my $git; diff --git a/t/15-failures.t b/t/15-failures.t index 0e31edd..cf6568b 100644 --- a/t/15-failures.t +++ b/t/15-failures.t @@ -6,25 +6,32 @@ use warnings; use Cwd (); # cwd use File::Spec (); # catdir -use Test::More tests => 43; +use Test::More tests => 44; use App::Rgit; local $SIG{__WARN__} = sub { die @_ }; my $res = eval { - App::Rgit->new() + local $ENV{GIT_DIR}; + App::Rgit->new(); }; -is $@, '', "App::Rgit->new(): no root: doesn't croak"; -is $res, undef, 'App::Rgit->new(): no root: returns undef'; +is $@, '', "App::Rgit->new(): no root, no GIT_DIR: doesn't croak"; +isa_ok $res,'App::Rgit','App::Rgit->new(): no root, no GIT_DIR: returns object'; + +$res = eval { + local $ENV{GIT_DIR} = Cwd::cwd; + App::Rgit->new(); +}; +is $@, '', "App::Rgit->new(): no root: doesn't croak"; +isa_ok $res, 'App::Rgit', 'App::Rgit->new(): no root: returns object'; $res = eval { App::Rgit->new( root => $0, ); }; -is $@, '', "App::Rgit->new(): wrong root: doesn't croak"; -is $res, undef, 'App::Rgit->new(): wrong root: returns undef'; +like $@, qr/Invalid root directory/, 'App::Rgit->new(): wrong root: croaks'; $res = eval { local $ENV{GIT_EXEC_PATH};