Add a test for the blobdiff action.
[catagits/Gitalist.git] / t / model_Git.t
CommitLineData
4805c465 1use strict;
2use warnings;
6dca83ef 3use FindBin qw/$Bin/;
43ca94af 4use Test::More;
4805c465 5
6BEGIN { use_ok 'Gitalist::Model::Git' }
7
da5fab33 8use Git::PurePerl;
0caa4dd2 9my $m = Git::Repos->new({ repo_dir => "$Bin/lib/repositories" });
43ca94af 10isa_ok($m, 'Git::Repos');
4805c465 11
12# 'bare.git' is a bare git repository in the repository dir
13use Path::Class;
14my $repoBare = Path::Class::Dir->new('t/lib/repositories/bare.git');
a88a2b6d 15ok( $m->is_git_repo( $repoBare ), 'is_git_repo true for bare git repo' );
4805c465 16
17# 'working' is a working copy w/ git repo in the repository dir
18my $repoWorking = Path::Class::Dir->new('t/lib/repositories/working');
a88a2b6d 19#ok( $m->is_git_repo( $repoWorking ), 'is_git_repo true for git repo in working copy' );
4805c465 20
21# 'empty.git' is an empty directory in the repository dir
22my $repoEmpty = Path::Class::Dir->new('t/lib/repositories/empty.git');
a88a2b6d 23ok( ! $m->is_git_repo( $repoEmpty ), 'is_git_repo is false for empty dir' );
4805c465 24
27e05d7b 25my $projectList = $m->list_projects('t/lib/repositories');
a88a2b6d 26ok( scalar @{$projectList} == 2, 'list_projects returns an array with the correct number of members' );
27e05d7b 27is( $projectList->[0]->{name}, 'bare.git', 'list_projects has correct name for "bare.git" repo' );
0e2b6322 28#ok( $projectList->[1]->{name} eq 'working/.git', 'list_projects has correct name for "working" repo' );
4805c465 29
4805c465 30
a88a2b6d 31# Liberally borrowed from rafl's gitweb
da5fab33 32$m->project('repo1');
33is($m->project, 'repo1', 'model project correct');
34my $pd = $m->project_dir($m->project);
e983a32e 35isa_ok($pd, 'Path::Class::Dir', 'model project_dir');
da5fab33 36is($pd, $m->repo_dir . '/' . $m->project, 'model project_dir correct');
37ok( $m->gpp(Git::PurePerl->new( gitdir => $pd, directory => $pd )), 'model gpp set ok' );
38like($m->head_hash('HEAD'), qr/^([0-9a-fA-F]{40})$/, 'head_hash');
a88a2b6d 39
40{
da5fab33 41 my @tree = $m->list_tree('3bc0634310b9c62222bb0e724c11ffdfb297b4ac');
89a18cae 42 is(scalar @tree, 1, "tree array contains one entry.");
a88a2b6d 43 is_deeply($tree[0], {
89a18cae 44 mode => oct 100644,
45 modestr => '-rw-r--r--',
46 type => 'blob',
47 object => '257cc5642cb1a054f08cc83f2d943e56fd3ebe99',
48 file => 'file1'
a88a2b6d 49 });
50
51 is($m->get_object_mode_string($tree[0]), '-rw-r--r--');
52}
53
da5fab33 54is($m->get_object_type('729a7c3f6ba5453b42d16a43692205f67fb23bc1'), 'tree');
55is($m->get_object_type('257cc5642cb1a054f08cc83f2d943e56fd3ebe99'), 'blob');
56is($m->get_object_type('5716ca5987cbf97d6bb54920bea6adde242d87e6'), 'blob');
57
58is($m->cat_file('257cc5642cb1a054f08cc83f2d943e56fd3ebe99'), "foo\n");
59is($m->cat_file('5716ca5987cbf97d6bb54920bea6adde242d87e6'), "bar\n");
60
61
62my $commit = $m->get_object('3f7567c7bdf7e7ebf410926493b92d398333116e');
63isa_ok($commit, 'Git::PurePerl::Object::Commit', "commit object type correct");
64my ($tree, $patch) = $m->diff(
65 commit => $commit,
66 parent => '',
67 file => '',
68 patch => 1,
69);
70$patch = $patch->[0];
71is($patch->{head}, 'diff --git a/file1 b/file1', 'patch->{head} is correct');
72is($patch->{a}, 'a/file1', 'patch->{a} is correct');
73is($patch->{b}, 'b/file1', 'patch->{b} is correct');
74is($patch->{file}, 'file1', 'patch->{file} is correct');
75is($patch->{mode}, '100644', 'patch->{mode} is correct');
76is($patch->{src}, '257cc5642cb1a054f08cc83f2d943e56fd3ebe99', 'patch->{src} is correct');
77is($patch->{index}, 'index 257cc5642cb1a054f08cc83f2d943e56fd3ebe99..5716ca5987cbf97d6bb54920bea6adde242d87e6 100644', 'patch->{index} is correct');
78is($patch->{diff}, '--- a/file1
a88a2b6d 79+++ b/file1
80@@ -1 +1 @@
81-foo
82+bar
da5fab33 83', 'patch->{diff} is correct');
84is($patch->{dst}, '5716ca5987cbf97d6bb54920bea6adde242d87e6', 'patch->{dst} is correct');
89a18cae 85
43ca94af 86done_testing;
87