Skip to content

Commit

Permalink
Add tests for git repository cloning
Browse files Browse the repository at this point in the history
  • Loading branch information
ferki committed Apr 17, 2023
1 parent 1409355 commit 4315171
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions t/scm/git.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/usr/bin/env perl

use v5.12.5;
use warnings;

our $VERSION = '9999.99.99_99'; # VERSION

use Test::More tests => 8;
use Test::Exception;

use File::Spec;
use File::Temp qw(tempdir);
use Rex::Commands;
use Rex::Commands::Run;
use Rex::Commands::SCM;
use Rex::Helper::Run;

$::QUIET = 1;

my $git = can_run('git');

if ( !defined $git ) {
plan skip_all => 'Can not find git command';
}

ok( $git, "Found git command at $git" );

my $git_version = i_run 'git version';
ok( $git_version, qq(Git version returned as '$git_version') );

my $test_repo_dir = tempdir( CLEANUP => 1 );
ok( -d $test_repo_dir, "$test_repo_dir is the test repo directory now" );

prepare_test_repo($test_repo_dir);
git_repo_ok($test_repo_dir);

my $test_repo_name = 'test_repo';

subtest 'clone into non-existing directory', sub {
plan tests => 6;

my $clone_target_dir = tempdir( CLEANUP => 1 );

set repository => $test_repo_name, url => $test_repo_dir;

ok( -d $clone_target_dir, "$clone_target_dir could be created" );

rmdir $clone_target_dir;

ok( !-d $clone_target_dir, "$clone_target_dir does not exist now" );

lives_ok { checkout $test_repo_name, path => $clone_target_dir }
'cloning into non-existing directory';

git_repo_ok($clone_target_dir);
};

subtest 'clone into existing directory', sub {
plan tests => 5;

my $clone_target_dir = tempdir( CLEANUP => 1 );

set repository => $test_repo_name, url => $test_repo_dir;

ok( -d $clone_target_dir,
"$clone_target_dir is the clone target directory now" );

lives_ok { checkout $test_repo_name, path => $clone_target_dir }
'cloning into existing directory';

git_repo_ok($clone_target_dir);
};

sub prepare_test_repo {
my $directory = shift;

i_run qq(git -C $directory init);

i_run qq(git -C $directory config user.name Rex);
i_run qq(git -C $directory config user.email noreply\@rexify.org);

i_run qq(git -C $directory commit --allow-empty -m commit);

return;
}

sub git_repo_ok {
my $directory = shift;

ok( -d $directory, "$directory exists" );
ok(
-d File::Spec->join( $directory, q(.git) ),
"$directory has .git subdirectory"
);

lives_ok { i_run qq(git -C $directory rev-parse --git-dir) }
"$directory looks like a git repository now";

return;
}

0 comments on commit 4315171

Please sign in to comment.