Git-Repository
view release on metacpan or search on metacpan
t/20-simple.t view on Meta::CPAN
use strict;
use warnings;
use Test::More;
use Test::Requires::Git;
use Test::Git;
use File::Temp qw( tempdir );
use File::Spec;
use Cwd qw( cwd abs_path );
use Git::Repository;
test_requires_git '1.5.5';
my $version = Git::Repository->version;
plan tests => my $tests;
# clean up the environment
delete @ENV{qw( GIT_DIR GIT_WORK_TREE )};
$ENV{LC_ALL} = 'C';
$ENV{GIT_AUTHOR_NAME} = 'Test Author';
$ENV{GIT_AUTHOR_EMAIL} = 'test.author@example.com';
$ENV{GIT_COMMITTER_NAME} = 'Test Committer';
$ENV{GIT_COMMITTER_EMAIL} = 'test.committer@example.com';
$ENV{GIT_CONFIG_NOSYSTEM} = 1;
delete $ENV{XDG_CONFIG_HOME};
delete $ENV{HOME};
my $home = cwd;
local $/ = chr rand 128;
# small helper sub
sub update_file {
my ( $file, $content ) = @_;
open my $fh, '>', $file or die "Can't open $file: $!";
print {$fh} $content;
close $fh;
}
# a place to put a git repository
my $dir = abs_path( tempdir( CLEANUP => 1 ) );
# a quiet git init:
my @init = qw( init );
push @init, '-q' if Git::Repository->version_ge('1.5.2.3');
# PASS - non-existent directory
BEGIN { $tests += 3 }
chdir $dir;
Git::Repository->run( @init );
my $r = Git::Repository->new();
isa_ok( $r, 'Git::Repository' );
chdir $home;
is( $r->work_tree, $dir, 'work tree' );
my $gitdir = $r->run( qw( rev-parse --git-dir ) );
$gitdir = File::Spec->catfile( $dir, $gitdir )
if ! File::Spec->file_name_is_absolute( $gitdir );
is( $gitdir, $r->git_dir, 'git-dir' );
# check usage exit code
BEGIN { $tests += 2 }
ok( ! eval { $r->run( qw( commit --bonk ) ); }, "FAIL with usage text" );
like( $@, qr/^usage: .*?git[- ]commit/m, '... expected usage message' );
# add file to the index
update_file( File::Spec->catfile( $dir, 'readme.txt' ), << 'TXT' );
Some readme text
for our example
TXT
$r->run( add => 'readme.txt' );
# unset all editors
delete @ENV{qw( EDITOR VISUAL GIT_EDITOR )};
SKIP: {
BEGIN { $tests += 2 }
skip "these tests require git >= 1.6.6, but we only have $version", 2
if Git::Repository->version_lt('1.6.6');
skip "editor defined directly in .gitconfig", 2
if $r->run( config => 'core.editor' );
skip "this test does not work with msysgit on Win32", 2
if $^O eq 'MSWin32';
# Thanks to @adelton and @gregora for the heads-up regarding change in behavior of git var GIT_EDITOR
# Refer to: https://github.com/book/Git-Repository/pull/23
# for more information.
skip "'git var GIT_EDITOR' behaviour was changed in git 2.40.0, and we have $version", 2
if Git::Repository->version_ge('2.40.0');
( run in 0.894 second using v1.01-cache-2.11-cpan-df04353d9ac )