Code

Merge branch 'maint' of git://repo.or.cz/git-gui into maint
[git.git] / t / t9700 / test.pl
1 #!/usr/bin/perl
2 use lib (split(/:/, $ENV{GITPERLLIB}));
4 use 5.006002;
5 use warnings;
6 use strict;
8 use Test::More qw(no_plan);
10 use Cwd;
11 use File::Basename;
13 BEGIN { use_ok('Git') }
15 # set up
16 our $repo_dir = "trash directory";
17 our $abs_repo_dir = Cwd->cwd;
18 die "this must be run by calling the t/t97* shell script(s)\n"
19     if basename(Cwd->cwd) ne $repo_dir;
20 ok(our $r = Git->repository(Directory => "."), "open repository");
22 # config
23 is($r->config("test.string"), "value", "config scalar: string");
24 is_deeply([$r->config("test.dupstring")], ["value1", "value2"],
25           "config array: string");
26 is($r->config("test.nonexistent"), undef, "config scalar: nonexistent");
27 is_deeply([$r->config("test.nonexistent")], [], "config array: nonexistent");
28 is($r->config_int("test.int"), 2048, "config_int: integer");
29 is($r->config_int("test.nonexistent"), undef, "config_int: nonexistent");
30 ok($r->config_bool("test.booltrue"), "config_bool: true");
31 ok(!$r->config_bool("test.boolfalse"), "config_bool: false");
32 our $ansi_green = "\x1b[32m";
33 is($r->get_color("color.test.slot1", "red"), $ansi_green, "get_color");
34 # Cannot test $r->get_colorbool("color.foo")) because we do not
35 # control whether our STDOUT is a terminal.
37 # Failure cases for config:
38 # Save and restore STDERR; we will probably extract this into a
39 # "dies_ok" method and possibly move the STDERR handling to Git.pm.
40 open our $tmpstderr, ">&STDERR" or die "cannot save STDERR"; close STDERR;
41 eval { $r->config("test.dupstring") };
42 ok($@, "config: duplicate entry in scalar context fails");
43 eval { $r->config_bool("test.boolother") };
44 ok($@, "config_bool: non-boolean values fail");
45 open STDERR, ">&", $tmpstderr or die "cannot restore STDERR";
47 # ident
48 like($r->ident("aUthor"), qr/^A U Thor <author\@example.com> [0-9]+ \+0000$/,
49      "ident scalar: author (type)");
50 like($r->ident("cOmmitter"), qr/^C O Mitter <committer\@example.com> [0-9]+ \+0000$/,
51      "ident scalar: committer (type)");
52 is($r->ident("invalid"), "invalid", "ident scalar: invalid ident string (no parsing)");
53 my ($name, $email, $time_tz) = $r->ident('author');
54 is_deeply([$name, $email], ["A U Thor", "author\@example.com"],
55          "ident array: author");
56 like($time_tz, qr/[0-9]+ \+0000/, "ident array: author");
57 is_deeply([$r->ident("Name <email> 123 +0000")], ["Name", "email", "123 +0000"],
58           "ident array: ident string");
59 is_deeply([$r->ident("invalid")], [], "ident array: invalid ident string");
61 # ident_person
62 is($r->ident_person("aUthor"), "A U Thor <author\@example.com>",
63    "ident_person: author (type)");
64 is($r->ident_person("Name <email> 123 +0000"), "Name <email>",
65    "ident_person: ident string");
66 is($r->ident_person("Name", "email", "123 +0000"), "Name <email>",
67    "ident_person: array");
69 # objects and hashes
70 ok(our $file1hash = $r->command_oneline('rev-parse', "HEAD:file1"), "(get file hash)");
71 my $tmpfile = "file.tmp";
72 open TEMPFILE, "+>$tmpfile" or die "Can't open $tmpfile: $!";
73 is($r->cat_blob($file1hash, \*TEMPFILE), 15, "cat_blob: size");
74 our $blobcontents;
75 { local $/; seek TEMPFILE, 0, 0; $blobcontents = <TEMPFILE>; }
76 is($blobcontents, "changed file 1\n", "cat_blob: data");
77 close TEMPFILE or die "Failed writing to $tmpfile: $!";
78 is(Git::hash_object("blob", $tmpfile), $file1hash, "hash_object: roundtrip");
79 open TEMPFILE, ">$tmpfile" or die "Can't open $tmpfile: $!";
80 print TEMPFILE my $test_text = "test blob, to be inserted\n";
81 close TEMPFILE or die "Failed writing to $tmpfile: $!";
82 like(our $newhash = $r->hash_and_insert_object($tmpfile), qr/[0-9a-fA-F]{40}/,
83      "hash_and_insert_object: returns hash");
84 open TEMPFILE, "+>$tmpfile" or die "Can't open $tmpfile: $!";
85 is($r->cat_blob($newhash, \*TEMPFILE), length $test_text, "cat_blob: roundtrip size");
86 { local $/; seek TEMPFILE, 0, 0; $blobcontents = <TEMPFILE>; }
87 is($blobcontents, $test_text, "cat_blob: roundtrip data");
88 close TEMPFILE;
89 unlink $tmpfile;
91 # paths
92 is($r->repo_path, "./.git", "repo_path");
93 is($r->wc_path, $abs_repo_dir . "/", "wc_path");
94 is($r->wc_subdir, "", "wc_subdir initial");
95 $r->wc_chdir("directory1");
96 is($r->wc_subdir, "directory1", "wc_subdir after wc_chdir");
97 TODO: {
98         local $TODO = "commands do not work after wc_chdir";
99         # Failure output is active even in non-verbose mode and thus
100         # annoying.  Hence we skip these tests as long as they fail.
101         todo_skip 'config after wc_chdir', 1;
102         is($r->config("color.string"), "value", "config after wc_chdir");