]>
Commit | Line | Data |
---|---|---|
b4780d72 LW |
1 | #!/usr/bin/perl |
2 | use lib (split(/:/, $ENV{GITPERLLIB})); | |
3 | ||
d48b2841 | 4 | use 5.008; |
b4780d72 LW |
5 | use warnings; |
6 | use strict; | |
7 | ||
8 | use Test::More qw(no_plan); | |
9 | ||
d998bd4a ÆAB |
10 | BEGIN { |
11 | # t9700-perl-git.sh kicks off our testing, so we have to go from | |
12 | # there. | |
15eeb6e9 BC |
13 | Test::More->builder->current_test(1); |
14 | Test::More->builder->no_ending(1); | |
d998bd4a ÆAB |
15 | } |
16 | ||
b4780d72 LW |
17 | use Cwd; |
18 | use File::Basename; | |
b4780d72 | 19 | |
839b6397 JK |
20 | sub adjust_dirsep { |
21 | my $path = shift; | |
22 | $path =~ s{\\}{/}g; | |
23 | return $path; | |
24 | } | |
25 | ||
b4780d72 LW |
26 | BEGIN { use_ok('Git') } |
27 | ||
28 | # set up | |
81f40262 | 29 | our $abs_repo_dir = cwd(); |
b4780d72 LW |
30 | ok(our $r = Git->repository(Directory => "."), "open repository"); |
31 | ||
32 | # config | |
33 | is($r->config("test.string"), "value", "config scalar: string"); | |
34 | is_deeply([$r->config("test.dupstring")], ["value1", "value2"], | |
35 | "config array: string"); | |
36 | is($r->config("test.nonexistent"), undef, "config scalar: nonexistent"); | |
37 | is_deeply([$r->config("test.nonexistent")], [], "config array: nonexistent"); | |
38 | is($r->config_int("test.int"), 2048, "config_int: integer"); | |
39 | is($r->config_int("test.nonexistent"), undef, "config_int: nonexistent"); | |
40 | ok($r->config_bool("test.booltrue"), "config_bool: true"); | |
41 | ok(!$r->config_bool("test.boolfalse"), "config_bool: false"); | |
839b6397 | 42 | is(adjust_dirsep($r->config_path("test.path")), $r->config("test.pathexpanded"), |
cb9c9df3 JN |
43 | "config_path: ~/foo expansion"); |
44 | is_deeply([$r->config_path("test.pathmulti")], ["foo", "bar"], | |
45 | "config_path: multiple values"); | |
b4780d72 LW |
46 | our $ansi_green = "\x1b[32m"; |
47 | is($r->get_color("color.test.slot1", "red"), $ansi_green, "get_color"); | |
48 | # Cannot test $r->get_colorbool("color.foo")) because we do not | |
49 | # control whether our STDOUT is a terminal. | |
50 | ||
51 | # Failure cases for config: | |
52 | # Save and restore STDERR; we will probably extract this into a | |
53 | # "dies_ok" method and possibly move the STDERR handling to Git.pm. | |
a749c0bb TR |
54 | open our $tmpstderr, ">&STDERR" or die "cannot save STDERR"; |
55 | open STDERR, ">", "/dev/null" or die "cannot redirect STDERR to /dev/null"; | |
00b347d3 | 56 | is($r->config("test.dupstring"), "value2", "config: multivar"); |
b4780d72 LW |
57 | eval { $r->config_bool("test.boolother") }; |
58 | ok($@, "config_bool: non-boolean values fail"); | |
59 | open STDERR, ">&", $tmpstderr or die "cannot restore STDERR"; | |
60 | ||
61 | # ident | |
62 | like($r->ident("aUthor"), qr/^A U Thor <author\@example.com> [0-9]+ \+0000$/, | |
63 | "ident scalar: author (type)"); | |
64 | like($r->ident("cOmmitter"), qr/^C O Mitter <committer\@example.com> [0-9]+ \+0000$/, | |
65 | "ident scalar: committer (type)"); | |
66 | is($r->ident("invalid"), "invalid", "ident scalar: invalid ident string (no parsing)"); | |
67 | my ($name, $email, $time_tz) = $r->ident('author'); | |
68 | is_deeply([$name, $email], ["A U Thor", "author\@example.com"], | |
69 | "ident array: author"); | |
70 | like($time_tz, qr/[0-9]+ \+0000/, "ident array: author"); | |
71 | is_deeply([$r->ident("Name <email> 123 +0000")], ["Name", "email", "123 +0000"], | |
72 | "ident array: ident string"); | |
73 | is_deeply([$r->ident("invalid")], [], "ident array: invalid ident string"); | |
74 | ||
75 | # ident_person | |
76 | is($r->ident_person("aUthor"), "A U Thor <author\@example.com>", | |
77 | "ident_person: author (type)"); | |
78 | is($r->ident_person("Name <email> 123 +0000"), "Name <email>", | |
79 | "ident_person: ident string"); | |
80 | is($r->ident_person("Name", "email", "123 +0000"), "Name <email>", | |
81 | "ident_person: array"); | |
82 | ||
83 | # objects and hashes | |
84 | ok(our $file1hash = $r->command_oneline('rev-parse', "HEAD:file1"), "(get file hash)"); | |
bf557788 BC |
85 | my $tmpfile = "file.tmp"; |
86 | open TEMPFILE, "+>$tmpfile" or die "Can't open $tmpfile: $!"; | |
87 | is($r->cat_blob($file1hash, \*TEMPFILE), 15, "cat_blob: size"); | |
b4780d72 | 88 | our $blobcontents; |
bf557788 | 89 | { local $/; seek TEMPFILE, 0, 0; $blobcontents = <TEMPFILE>; } |
b4780d72 | 90 | is($blobcontents, "changed file 1\n", "cat_blob: data"); |
bf557788 | 91 | close TEMPFILE or die "Failed writing to $tmpfile: $!"; |
b4780d72 | 92 | is(Git::hash_object("blob", $tmpfile), $file1hash, "hash_object: roundtrip"); |
bf557788 BC |
93 | open TEMPFILE, ">$tmpfile" or die "Can't open $tmpfile: $!"; |
94 | print TEMPFILE my $test_text = "test blob, to be inserted\n"; | |
95 | close TEMPFILE or die "Failed writing to $tmpfile: $!"; | |
b4780d72 LW |
96 | like(our $newhash = $r->hash_and_insert_object($tmpfile), qr/[0-9a-fA-F]{40}/, |
97 | "hash_and_insert_object: returns hash"); | |
bf557788 BC |
98 | open TEMPFILE, "+>$tmpfile" or die "Can't open $tmpfile: $!"; |
99 | is($r->cat_blob($newhash, \*TEMPFILE), length $test_text, "cat_blob: roundtrip size"); | |
100 | { local $/; seek TEMPFILE, 0, 0; $blobcontents = <TEMPFILE>; } | |
b4780d72 | 101 | is($blobcontents, $test_text, "cat_blob: roundtrip data"); |
bf557788 BC |
102 | close TEMPFILE; |
103 | unlink $tmpfile; | |
b4780d72 LW |
104 | |
105 | # paths | |
fe53bbc9 | 106 | is($r->repo_path, $abs_repo_dir . "/.git", "repo_path"); |
b4780d72 LW |
107 | is($r->wc_path, $abs_repo_dir . "/", "wc_path"); |
108 | is($r->wc_subdir, "", "wc_subdir initial"); | |
109 | $r->wc_chdir("directory1"); | |
110 | is($r->wc_subdir, "directory1", "wc_subdir after wc_chdir"); | |
fe53bbc9 | 111 | is($r->config("test.string"), "value", "config after wc_chdir"); |
da159c77 FL |
112 | |
113 | # Object generation in sub directory | |
114 | chdir("directory2"); | |
115 | my $r2 = Git->repository(); | |
116 | is($r2->repo_path, $abs_repo_dir . "/.git", "repo_path (2)"); | |
117 | is($r2->wc_path, $abs_repo_dir . "/", "wc_path (2)"); | |
118 | is($r2->wc_subdir, "directory2/", "wc_subdir initial (2)"); | |
119 | ||
120 | # commands in sub directory | |
121 | my $last_commit = $r2->command_oneline(qw(rev-parse --verify HEAD)); | |
122 | like($last_commit, qr/^[0-9a-fA-F]{40}$/, 'rev-parse returned hash'); | |
123 | my $dir_commit = $r2->command_oneline('log', '-n1', '--pretty=format:%H', '.'); | |
124 | isnt($last_commit, $dir_commit, 'log . does not show last commit'); | |
d998bd4a | 125 | |
48d9e6ae MO |
126 | # commands outside working tree |
127 | chdir($abs_repo_dir . '/..'); | |
128 | my $r3 = Git->repository(Directory => $abs_repo_dir); | |
129 | my $tmpfile3 = "$abs_repo_dir/file3.tmp"; | |
130 | open TEMPFILE3, "+>$tmpfile3" or die "Can't open $tmpfile3: $!"; | |
131 | is($r3->cat_blob($file1hash, \*TEMPFILE3), 15, "cat_blob(outside): size"); | |
132 | close TEMPFILE3; | |
133 | unlink $tmpfile3; | |
134 | chdir($abs_repo_dir); | |
135 | ||
3f9c637e PW |
136 | # unquoting paths |
137 | is(Git::unquote_path('abc'), 'abc', 'unquote unquoted path'); | |
138 | is(Git::unquote_path('"abc def"'), 'abc def', 'unquote simple quoted path'); | |
139 | is(Git::unquote_path('"abc\"\\\\ \a\b\t\n\v\f\r\001\040"'), | |
140 | "abc\"\\ \x07\x08\x09\x0a\x0b\x0c\x0d\x01 ", | |
141 | 'unquote escape sequences'); | |
142 | ||
15eeb6e9 | 143 | printf "1..%d\n", Test::More->builder->current_test; |
d998bd4a | 144 | |
635155fa ÆAB |
145 | my $is_passing = eval { Test::More->is_passing }; |
146 | exit($is_passing ? 0 : 1) unless $@ =~ /Can't locate object method/; |