Lea Wiemann | b4780d7 | 2008-06-19 22:32:49 +0200 | [diff] [blame] | 1 | #!/usr/bin/perl |
| 2 | use lib (split(/:/, $ENV{GITPERLLIB})); |
| 3 | |
Ævar Arnfjörð Bjarmason | d48b284 | 2010-09-24 20:00:52 +0000 | [diff] [blame] | 4 | use 5.008; |
Lea Wiemann | b4780d7 | 2008-06-19 22:32:49 +0200 | [diff] [blame] | 5 | use warnings; |
| 6 | use strict; |
| 7 | |
| 8 | use Test::More qw(no_plan); |
| 9 | |
Ævar Arnfjörð Bjarmason | d998bd4 | 2010-06-24 17:44:46 +0000 | [diff] [blame] | 10 | BEGIN { |
| 11 | # t9700-perl-git.sh kicks off our testing, so we have to go from |
| 12 | # there. |
Brandon Casey | 15eeb6e | 2010-06-28 17:51:02 -0500 | [diff] [blame] | 13 | Test::More->builder->current_test(1); |
| 14 | Test::More->builder->no_ending(1); |
Ævar Arnfjörð Bjarmason | d998bd4 | 2010-06-24 17:44:46 +0000 | [diff] [blame] | 15 | } |
| 16 | |
Lea Wiemann | b4780d7 | 2008-06-19 22:32:49 +0200 | [diff] [blame] | 17 | use Cwd; |
| 18 | use File::Basename; |
Lea Wiemann | b4780d7 | 2008-06-19 22:32:49 +0200 | [diff] [blame] | 19 | |
Jeff King | 839b639 | 2016-03-04 06:43:21 -0500 | [diff] [blame] | 20 | sub adjust_dirsep { |
| 21 | my $path = shift; |
| 22 | $path =~ s{\\}{/}g; |
| 23 | return $path; |
| 24 | } |
| 25 | |
brian m. carlson | e0a646e | 2020-07-29 23:14:15 +0000 | [diff] [blame] | 26 | my $oid_re = qr/^[0-9a-fA-F]{40}(?:[0-9a-fA-F]{24})?$/; |
| 27 | |
Lea Wiemann | b4780d7 | 2008-06-19 22:32:49 +0200 | [diff] [blame] | 28 | BEGIN { use_ok('Git') } |
| 29 | |
| 30 | # set up |
Ramsay Jones | 81f4026 | 2009-11-19 18:41:20 +0000 | [diff] [blame] | 31 | our $abs_repo_dir = cwd(); |
Lea Wiemann | b4780d7 | 2008-06-19 22:32:49 +0200 | [diff] [blame] | 32 | ok(our $r = Git->repository(Directory => "."), "open repository"); |
Jeff King | 20da61f | 2022-10-22 18:08:59 -0400 | [diff] [blame^] | 33 | { |
| 34 | local $ENV{GIT_TEST_ASSUME_DIFFERENT_OWNER} = 1; |
| 35 | my $failed; |
| 36 | |
| 37 | $failed = eval { Git->repository(Directory => $abs_repo_dir) }; |
| 38 | ok(!$failed, "reject unsafe non-bare repository"); |
| 39 | like($@, qr/not a git repository/i, "unsafe error message"); |
| 40 | |
| 41 | $failed = eval { Git->repository(Directory => "$abs_repo_dir/bare.git") }; |
| 42 | ok(!$failed, "reject unsafe bare repository"); |
| 43 | like($@, qr/not a git repository/i, "unsafe error message"); |
| 44 | } |
Lea Wiemann | b4780d7 | 2008-06-19 22:32:49 +0200 | [diff] [blame] | 45 | |
| 46 | # config |
| 47 | is($r->config("test.string"), "value", "config scalar: string"); |
| 48 | is_deeply([$r->config("test.dupstring")], ["value1", "value2"], |
| 49 | "config array: string"); |
| 50 | is($r->config("test.nonexistent"), undef, "config scalar: nonexistent"); |
| 51 | is_deeply([$r->config("test.nonexistent")], [], "config array: nonexistent"); |
| 52 | is($r->config_int("test.int"), 2048, "config_int: integer"); |
| 53 | is($r->config_int("test.nonexistent"), undef, "config_int: nonexistent"); |
| 54 | ok($r->config_bool("test.booltrue"), "config_bool: true"); |
| 55 | ok(!$r->config_bool("test.boolfalse"), "config_bool: false"); |
Jeff King | 839b639 | 2016-03-04 06:43:21 -0500 | [diff] [blame] | 56 | is(adjust_dirsep($r->config_path("test.path")), $r->config("test.pathexpanded"), |
Jakub Narebski | cb9c9df | 2011-10-21 20:42:44 +0200 | [diff] [blame] | 57 | "config_path: ~/foo expansion"); |
| 58 | is_deeply([$r->config_path("test.pathmulti")], ["foo", "bar"], |
| 59 | "config_path: multiple values"); |
Lea Wiemann | b4780d7 | 2008-06-19 22:32:49 +0200 | [diff] [blame] | 60 | our $ansi_green = "\x1b[32m"; |
| 61 | is($r->get_color("color.test.slot1", "red"), $ansi_green, "get_color"); |
| 62 | # Cannot test $r->get_colorbool("color.foo")) because we do not |
| 63 | # control whether our STDOUT is a terminal. |
| 64 | |
| 65 | # Failure cases for config: |
| 66 | # Save and restore STDERR; we will probably extract this into a |
| 67 | # "dies_ok" method and possibly move the STDERR handling to Git.pm. |
Thomas Rast | a749c0b | 2013-04-04 22:41:42 +0200 | [diff] [blame] | 68 | open our $tmpstderr, ">&STDERR" or die "cannot save STDERR"; |
| 69 | open STDERR, ">", "/dev/null" or die "cannot redirect STDERR to /dev/null"; |
Jeff King | 00b347d | 2012-10-23 16:52:44 -0400 | [diff] [blame] | 70 | is($r->config("test.dupstring"), "value2", "config: multivar"); |
Lea Wiemann | b4780d7 | 2008-06-19 22:32:49 +0200 | [diff] [blame] | 71 | eval { $r->config_bool("test.boolother") }; |
| 72 | ok($@, "config_bool: non-boolean values fail"); |
| 73 | open STDERR, ">&", $tmpstderr or die "cannot restore STDERR"; |
| 74 | |
| 75 | # ident |
Jeff King | fccf41e | 2020-07-09 16:35:50 -0400 | [diff] [blame] | 76 | like($r->ident("aUthor"), qr/^A U Thor <author\@example.com> [0-9]+ [+-]\d{4}$/, |
Lea Wiemann | b4780d7 | 2008-06-19 22:32:49 +0200 | [diff] [blame] | 77 | "ident scalar: author (type)"); |
Jeff King | fccf41e | 2020-07-09 16:35:50 -0400 | [diff] [blame] | 78 | like($r->ident("cOmmitter"), qr/^C O Mitter <committer\@example.com> [0-9]+ [+-]\d{4}$/, |
Lea Wiemann | b4780d7 | 2008-06-19 22:32:49 +0200 | [diff] [blame] | 79 | "ident scalar: committer (type)"); |
| 80 | is($r->ident("invalid"), "invalid", "ident scalar: invalid ident string (no parsing)"); |
| 81 | my ($name, $email, $time_tz) = $r->ident('author'); |
| 82 | is_deeply([$name, $email], ["A U Thor", "author\@example.com"], |
| 83 | "ident array: author"); |
Jeff King | fccf41e | 2020-07-09 16:35:50 -0400 | [diff] [blame] | 84 | like($time_tz, qr/[0-9]+ [+-]\d{4}/, "ident array: author"); |
Lea Wiemann | b4780d7 | 2008-06-19 22:32:49 +0200 | [diff] [blame] | 85 | is_deeply([$r->ident("Name <email> 123 +0000")], ["Name", "email", "123 +0000"], |
| 86 | "ident array: ident string"); |
| 87 | is_deeply([$r->ident("invalid")], [], "ident array: invalid ident string"); |
| 88 | |
| 89 | # ident_person |
| 90 | is($r->ident_person("aUthor"), "A U Thor <author\@example.com>", |
| 91 | "ident_person: author (type)"); |
| 92 | is($r->ident_person("Name <email> 123 +0000"), "Name <email>", |
| 93 | "ident_person: ident string"); |
| 94 | is($r->ident_person("Name", "email", "123 +0000"), "Name <email>", |
| 95 | "ident_person: array"); |
| 96 | |
| 97 | # objects and hashes |
| 98 | ok(our $file1hash = $r->command_oneline('rev-parse', "HEAD:file1"), "(get file hash)"); |
Brandon Casey | bf55778 | 2008-09-15 11:25:22 -0500 | [diff] [blame] | 99 | my $tmpfile = "file.tmp"; |
| 100 | open TEMPFILE, "+>$tmpfile" or die "Can't open $tmpfile: $!"; |
| 101 | is($r->cat_blob($file1hash, \*TEMPFILE), 15, "cat_blob: size"); |
Lea Wiemann | b4780d7 | 2008-06-19 22:32:49 +0200 | [diff] [blame] | 102 | our $blobcontents; |
Brandon Casey | bf55778 | 2008-09-15 11:25:22 -0500 | [diff] [blame] | 103 | { local $/; seek TEMPFILE, 0, 0; $blobcontents = <TEMPFILE>; } |
Lea Wiemann | b4780d7 | 2008-06-19 22:32:49 +0200 | [diff] [blame] | 104 | is($blobcontents, "changed file 1\n", "cat_blob: data"); |
Brandon Casey | bf55778 | 2008-09-15 11:25:22 -0500 | [diff] [blame] | 105 | close TEMPFILE or die "Failed writing to $tmpfile: $!"; |
Lea Wiemann | b4780d7 | 2008-06-19 22:32:49 +0200 | [diff] [blame] | 106 | is(Git::hash_object("blob", $tmpfile), $file1hash, "hash_object: roundtrip"); |
Brandon Casey | bf55778 | 2008-09-15 11:25:22 -0500 | [diff] [blame] | 107 | open TEMPFILE, ">$tmpfile" or die "Can't open $tmpfile: $!"; |
| 108 | print TEMPFILE my $test_text = "test blob, to be inserted\n"; |
| 109 | close TEMPFILE or die "Failed writing to $tmpfile: $!"; |
brian m. carlson | e0a646e | 2020-07-29 23:14:15 +0000 | [diff] [blame] | 110 | like(our $newhash = $r->hash_and_insert_object($tmpfile), $oid_re, |
Lea Wiemann | b4780d7 | 2008-06-19 22:32:49 +0200 | [diff] [blame] | 111 | "hash_and_insert_object: returns hash"); |
Brandon Casey | bf55778 | 2008-09-15 11:25:22 -0500 | [diff] [blame] | 112 | open TEMPFILE, "+>$tmpfile" or die "Can't open $tmpfile: $!"; |
| 113 | is($r->cat_blob($newhash, \*TEMPFILE), length $test_text, "cat_blob: roundtrip size"); |
| 114 | { local $/; seek TEMPFILE, 0, 0; $blobcontents = <TEMPFILE>; } |
Lea Wiemann | b4780d7 | 2008-06-19 22:32:49 +0200 | [diff] [blame] | 115 | is($blobcontents, $test_text, "cat_blob: roundtrip data"); |
Brandon Casey | bf55778 | 2008-09-15 11:25:22 -0500 | [diff] [blame] | 116 | close TEMPFILE; |
| 117 | unlink $tmpfile; |
Lea Wiemann | b4780d7 | 2008-06-19 22:32:49 +0200 | [diff] [blame] | 118 | |
| 119 | # paths |
Frank Lichtenheld | fe53bbc | 2009-05-07 15:41:28 +0200 | [diff] [blame] | 120 | is($r->repo_path, $abs_repo_dir . "/.git", "repo_path"); |
Lea Wiemann | b4780d7 | 2008-06-19 22:32:49 +0200 | [diff] [blame] | 121 | is($r->wc_path, $abs_repo_dir . "/", "wc_path"); |
| 122 | is($r->wc_subdir, "", "wc_subdir initial"); |
| 123 | $r->wc_chdir("directory1"); |
| 124 | is($r->wc_subdir, "directory1", "wc_subdir after wc_chdir"); |
Frank Lichtenheld | fe53bbc | 2009-05-07 15:41:28 +0200 | [diff] [blame] | 125 | is($r->config("test.string"), "value", "config after wc_chdir"); |
Frank Lichtenheld | da159c7 | 2009-05-07 15:41:27 +0200 | [diff] [blame] | 126 | |
| 127 | # Object generation in sub directory |
| 128 | chdir("directory2"); |
| 129 | my $r2 = Git->repository(); |
| 130 | is($r2->repo_path, $abs_repo_dir . "/.git", "repo_path (2)"); |
| 131 | is($r2->wc_path, $abs_repo_dir . "/", "wc_path (2)"); |
| 132 | is($r2->wc_subdir, "directory2/", "wc_subdir initial (2)"); |
| 133 | |
| 134 | # commands in sub directory |
| 135 | my $last_commit = $r2->command_oneline(qw(rev-parse --verify HEAD)); |
brian m. carlson | e0a646e | 2020-07-29 23:14:15 +0000 | [diff] [blame] | 136 | like($last_commit, $oid_re, 'rev-parse returned hash'); |
Frank Lichtenheld | da159c7 | 2009-05-07 15:41:27 +0200 | [diff] [blame] | 137 | my $dir_commit = $r2->command_oneline('log', '-n1', '--pretty=format:%H', '.'); |
| 138 | isnt($last_commit, $dir_commit, 'log . does not show last commit'); |
Ævar Arnfjörð Bjarmason | d998bd4 | 2010-06-24 17:44:46 +0000 | [diff] [blame] | 139 | |
Masatake Osanai | 48d9e6a | 2011-02-15 07:13:04 +0900 | [diff] [blame] | 140 | # commands outside working tree |
| 141 | chdir($abs_repo_dir . '/..'); |
| 142 | my $r3 = Git->repository(Directory => $abs_repo_dir); |
| 143 | my $tmpfile3 = "$abs_repo_dir/file3.tmp"; |
| 144 | open TEMPFILE3, "+>$tmpfile3" or die "Can't open $tmpfile3: $!"; |
| 145 | is($r3->cat_blob($file1hash, \*TEMPFILE3), 15, "cat_blob(outside): size"); |
| 146 | close TEMPFILE3; |
| 147 | unlink $tmpfile3; |
| 148 | chdir($abs_repo_dir); |
| 149 | |
Phillip Wood | 3f9c637 | 2017-06-30 10:49:12 +0100 | [diff] [blame] | 150 | # unquoting paths |
| 151 | is(Git::unquote_path('abc'), 'abc', 'unquote unquoted path'); |
| 152 | is(Git::unquote_path('"abc def"'), 'abc def', 'unquote simple quoted path'); |
| 153 | is(Git::unquote_path('"abc\"\\\\ \a\b\t\n\v\f\r\001\040"'), |
| 154 | "abc\"\\ \x07\x08\x09\x0a\x0b\x0c\x0d\x01 ", |
| 155 | 'unquote escape sequences'); |
| 156 | |
Brandon Casey | 15eeb6e | 2010-06-28 17:51:02 -0500 | [diff] [blame] | 157 | printf "1..%d\n", Test::More->builder->current_test; |
Ævar Arnfjörð Bjarmason | d998bd4 | 2010-06-24 17:44:46 +0000 | [diff] [blame] | 158 | |
Ævar Arnfjörð Bjarmason | 635155f | 2010-06-26 12:42:41 +0000 | [diff] [blame] | 159 | my $is_passing = eval { Test::More->is_passing }; |
| 160 | exit($is_passing ? 0 : 1) unless $@ =~ /Can't locate object method/; |