Hg

 view release on metacpan or  search on metacpan

t/10_hg_repository.t  view on Meta::CPAN

use strict;
use warnings;
use 5.12.0;

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

BEGIN { use_ok( 'Hg::Repository' ); }

my $test_repo = '/tmp/perl-hg-test';
my $hg        = `which hg`;
chomp $hg;

sub init_test_repo {
    my $clean_repo_command = "rm -rf $test_repo";
    my $mkdir_command      = "mkdir -p $test_repo";
    my $repo_init_command  = "( cd $test_repo; $hg init )";

    my $command_result;
    $command_result = `$clean_repo_command`;
    $command_result = `$mkdir_command`;
    $command_result = `$repo_init_command`;
}

sub add_a_file {
    my $create_file_command = "echo 'Foo Bar Baz' > $test_repo/test_file";
    my $add_file_command    = "$hg -R $test_repo add $test_repo/test_file";

    my $command_result;
    $command_result = `$create_file_command`;
    $command_result = `$add_file_command`;
}

sub edit_a_file {
    my $edit_file_command = "echo 'New Content' > $test_repo/test_file";

    my $command_result;
    $command_result = `$edit_file_command`;
}

sub update_repo {
    my $revision = shift || 0;

    my $update_command = "$hg -R $test_repo update -r $revision";

    my $command_result;
    $command_result = `$update_command`;
}

sub commit_repo {
    my $message = shift || 'Test Commit';

    my $commit_repo_command = "$hg -R $test_repo commit -m '$message'";

    my $command_result;
    $command_result = `$commit_repo_command`;
}

init_test_repo;
throws_ok {
    my $repo = Hg::Repository->new(
        dir => $test_repo,
        hg => '/not/a/real/path',
    );
} qr/Can't find a working version of Mercurial at .*/, 
"The constructor fails when given a bad path to hg";

init_test_repo;
lives_ok {
    my $repo = Hg::Repository->new(
            dir => $test_repo,
            hg => $hg,
        );
} 
"The constructor succeeds when given a good path to hg";

init_test_repo;
throws_ok {
    my $repo = Hg::Repository->new(
            dir => '/not/a/real/repo',
            hg => $hg,
        );
} qr/Can't find a Mercurial repository at .*/,
"The constructor fails when given a bad repository path";

init_test_repo;
lives_ok {
    my $repo = Hg::Repository->new(
            dir => $test_repo,
            hg => $hg,
        );
}
"The constructor succeeds when given a good repository path";

lives_ok {
    my $repo = Hg::Repository->new(
            dir => $test_repo,
            hg => $hg,
        );

    is( scalar(@{ $repo->revisions }), 0, "Revisions is an empty list" );



( run in 3.537 seconds using v1.01-cache-2.11-cpan-5c0b1e786e0 )