App-karr
view release on metacpan or search on metacpan
t/30-foundation.t view on Meta::CPAN
use strict;
use warnings;
use Test::More;
use Path::Tiny qw( path tempdir );
use YAML::XS ();
use App::karr::Foundation;
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
sub new_foundation {
my (%opts) = @_;
# MooX::Options::new_with_options reads @ARGV â bypass by ->new
return App::karr::Foundation->new( %opts );
}
sub make_git_repo {
my $dir = tempdir( CLEANUP => 1 );
system( 'git', '-C', "$dir", 'init', '-q' ) == 0
or die "git init failed";
system( 'git', '-C', "$dir", 'config', 'user.email', 'test@example.invalid' ) == 0
or die "git config email failed";
system( 'git', '-C', "$dir", 'config', 'user.name', 'Test' ) == 0
or die "git config name failed";
return $dir;
}
sub write_karr_file {
my ( $dir, %opts ) = @_;
my $content = "command: " . ( $opts{command} // 'echo hello' ) . "\n";
$content .= "on_idle: " . ( $opts{on_idle} // 'skip' ) . "\n";
$content .= "max_runtime: " . ( $opts{max_runtime} // 1800 ) . "\n";
$dir->child('.karr')->spew_utf8( $content );
}
# Returns ($cfg_dir, $cfg_file) â caller must keep $cfg_dir alive to avoid cleanup
sub write_config {
my ( $dirs ) = @_;
my $cfg_dir = tempdir( CLEANUP => 1 );
my $cfg_file = $cfg_dir->child('config.yml');
$cfg_file->spew_utf8( "dirs:\n" . join( '', map { " - $_\n" } @$dirs ) );
return ( $cfg_dir, $cfg_file );
}
# ---------------------------------------------------------------------------
# Compilation
# ---------------------------------------------------------------------------
subtest 'module loads' => sub {
use_ok('App::karr::Foundation');
};
# ---------------------------------------------------------------------------
# Config loading
# ---------------------------------------------------------------------------
subtest 'missing config warns and returns empty' => sub {
# Isolate HOME so the default ~/.config/karr-foundation/config.yml path
# cannot resolve to a real file on the machine running the tests.
local $ENV{HOME} = tempdir( CLEANUP => 1 );
my $f = new_foundation();
my $cfg = $f->_config_data;
is ref $cfg, 'HASH', 'returns hashref';
is scalar keys %$cfg, 0, 'empty when no config';
};
subtest 'config file loaded' => sub {
my $tmp = tempdir( CLEANUP => 1 );
my $cfg = $tmp->child('config.yml');
$cfg->spew_utf8("dirs:\n - /tmp/fake-repo\n");
my $f = new_foundation( config => "$cfg" );
my $data = $f->_config_data;
is ref $data->{dirs}, 'ARRAY', 'dirs is array';
is $data->{dirs}[0], '/tmp/fake-repo', 'correct dir';
};
# ---------------------------------------------------------------------------
# Repo discovery
# ---------------------------------------------------------------------------
subtest '_discover_repos: explicit dirs' => sub {
my $repo1 = make_git_repo();
my $repo2 = make_git_repo();
write_karr_file( $repo1 );
write_karr_file( $repo2 );
my ( $cfg_dir, $cfg ) = write_config( [ "$repo1", "$repo2" ] );
my $f = new_foundation( config => "$cfg" );
my @repos = $f->_discover_repos;
is scalar @repos, 2, 'found 2 repos';
};
subtest '_discover_repos: scan parent dir' => sub {
my $parent = tempdir( CLEANUP => 1 );
my $repo1 = $parent->child('proj1');
my $repo2 = $parent->child('proj2');
( run in 1.925 second using v1.01-cache-2.11-cpan-df04353d9ac )