]> git.ipfire.org Git - thirdparty/git.git/blame - t/t9700/test.pl
perl: bump the required Perl version to 5.8 from 5.6.[21]
[thirdparty/git.git] / t / t9700 / test.pl
CommitLineData
b4780d72
LW
1#!/usr/bin/perl
2use lib (split(/:/, $ENV{GITPERLLIB}));
3
d48b2841 4use 5.008;
b4780d72
LW
5use warnings;
6use strict;
7
8use Test::More qw(no_plan);
9
d998bd4a
ÆAB
10BEGIN {
11 # t9700-perl-git.sh kicks off our testing, so we have to go from
12 # there.
15eeb6e9
BC
13 Test::More->builder->current_test(1);
14 Test::More->builder->no_ending(1);
d998bd4a
ÆAB
15}
16
b4780d72
LW
17use Cwd;
18use File::Basename;
b4780d72
LW
19
20BEGIN { use_ok('Git') }
21
22# set up
81f40262 23our $abs_repo_dir = cwd();
b4780d72
LW
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.
8409bb37 44open our $tmpstderr, ">&STDERR" or die "cannot save STDERR"; close STDERR;
b4780d72
LW
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)");
bf557788
BC
75my $tmpfile = "file.tmp";
76open TEMPFILE, "+>$tmpfile" or die "Can't open $tmpfile: $!";
77is($r->cat_blob($file1hash, \*TEMPFILE), 15, "cat_blob: size");
b4780d72 78our $blobcontents;
bf557788 79{ local $/; seek TEMPFILE, 0, 0; $blobcontents = <TEMPFILE>; }
b4780d72 80is($blobcontents, "changed file 1\n", "cat_blob: data");
bf557788 81close TEMPFILE or die "Failed writing to $tmpfile: $!";
b4780d72 82is(Git::hash_object("blob", $tmpfile), $file1hash, "hash_object: roundtrip");
bf557788
BC
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: $!";
b4780d72
LW
86like(our $newhash = $r->hash_and_insert_object($tmpfile), qr/[0-9a-fA-F]{40}/,
87 "hash_and_insert_object: returns hash");
bf557788
BC
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>; }
b4780d72 91is($blobcontents, $test_text, "cat_blob: roundtrip data");
bf557788
BC
92close TEMPFILE;
93unlink $tmpfile;
b4780d72
LW
94
95# paths
fe53bbc9 96is($r->repo_path, $abs_repo_dir . "/.git", "repo_path");
b4780d72
LW
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");
fe53bbc9 101is($r->config("test.string"), "value", "config after wc_chdir");
da159c77
FL
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');
d998bd4a 115
15eeb6e9 116printf "1..%d\n", Test::More->builder->current_test;
d998bd4a 117
635155fa
ÆAB
118my $is_passing = eval { Test::More->is_passing };
119exit($is_passing ? 0 : 1) unless $@ =~ /Can't locate object method/;