Merge branch 'jm/dedup-test-config' into maint
[git.git] / t / t9700 / test.pl
blob1140767b50706f03b0da5b2c8befdae6898e4136
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 BEGIN { use_ok('Git') }
22 # set up
23 our $abs_repo_dir = cwd();
24 ok(our $r = Git->repository(Directory => "."), "open repository");
26 # config
27 is($r->config("test.string"), "value", "config scalar: string");
28 is_deeply([$r->config("test.dupstring")], ["value1", "value2"],
29 "config array: string");
30 is($r->config("test.nonexistent"), undef, "config scalar: nonexistent");
31 is_deeply([$r->config("test.nonexistent")], [], "config array: nonexistent");
32 is($r->config_int("test.int"), 2048, "config_int: integer");
33 is($r->config_int("test.nonexistent"), undef, "config_int: nonexistent");
34 ok($r->config_bool("test.booltrue"), "config_bool: true");
35 ok(!$r->config_bool("test.boolfalse"), "config_bool: false");
36 is($r->config_path("test.path"), $r->config("test.pathexpanded"),
37 "config_path: ~/foo expansion");
38 is_deeply([$r->config_path("test.pathmulti")], ["foo", "bar"],
39 "config_path: multiple values");
40 our $ansi_green = "\x1b[32m";
41 is($r->get_color("color.test.slot1", "red"), $ansi_green, "get_color");
42 # Cannot test $r->get_colorbool("color.foo")) because we do not
43 # control whether our STDOUT is a terminal.
45 # Failure cases for config:
46 # Save and restore STDERR; we will probably extract this into a
47 # "dies_ok" method and possibly move the STDERR handling to Git.pm.
48 open our $tmpstderr, ">&STDERR" or die "cannot save STDERR";
49 open STDERR, ">", "/dev/null" or die "cannot redirect STDERR to /dev/null";
50 is($r->config("test.dupstring"), "value2", "config: multivar");
51 eval { $r->config_bool("test.boolother") };
52 ok($@, "config_bool: non-boolean values fail");
53 open STDERR, ">&", $tmpstderr or die "cannot restore STDERR";
55 # ident
56 like($r->ident("aUthor"), qr/^A U Thor <author\@example.com> [0-9]+ \+0000$/,
57 "ident scalar: author (type)");
58 like($r->ident("cOmmitter"), qr/^C O Mitter <committer\@example.com> [0-9]+ \+0000$/,
59 "ident scalar: committer (type)");
60 is($r->ident("invalid"), "invalid", "ident scalar: invalid ident string (no parsing)");
61 my ($name, $email, $time_tz) = $r->ident('author');
62 is_deeply([$name, $email], ["A U Thor", "author\@example.com"],
63 "ident array: author");
64 like($time_tz, qr/[0-9]+ \+0000/, "ident array: author");
65 is_deeply([$r->ident("Name <email> 123 +0000")], ["Name", "email", "123 +0000"],
66 "ident array: ident string");
67 is_deeply([$r->ident("invalid")], [], "ident array: invalid ident string");
69 # ident_person
70 is($r->ident_person("aUthor"), "A U Thor <author\@example.com>",
71 "ident_person: author (type)");
72 is($r->ident_person("Name <email> 123 +0000"), "Name <email>",
73 "ident_person: ident string");
74 is($r->ident_person("Name", "email", "123 +0000"), "Name <email>",
75 "ident_person: array");
77 # objects and hashes
78 ok(our $file1hash = $r->command_oneline('rev-parse', "HEAD:file1"), "(get file hash)");
79 my $tmpfile = "file.tmp";
80 open TEMPFILE, "+>$tmpfile" or die "Can't open $tmpfile: $!";
81 is($r->cat_blob($file1hash, \*TEMPFILE), 15, "cat_blob: size");
82 our $blobcontents;
83 { local $/; seek TEMPFILE, 0, 0; $blobcontents = <TEMPFILE>; }
84 is($blobcontents, "changed file 1\n", "cat_blob: data");
85 close TEMPFILE or die "Failed writing to $tmpfile: $!";
86 is(Git::hash_object("blob", $tmpfile), $file1hash, "hash_object: roundtrip");
87 open TEMPFILE, ">$tmpfile" or die "Can't open $tmpfile: $!";
88 print TEMPFILE my $test_text = "test blob, to be inserted\n";
89 close TEMPFILE or die "Failed writing to $tmpfile: $!";
90 like(our $newhash = $r->hash_and_insert_object($tmpfile), qr/[0-9a-fA-F]{40}/,
91 "hash_and_insert_object: returns hash");
92 open TEMPFILE, "+>$tmpfile" or die "Can't open $tmpfile: $!";
93 is($r->cat_blob($newhash, \*TEMPFILE), length $test_text, "cat_blob: roundtrip size");
94 { local $/; seek TEMPFILE, 0, 0; $blobcontents = <TEMPFILE>; }
95 is($blobcontents, $test_text, "cat_blob: roundtrip data");
96 close TEMPFILE;
97 unlink $tmpfile;
99 # paths
100 is($r->repo_path, $abs_repo_dir . "/.git", "repo_path");
101 is($r->wc_path, $abs_repo_dir . "/", "wc_path");
102 is($r->wc_subdir, "", "wc_subdir initial");
103 $r->wc_chdir("directory1");
104 is($r->wc_subdir, "directory1", "wc_subdir after wc_chdir");
105 is($r->config("test.string"), "value", "config after wc_chdir");
107 # Object generation in sub directory
108 chdir("directory2");
109 my $r2 = Git->repository();
110 is($r2->repo_path, $abs_repo_dir . "/.git", "repo_path (2)");
111 is($r2->wc_path, $abs_repo_dir . "/", "wc_path (2)");
112 is($r2->wc_subdir, "directory2/", "wc_subdir initial (2)");
114 # commands in sub directory
115 my $last_commit = $r2->command_oneline(qw(rev-parse --verify HEAD));
116 like($last_commit, qr/^[0-9a-fA-F]{40}$/, 'rev-parse returned hash');
117 my $dir_commit = $r2->command_oneline('log', '-n1', '--pretty=format:%H', '.');
118 isnt($last_commit, $dir_commit, 'log . does not show last commit');
120 # commands outside working tree
121 chdir($abs_repo_dir . '/..');
122 my $r3 = Git->repository(Directory => $abs_repo_dir);
123 my $tmpfile3 = "$abs_repo_dir/file3.tmp";
124 open TEMPFILE3, "+>$tmpfile3" or die "Can't open $tmpfile3: $!";
125 is($r3->cat_blob($file1hash, \*TEMPFILE3), 15, "cat_blob(outside): size");
126 close TEMPFILE3;
127 unlink $tmpfile3;
128 chdir($abs_repo_dir);
130 printf "1..%d\n", Test::More->builder->current_test;
132 my $is_passing = eval { Test::More->is_passing };
133 exit($is_passing ? 0 : 1) unless $@ =~ /Can't locate object method/;