]>
Commit | Line | Data |
---|---|---|
b4780d72 LW |
1 | #!/usr/bin/perl |
2 | use lib (split(/:/, $ENV{GITPERLLIB})); | |
3 | ||
4 | use 5.006002; | |
5 | use warnings; | |
6 | use strict; | |
7 | ||
8 | use Test::More qw(no_plan); | |
9 | ||
10 | use Cwd; | |
11 | use File::Basename; | |
b4780d72 LW |
12 | |
13 | BEGIN { use_ok('Git') } | |
14 | ||
15 | # set up | |
b4780d72 | 16 | our $abs_repo_dir = Cwd->cwd; |
b4780d72 LW |
17 | ok(our $r = Git->repository(Directory => "."), "open repository"); |
18 | ||
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. | |
33 | ||
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. | |
8409bb37 | 37 | open our $tmpstderr, ">&STDERR" or die "cannot save STDERR"; close STDERR; |
b4780d72 LW |
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"; | |
43 | ||
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"); | |
57 | ||
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"); | |
65 | ||
66 | # objects and hashes | |
67 | ok(our $file1hash = $r->command_oneline('rev-parse', "HEAD:file1"), "(get file hash)"); | |
bf557788 BC |
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"); | |
b4780d72 | 71 | our $blobcontents; |
bf557788 | 72 | { local $/; seek TEMPFILE, 0, 0; $blobcontents = <TEMPFILE>; } |
b4780d72 | 73 | is($blobcontents, "changed file 1\n", "cat_blob: data"); |
bf557788 | 74 | close TEMPFILE or die "Failed writing to $tmpfile: $!"; |
b4780d72 | 75 | is(Git::hash_object("blob", $tmpfile), $file1hash, "hash_object: roundtrip"); |
bf557788 BC |
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: $!"; | |
b4780d72 LW |
79 | like(our $newhash = $r->hash_and_insert_object($tmpfile), qr/[0-9a-fA-F]{40}/, |
80 | "hash_and_insert_object: returns hash"); | |
bf557788 BC |
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>; } | |
b4780d72 | 84 | is($blobcontents, $test_text, "cat_blob: roundtrip data"); |
bf557788 BC |
85 | close TEMPFILE; |
86 | unlink $tmpfile; | |
b4780d72 LW |
87 | |
88 | # paths | |
89 | is($r->repo_path, "./.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 | TODO: { | |
95 | local $TODO = "commands do not work after wc_chdir"; | |
96 | # Failure output is active even in non-verbose mode and thus | |
97 | # annoying. Hence we skip these tests as long as they fail. | |
98 | todo_skip 'config after wc_chdir', 1; | |
99 | is($r->config("color.string"), "value", "config after wc_chdir"); | |
100 | } |