Code-TidyAll
view release on metacpan or search on metacpan
t/lib/TestFor/Code/TidyAll/Git.pm view on Meta::CPAN
package TestFor::Code::TidyAll::Git;
use Capture::Tiny qw(capture capture_stderr);
use Code::TidyAll::Git::Util qw(git_files_to_commit git_modified_files);
use Code::TidyAll::Util qw(tempdir_simple);
use Code::TidyAll;
use File::pushd qw(pushd);
use File::Spec;
use FindBin qw( $Bin );
use IPC::System::Simple qw(capturex runx);
use Path::Tiny qw(path);
use Test::Class::Most parent => 'TestHelper::Test::Class';
use Try::Tiny;
use constant IS_WIN32 => $^O eq 'MSWin32';
my ( $precommit_hook_template, $prereceive_hook_template, $tidyall_ini_template );
$ENV{GIT_AUTHOR_NAME} = $ENV{GIT_COMMITTER_NAME} = 'G. Author';
$ENV{GIT_AUTHOR_EMAIL} = $ENV{GIT_COMMITTER_EMAIL} = 'git-author@example.com';
# Ignore local configuration files, which may change the default branch from
# "master" to "main".
$ENV{GIT_CONFIG_GLOBAL} = $ENV{GIT_CONFIG_SYSTEM} = File::Spec->devnull;
BEGIN {
if (IS_WIN32) {
__PACKAGE__->SKIP_CLASS(
q{These tests behave oddly on Windows (at least in Azure). I think it has to do with differences in how output is captured and possible also some line ending issues when the test plugins like UpperText are invoked.}
);
}
}
sub test_git : Tests {
my ($self) = @_;
return unless $self->require_executable('git');
my ( $temp_dir, $work_dir, $pushd ) = $self->_make_working_dir_and_repo;
subtest 'add foo.txt', sub {
$work_dir->child('foo.txt')->spew_raw("abc\n");
cmp_deeply( [ git_files_to_commit($work_dir) ], [], 'no files to commit' );
runx(qw( git add foo.txt ));
cmp_deeply(
[ map { $_->stringify } git_files_to_commit($work_dir) ],
[ $work_dir->child('foo.txt')->stringify ], 'one file to commit'
);
};
subtest 'attempt to commit untidy file', sub {
my $output = capture_stderr { system(qw( git commit -q -m changed -a )) };
like( $output, qr/1 file did not pass tidyall check/, '1 file did not pass tidyall check' );
like( $output, qr/needs tidying/, 'needs tidying' );
$self->_assert_something_to_commit($work_dir);
};
subtest 'successfully commit tidied file', sub {
$work_dir->child('foo.txt')->spew_raw("ABC\n");
my $output = capture_stderr { runx(qw( git commit -q -m changed -a )) };
like( $output, qr/\[checked\] foo\.txt/, 'checked foo.txt' );
$self->_assert_nothing_to_commit($work_dir);
};
subtest 'add another file which is tidied', sub {
$work_dir->child('bar.txt')->spew_raw('ABC');
runx(qw( git add bar.txt ));
runx(qw( git commit -q -m bar.txt ));
$work_dir->child('bar.txt')->spew('def');
cmp_deeply( [ git_files_to_commit($work_dir) ], [], 'no files to commit' );
cmp_deeply(
( run in 2.800 seconds using v1.01-cache-2.11-cpan-5837b0d9d2c )