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