t/t6035-merge-dir-to-symlink.sh: Remove TODO on passing test
[git.git] / t / t9700 / test.pl
blob666722d9bf1050522a687f4af95792a8e0ec5d64
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 $abs_repo_dir = cwd();
17 ok(our $r = Git->repository(Directory => "."), "open repository");
19 # config
20 is($r->config("test.string"), "value", "config scalar: string");
21 is_deeply([$r->config("test.dupstring")], ["value1", "value2"],
22 "config array: string");
23 is($r->config("test.nonexistent"), undef, "config scalar: nonexistent");
24 is_deeply([$r->config("test.nonexistent")], [], "config array: nonexistent");
25 is($r->config_int("test.int"), 2048, "config_int: integer");
26 is($r->config_int("test.nonexistent"), undef, "config_int: nonexistent");
27 ok($r->config_bool("test.booltrue"), "config_bool: true");
28 ok(!$r->config_bool("test.boolfalse"), "config_bool: false");
29 our $ansi_green = "\x1b[32m";
30 is($r->get_color("color.test.slot1", "red"), $ansi_green, "get_color");
31 # Cannot test $r->get_colorbool("color.foo")) because we do not
32 # control whether our STDOUT is a terminal.
34 # Failure cases for config:
35 # Save and restore STDERR; we will probably extract this into a
36 # "dies_ok" method and possibly move the STDERR handling to Git.pm.
37 open our $tmpstderr, ">&STDERR" or die "cannot save STDERR"; close STDERR;
38 eval { $r->config("test.dupstring") };
39 ok($@, "config: duplicate entry in scalar context fails");
40 eval { $r->config_bool("test.boolother") };
41 ok($@, "config_bool: non-boolean values fail");
42 open STDERR, ">&", $tmpstderr or die "cannot restore STDERR";
44 # ident
45 like($r->ident("aUthor"), qr/^A U Thor <author\@example.com> [0-9]+ \+0000$/,
46 "ident scalar: author (type)");
47 like($r->ident("cOmmitter"), qr/^C O Mitter <committer\@example.com> [0-9]+ \+0000$/,
48 "ident scalar: committer (type)");
49 is($r->ident("invalid"), "invalid", "ident scalar: invalid ident string (no parsing)");
50 my ($name, $email, $time_tz) = $r->ident('author');
51 is_deeply([$name, $email], ["A U Thor", "author\@example.com"],
52 "ident array: author");
53 like($time_tz, qr/[0-9]+ \+0000/, "ident array: author");
54 is_deeply([$r->ident("Name <email> 123 +0000")], ["Name", "email", "123 +0000"],
55 "ident array: ident string");
56 is_deeply([$r->ident("invalid")], [], "ident array: invalid ident string");
58 # ident_person
59 is($r->ident_person("aUthor"), "A U Thor <author\@example.com>",
60 "ident_person: author (type)");
61 is($r->ident_person("Name <email> 123 +0000"), "Name <email>",
62 "ident_person: ident string");
63 is($r->ident_person("Name", "email", "123 +0000"), "Name <email>",
64 "ident_person: array");
66 # objects and hashes
67 ok(our $file1hash = $r->command_oneline('rev-parse', "HEAD:file1"), "(get file hash)");
68 my $tmpfile = "file.tmp";
69 open TEMPFILE, "+>$tmpfile" or die "Can't open $tmpfile: $!";
70 is($r->cat_blob($file1hash, \*TEMPFILE), 15, "cat_blob: size");
71 our $blobcontents;
72 { local $/; seek TEMPFILE, 0, 0; $blobcontents = <TEMPFILE>; }
73 is($blobcontents, "changed file 1\n", "cat_blob: data");
74 close TEMPFILE or die "Failed writing to $tmpfile: $!";
75 is(Git::hash_object("blob", $tmpfile), $file1hash, "hash_object: roundtrip");
76 open TEMPFILE, ">$tmpfile" or die "Can't open $tmpfile: $!";
77 print TEMPFILE my $test_text = "test blob, to be inserted\n";
78 close TEMPFILE or die "Failed writing to $tmpfile: $!";
79 like(our $newhash = $r->hash_and_insert_object($tmpfile), qr/[0-9a-fA-F]{40}/,
80 "hash_and_insert_object: returns hash");
81 open TEMPFILE, "+>$tmpfile" or die "Can't open $tmpfile: $!";
82 is($r->cat_blob($newhash, \*TEMPFILE), length $test_text, "cat_blob: roundtrip size");
83 { local $/; seek TEMPFILE, 0, 0; $blobcontents = <TEMPFILE>; }
84 is($blobcontents, $test_text, "cat_blob: roundtrip data");
85 close TEMPFILE;
86 unlink $tmpfile;
88 # paths
89 is($r->repo_path, $abs_repo_dir . "/.git", "repo_path");
90 is($r->wc_path, $abs_repo_dir . "/", "wc_path");
91 is($r->wc_subdir, "", "wc_subdir initial");
92 $r->wc_chdir("directory1");
93 is($r->wc_subdir, "directory1", "wc_subdir after wc_chdir");
94 is($r->config("test.string"), "value", "config after wc_chdir");
96 # Object generation in sub directory
97 chdir("directory2");
98 my $r2 = Git->repository();
99 is($r2->repo_path, $abs_repo_dir . "/.git", "repo_path (2)");
100 is($r2->wc_path, $abs_repo_dir . "/", "wc_path (2)");
101 is($r2->wc_subdir, "directory2/", "wc_subdir initial (2)");
103 # commands in sub directory
104 my $last_commit = $r2->command_oneline(qw(rev-parse --verify HEAD));
105 like($last_commit, qr/^[0-9a-fA-F]{40}$/, 'rev-parse returned hash');
106 my $dir_commit = $r2->command_oneline('log', '-n1', '--pretty=format:%H', '.');
107 isnt($last_commit, $dir_commit, 'log . does not show last commit');