Skip to content

Commit

Permalink
Git interaction abstraction + tests - closes KSP-CKAN#10 and relates to
Browse files Browse the repository at this point in the history
  • Loading branch information
techman83 committed Jun 28, 2015
1 parent 39f5a2f commit dd3ff32
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 30 deletions.
63 changes: 56 additions & 7 deletions lib/App/KSP_CKAN/Git.pm
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ has 'local' => ( is => 'ro', required => 1 );
has 'working' => ( is => 'ro', lazy => 1, builder => 1 );
has 'clean' => ( is => 'ro', default => sub { 0 } );
has 'shallow' => ( is => 'ro', default => sub { 1 } );
has 'branch' => ( is => 'rw', lazy => 1, builder => 1 );
has '_git' => ( is => 'rw', isa => sub { "Git::Wrapper" }, lazy => 1, builder => 1 );

method _build__git {
Expand Down Expand Up @@ -113,6 +114,11 @@ method _clean {
return;
}

method _build_branch {
my @parse = $self->_git->rev_parse(qw|--abbrev-ref HEAD|);
return $parse[0];
}

=method add_all
$git->add;
Expand All @@ -122,27 +128,37 @@ This method will perform a 'git add -A'
=cut

# TODO: It'd probably be nice to allow a list of
# files instead
# files
method add {
$self->_git->add({ A => 1 });
$self->_git->add(".");
return;
}

=method changed
my @changed = $git->changed;
Will return a list of changed files. Can be used in scalar
context or an if block.
Will return a list of changed files when compared to
origin/current_branch. Can be used in scalar context
(number of commited files) or an if block.
if ($git->changed) {
say "We've got changed files!";
}
Takes an optional bool parameter of 'origin' if you want
a list of comparing local.
my @local = $git->changed( origin => 0 );
=cut

method changed {
return $self->_git->diff({ 'name-only' => 1, }, qw|--stat origin/master| );
method changed(:$origin = 1) {
if ( $origin ) {
return $self->_git->diff({ 'name-only' => 1, }, "--stat", "origin/".$self->branch );
} else {
return $self->_git->diff({ 'name-only' => 1, });
}
}

=method commit
Expand Down Expand Up @@ -175,8 +191,41 @@ method commit(:$all = 0, :$file = 0, :$message = "Generic Commit") {
if ($all || ! $file) {
return $self->_git->commit({ a => 1 }, "-m $message");
} else {
return $self->_git->commit("$file -m \"$message\"");
return $self->_git->commit($file, "-m \"$message\"");
}
}

=method push
$git->push;
Will push the local branch to origin/branh.
=cut

method push {
return $self->_git->push("origin",$self->branch);
}

=method pull
$git->pull;
Performs a git pull. Takes optional bool arguments of
'ours' and 'theirs' which will tell git who wins when
merge conflicts arise.
=cut

method pull(:$ours?,:$theirs?) {
if ($theirs) {
$self->_git->pull(qw|-X theirs|);
} elsif ($ours) {
$self->_git->pull(qw|-X ours|);
} else {
$self->_git->pull;
}
return;
}

1;
93 changes: 70 additions & 23 deletions t/App/KSP_CKAN/Git.t
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use Test::Warnings;
use File::Path qw(remove_tree mkpath);
use File::chdir;
use File::Spec 'tmpdir';
use File::Copy::Recursive 'dircopy';
use File::Copy::Recursive qw(dircopy dirmove);

# Setup our test environment
our $tmp = File::Spec->tmpdir();
Expand All @@ -19,24 +19,30 @@ dircopy("t/data", "$testpath/data");
system("git", "init");
system("git", "add", "-A");
system("git", "commit", "-a", "-m", "Commit ALL THE THINGS!");
chdir("../");
dirmove("CKAN-meta", "CKAN-meta-tmp");
system("git", "clone", "--bare", "CKAN-meta-tmp", "CKAN-meta");

}

use_ok("App::KSP_CKAN::Git");

# Test we can get our working directory
my @test_git = (
"$testpath/data/CKAN-meta",
'git@github.com:techman83/CKAN-meta.git',
'https://github.com/techman83/CKAN-meta.git',
);

foreach my $working (@test_git) {
my $git = App::KSP_CKAN::Git->new(
remote => $working,
local => $testpath,
subtest 'Working Dir Parsing' => sub {
my @test_git = (
"$testpath/data/CKAN-meta",
'git@github.com:techman83/CKAN-meta.git',
'https://github.com/techman83/CKAN-meta.git',
);

is($git->working, 'CKAN-meta', "'CKAN-meta' parsed from $working");

foreach my $working (@test_git) {
my $git = App::KSP_CKAN::Git->new(
remote => $working,
local => $testpath,
);

is($git->working, 'CKAN-meta', "'CKAN-meta' parsed from $working");
};
};

# Test our instantiation
Expand All @@ -61,23 +67,64 @@ print $in "{\n}";
close $in;

# Test adding
is($git->changed, 0, "No files were added");
is($git->changed, 0, "No file was added");
$git->add;
is($git->changed, 1, "Files were added");
is($git->changed, 1, "File was added");

# Test Committing a single file
subtest 'Committing' => sub {
my @files = $git->changed;
$git->commit(file => $files[0]);
is($git->changed(origin => 0), 0, "Commit successful");
is($git->changed, 1, "Commit not yet pushed");
$git->push;
is($git->changed, 0, "Commit pushed");

# Test committing all files
for my $filename (qw(test_file2.ckan test_file3.ckan)) {
open my $in, '>', "$testpath/CKAN-meta/$filename";
print $in "{\n}";
close $in;
}
$git->add;
is($git->changed, 2, "Files were added");
$git->commit(all => 1, message => "All the comitting");
is($git->changed(origin => 0), 0, "Commit successful");
$git->push;
is($git->changed, 0, "Commit pushed");
};

# Pull tests
# TODO: Expand these
my $pull = App::KSP_CKAN::Git->new(
remote => "$testpath/data/CKAN-meta",
working => "CKAN-meta-pull",
local => $testpath,
clean => 1,
);
$pull->pull;
{
local $CWD = "$testpath/CKAN-meta-pull";
open my $in, '>', "$testpath/CKAN-meta-pull/test_pull.ckan";
print $in "{\n}";
close $in;
}
$pull->add;
$pull->commit(all => 1);
$pull->push;
$git->pull;
is(-e "$testpath/CKAN-meta/test_pull.ckan", 1, "Pull successful");

# TODO: This is broken
my @files = $git->changed;
$git->commit( all => 1);
is($git->changed, 0, "Commit successful");
# Test accidental deletes
unlink("$testpath/CKAN-meta/test_file.ckan");
$git->add;
is($git->changed, 1, "File delete not commited");

# Cleanup after ourselves
{
if ( -d $testpath ) {
remove_tree($testpath);
}
if ( -d $testpath ) {
remove_tree($testpath);
}


done_testing();
__END__

0 comments on commit dd3ff32

Please sign in to comment.