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