Git-Archive
view release on metacpan or search on metacpan
use IPC::Cmd qw[can_run];
unless ( can_run('git') ) {
ok(1,'No git, no dice');
done_testing;
exit 0;
}
my ($ld, $rd) = ('t/local', 't/remote');
remove_tree($ld, $rd,'t/nongit');
# These should JFW and go without saying, or something went horribly wrong
like(`git --version`, qr/git version/, 'Git is installed');
use_ok('Git::Repository');
# Right. We need a repo with a remote, so we'll need to create a bare repo and clone it
mkdir($rd);
ok(!system("git init --bare $rd"), 'Bare git dir setup');
my $origin = Git::Repository->new( git_dir => $rd );
Git::Repository->run( clone => $rd, $ld );
ok(-e "$ld/.git", 'Successful git clone');
my $repo = Git::Repository->new( work_tree => $ld );
## Populate name & email if not already done
unless ( $repo->run( 'config', 'user.email' ) ) {
system( $repo->run( 'config', 'user.email', '"git.user@example.com"' ) );
}
unless ( $repo->run( 'config', 'user.name' ) ) {
system( $repo->run( 'config', 'user.name', '"Automated Commit"' ) );
}
# Right.. initial commit time
{
open(my $foo, '>', "$ld/foo");
print $foo "First line\n";
close $foo;
$repo->run( add => 'foo' );
$repo->run( commit => '-m "First post!"' );
$repo->run( push => '--set-upstream', 'origin', 'master');
my @logs = $repo->run( log => '--pretty=oneline');
is(scalar @logs, 1, 'Initial commit successful');
my @o_logs = $repo->run( log => '--pretty=oneline', 'origin/master');
is(scalar @o_logs, 1, 'Initial commit push successful');
}
# And a second commit, for luck
{
open(my $foo, '>>', "$ld/foo");
print $foo "Second line\n";
close $foo;
$repo->run( add => 'foo' );
$repo->run( commit => '-m "Second post!"' );
$repo->run( 'push' );
$repo->run( 'pull' );
my @logs = $repo->run( log => '--pretty=oneline');
is(scalar @logs, 2, 'Second commit successful');
my @o_logs = $repo->run( log => '--pretty=oneline', 'origin/master');
is(@o_logs, 2, 'Second commit push successful');
}
# And on to testing the actual code...
sub update_foo {
my $str = shift;
open(my $foo, '>>', "$ld/foo");
if ($str) {
print $foo "$str\n";
}
else {
print $foo "Another line\n";
}
close $foo;
}
sub basic_data {
return (
msg => 'an update',
files => 'foo',
error => sub { shift; return shift },
);
}
{ # Test the param-testing code
update_foo();
my %no_msg = basic_data();
delete $no_msg{msg};
like(Git::Archive->commit(\%no_msg), qr/No commit message/, 'Correct msg error');
my %no_files = basic_data();
delete $no_files{files};
like(Git::Archive->commit(\%no_files), qr/No files specified/, 'Correct files error');
}
{ # Test the environment-checking
## Non-existent directory
my %bad_dir = basic_data();
$bad_dir{git_dir} = 'flibble';
like(Git::Archive->commit(\%bad_dir), qr/No such directory/, 'Correct git error');
## Non-git directory
%bad_dir = basic_data();
mkdir('t/nongit');
$bad_dir{git_dir} = 't/nongit';
like(Git::Archive->commit(\%bad_dir), qr/No \.git found/, 'Correct git error');
## File already staged
open(my $bar, '>>', "$ld/bar");
print $bar "A line\n";
close $bar;
my %data = basic_data();
$data{git_dir} = $ld;
my $repo = Git::Repository->new( work_tree => $ld );
$repo->run( add => 'bar' );
like(Git::Archive->commit(\%data), qr/Repo already has staged files/, 'Staged files');
$repo->run( commit => '-m "Commit bar"' );
}
# Seems all the right error checking works. Can we commit?
{ # Commit named file
update_foo();
my %data = basic_data();
$data{git_dir} = $ld;
is(Git::Archive->commit(\%data), 0, 'Committed file');
my @logs = $repo->run( log => '--pretty=oneline');
is(scalar @logs, 4, 'Commit successful');
}
( run in 1.050 second using v1.01-cache-2.11-cpan-9581c071862 )