t / t9700 / test.plon commit Merge branch 'maint' (9a6e2d7)
   1#!/usr/bin/perl
   2use lib (split(/:/, $ENV{GITPERLLIB}));
   3
   4use 5.008;
   5use warnings;
   6use strict;
   7
   8use Test::More qw(no_plan);
   9
  10BEGIN {
  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);
  15}
  16
  17use Cwd;
  18use File::Basename;
  19
  20BEGIN { use_ok('Git') }
  21
  22# set up
  23our $abs_repo_dir = cwd();
  24ok(our $r = Git->repository(Directory => "."), "open repository");
  25
  26# config
  27is($r->config("test.string"), "value", "config scalar: string");
  28is_deeply([$r->config("test.dupstring")], ["value1", "value2"],
  29          "config array: string");
  30is($r->config("test.nonexistent"), undef, "config scalar: nonexistent");
  31is_deeply([$r->config("test.nonexistent")], [], "config array: nonexistent");
  32is($r->config_int("test.int"), 2048, "config_int: integer");
  33is($r->config_int("test.nonexistent"), undef, "config_int: nonexistent");
  34ok($r->config_bool("test.booltrue"), "config_bool: true");
  35ok(!$r->config_bool("test.boolfalse"), "config_bool: false");
  36is($r->config_path("test.path"), $r->config("test.pathexpanded"),
  37   "config_path: ~/foo expansion");
  38is_deeply([$r->config_path("test.pathmulti")], ["foo", "bar"],
  39   "config_path: multiple values");
  40our $ansi_green = "\x1b[32m";
  41is($r->get_color("color.test.slot1", "red"), $ansi_green, "get_color");
  42# Cannot test $r->get_colorbool("color.foo")) because we do not
  43# control whether our STDOUT is a terminal.
  44
  45# Failure cases for config:
  46# Save and restore STDERR; we will probably extract this into a
  47# "dies_ok" method and possibly move the STDERR handling to Git.pm.
  48open our $tmpstderr, ">&STDERR" or die "cannot save STDERR"; close STDERR;
  49eval { $r->config("test.dupstring") };
  50ok($@, "config: duplicate entry in scalar context fails");
  51eval { $r->config_bool("test.boolother") };
  52ok($@, "config_bool: non-boolean values fail");
  53open STDERR, ">&", $tmpstderr or die "cannot restore STDERR";
  54
  55# ident
  56like($r->ident("aUthor"), qr/^A U Thor <author\@example.com> [0-9]+ \+0000$/,
  57     "ident scalar: author (type)");
  58like($r->ident("cOmmitter"), qr/^C O Mitter <committer\@example.com> [0-9]+ \+0000$/,
  59     "ident scalar: committer (type)");
  60is($r->ident("invalid"), "invalid", "ident scalar: invalid ident string (no parsing)");
  61my ($name, $email, $time_tz) = $r->ident('author');
  62is_deeply([$name, $email], ["A U Thor", "author\@example.com"],
  63         "ident array: author");
  64like($time_tz, qr/[0-9]+ \+0000/, "ident array: author");
  65is_deeply([$r->ident("Name <email> 123 +0000")], ["Name", "email", "123 +0000"],
  66          "ident array: ident string");
  67is_deeply([$r->ident("invalid")], [], "ident array: invalid ident string");
  68
  69# ident_person
  70is($r->ident_person("aUthor"), "A U Thor <author\@example.com>",
  71   "ident_person: author (type)");
  72is($r->ident_person("Name <email> 123 +0000"), "Name <email>",
  73   "ident_person: ident string");
  74is($r->ident_person("Name", "email", "123 +0000"), "Name <email>",
  75   "ident_person: array");
  76
  77# objects and hashes
  78ok(our $file1hash = $r->command_oneline('rev-parse', "HEAD:file1"), "(get file hash)");
  79my $tmpfile = "file.tmp";
  80open TEMPFILE, "+>$tmpfile" or die "Can't open $tmpfile: $!";
  81is($r->cat_blob($file1hash, \*TEMPFILE), 15, "cat_blob: size");
  82our $blobcontents;
  83{ local $/; seek TEMPFILE, 0, 0; $blobcontents = <TEMPFILE>; }
  84is($blobcontents, "changed file 1\n", "cat_blob: data");
  85close TEMPFILE or die "Failed writing to $tmpfile: $!";
  86is(Git::hash_object("blob", $tmpfile), $file1hash, "hash_object: roundtrip");
  87open TEMPFILE, ">$tmpfile" or die "Can't open $tmpfile: $!";
  88print TEMPFILE my $test_text = "test blob, to be inserted\n";
  89close TEMPFILE or die "Failed writing to $tmpfile: $!";
  90like(our $newhash = $r->hash_and_insert_object($tmpfile), qr/[0-9a-fA-F]{40}/,
  91     "hash_and_insert_object: returns hash");
  92open TEMPFILE, "+>$tmpfile" or die "Can't open $tmpfile: $!";
  93is($r->cat_blob($newhash, \*TEMPFILE), length $test_text, "cat_blob: roundtrip size");
  94{ local $/; seek TEMPFILE, 0, 0; $blobcontents = <TEMPFILE>; }
  95is($blobcontents, $test_text, "cat_blob: roundtrip data");
  96close TEMPFILE;
  97unlink $tmpfile;
  98
  99# paths
 100is($r->repo_path, $abs_repo_dir . "/.git", "repo_path");
 101is($r->wc_path, $abs_repo_dir . "/", "wc_path");
 102is($r->wc_subdir, "", "wc_subdir initial");
 103$r->wc_chdir("directory1");
 104is($r->wc_subdir, "directory1", "wc_subdir after wc_chdir");
 105is($r->config("test.string"), "value", "config after wc_chdir");
 106
 107# Object generation in sub directory
 108chdir("directory2");
 109my $r2 = Git->repository();
 110is($r2->repo_path, $abs_repo_dir . "/.git", "repo_path (2)");
 111is($r2->wc_path, $abs_repo_dir . "/", "wc_path (2)");
 112is($r2->wc_subdir, "directory2/", "wc_subdir initial (2)");
 113
 114# commands in sub directory
 115my $last_commit = $r2->command_oneline(qw(rev-parse --verify HEAD));
 116like($last_commit, qr/^[0-9a-fA-F]{40}$/, 'rev-parse returned hash');
 117my $dir_commit = $r2->command_oneline('log', '-n1', '--pretty=format:%H', '.');
 118isnt($last_commit, $dir_commit, 'log . does not show last commit');
 119
 120# commands outside working tree
 121chdir($abs_repo_dir . '/..');
 122my $r3 = Git->repository(Directory => $abs_repo_dir);
 123my $tmpfile3 = "$abs_repo_dir/file3.tmp";
 124open TEMPFILE3, "+>$tmpfile3" or die "Can't open $tmpfile3: $!";
 125is($r3->cat_blob($file1hash, \*TEMPFILE3), 15, "cat_blob(outside): size");
 126close TEMPFILE3;
 127unlink $tmpfile3;
 128chdir($abs_repo_dir);
 129
 130printf "1..%d\n", Test::More->builder->current_test;
 131
 132my $is_passing = eval { Test::More->is_passing };
 133exit($is_passing ? 0 : 1) unless $@ =~ /Can't locate object method/;