1#!/usr/bin/perl
2use lib (split(/:/, $ENV{GITPERLLIB}));
3
4use 5.006002;
5use warnings;
6use strict;
7
8use Test::More qw(no_plan);
9
10use Cwd;
11use File::Basename;
12use File::Temp;
13
14BEGIN { use_ok('Git') }
15
16# set up
17our $abs_repo_dir = Cwd->cwd;
18ok(our $r = Git->repository(Directory => "."), "open repository");
19
20# config
21is($r->config("test.string"), "value", "config scalar: string");
22is_deeply([$r->config("test.dupstring")], ["value1", "value2"],
23 "config array: string");
24is($r->config("test.nonexistent"), undef, "config scalar: nonexistent");
25is_deeply([$r->config("test.nonexistent")], [], "config array: nonexistent");
26is($r->config_int("test.int"), 2048, "config_int: integer");
27is($r->config_int("test.nonexistent"), undef, "config_int: nonexistent");
28ok($r->config_bool("test.booltrue"), "config_bool: true");
29ok(!$r->config_bool("test.boolfalse"), "config_bool: false");
30our $ansi_green = "\x1b[32m";
31is($r->get_color("color.test.slot1", "red"), $ansi_green, "get_color");
32# Cannot test $r->get_colorbool("color.foo")) because we do not
33# control whether our STDOUT is a terminal.
34
35# Failure cases for config:
36# Save and restore STDERR; we will probably extract this into a
37# "dies_ok" method and possibly move the STDERR handling to Git.pm.
38open our $tmpstderr, ">&", STDERR or die "cannot save STDERR"; close STDERR;
39eval { $r->config("test.dupstring") };
40ok($@, "config: duplicate entry in scalar context fails");
41eval { $r->config_bool("test.boolother") };
42ok($@, "config_bool: non-boolean values fail");
43open STDERR, ">&", $tmpstderr or die "cannot restore STDERR";
44
45# ident
46like($r->ident("aUthor"), qr/^A U Thor <author\@example.com> [0-9]+ \+0000$/,
47 "ident scalar: author (type)");
48like($r->ident("cOmmitter"), qr/^C O Mitter <committer\@example.com> [0-9]+ \+0000$/,
49 "ident scalar: committer (type)");
50is($r->ident("invalid"), "invalid", "ident scalar: invalid ident string (no parsing)");
51my ($name, $email, $time_tz) = $r->ident('author');
52is_deeply([$name, $email], ["A U Thor", "author\@example.com"],
53 "ident array: author");
54like($time_tz, qr/[0-9]+ \+0000/, "ident array: author");
55is_deeply([$r->ident("Name <email> 123 +0000")], ["Name", "email", "123 +0000"],
56 "ident array: ident string");
57is_deeply([$r->ident("invalid")], [], "ident array: invalid ident string");
58
59# ident_person
60is($r->ident_person("aUthor"), "A U Thor <author\@example.com>",
61 "ident_person: author (type)");
62is($r->ident_person("Name <email> 123 +0000"), "Name <email>",
63 "ident_person: ident string");
64is($r->ident_person("Name", "email", "123 +0000"), "Name <email>",
65 "ident_person: array");
66
67# objects and hashes
68ok(our $file1hash = $r->command_oneline('rev-parse', "HEAD:file1"), "(get file hash)");
69our $tmpfile = File::Temp->new;
70is($r->cat_blob($file1hash, $tmpfile), 15, "cat_blob: size");
71our $blobcontents;
72{ local $/; seek $tmpfile, 0, 0; $blobcontents = <$tmpfile>; }
73is($blobcontents, "changed file 1\n", "cat_blob: data");
74seek $tmpfile, 0, 0;
75is(Git::hash_object("blob", $tmpfile), $file1hash, "hash_object: roundtrip");
76$tmpfile = File::Temp->new();
77print $tmpfile my $test_text = "test blob, to be inserted\n";
78like(our $newhash = $r->hash_and_insert_object($tmpfile), qr/[0-9a-fA-F]{40}/,
79 "hash_and_insert_object: returns hash");
80$tmpfile = File::Temp->new;
81is($r->cat_blob($newhash, $tmpfile), length $test_text, "cat_blob: roundtrip size");
82{ local $/; seek $tmpfile, 0, 0; $blobcontents = <$tmpfile>; }
83is($blobcontents, $test_text, "cat_blob: roundtrip data");
84
85# paths
86is($r->repo_path, "./.git", "repo_path");
87is($r->wc_path, $abs_repo_dir . "/", "wc_path");
88is($r->wc_subdir, "", "wc_subdir initial");
89$r->wc_chdir("directory1");
90is($r->wc_subdir, "directory1", "wc_subdir after wc_chdir");
91TODO: {
92 local $TODO = "commands do not work after wc_chdir";
93 # Failure output is active even in non-verbose mode and thus
94 # annoying. Hence we skip these tests as long as they fail.
95 todo_skip 'config after wc_chdir', 1;
96 is($r->config("color.string"), "value", "config after wc_chdir");
97}