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