t / t9700 / test.plon commit Merge branch 'jc/unused-symbols' (93a96cc)
   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
  20sub adjust_dirsep {
  21        my $path = shift;
  22        $path =~ s{\\}{/}g;
  23        return $path;
  24}
  25
  26BEGIN { use_ok('Git') }
  27
  28# set up
  29our $abs_repo_dir = cwd();
  30ok(our $r = Git->repository(Directory => "."), "open repository");
  31
  32# config
  33is($r->config("test.string"), "value", "config scalar: string");
  34is_deeply([$r->config("test.dupstring")], ["value1", "value2"],
  35          "config array: string");
  36is($r->config("test.nonexistent"), undef, "config scalar: nonexistent");
  37is_deeply([$r->config("test.nonexistent")], [], "config array: nonexistent");
  38is($r->config_int("test.int"), 2048, "config_int: integer");
  39is($r->config_int("test.nonexistent"), undef, "config_int: nonexistent");
  40ok($r->config_bool("test.booltrue"), "config_bool: true");
  41ok(!$r->config_bool("test.boolfalse"), "config_bool: false");
  42is(adjust_dirsep($r->config_path("test.path")), $r->config("test.pathexpanded"),
  43   "config_path: ~/foo expansion");
  44is_deeply([$r->config_path("test.pathmulti")], ["foo", "bar"],
  45   "config_path: multiple values");
  46our $ansi_green = "\x1b[32m";
  47is($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.
  54open our $tmpstderr, ">&STDERR" or die "cannot save STDERR";
  55open STDERR, ">", "/dev/null" or die "cannot redirect STDERR to /dev/null";
  56is($r->config("test.dupstring"), "value2", "config: multivar");
  57eval { $r->config_bool("test.boolother") };
  58ok($@, "config_bool: non-boolean values fail");
  59open STDERR, ">&", $tmpstderr or die "cannot restore STDERR";
  60
  61# ident
  62like($r->ident("aUthor"), qr/^A U Thor <author\@example.com> [0-9]+ \+0000$/,
  63     "ident scalar: author (type)");
  64like($r->ident("cOmmitter"), qr/^C O Mitter <committer\@example.com> [0-9]+ \+0000$/,
  65     "ident scalar: committer (type)");
  66is($r->ident("invalid"), "invalid", "ident scalar: invalid ident string (no parsing)");
  67my ($name, $email, $time_tz) = $r->ident('author');
  68is_deeply([$name, $email], ["A U Thor", "author\@example.com"],
  69         "ident array: author");
  70like($time_tz, qr/[0-9]+ \+0000/, "ident array: author");
  71is_deeply([$r->ident("Name <email> 123 +0000")], ["Name", "email", "123 +0000"],
  72          "ident array: ident string");
  73is_deeply([$r->ident("invalid")], [], "ident array: invalid ident string");
  74
  75# ident_person
  76is($r->ident_person("aUthor"), "A U Thor <author\@example.com>",
  77   "ident_person: author (type)");
  78is($r->ident_person("Name <email> 123 +0000"), "Name <email>",
  79   "ident_person: ident string");
  80is($r->ident_person("Name", "email", "123 +0000"), "Name <email>",
  81   "ident_person: array");
  82
  83# objects and hashes
  84ok(our $file1hash = $r->command_oneline('rev-parse', "HEAD:file1"), "(get file hash)");
  85my $tmpfile = "file.tmp";
  86open TEMPFILE, "+>$tmpfile" or die "Can't open $tmpfile: $!";
  87is($r->cat_blob($file1hash, \*TEMPFILE), 15, "cat_blob: size");
  88our $blobcontents;
  89{ local $/; seek TEMPFILE, 0, 0; $blobcontents = <TEMPFILE>; }
  90is($blobcontents, "changed file 1\n", "cat_blob: data");
  91close TEMPFILE or die "Failed writing to $tmpfile: $!";
  92is(Git::hash_object("blob", $tmpfile), $file1hash, "hash_object: roundtrip");
  93open TEMPFILE, ">$tmpfile" or die "Can't open $tmpfile: $!";
  94print TEMPFILE my $test_text = "test blob, to be inserted\n";
  95close TEMPFILE or die "Failed writing to $tmpfile: $!";
  96like(our $newhash = $r->hash_and_insert_object($tmpfile), qr/[0-9a-fA-F]{40}/,
  97     "hash_and_insert_object: returns hash");
  98open TEMPFILE, "+>$tmpfile" or die "Can't open $tmpfile: $!";
  99is($r->cat_blob($newhash, \*TEMPFILE), length $test_text, "cat_blob: roundtrip size");
 100{ local $/; seek TEMPFILE, 0, 0; $blobcontents = <TEMPFILE>; }
 101is($blobcontents, $test_text, "cat_blob: roundtrip data");
 102close TEMPFILE;
 103unlink $tmpfile;
 104
 105# paths
 106is($r->repo_path, $abs_repo_dir . "/.git", "repo_path");
 107is($r->wc_path, $abs_repo_dir . "/", "wc_path");
 108is($r->wc_subdir, "", "wc_subdir initial");
 109$r->wc_chdir("directory1");
 110is($r->wc_subdir, "directory1", "wc_subdir after wc_chdir");
 111is($r->config("test.string"), "value", "config after wc_chdir");
 112
 113# Object generation in sub directory
 114chdir("directory2");
 115my $r2 = Git->repository();
 116is($r2->repo_path, $abs_repo_dir . "/.git", "repo_path (2)");
 117is($r2->wc_path, $abs_repo_dir . "/", "wc_path (2)");
 118is($r2->wc_subdir, "directory2/", "wc_subdir initial (2)");
 119
 120# commands in sub directory
 121my $last_commit = $r2->command_oneline(qw(rev-parse --verify HEAD));
 122like($last_commit, qr/^[0-9a-fA-F]{40}$/, 'rev-parse returned hash');
 123my $dir_commit = $r2->command_oneline('log', '-n1', '--pretty=format:%H', '.');
 124isnt($last_commit, $dir_commit, 'log . does not show last commit');
 125
 126# commands outside working tree
 127chdir($abs_repo_dir . '/..');
 128my $r3 = Git->repository(Directory => $abs_repo_dir);
 129my $tmpfile3 = "$abs_repo_dir/file3.tmp";
 130open TEMPFILE3, "+>$tmpfile3" or die "Can't open $tmpfile3: $!";
 131is($r3->cat_blob($file1hash, \*TEMPFILE3), 15, "cat_blob(outside): size");
 132close TEMPFILE3;
 133unlink $tmpfile3;
 134chdir($abs_repo_dir);
 135
 136printf "1..%d\n", Test::More->builder->current_test;
 137
 138my $is_passing = eval { Test::More->is_passing };
 139exit($is_passing ? 0 : 1) unless $@ =~ /Can't locate object method/;