Git 2.38
[alt-git.git] / t / t9700 / test.pl
blobe046f7db762d355228425ff5b77639be65b1272c
1 #!/usr/bin/perl
2 use lib (split(/:/, $ENV{GITPERLLIB}));
4 use 5.008;
5 use warnings;
6 use strict;
8 use Test::More qw(no_plan);
10 BEGIN {
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);
17 use Cwd;
18 use File::Basename;
20 sub adjust_dirsep {
21 my $path = shift;
22 $path =~ s{\\}{/}g;
23 return $path;
26 my $oid_re = qr/^[0-9a-fA-F]{40}(?:[0-9a-fA-F]{24})?$/;
28 BEGIN { use_ok('Git') }
30 # set up
31 our $abs_repo_dir = cwd();
32 ok(our $r = Git->repository(Directory => "."), "open repository");
34 # config
35 is($r->config("test.string"), "value", "config scalar: string");
36 is_deeply([$r->config("test.dupstring")], ["value1", "value2"],
37 "config array: string");
38 is($r->config("test.nonexistent"), undef, "config scalar: nonexistent");
39 is_deeply([$r->config("test.nonexistent")], [], "config array: nonexistent");
40 is($r->config_int("test.int"), 2048, "config_int: integer");
41 is($r->config_int("test.nonexistent"), undef, "config_int: nonexistent");
42 ok($r->config_bool("test.booltrue"), "config_bool: true");
43 ok(!$r->config_bool("test.boolfalse"), "config_bool: false");
44 is(adjust_dirsep($r->config_path("test.path")), $r->config("test.pathexpanded"),
45 "config_path: ~/foo expansion");
46 is_deeply([$r->config_path("test.pathmulti")], ["foo", "bar"],
47 "config_path: multiple values");
48 our $ansi_green = "\x1b[32m";
49 is($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.
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.
56 open our $tmpstderr, ">&STDERR" or die "cannot save STDERR";
57 open STDERR, ">", "/dev/null" or die "cannot redirect STDERR to /dev/null";
58 is($r->config("test.dupstring"), "value2", "config: multivar");
59 eval { $r->config_bool("test.boolother") };
60 ok($@, "config_bool: non-boolean values fail");
61 open STDERR, ">&", $tmpstderr or die "cannot restore STDERR";
63 # ident
64 like($r->ident("aUthor"), qr/^A U Thor <author\@example.com> [0-9]+ [+-]\d{4}$/,
65 "ident scalar: author (type)");
66 like($r->ident("cOmmitter"), qr/^C O Mitter <committer\@example.com> [0-9]+ [+-]\d{4}$/,
67 "ident scalar: committer (type)");
68 is($r->ident("invalid"), "invalid", "ident scalar: invalid ident string (no parsing)");
69 my ($name, $email, $time_tz) = $r->ident('author');
70 is_deeply([$name, $email], ["A U Thor", "author\@example.com"],
71 "ident array: author");
72 like($time_tz, qr/[0-9]+ [+-]\d{4}/, "ident array: author");
73 is_deeply([$r->ident("Name <email> 123 +0000")], ["Name", "email", "123 +0000"],
74 "ident array: ident string");
75 is_deeply([$r->ident("invalid")], [], "ident array: invalid ident string");
77 # ident_person
78 is($r->ident_person("aUthor"), "A U Thor <author\@example.com>",
79 "ident_person: author (type)");
80 is($r->ident_person("Name <email> 123 +0000"), "Name <email>",
81 "ident_person: ident string");
82 is($r->ident_person("Name", "email", "123 +0000"), "Name <email>",
83 "ident_person: array");
85 # objects and hashes
86 ok(our $file1hash = $r->command_oneline('rev-parse', "HEAD:file1"), "(get file hash)");
87 my $tmpfile = "file.tmp";
88 open TEMPFILE, "+>$tmpfile" or die "Can't open $tmpfile: $!";
89 is($r->cat_blob($file1hash, \*TEMPFILE), 15, "cat_blob: size");
90 our $blobcontents;
91 { local $/; seek TEMPFILE, 0, 0; $blobcontents = <TEMPFILE>; }
92 is($blobcontents, "changed file 1\n", "cat_blob: data");
93 close TEMPFILE or die "Failed writing to $tmpfile: $!";
94 is(Git::hash_object("blob", $tmpfile), $file1hash, "hash_object: roundtrip");
95 open TEMPFILE, ">$tmpfile" or die "Can't open $tmpfile: $!";
96 print TEMPFILE my $test_text = "test blob, to be inserted\n";
97 close TEMPFILE or die "Failed writing to $tmpfile: $!";
98 like(our $newhash = $r->hash_and_insert_object($tmpfile), $oid_re,
99 "hash_and_insert_object: returns hash");
100 open TEMPFILE, "+>$tmpfile" or die "Can't open $tmpfile: $!";
101 is($r->cat_blob($newhash, \*TEMPFILE), length $test_text, "cat_blob: roundtrip size");
102 { local $/; seek TEMPFILE, 0, 0; $blobcontents = <TEMPFILE>; }
103 is($blobcontents, $test_text, "cat_blob: roundtrip data");
104 close TEMPFILE;
105 unlink $tmpfile;
107 # paths
108 is($r->repo_path, $abs_repo_dir . "/.git", "repo_path");
109 is($r->wc_path, $abs_repo_dir . "/", "wc_path");
110 is($r->wc_subdir, "", "wc_subdir initial");
111 $r->wc_chdir("directory1");
112 is($r->wc_subdir, "directory1", "wc_subdir after wc_chdir");
113 is($r->config("test.string"), "value", "config after wc_chdir");
115 # Object generation in sub directory
116 chdir("directory2");
117 my $r2 = Git->repository();
118 is($r2->repo_path, $abs_repo_dir . "/.git", "repo_path (2)");
119 is($r2->wc_path, $abs_repo_dir . "/", "wc_path (2)");
120 is($r2->wc_subdir, "directory2/", "wc_subdir initial (2)");
122 # commands in sub directory
123 my $last_commit = $r2->command_oneline(qw(rev-parse --verify HEAD));
124 like($last_commit, $oid_re, 'rev-parse returned hash');
125 my $dir_commit = $r2->command_oneline('log', '-n1', '--pretty=format:%H', '.');
126 isnt($last_commit, $dir_commit, 'log . does not show last commit');
128 # commands outside working tree
129 chdir($abs_repo_dir . '/..');
130 my $r3 = Git->repository(Directory => $abs_repo_dir);
131 my $tmpfile3 = "$abs_repo_dir/file3.tmp";
132 open TEMPFILE3, "+>$tmpfile3" or die "Can't open $tmpfile3: $!";
133 is($r3->cat_blob($file1hash, \*TEMPFILE3), 15, "cat_blob(outside): size");
134 close TEMPFILE3;
135 unlink $tmpfile3;
136 chdir($abs_repo_dir);
138 # unquoting paths
139 is(Git::unquote_path('abc'), 'abc', 'unquote unquoted path');
140 is(Git::unquote_path('"abc def"'), 'abc def', 'unquote simple quoted path');
141 is(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');
145 printf "1..%d\n", Test::More->builder->current_test;
147 my $is_passing = eval { Test::More->is_passing };
148 exit($is_passing ? 0 : 1) unless $@ =~ /Can't locate object method/;