1#!/usr/bin/perl
2use lib (split(/:/, $ENV{GITPERLLIB}));
3
4use 5.008;
5use warnings;
6use strict;
7
8use Test::More qw(no_plan);
9
10BEGIN {
11 # t9700-perl-git.sh kicks off our testing, so we have to go from
12 # there.
13 Test::More->builder->current_test(1);
14 Test::More->builder->no_ending(1);
15}
16
17use Cwd;
18use File::Basename;
19
20BEGIN { use_ok('Git') }
21
22# set up
23our $abs_repo_dir = cwd();
24ok(our $r = Git->repository(Directory => "."), "open repository");
25
26# config
27is($r->config("test.string"), "value", "config scalar: string");
28is_deeply([$r->config("test.dupstring")], ["value1", "value2"],
29 "config array: string");
30is($r->config("test.nonexistent"), undef, "config scalar: nonexistent");
31is_deeply([$r->config("test.nonexistent")], [], "config array: nonexistent");
32is($r->config_int("test.int"), 2048, "config_int: integer");
33is($r->config_int("test.nonexistent"), undef, "config_int: nonexistent");
34ok($r->config_bool("test.booltrue"), "config_bool: true");
35ok(!$r->config_bool("test.boolfalse"), "config_bool: false");
36our $ansi_green = "\x1b[32m";
37is($r->get_color("color.test.slot1", "red"), $ansi_green, "get_color");
38# Cannot test $r->get_colorbool("color.foo")) because we do not
39# control whether our STDOUT is a terminal.
40
41# Failure cases for config:
42# Save and restore STDERR; we will probably extract this into a
43# "dies_ok" method and possibly move the STDERR handling to Git.pm.
44open our $tmpstderr, ">&STDERR" or die "cannot save STDERR"; close STDERR;
45eval { $r->config("test.dupstring") };
46ok($@, "config: duplicate entry in scalar context fails");
47eval { $r->config_bool("test.boolother") };
48ok($@, "config_bool: non-boolean values fail");
49open STDERR, ">&", $tmpstderr or die "cannot restore STDERR";
50
51# ident
52like($r->ident("aUthor"), qr/^A U Thor <author\@example.com> [0-9]+ \+0000$/,
53 "ident scalar: author (type)");
54like($r->ident("cOmmitter"), qr/^C O Mitter <committer\@example.com> [0-9]+ \+0000$/,
55 "ident scalar: committer (type)");
56is($r->ident("invalid"), "invalid", "ident scalar: invalid ident string (no parsing)");
57my ($name, $email, $time_tz) = $r->ident('author');
58is_deeply([$name, $email], ["A U Thor", "author\@example.com"],
59 "ident array: author");
60like($time_tz, qr/[0-9]+ \+0000/, "ident array: author");
61is_deeply([$r->ident("Name <email> 123 +0000")], ["Name", "email", "123 +0000"],
62 "ident array: ident string");
63is_deeply([$r->ident("invalid")], [], "ident array: invalid ident string");
64
65# ident_person
66is($r->ident_person("aUthor"), "A U Thor <author\@example.com>",
67 "ident_person: author (type)");
68is($r->ident_person("Name <email> 123 +0000"), "Name <email>",
69 "ident_person: ident string");
70is($r->ident_person("Name", "email", "123 +0000"), "Name <email>",
71 "ident_person: array");
72
73# objects and hashes
74ok(our $file1hash = $r->command_oneline('rev-parse', "HEAD:file1"), "(get file hash)");
75my $tmpfile = "file.tmp";
76open TEMPFILE, "+>$tmpfile" or die "Can't open $tmpfile: $!";
77is($r->cat_blob($file1hash, \*TEMPFILE), 15, "cat_blob: size");
78our $blobcontents;
79{ local $/; seek TEMPFILE, 0, 0; $blobcontents = <TEMPFILE>; }
80is($blobcontents, "changed file 1\n", "cat_blob: data");
81close TEMPFILE or die "Failed writing to $tmpfile: $!";
82is(Git::hash_object("blob", $tmpfile), $file1hash, "hash_object: roundtrip");
83open TEMPFILE, ">$tmpfile" or die "Can't open $tmpfile: $!";
84print TEMPFILE my $test_text = "test blob, to be inserted\n";
85close TEMPFILE or die "Failed writing to $tmpfile: $!";
86like(our $newhash = $r->hash_and_insert_object($tmpfile), qr/[0-9a-fA-F]{40}/,
87 "hash_and_insert_object: returns hash");
88open TEMPFILE, "+>$tmpfile" or die "Can't open $tmpfile: $!";
89is($r->cat_blob($newhash, \*TEMPFILE), length $test_text, "cat_blob: roundtrip size");
90{ local $/; seek TEMPFILE, 0, 0; $blobcontents = <TEMPFILE>; }
91is($blobcontents, $test_text, "cat_blob: roundtrip data");
92close TEMPFILE;
93unlink $tmpfile;
94
95# paths
96is($r->repo_path, $abs_repo_dir . "/.git", "repo_path");
97is($r->wc_path, $abs_repo_dir . "/", "wc_path");
98is($r->wc_subdir, "", "wc_subdir initial");
99$r->wc_chdir("directory1");
100is($r->wc_subdir, "directory1", "wc_subdir after wc_chdir");
101is($r->config("test.string"), "value", "config after wc_chdir");
102
103# Object generation in sub directory
104chdir("directory2");
105my $r2 = Git->repository();
106is($r2->repo_path, $abs_repo_dir . "/.git", "repo_path (2)");
107is($r2->wc_path, $abs_repo_dir . "/", "wc_path (2)");
108is($r2->wc_subdir, "directory2/", "wc_subdir initial (2)");
109
110# commands in sub directory
111my $last_commit = $r2->command_oneline(qw(rev-parse --verify HEAD));
112like($last_commit, qr/^[0-9a-fA-F]{40}$/, 'rev-parse returned hash');
113my $dir_commit = $r2->command_oneline('log', '-n1', '--pretty=format:%H', '.');
114isnt($last_commit, $dir_commit, 'log . does not show last commit');
115
116printf "1..%d\n", Test::More->builder->current_test;
117
118my $is_passing = eval { Test::More->is_passing };
119exit($is_passing ? 0 : 1) unless $@ =~ /Can't locate object method/;