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;
use App::karr::Git;

# ---------------------------------------------------------------------------
# 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 );
}

# karr-init a repo the way `karr init` does: write refs/karr/config, no .karr
# file. Detection must therefore rely on the ref, not a sidecar file.
sub init_karr_board {
  my ( $dir ) = @_;
  App::karr::Git->new( dir => "$dir" )
    ->write_config_ref( { board => { name => 'Test Board' } } );
}

# ---------------------------------------------------------------------------
# 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();
  # The warning is the subject of this subtest, so catch it rather than let it
  # through to the harness's STDERR. That handle has no :encoding(UTF-8) layer
  # here -- bin/karr-foundation installs it via enable_std_utf8, an in-process
  # caller does not -- so the em dash in the message (ticket #108) would print
  # wide and warn about it on the way out.
  my @warnings;
  my $cfg = do {
    local $SIG{__WARN__} = sub { push @warnings, $_[0] };
    $f->_config_data;
  };
  is ref $cfg, 'HASH', 'returns hashref';
  is scalar keys %$cfg, 0, 'empty when no config';
  is scalar @warnings, 1, 'and says so exactly once';
  like $warnings[0], qr/config not found/, '...naming the missing 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';
};

# ---------------------------------------------------------------------------



( run in 2.064 seconds using v1.01-cache-2.11-cpan-14f38c9f855 )