84ac439fd591a84ef6e238b7598021a87c1c4011
[catagits/Gitalist.git] / t / 02git_object.t
1 use FindBin qw/$Bin/;
2 BEGIN {
3     my $env = "$FindBin::Bin/../script/env";
4     if (-r $env) {
5         do $env or die $@;
6     }
7 }
8
9 use strict;
10 use warnings;
11 use Test::More;
12 use Test::Exception;
13 use Data::Dumper;
14
15 use Path::Class;
16 use Gitalist::Git::Repository;
17 my $repository = Gitalist::Git::Repository->new(
18     dir("$Bin/lib/repositories/repo1"),
19 );
20
21 BEGIN {
22     use_ok 'Gitalist::Git::Object::Tree';
23     use_ok 'Gitalist::Git::Object::Blob';
24     use_ok 'Gitalist::Git::Object::Commit';
25     use_ok 'Gitalist::Git::Object::Tag';
26     }
27
28 my $object = Gitalist::Git::Object::Tree->new(
29     repository => $repository,
30     sha1 => '729a7c3f6ba5453b42d16a43692205f67fb23bc1',
31     type => 'tree',
32     file => 'dir1',
33     mode => 16384,
34 );
35 isa_ok($object, 'Gitalist::Git::Object::Tree', 'tree object');
36 is($object->sha1,'729a7c3f6ba5453b42d16a43692205f67fb23bc1', 'sha1 is correct');
37 is($object->type, 'tree', 'type is correct');
38 is($object->file, 'dir1', 'file is correct');
39 is($object->mode, 16384, 'mode is correct');
40 is($object->modestr, 'drwxr-xr-x', "modestr is correct" );
41 is($object->size, 33, "size is correct");
42 is($object,'729a7c3f6ba5453b42d16a43692205f67fb23bc1', 'stringifies correctly');
43
44 is_deeply $object->pack, {
45     __CLASS__
46          => 'Gitalist::Git::Object::Tree',
47     file   => 'dir1',
48     mode   => 16384,
49     modestr
50          => 'drwxr-xr-x',
51     repository
52          => {
53              __CLASS__   => 'Gitalist::Git::Repository',
54              description => 'some test repository',
55              is_bare     => 1,
56              last_change => '2009-11-12T19:00:34Z',
57              name        => 'repo1',
58              owner       => 'Dan'
59          },
60     sha1   => '729a7c3f6ba5453b42d16a43692205f67fb23bc1',
61     size   => 33,
62     type   => 'tree'
63 }, 'Serialized tree correctly';
64
65 # Create object from sha1.
66 my $obj2 = Gitalist::Git::Object::Blob->new(
67     repository => $repository,
68     sha1 => '5716ca5987cbf97d6bb54920bea6adde242d87e6',
69 );
70 isa_ok($obj2, 'Gitalist::Git::Object::Blob', 'blob object');
71 is($obj2->sha1,'5716ca5987cbf97d6bb54920bea6adde242d87e6', 'sha1 is correct');
72 is($obj2->type, 'blob', 'type is correct');
73 is($obj2->mode, 0, 'mode is correct');
74 is($obj2->modestr, '----------', "modestr is correct" );
75 is($obj2->content, "bar\n", 'obj2 contents is correct');
76 is($obj2->size, 4, "size is correct");
77 dies_ok {
78     print $obj2->tree_sha1;
79 } 'tree_sha1 on a blob is an exception';
80 dies_ok {
81     print $obj2->comment;
82 } 'comment is an empty string';
83
84 is_deeply $obj2->pack,  {
85     __CLASS__
86          => 'Gitalist::Git::Object::Blob',
87     mode   => 0,
88     modestr
89          => '----------',
90     repository
91          => {
92              __CLASS__   => 'Gitalist::Git::Repository',
93              description => 'some test repository',
94              is_bare     => 1,
95              last_change => '2009-11-12T19:00:34Z',
96              name        => 'repo1',
97              owner       => 'Dan'
98          },
99     sha1   => '5716ca5987cbf97d6bb54920bea6adde242d87e6',
100     size   => 4,
101     type   => 'blob'
102 }, 'Serialized blob correctly';
103
104 my $commit_obj = Gitalist::Git::Object::Commit->new(
105     repository => $repository,
106     sha1 => '3f7567c7bdf7e7ebf410926493b92d398333116e',
107 );
108 isa_ok($commit_obj, 'Gitalist::Git::Object::Commit', "commit object");
109 isa_ok($commit_obj->tree->[0], 'Gitalist::Git::Object::Tree');
110
111 is_deeply $commit_obj->pack,  {
112     __CLASS__
113          => 'Gitalist::Git::Object::Commit',
114     mode   => 0,
115     modestr
116          => '----------',
117     repository
118          => {
119              __CLASS__   => 'Gitalist::Git::Repository',
120              description => 'some test repository',
121              is_bare     => 1,
122              last_change => '2009-11-12T19:00:34Z',
123              name        => 'repo1',
124              owner       => 'Dan'
125          },
126     sha1   => '3f7567c7bdf7e7ebf410926493b92d398333116e',
127     size   => 218,
128     tree   => [ {
129         __CLASS__
130              => 'Gitalist::Git::Object::Tree',
131         mode   => 0,
132         modestr
133              => '----------',
134         repository
135              => {
136                  __CLASS__   => 'Gitalist::Git::Repository',
137                  description => 'some test repository',
138                  is_bare     => 1,
139                  last_change => '2009-11-12T19:00:34Z',
140                  name        => 'repo1',
141                  owner       => 'Dan'
142              },
143         sha1   => '9062594aebb5df0de7fb92413f17a9eced196c22',
144         size   => 33,
145         type   => 'tree'
146     } ],
147     type   => 'commit'
148 }, 'Serialized commit correctly';
149
150 my ($tree, $patch) = $commit_obj->diff(
151     patch => 1,
152 );
153 $patch = $patch->[0];
154 is($patch->{head}, 'diff --git a/file1 b/file1', 'patch->{head} is correct');
155 is($patch->{a}, 'a/file1', 'patch->{a} is correct');
156 is($patch->{b}, 'b/file1', 'patch->{b} is correct');
157 is($patch->{file}, 'file1', 'patch->{file} is correct');
158 is($patch->{mode}, '100644', 'patch->{mode} is correct');
159 is($patch->{src}, '257cc5642cb1a054f08cc83f2d943e56fd3ebe99', 'patch->{src} is correct');
160 is($patch->{index}, 'index 257cc5642cb1a054f08cc83f2d943e56fd3ebe99..5716ca5987cbf97d6bb54920bea6adde242d87e6 100644', 'patch->{index} is correct');
161 is($patch->{diff}, '--- a/file1
162 +++ b/file1
163 @@ -1 +1 @@
164 -foo
165 +bar
166 ', 'patch->{diff} is correct');
167 is($patch->{dst}, '5716ca5987cbf97d6bb54920bea6adde242d87e6', 'patch->{dst} is correct');
168
169 {
170     my $contents = do { local $/; my $fh = $commit_obj->get_patch; <$fh> };
171 ok(index($contents,
172 'From 3f7567c7bdf7e7ebf410926493b92d398333116e Mon Sep 17 00:00:00 2001
173 From: Florian Ragwitz <rafl@debian.org>
174 Date: Tue, 6 Mar 2007 20:39:45 +0100
175 Subject: [PATCH] bar
176
177 ---
178  file1 |    2 +-
179  1 files changed, 1 insertions(+), 1 deletions(-)
180
181 diff --git a/file1 b/file1
182 index 257cc56..5716ca5 100644
183 --- a/file1
184 +++ b/file1
185 @@ -1 +1 @@
186 -foo
187 +bar
188 --') == 0, 'commit_obj->get_patch can return a patch')
189     or warn("Got instead: $contents");
190 }
191
192 # Note - 2 patches = 3 parts due to where we split.
193 {
194     my $contents = do { local $/; my $fh = $commit_obj->get_patch(undef, 3); <$fh> };
195     my @bits = split /Subject: \[PATC/, $contents;
196     is(scalar(@bits), 3,
197         'commit_obj->get_patch can return a patchset')
198         or warn("Contents was $contents");
199 }
200 done_testing;
201