gitweb: use new Git::Repo API, and add optional caching
[git/gitweb-caching.git] / t / t9710 / test.pl
blob8fca725412fc5b3f890973f6caea6401f6169695
1 #!/usr/bin/perl
2 use lib (split(/:/, $ENV{GITPERLLIB}));
4 use warnings;
5 use strict;
7 use Test::More qw(no_plan);
8 use Cwd;
9 use File::Basename;
10 use File::Temp;
12 BEGIN { use_ok('Git::Repo') }
14 sub dies_ok (&;$) {
15 my ($coderef, $descr) = @_;
16 eval { $coderef->(); };
17 ok($@, $descr);
20 sub lives_ok (&;$) {
21 my ($coderef, $descr) = @_;
22 eval { $coderef->(); };
23 ok(!$@, $descr);
26 our $old_stderr;
27 sub discard_stderr {
28 open our $old_stderr, ">&", STDERR or die "cannot save STDERR";
29 close STDERR;
31 sub restore_stderr {
32 open STDERR, ">&", $old_stderr or die "cannot restore STDERR";
35 # set up
36 our $abs_wc_dir = Cwd->cwd;
37 ok(our $r = Git::Repo->new(repo_dir => "./.git"), 'open repository');
38 sub rev_parse {
39 my $name = shift;
40 chomp(my $sha1 = `git rev-parse $name 2> /dev/null`);
41 $sha1 or undef;
44 my @revisions = split /\s/, `git-rev-list --first-parent HEAD`;
45 my $head = $revisions[0];
47 # get_sha1
48 is($r->get_sha1('HEAD'), $head, 'get_sha1: scalar');
49 is($r->get_sha1('HEAD'), $head, 'get_sha1: scalar, repeated');
50 my($sha1, $type, $head_size) = $r->get_sha1('HEAD');
51 is($sha1, $head, 'get_sha1: array (SHA1)');
52 is($type, 'commit', 'get_sha1: array (commit)');
53 ok($head_size > 0, 'get_sha1: array (size)');
55 # get_object
56 is_deeply([$r->get_object($r->get_sha1("$revisions[-1]:file1"))], ['blob', "test file 1\n"], 'get_object: blob');
57 is_deeply([$r->get_object($r->get_sha1("$revisions[-1]:file1"))], ['blob', "test file 1\n"], 'get_object: blob, repeated');
58 dies_ok { $r->get_object('0' x 40) } 'get_object: non-existent sha1';
60 # get_commit
61 isa_ok($r->get_commit($revisions[-1]), 'Git::Commit',
62 'get_commit: returns Git::Commit object');
64 # get_tag
65 isa_ok($r->get_tag($r->get_sha1('tag-object-1')), 'Git::Tag',
66 'get_tag: returns Git::Tag object');
68 # name_rev
69 is($r->name_rev($revisions[-2]), 'branch-2', 'name_rev: branch');
70 is($r->name_rev($head, 1), undef, 'name_rev: branch, tags only');
71 is($r->name_rev($revisions[-1]), 'tags/tag-object-1^0', 'name_rev: tag object');
72 is($r->name_rev($revisions[-1], 1), 'tag-object-1^0', 'name_rev: tag object, tags only');
76 # Git::Commmit
77 print "# Git::Commit:\n";
79 BEGIN { use_ok('Git::Commit') }
81 my $invalid_commit = Git::Commit->new($r, '0' x 40);
82 is($invalid_commit->sha1, '0' x 40, 'new, sha1: accept invalid SHA1');
83 dies_ok { $invalid_commit->tree } 'die on accessing properties of invalid SHA1s';
85 $invalid_commit = Git::Commit->new($r, $r->get_sha1('HEAD:')); # tree, not commit
86 dies_ok { $invalid_commit->tree } 'die on accessing properties of non-commit objects';
88 my $c = Git::Commit->new($r, $revisions[-2]);
89 is($c->repo, $r, 'repo: basic');
90 is($c->sha1, $revisions[-2], 'sha1: basic');
91 is($c->{parents}, undef, 'lazy loading: not loaded after reading SHA1');
92 is($c->tree, $r->get_sha1("$revisions[-2]:"), 'tree: basic');
93 ok($c->{parents}, 'lazy loading: loaded after reading tree');
94 is_deeply([$c->parents], [$revisions[-1]], 'parents: basic');
95 like($c->author, qr/A U Thor <author\@example.com> [0-9]+ \+0000/, 'author: basic');
96 like($c->committer, qr/C O Mitter <committer\@example.com> [0-9]+ \+0000/, 'committer: basic');
97 is($c->encoding, undef, 'encoding: undef');
98 is($c->message, "second commit\n", 'message: basic');
99 is($c, $c->sha1, 'stringify: basic');
101 # error handling
102 dies_ok { Git::Commit->new($r, $r->get_sha1('tag-object-3'))->_load }
103 'new: pass tag SHA1 (dies)';
104 dies_ok { Git::Commit->new($r, '0' x 40)->_load }
105 'new: pass invalid SHA1 (dies)';
108 # Git::Tag
109 print "# Git::Tag:\n";
111 BEGIN { use_ok('Git::Tag') }
113 # We don't test functionality inherited from Git::Object that we
114 # already tested in the Git::Commit tests.
116 my $t = Git::Tag->new($r, $r->get_sha1('tag-object-1'));
117 is($t->tag, 'tag-object-1', 'tag: basic');
118 is($t->object, $revisions[-1], 'object: basic');
119 is($t->type, 'commit', 'tag: type');
120 like($t->tagger, qr/C O Mitter <committer\@example.com> [0-9]+ \+0000/, 'tagger: basic');
121 is($t->encoding, undef, 'encoding: undef');
122 is($t->message, "tag message 1\n", 'message: basic');
124 # error handling
125 dies_ok { Git::Tag->new($r, $head)->_load } 'new: pass commit SHA1 (dies)';
126 dies_ok { Git::Tag->new($r, '0' x 40)->_load } 'new: pass invalid SHA1 (dies)';
129 # Git::RepoRoot
130 print "# Git::RepoRoot:\n";
132 BEGIN { use_ok('Git::RepoRoot'); }
134 my $reporoot = Git::RepoRoot->new(root_dir => $abs_wc_dir);
135 is($reporoot->repo(repo_dir => '.git')->get_sha1('HEAD'), $head,
136 'repo: basic');