]> git.ipfire.org Git - thirdparty/git.git/blame - t/t9700/test.pl
perl: bump the required Perl version to 5.8.1 from 5.8.0
[thirdparty/git.git] / t / t9700 / test.pl
CommitLineData
b4780d72
LW
1#!/usr/bin/perl
2use lib (split(/:/, $ENV{GITPERLLIB}));
3
d13a73e3 4use 5.008001;
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 19
839b6397
JK
20sub adjust_dirsep {
21 my $path = shift;
22 $path =~ s{\\}{/}g;
23 return $path;
24}
25
e0a646ed 26my $oid_re = qr/^[0-9a-fA-F]{40}(?:[0-9a-fA-F]{24})?$/;
27
b4780d72
LW
28BEGIN { use_ok('Git') }
29
30# set up
81f40262 31our $abs_repo_dir = cwd();
b4780d72 32ok(our $r = Git->repository(Directory => "."), "open repository");
20da61f2
JK
33{
34 local $ENV{GIT_TEST_ASSUME_DIFFERENT_OWNER} = 1;
35 my $failed;
36
37 $failed = eval { Git->repository(Directory => $abs_repo_dir) };
38 ok(!$failed, "reject unsafe non-bare repository");
39 like($@, qr/not a git repository/i, "unsafe error message");
40
41 $failed = eval { Git->repository(Directory => "$abs_repo_dir/bare.git") };
42 ok(!$failed, "reject unsafe bare repository");
43 like($@, qr/not a git repository/i, "unsafe error message");
44}
b4780d72
LW
45
46# config
47is($r->config("test.string"), "value", "config scalar: string");
48is_deeply([$r->config("test.dupstring")], ["value1", "value2"],
49 "config array: string");
50is($r->config("test.nonexistent"), undef, "config scalar: nonexistent");
51is_deeply([$r->config("test.nonexistent")], [], "config array: nonexistent");
52is($r->config_int("test.int"), 2048, "config_int: integer");
53is($r->config_int("test.nonexistent"), undef, "config_int: nonexistent");
54ok($r->config_bool("test.booltrue"), "config_bool: true");
55ok(!$r->config_bool("test.boolfalse"), "config_bool: false");
839b6397 56is(adjust_dirsep($r->config_path("test.path")), $r->config("test.pathexpanded"),
cb9c9df3
JN
57 "config_path: ~/foo expansion");
58is_deeply([$r->config_path("test.pathmulti")], ["foo", "bar"],
59 "config_path: multiple values");
b4780d72
LW
60our $ansi_green = "\x1b[32m";
61is($r->get_color("color.test.slot1", "red"), $ansi_green, "get_color");
62# Cannot test $r->get_colorbool("color.foo")) because we do not
63# control whether our STDOUT is a terminal.
64
65# Failure cases for config:
66# Save and restore STDERR; we will probably extract this into a
67# "dies_ok" method and possibly move the STDERR handling to Git.pm.
a749c0bb
TR
68open our $tmpstderr, ">&STDERR" or die "cannot save STDERR";
69open STDERR, ">", "/dev/null" or die "cannot redirect STDERR to /dev/null";
00b347d3 70is($r->config("test.dupstring"), "value2", "config: multivar");
b4780d72
LW
71eval { $r->config_bool("test.boolother") };
72ok($@, "config_bool: non-boolean values fail");
73open STDERR, ">&", $tmpstderr or die "cannot restore STDERR";
74
75# ident
fccf41e3 76like($r->ident("aUthor"), qr/^A U Thor <author\@example.com> [0-9]+ [+-]\d{4}$/,
b4780d72 77 "ident scalar: author (type)");
fccf41e3 78like($r->ident("cOmmitter"), qr/^C O Mitter <committer\@example.com> [0-9]+ [+-]\d{4}$/,
b4780d72
LW
79 "ident scalar: committer (type)");
80is($r->ident("invalid"), "invalid", "ident scalar: invalid ident string (no parsing)");
81my ($name, $email, $time_tz) = $r->ident('author');
82is_deeply([$name, $email], ["A U Thor", "author\@example.com"],
83 "ident array: author");
fccf41e3 84like($time_tz, qr/[0-9]+ [+-]\d{4}/, "ident array: author");
b4780d72
LW
85is_deeply([$r->ident("Name <email> 123 +0000")], ["Name", "email", "123 +0000"],
86 "ident array: ident string");
87is_deeply([$r->ident("invalid")], [], "ident array: invalid ident string");
88
89# ident_person
90is($r->ident_person("aUthor"), "A U Thor <author\@example.com>",
91 "ident_person: author (type)");
92is($r->ident_person("Name <email> 123 +0000"), "Name <email>",
93 "ident_person: ident string");
94is($r->ident_person("Name", "email", "123 +0000"), "Name <email>",
95 "ident_person: array");
96
97# objects and hashes
98ok(our $file1hash = $r->command_oneline('rev-parse', "HEAD:file1"), "(get file hash)");
bf557788
BC
99my $tmpfile = "file.tmp";
100open TEMPFILE, "+>$tmpfile" or die "Can't open $tmpfile: $!";
101is($r->cat_blob($file1hash, \*TEMPFILE), 15, "cat_blob: size");
b4780d72 102our $blobcontents;
bf557788 103{ local $/; seek TEMPFILE, 0, 0; $blobcontents = <TEMPFILE>; }
b4780d72 104is($blobcontents, "changed file 1\n", "cat_blob: data");
bf557788 105close TEMPFILE or die "Failed writing to $tmpfile: $!";
b4780d72 106is(Git::hash_object("blob", $tmpfile), $file1hash, "hash_object: roundtrip");
bf557788
BC
107open TEMPFILE, ">$tmpfile" or die "Can't open $tmpfile: $!";
108print TEMPFILE my $test_text = "test blob, to be inserted\n";
109close TEMPFILE or die "Failed writing to $tmpfile: $!";
e0a646ed 110like(our $newhash = $r->hash_and_insert_object($tmpfile), $oid_re,
b4780d72 111 "hash_and_insert_object: returns hash");
bf557788
BC
112open TEMPFILE, "+>$tmpfile" or die "Can't open $tmpfile: $!";
113is($r->cat_blob($newhash, \*TEMPFILE), length $test_text, "cat_blob: roundtrip size");
114{ local $/; seek TEMPFILE, 0, 0; $blobcontents = <TEMPFILE>; }
b4780d72 115is($blobcontents, $test_text, "cat_blob: roundtrip data");
bf557788
BC
116close TEMPFILE;
117unlink $tmpfile;
b4780d72
LW
118
119# paths
fe53bbc9 120is($r->repo_path, $abs_repo_dir . "/.git", "repo_path");
b4780d72
LW
121is($r->wc_path, $abs_repo_dir . "/", "wc_path");
122is($r->wc_subdir, "", "wc_subdir initial");
123$r->wc_chdir("directory1");
124is($r->wc_subdir, "directory1", "wc_subdir after wc_chdir");
fe53bbc9 125is($r->config("test.string"), "value", "config after wc_chdir");
da159c77
FL
126
127# Object generation in sub directory
128chdir("directory2");
129my $r2 = Git->repository();
130is($r2->repo_path, $abs_repo_dir . "/.git", "repo_path (2)");
131is($r2->wc_path, $abs_repo_dir . "/", "wc_path (2)");
132is($r2->wc_subdir, "directory2/", "wc_subdir initial (2)");
133
134# commands in sub directory
135my $last_commit = $r2->command_oneline(qw(rev-parse --verify HEAD));
e0a646ed 136like($last_commit, $oid_re, 'rev-parse returned hash');
da159c77
FL
137my $dir_commit = $r2->command_oneline('log', '-n1', '--pretty=format:%H', '.');
138isnt($last_commit, $dir_commit, 'log . does not show last commit');
d998bd4a 139
48d9e6ae
MO
140# commands outside working tree
141chdir($abs_repo_dir . '/..');
142my $r3 = Git->repository(Directory => $abs_repo_dir);
143my $tmpfile3 = "$abs_repo_dir/file3.tmp";
144open TEMPFILE3, "+>$tmpfile3" or die "Can't open $tmpfile3: $!";
145is($r3->cat_blob($file1hash, \*TEMPFILE3), 15, "cat_blob(outside): size");
146close TEMPFILE3;
147unlink $tmpfile3;
148chdir($abs_repo_dir);
149
3f9c637e
PW
150# unquoting paths
151is(Git::unquote_path('abc'), 'abc', 'unquote unquoted path');
152is(Git::unquote_path('"abc def"'), 'abc def', 'unquote simple quoted path');
153is(Git::unquote_path('"abc\"\\\\ \a\b\t\n\v\f\r\001\040"'),
154 "abc\"\\ \x07\x08\x09\x0a\x0b\x0c\x0d\x01 ",
155 'unquote escape sequences');
156
15eeb6e9 157printf "1..%d\n", Test::More->builder->current_test;
d998bd4a 158
635155fa
ÆAB
159my $is_passing = eval { Test::More->is_passing };
160exit($is_passing ? 0 : 1) unless $@ =~ /Can't locate object method/;