Commit | Line | Data |
cce03a80 |
1 | use FindBin qw/$Bin/; |
df629266 |
2 | BEGIN { |
0556ab26 |
3 | my $env = "$FindBin::Bin/../script/env"; |
df629266 |
4 | if (-r $env) { |
5 | do $env or die $@; |
6 | } |
7 | } |
8 | |
a8a8f8f9 |
9 | use strict; |
10 | use warnings; |
5156786b |
11 | use Test::More; |
56908878 |
12 | use Test::Exception; |
a8a8f8f9 |
13 | use Data::Dumper; |
d3f6e521 |
14 | use Test::Deep; |
a8a8f8f9 |
15 | |
50394a3e |
16 | use Path::Class; |
44a9ed75 |
17 | use Gitalist::Git::Repository; |
82bc0f05 |
18 | my $repository = Gitalist::Git::Repository->new( |
b5b638f7 |
19 | dir("$Bin/lib/repositories/repo1"), |
50394a3e |
20 | ); |
21 | |
467fa7d9 |
22 | BEGIN { |
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 |
29 | my $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 |
36 | isa_ok($object, 'Gitalist::Git::Object::Tree', 'tree object'); |
50394a3e |
37 | is($object->sha1,'729a7c3f6ba5453b42d16a43692205f67fb23bc1', 'sha1 is correct'); |
38 | is($object->type, 'tree', 'type is correct'); |
39 | is($object->file, 'dir1', 'file is correct'); |
a8a8f8f9 |
40 | is($object->mode, 16384, 'mode is correct'); |
65ffc9fb |
41 | is($object->modestr, 'drwxr-xr-x', "modestr is correct" ); |
483b98b7 |
42 | is($object->size, 33, "size is correct"); |
acd0be23 |
43 | is($object,'729a7c3f6ba5453b42d16a43692205f67fb23bc1', 'stringifies correctly'); |
a8a8f8f9 |
44 | |
d3f6e521 |
45 | cmp_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 |
67 | my $obj2 = Gitalist::Git::Object::Blob->new( |
82bc0f05 |
68 | repository => $repository, |
50394a3e |
69 | sha1 => '5716ca5987cbf97d6bb54920bea6adde242d87e6', |
50394a3e |
70 | ); |
467fa7d9 |
71 | isa_ok($obj2, 'Gitalist::Git::Object::Blob', 'blob object'); |
50394a3e |
72 | is($obj2->sha1,'5716ca5987cbf97d6bb54920bea6adde242d87e6', 'sha1 is correct'); |
73 | is($obj2->type, 'blob', 'type is correct'); |
54368e9d |
74 | is($obj2->mode, 0, 'mode is correct'); |
65ffc9fb |
75 | is($obj2->modestr, '----------', "modestr is correct" ); |
56908878 |
76 | is($obj2->content, "bar\n", 'obj2 contents is correct'); |
483b98b7 |
77 | is($obj2->size, 4, "size is correct"); |
56908878 |
78 | dies_ok { |
79 | print $obj2->tree_sha1; |
80 | } 'tree_sha1 on a blob is an exception'; |
81 | dies_ok { |
82 | print $obj2->comment; |
83 | } 'comment is an empty string'; |
85762693 |
84 | |
d3f6e521 |
85 | cmp_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 |
105 | my $commit_obj = Gitalist::Git::Object::Commit->new( |
82bc0f05 |
106 | repository => $repository, |
85762693 |
107 | sha1 => '3f7567c7bdf7e7ebf410926493b92d398333116e', |
108 | ); |
467fa7d9 |
109 | isa_ok($commit_obj, 'Gitalist::Git::Object::Commit', "commit object"); |
220ff256 |
110 | isa_ok($commit_obj->tree->[0], 'Gitalist::Git::Object::Tree'); |
1aae440e |
111 | |
d3f6e521 |
112 | cmp_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 |
151 | my ($tree, $patch) = $commit_obj->diff( |
85762693 |
152 | patch => 1, |
153 | ); |
154 | $patch = $patch->[0]; |
155 | is($patch->{head}, 'diff --git a/file1 b/file1', 'patch->{head} is correct'); |
156 | is($patch->{a}, 'a/file1', 'patch->{a} is correct'); |
157 | is($patch->{b}, 'b/file1', 'patch->{b} is correct'); |
158 | is($patch->{file}, 'file1', 'patch->{file} is correct'); |
159 | is($patch->{mode}, '100644', 'patch->{mode} is correct'); |
160 | is($patch->{src}, '257cc5642cb1a054f08cc83f2d943e56fd3ebe99', 'patch->{src} is correct'); |
161 | is($patch->{index}, 'index 257cc5642cb1a054f08cc83f2d943e56fd3ebe99..5716ca5987cbf97d6bb54920bea6adde242d87e6 100644', 'patch->{index} is correct'); |
162 | is($patch->{diff}, '--- a/file1 |
163 | +++ b/file1 |
164 | @@ -1 +1 @@ |
165 | -foo |
166 | +bar |
167 | ', 'patch->{diff} is correct'); |
168 | is($patch->{dst}, '5716ca5987cbf97d6bb54920bea6adde242d87e6', 'patch->{dst} is correct'); |
377bf360 |
169 | |
e4f50c24 |
170 | { |
171 | my $contents = do { local $/; my $fh = $commit_obj->get_patch; <$fh> }; |
a52c5736 |
172 | like $contents, qr{^\QFrom 3f7567c7bdf7e7ebf410926493b92d398333116e Mon Sep 17 00:00:00 2001 |
173 | From: Florian Ragwitz <\Erafl[@]debian\Q.org> |
377bf360 |
174 | Date: Tue, 6 Mar 2007 20:39:45 +0100 |
175 | Subject: [PATCH] bar |
176 | |
f45b8ead |
177 | --- |
a52c5736 |
178 | file1 |\E\s+2\Q +- |
179 | 1 \Efiles?\Q changed, 1 \Einsertions?\Q(+), 1 \Edeletions?\Q(-) |
377bf360 |
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 |
a52c5736 |
188 | --}, 'commit_obj->get_patch can return a patch'; |
e4f50c24 |
189 | } |
f707d264 |
190 | |
0af41853 |
191 | # Note - 2 patches = 3 parts due to where we split. |
192 | { |
e4f50c24 |
193 | my $contents = do { local $/; my $fh = $commit_obj->get_patch(undef, 3); <$fh> }; |
194 | my @bits = split /Subject: \[PATC/, $contents; |
0af41853 |
195 | is(scalar(@bits), 3, |
e4f50c24 |
196 | 'commit_obj->get_patch can return a patchset') |
197 | or warn("Contents was $contents"); |
0af41853 |
198 | } |
09717a40 |
199 | |
200 | my $blame_this = Gitalist::Git::Object::Commit->new( |
201 | repository => $repository, |
202 | sha1 => 'd6ddf8b26be63066e01d96a0922c87cd8d6e2270', |
203 | ); |
204 | |
205 | { |
206 | local $SIG{ALRM} = sub { die "Regressions suck!" }; |
207 | alarm 1; |
208 | eval { $blame_this->blame('empty-for-a-reason', $blame_this->sha1) }; |
209 | is $@, '', "Silly infinite loop didn't manifest for an empty file."; |
210 | } |
211 | |
5156786b |
212 | done_testing; |
213 | |
d3f6e521 |
214 | sub is_system_account_name { |
215 | my $name = shift; |
216 | return 0 if !$name; |
217 | return 1; |
218 | } |