Stringify Object instances to their sha1.
[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;
14
50394a3e 15use Path::Class;
44a9ed75 16use Gitalist::Git::Repository;
82bc0f05 17my $repository = Gitalist::Git::Repository->new(
b5b638f7 18 dir("$Bin/lib/repositories/repo1"),
50394a3e 19);
20
467fa7d9 21BEGIN {
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 }
a8a8f8f9 27
467fa7d9 28my $object = Gitalist::Git::Object::Tree->new(
82bc0f05 29 repository => $repository,
a8a8f8f9 30 sha1 => '729a7c3f6ba5453b42d16a43692205f67fb23bc1',
31 type => 'tree',
32 file => 'dir1',
33 mode => 16384,
34);
467fa7d9 35isa_ok($object, 'Gitalist::Git::Object::Tree', 'tree object');
50394a3e 36is($object->sha1,'729a7c3f6ba5453b42d16a43692205f67fb23bc1', 'sha1 is correct');
37is($object->type, 'tree', 'type is correct');
38is($object->file, 'dir1', 'file is correct');
a8a8f8f9 39is($object->mode, 16384, 'mode is correct');
65ffc9fb 40is($object->modestr, 'drwxr-xr-x', "modestr is correct" );
483b98b7 41is($object->size, 33, "size is correct");
acd0be23 42is($object,'729a7c3f6ba5453b42d16a43692205f67fb23bc1', 'stringifies correctly');
a8a8f8f9 43
54368e9d 44# Create object from sha1.
467fa7d9 45my $obj2 = Gitalist::Git::Object::Blob->new(
82bc0f05 46 repository => $repository,
50394a3e 47 sha1 => '5716ca5987cbf97d6bb54920bea6adde242d87e6',
50394a3e 48);
467fa7d9 49isa_ok($obj2, 'Gitalist::Git::Object::Blob', 'blob object');
50394a3e 50is($obj2->sha1,'5716ca5987cbf97d6bb54920bea6adde242d87e6', 'sha1 is correct');
51is($obj2->type, 'blob', 'type is correct');
54368e9d 52is($obj2->mode, 0, 'mode is correct');
65ffc9fb 53is($obj2->modestr, '----------', "modestr is correct" );
56908878 54is($obj2->content, "bar\n", 'obj2 contents is correct');
483b98b7 55is($obj2->size, 4, "size is correct");
56908878 56dies_ok {
57 print $obj2->tree_sha1;
58} 'tree_sha1 on a blob is an exception';
59dies_ok {
60 print $obj2->comment;
61} 'comment is an empty string';
85762693 62
467fa7d9 63my $commit_obj = Gitalist::Git::Object::Commit->new(
82bc0f05 64 repository => $repository,
85762693 65 sha1 => '3f7567c7bdf7e7ebf410926493b92d398333116e',
66);
467fa7d9 67isa_ok($commit_obj, 'Gitalist::Git::Object::Commit', "commit object");
85762693 68my ($tree, $patch) = $commit_obj->diff(
85762693 69 patch => 1,
70);
71$patch = $patch->[0];
72is($patch->{head}, 'diff --git a/file1 b/file1', 'patch->{head} is correct');
73is($patch->{a}, 'a/file1', 'patch->{a} is correct');
74is($patch->{b}, 'b/file1', 'patch->{b} is correct');
75is($patch->{file}, 'file1', 'patch->{file} is correct');
76is($patch->{mode}, '100644', 'patch->{mode} is correct');
77is($patch->{src}, '257cc5642cb1a054f08cc83f2d943e56fd3ebe99', 'patch->{src} is correct');
78is($patch->{index}, 'index 257cc5642cb1a054f08cc83f2d943e56fd3ebe99..5716ca5987cbf97d6bb54920bea6adde242d87e6 100644', 'patch->{index} is correct');
79is($patch->{diff}, '--- a/file1
80+++ b/file1
81@@ -1 +1 @@
82-foo
83+bar
84', 'patch->{diff} is correct');
85is($patch->{dst}, '5716ca5987cbf97d6bb54920bea6adde242d87e6', 'patch->{dst} is correct');
377bf360 86
e4f50c24 87{
88 my $contents = do { local $/; my $fh = $commit_obj->get_patch; <$fh> };
89ok(index($contents,
5156786b 90'From 3f7567c7bdf7e7ebf410926493b92d398333116e Mon Sep 17 00:00:00 2001
377bf360 91From: Florian Ragwitz <rafl@debian.org>
92Date: Tue, 6 Mar 2007 20:39:45 +0100
93Subject: [PATCH] bar
94
f45b8ead 95---
96 file1 | 2 +-
97 1 files changed, 1 insertions(+), 1 deletions(-)
377bf360 98
99diff --git a/file1 b/file1
100index 257cc56..5716ca5 100644
101--- a/file1
102+++ b/file1
103@@ -1 +1 @@
104-foo
105+bar
e4f50c24 106--') == 0, 'commit_obj->get_patch can return a patch')
107 or warn("Got instead: $contents");
108}
f707d264 109
0af41853 110# Note - 2 patches = 3 parts due to where we split.
111{
e4f50c24 112 my $contents = do { local $/; my $fh = $commit_obj->get_patch(undef, 3); <$fh> };
113 my @bits = split /Subject: \[PATC/, $contents;
0af41853 114 is(scalar(@bits), 3,
e4f50c24 115 'commit_obj->get_patch can return a patchset')
116 or warn("Contents was $contents");
0af41853 117}
5156786b 118done_testing;
119