5b4eac8b5bbf8c7f677980d04e33c59a33b96f07
[catagits/Gitalist.git] / t / 02git_object.t
1 use strict;
2 use warnings;
3 use FindBin qw/$Bin/;
4 use Test::More;
5 use Test::Exception;
6 use Data::Dumper;
7
8 use Path::Class;
9 use Gitalist::Git::Repository;
10 my $repository = Gitalist::Git::Repository->new(
11     dir("$Bin/lib/repositories/repo1"),
12 );
13
14 BEGIN {
15     use_ok 'Gitalist::Git::Object::Tree';
16     use_ok 'Gitalist::Git::Object::Blob';
17     use_ok 'Gitalist::Git::Object::Commit';
18     use_ok 'Gitalist::Git::Object::Tag';
19     }
20
21 my $object = Gitalist::Git::Object::Tree->new(
22     repository => $repository,
23     sha1 => '729a7c3f6ba5453b42d16a43692205f67fb23bc1',
24     type => 'tree',
25     file => 'dir1',
26     mode => 16384,
27 );
28 isa_ok($object, 'Gitalist::Git::Object::Tree', 'tree object');
29 is($object->sha1,'729a7c3f6ba5453b42d16a43692205f67fb23bc1', 'sha1 is correct');
30 is($object->type, 'tree', 'type is correct');
31 is($object->file, 'dir1', 'file is correct');
32 is($object->mode, 16384, 'mode is correct');
33 is($object->modestr, 'drwxr-xr-x', "modestr is correct" );
34 is($object->size, 33, "size is correct");
35
36 # Create object from sha1.
37 my $obj2 = Gitalist::Git::Object::Blob->new(
38     repository => $repository,
39     sha1 => '5716ca5987cbf97d6bb54920bea6adde242d87e6',
40 );
41 isa_ok($obj2, 'Gitalist::Git::Object::Blob', 'blob object');
42 is($obj2->sha1,'5716ca5987cbf97d6bb54920bea6adde242d87e6', 'sha1 is correct');
43 is($obj2->type, 'blob', 'type is correct');
44 is($obj2->mode, 0, 'mode is correct');
45 is($obj2->modestr, '----------', "modestr is correct" );
46 is($obj2->content, "bar\n", 'obj2 contents is correct');
47 is($obj2->size, 4, "size is correct");
48 dies_ok {
49     print $obj2->tree_sha1;
50 } 'tree_sha1 on a blob is an exception';
51 dies_ok {
52     print $obj2->comment;
53 } 'comment is an empty string';
54
55 my $commit_obj = Gitalist::Git::Object::Commit->new(
56     repository => $repository,
57     sha1 => '3f7567c7bdf7e7ebf410926493b92d398333116e',
58 );
59 isa_ok($commit_obj, 'Gitalist::Git::Object::Commit', "commit object");
60 my ($tree, $patch) = $commit_obj->diff(
61     patch => 1,
62 );
63 $patch = $patch->[0];
64 is($patch->{head}, 'diff --git a/file1 b/file1', 'patch->{head} is correct');
65 is($patch->{a}, 'a/file1', 'patch->{a} is correct');
66 is($patch->{b}, 'b/file1', 'patch->{b} is correct');
67 is($patch->{file}, 'file1', 'patch->{file} is correct');
68 is($patch->{mode}, '100644', 'patch->{mode} is correct');
69 is($patch->{src}, '257cc5642cb1a054f08cc83f2d943e56fd3ebe99', 'patch->{src} is correct');
70 is($patch->{index}, 'index 257cc5642cb1a054f08cc83f2d943e56fd3ebe99..5716ca5987cbf97d6bb54920bea6adde242d87e6 100644', 'patch->{index} is correct');
71 is($patch->{diff}, '--- a/file1
72 +++ b/file1
73 @@ -1 +1 @@
74 -foo
75 +bar
76 ', 'patch->{diff} is correct');
77 is($patch->{dst}, '5716ca5987cbf97d6bb54920bea6adde242d87e6', 'patch->{dst} is correct');
78
79 {
80     my $contents = do { local $/; my $fh = $commit_obj->get_patch; <$fh> };
81 ok(index($contents,
82 'From 3f7567c7bdf7e7ebf410926493b92d398333116e Mon Sep 17 00:00:00 2001
83 From: Florian Ragwitz <rafl@debian.org>
84 Date: Tue, 6 Mar 2007 20:39:45 +0100
85 Subject: [PATCH] bar
86
87 ---
88  file1 |    2 +-
89  1 files changed, 1 insertions(+), 1 deletions(-)
90
91 diff --git a/file1 b/file1
92 index 257cc56..5716ca5 100644
93 --- a/file1
94 +++ b/file1
95 @@ -1 +1 @@
96 -foo
97 +bar
98 --') == 0, 'commit_obj->get_patch can return a patch')
99     or warn("Got instead: $contents");
100 }
101
102 # Note - 2 patches = 3 parts due to where we split.
103 {
104     my $contents = do { local $/; my $fh = $commit_obj->get_patch(undef, 3); <$fh> };
105     my @bits = split /Subject: \[PATC/, $contents;
106     is(scalar(@bits), 3,
107         'commit_obj->get_patch can return a patchset')
108         or warn("Contents was $contents");
109 }
110 done_testing;
111