Doit

 view release on metacpan or  search on metacpan

t/git.t  view on Meta::CPAN

#!/usr/bin/perl -w
# -*- cperl -*-

#
# Author: Slaven Rezic
#

use strict;
use FindBin;
use lib $FindBin::RealBin;

use Cwd qw(realpath getcwd);
use File::Temp qw(tempdir);
use Test::More;

use Doit;
use Doit::Util qw(in_directory);

use TestUtil qw(is_dir_eq);

sub git_init ();

my $d = Doit->init;

if (!$d->which('git')) {
    plan skip_all => 'git not in PATH';
}

plan 'no_plan';

$d->add_component('git');

my $git_version = $d->info_qx({quiet=>1}, 'git', '--version');
diag $git_version;

my $git_less_directory;
if (-d '/' && !-d '/.git') {
    $git_less_directory = '/';
}

# Unset most GIT_* environment variables
for my $git_key (sort keys %ENV) {
    next if $git_key !~ m{^GIT_};
    # These are OK
    next if $git_key =~ m{^GIT_(
			      AUTHOR_.*
			  |   COMMITTER_.*
			  |   EDITOR
			  |   PAGER
			  |   DIFF_.*
			  |   EXTERNAL_DIFF
			  |   MERGE_VERBOSITY
			  |   SSH
			  |   ASKPASS
			  |   CONFIG_NOSYSTEM
			  |   FLUSH
			  |   TRACE.*
			  |   .*_PATHSPECS
			  |   REFLOG_ACTION
			  )$}x;
    $d->unsetenv($git_key);
}

# realpath() needed on darwin (/private/tmp vs. /tmp)
my $dir = realpath(tempdir('doit-git-XXXXXXXX', CLEANUP => 1, TMPDIR => 1));

# A private git-short-status script; should behave the same as the git_short_status command.
my $my_git_short_status;
if ($ENV{HOME} && -x "$ENV{HOME}/bin/sh/git-short-status") {
    $my_git_short_status = "$ENV{HOME}/bin/sh/git-short-status";
}

## enable the following to test different default branches (master, main, user-defined in ~/.gitconfig...)
#$d->system(qw(git config --global init.defaultBranch an-unexpected-default-branch));

######################################################################
# Tests with the Doit repository (if checked out)
SKIP: {
#    skip "git_short_status does not work as expected (TODO)" if $ENV{GITHUB_ACTIONS}; # this does not seem to work if git-lfs is in use, which is the default with actions/checkout@v1 and may be configured with actions/checkout@v2, but is not current...
    my $self_git = eval { $d->git_root };
    skip "Not a git checkout", 1 if !$self_git;
    skip "Current git checkout is not the Doit git checkout", 1 # ... but probably a git directory in an upper directory
	if realpath("$FindBin::RealBin/..") ne realpath($self_git);
    skip "shallow repositories cannot be cloned", 1 if $d->git_is_shallow;
    skip "unexpected current branch", 1 if $d->git_current_branch ne 'master';

    my $workdir = "$dir/doit";

    run_tests($self_git, $workdir);
}

######################################################################
# Error cases
for my $meth (qw(git_repo_update git_short_status git_root git_get_commit_hash git_get_commit_files git_get_changed_files git_is_shallow git_current_branch git_config)) {
    eval { $d->$meth('unhandled-option' => 1) };
    like $@, qr{ERROR.*Unhandled options: unhandled-option}, "unhandled option in method $meth";
}

eval {
    $d->git_repo_update(
			repository => '/repo1',
			directory  => '/repo2',
			refresh    => 'invalid',
		       );
};
like $@, qr{ERROR.*refresh may be 'always' or 'never' at };

eval { $d->git_short_status('untracked_files' => 'blubber') };
like $@, qr{ERROR.*only values 'normal' or 'no' supported for untracked_files};

eval { $d->git_get_commit_files(directory => '/non-existent-directory') };
like $@, qr{ERROR.*Can't chdir to /non-existent-directory};

SKIP: {
    my @methods = qw(git_short_status git_root git_get_commit_hash git_get_commit_files git_get_changed_files git_is_shallow git_current_branch);
    skip "No git-less directory available", 2*scalar(@methods)
	if !defined $git_less_directory;
    for my $meth (@methods) {
	ok !eval { $d->$meth(directory => $git_less_directory); 1 }, "method $meth failed on non-git directory";
	in_directory {
	    ok !eval { $d->$meth; 1 }, "method $meth failed on non-git directory (using cwd)";



( run in 2.012 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )