t / t9700 / test.plon commit Merge branch 'jn/maint-fix-pager' (52ebb06)
   1#!/usr/bin/perl
   2use lib (split(/:/, $ENV{GITPERLLIB}));
   3
   4use 5.006002;
   5use warnings;
   6use strict;
   7
   8use Test::More qw(no_plan);
   9
  10use Cwd;
  11use File::Basename;
  12
  13BEGIN { use_ok('Git') }
  14
  15# set up
  16our $abs_repo_dir = cwd();
  17ok(our $r = Git->repository(Directory => "."), "open repository");
  18
  19# config
  20is($r->config("test.string"), "value", "config scalar: string");
  21is_deeply([$r->config("test.dupstring")], ["value1", "value2"],
  22          "config array: string");
  23is($r->config("test.nonexistent"), undef, "config scalar: nonexistent");
  24is_deeply([$r->config("test.nonexistent")], [], "config array: nonexistent");
  25is($r->config_int("test.int"), 2048, "config_int: integer");
  26is($r->config_int("test.nonexistent"), undef, "config_int: nonexistent");
  27ok($r->config_bool("test.booltrue"), "config_bool: true");
  28ok(!$r->config_bool("test.boolfalse"), "config_bool: false");
  29our $ansi_green = "\x1b[32m";
  30is($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.
  37open our $tmpstderr, ">&STDERR" or die "cannot save STDERR"; close STDERR;
  38eval { $r->config("test.dupstring") };
  39ok($@, "config: duplicate entry in scalar context fails");
  40eval { $r->config_bool("test.boolother") };
  41ok($@, "config_bool: non-boolean values fail");
  42open STDERR, ">&", $tmpstderr or die "cannot restore STDERR";
  43
  44# ident
  45like($r->ident("aUthor"), qr/^A U Thor <author\@example.com> [0-9]+ \+0000$/,
  46     "ident scalar: author (type)");
  47like($r->ident("cOmmitter"), qr/^C O Mitter <committer\@example.com> [0-9]+ \+0000$/,
  48     "ident scalar: committer (type)");
  49is($r->ident("invalid"), "invalid", "ident scalar: invalid ident string (no parsing)");
  50my ($name, $email, $time_tz) = $r->ident('author');
  51is_deeply([$name, $email], ["A U Thor", "author\@example.com"],
  52         "ident array: author");
  53like($time_tz, qr/[0-9]+ \+0000/, "ident array: author");
  54is_deeply([$r->ident("Name <email> 123 +0000")], ["Name", "email", "123 +0000"],
  55          "ident array: ident string");
  56is_deeply([$r->ident("invalid")], [], "ident array: invalid ident string");
  57
  58# ident_person
  59is($r->ident_person("aUthor"), "A U Thor <author\@example.com>",
  60   "ident_person: author (type)");
  61is($r->ident_person("Name <email> 123 +0000"), "Name <email>",
  62   "ident_person: ident string");
  63is($r->ident_person("Name", "email", "123 +0000"), "Name <email>",
  64   "ident_person: array");
  65
  66# objects and hashes
  67ok(our $file1hash = $r->command_oneline('rev-parse', "HEAD:file1"), "(get file hash)");
  68my $tmpfile = "file.tmp";
  69open TEMPFILE, "+>$tmpfile" or die "Can't open $tmpfile: $!";
  70is($r->cat_blob($file1hash, \*TEMPFILE), 15, "cat_blob: size");
  71our $blobcontents;
  72{ local $/; seek TEMPFILE, 0, 0; $blobcontents = <TEMPFILE>; }
  73is($blobcontents, "changed file 1\n", "cat_blob: data");
  74close TEMPFILE or die "Failed writing to $tmpfile: $!";
  75is(Git::hash_object("blob", $tmpfile), $file1hash, "hash_object: roundtrip");
  76open TEMPFILE, ">$tmpfile" or die "Can't open $tmpfile: $!";
  77print TEMPFILE my $test_text = "test blob, to be inserted\n";
  78close TEMPFILE or die "Failed writing to $tmpfile: $!";
  79like(our $newhash = $r->hash_and_insert_object($tmpfile), qr/[0-9a-fA-F]{40}/,
  80     "hash_and_insert_object: returns hash");
  81open TEMPFILE, "+>$tmpfile" or die "Can't open $tmpfile: $!";
  82is($r->cat_blob($newhash, \*TEMPFILE), length $test_text, "cat_blob: roundtrip size");
  83{ local $/; seek TEMPFILE, 0, 0; $blobcontents = <TEMPFILE>; }
  84is($blobcontents, $test_text, "cat_blob: roundtrip data");
  85close TEMPFILE;
  86unlink $tmpfile;
  87
  88# paths
  89is($r->repo_path, $abs_repo_dir . "/.git", "repo_path");
  90is($r->wc_path, $abs_repo_dir . "/", "wc_path");
  91is($r->wc_subdir, "", "wc_subdir initial");
  92$r->wc_chdir("directory1");
  93is($r->wc_subdir, "directory1", "wc_subdir after wc_chdir");
  94is($r->config("test.string"), "value", "config after wc_chdir");
  95
  96# Object generation in sub directory
  97chdir("directory2");
  98my $r2 = Git->repository();
  99is($r2->repo_path, $abs_repo_dir . "/.git", "repo_path (2)");
 100is($r2->wc_path, $abs_repo_dir . "/", "wc_path (2)");
 101is($r2->wc_subdir, "directory2/", "wc_subdir initial (2)");
 102
 103# commands in sub directory
 104my $last_commit = $r2->command_oneline(qw(rev-parse --verify HEAD));
 105like($last_commit, qr/^[0-9a-fA-F]{40}$/, 'rev-parse returned hash');
 106my $dir_commit = $r2->command_oneline('log', '-n1', '--pretty=format:%H', '.');
 107isnt($last_commit, $dir_commit, 'log . does not show last commit');