git-p4: Add simple test case for branch import
[git/mingw.git] / t / t9700 / test.pl
blob13ba96e21a9295805aed40c89ecd5178db6bfae4
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 our $ansi_green = "\x1b[32m";
37 is($r->get_color("color.test.slot1", "red"), $ansi_green, "get_color");
38 # Cannot test $r->get_colorbool("color.foo")) because we do not
39 # control whether our STDOUT is a terminal.
41 # Failure cases for config:
42 # Save and restore STDERR; we will probably extract this into a
43 # "dies_ok" method and possibly move the STDERR handling to Git.pm.
44 open our $tmpstderr, ">&STDERR" or die "cannot save STDERR"; close STDERR;
45 eval { $r->config("test.dupstring") };
46 ok($@, "config: duplicate entry in scalar context fails");
47 eval { $r->config_bool("test.boolother") };
48 ok($@, "config_bool: non-boolean values fail");
49 open STDERR, ">&", $tmpstderr or die "cannot restore STDERR";
51 # ident
52 like($r->ident("aUthor"), qr/^A U Thor <author\@example.com> [0-9]+ \+0000$/,
53 "ident scalar: author (type)");
54 like($r->ident("cOmmitter"), qr/^C O Mitter <committer\@example.com> [0-9]+ \+0000$/,
55 "ident scalar: committer (type)");
56 is($r->ident("invalid"), "invalid", "ident scalar: invalid ident string (no parsing)");
57 my ($name, $email, $time_tz) = $r->ident('author');
58 is_deeply([$name, $email], ["A U Thor", "author\@example.com"],
59 "ident array: author");
60 like($time_tz, qr/[0-9]+ \+0000/, "ident array: author");
61 is_deeply([$r->ident("Name <email> 123 +0000")], ["Name", "email", "123 +0000"],
62 "ident array: ident string");
63 is_deeply([$r->ident("invalid")], [], "ident array: invalid ident string");
65 # ident_person
66 is($r->ident_person("aUthor"), "A U Thor <author\@example.com>",
67 "ident_person: author (type)");
68 is($r->ident_person("Name <email> 123 +0000"), "Name <email>",
69 "ident_person: ident string");
70 is($r->ident_person("Name", "email", "123 +0000"), "Name <email>",
71 "ident_person: array");
73 # objects and hashes
74 ok(our $file1hash = $r->command_oneline('rev-parse', "HEAD:file1"), "(get file hash)");
75 my $tmpfile = "file.tmp";
76 open TEMPFILE, "+>$tmpfile" or die "Can't open $tmpfile: $!";
77 is($r->cat_blob($file1hash, \*TEMPFILE), 15, "cat_blob: size");
78 our $blobcontents;
79 { local $/; seek TEMPFILE, 0, 0; $blobcontents = <TEMPFILE>; }
80 is($blobcontents, "changed file 1\n", "cat_blob: data");
81 close TEMPFILE or die "Failed writing to $tmpfile: $!";
82 is(Git::hash_object("blob", $tmpfile), $file1hash, "hash_object: roundtrip");
83 open TEMPFILE, ">$tmpfile" or die "Can't open $tmpfile: $!";
84 print TEMPFILE my $test_text = "test blob, to be inserted\n";
85 close TEMPFILE or die "Failed writing to $tmpfile: $!";
86 like(our $newhash = $r->hash_and_insert_object($tmpfile), qr/[0-9a-fA-F]{40}/,
87 "hash_and_insert_object: returns hash");
88 open TEMPFILE, "+>$tmpfile" or die "Can't open $tmpfile: $!";
89 is($r->cat_blob($newhash, \*TEMPFILE), length $test_text, "cat_blob: roundtrip size");
90 { local $/; seek TEMPFILE, 0, 0; $blobcontents = <TEMPFILE>; }
91 is($blobcontents, $test_text, "cat_blob: roundtrip data");
92 close TEMPFILE;
93 unlink $tmpfile;
95 # paths
96 is($r->repo_path, $abs_repo_dir . "/.git", "repo_path");
97 is($r->wc_path, $abs_repo_dir . "/", "wc_path");
98 is($r->wc_subdir, "", "wc_subdir initial");
99 $r->wc_chdir("directory1");
100 is($r->wc_subdir, "directory1", "wc_subdir after wc_chdir");
101 is($r->config("test.string"), "value", "config after wc_chdir");
103 # Object generation in sub directory
104 chdir("directory2");
105 my $r2 = Git->repository();
106 is($r2->repo_path, $abs_repo_dir . "/.git", "repo_path (2)");
107 is($r2->wc_path, $abs_repo_dir . "/", "wc_path (2)");
108 is($r2->wc_subdir, "directory2/", "wc_subdir initial (2)");
110 # commands in sub directory
111 my $last_commit = $r2->command_oneline(qw(rev-parse --verify HEAD));
112 like($last_commit, qr/^[0-9a-fA-F]{40}$/, 'rev-parse returned hash');
113 my $dir_commit = $r2->command_oneline('log', '-n1', '--pretty=format:%H', '.');
114 isnt($last_commit, $dir_commit, 'log . does not show last commit');
116 # commands outside working tree
117 chdir($abs_repo_dir . '/..');
118 my $r3 = Git->repository(Directory => $abs_repo_dir);
119 my $tmpfile3 = "$abs_repo_dir/file3.tmp";
120 open TEMPFILE3, "+>$tmpfile3" or die "Can't open $tmpfile3: $!";
121 is($r3->cat_blob($file1hash, \*TEMPFILE3), 15, "cat_blob(outside): size");
122 close TEMPFILE3;
123 unlink $tmpfile3;
124 chdir($abs_repo_dir);
126 printf "1..%d\n", Test::More->builder->current_test;
128 my $is_passing = eval { Test::More->is_passing };
129 exit($is_passing ? 0 : 1) unless $@ =~ /Can't locate object method/;