App-BatchRelease

 view release on metacpan or  search on metacpan

t/App-BatchRelease.t  view on Meta::CPAN

    @tested = ();
    for my $path ( values %built ) {
        Mojo::File->new($path)->make_path;
    }
    $fails{'Test-App'} = 'the release tests exited 1';
    ( undef, undef, $checked ) = capture { App::BatchRelease->new->check_all($plan) };
    is( \@tested,                                        [qw{Test-Base Test-App Test-Other}], 'release tests that fail stop the tests of what requires it, and the rest go on' );
    is( [ map { $_->{name} } @{ $checked->{release} } ], [qw{Test-Base Test-Other}],          'to release: those whose release tests passed' );
    is(
        [ map { $_->{skip} } @{ $checked->{skipped} } ],
        [ 'no dist.ini, so it is not a Dist::Zilla distribution', 'its release tests failed: the release tests exited 1', 'requires Test-App, which failed' ],
        'skipped: those of the plan, then the one whose release tests failed, then what requires it, each with why'
    );
    ok( -e $built{'Test-App'}, 'the build of the one that failed stays, for a person to look at' );

    @tested = ();
    my @pulled = map { +{ %$_, defined $_->{needs}[0] ? () : ( pull => 'fast-forward 2 commits' ) } } @{ $plan->{release} }[ 0, 1 ];
    ( undef, undef, $checked ) = capture { App::BatchRelease->new->check_all( { release => \@pulled, skipped => [] } ) };
    is( \@tested,                                            [],                                                                                   'a dry run tests no work tree that a pull would change, nor what requires it' );
    is( [ map { $_->{untested} } @{ $checked->{release} } ], [ 'the pull comes first', 'it requires Test-Base, whose release tests did not run' ], 'and keeps them in the list, each with why its tests did not run' );

    my %options;
    $mock->redefine( test => sub ( $root, %given ) { %options = %given; return { error => undef, built => undef } } );
    my $app = App::BatchRelease->new;
    $app->credentials->from_stdin(1);
    my $tty = File::Temp->new;
    local $App::BatchRelease::Prompt::TERMINAL = $tty->filename;
    capture { $app->check_all( { release => [ { name => 'Test-Base', path => '/bogus/Test-Base', needs => [] } ], skipped => [] } ) };
    is( \%options, { stdin => $tty->filename }, 'when the credentials came on standard input, each test run reads its questions from the terminal' );
};

subtest confirm => sub {
    my ( @asked, $answer, $interactive );
    my $prompt = Test::MockModule->new('App::BatchRelease::Prompt');
    $prompt->redefine( ask         => sub ( $message, %options ) { push @asked, [ $message, \%options ]; return $answer } );
    $prompt->redefine( interactive => sub () { return $interactive } );
    $prompt->redefine( at_terminal => sub ($what) { return ( -in => 'tty', -out => 'tty' ) } );
    my $plan = { release => [ {}, {} ] };

    ( $interactive, $answer ) = ( 1, ' Yes ' );
    ok( App::BatchRelease->new->confirm($plan), 'yes, in any case and with spaces, confirms' );
    is( $asked[0], [ 'Release these 2 distributions to CPAN? [y/N]', {} ], 'the question counts them' );
    $answer = q{};
    ok( !App::BatchRelease->new->confirm($plan), 'an empty answer does not' );
    $answer = 'yep';
    ok( !App::BatchRelease->new->confirm($plan), 'nor anything else' );

    $interactive = 0;
    is( dies { App::BatchRelease->new->confirm($plan) }, "Nobody can confirm the release: standard input is not a terminal.\nPass --yes to release without asking.\n", 'nobody to ask: it dies and says how to go on' );
    @asked = ();
    my $app = App::BatchRelease->new;
    $app->credentials->from_stdin(1);
    $answer = 'y';
    ok( $app->confirm($plan), 'with the credentials on standard input' );
    is( $asked[0][1], { -in => 'tty', -out => 'tty' }, 'it asks at the terminal' );
};

subtest run => sub {
    app_requires_base();
    my ( @released, @calls );
    my $release = Test::MockModule->new('App::BatchRelease::Release')->redefine( release => sub ( $root, $credentials, %options ) { push @released, [ Mojo::File->new($root)->basename, $credentials->password ]; return { error => undef, built => undef ...
    $release->redefine( test => sub ( $root, %options ) { push @calls, 'test'; return { error => undef, built => undef } } );
    my $testing     = "\n== Testing Test-Base from $dir{'Test-Base'}\n\n== Testing Test-App from $dir{'Test-App'}\n";
    my $credentials = Test::MockModule->new('App::BatchRelease::Credentials');
    $credentials->redefine( load             => sub ( $self, $fh = undef ) { push @calls, 'load';  return $self->from_stdin(1) } );
    $credentials->redefine( ask              => sub ($self) { push @calls, 'ask';                  return $self->username('TESTER')->password('hunter2') } );
    $credentials->redefine( check_with_pause => sub ( $self, $ua = undef ) { push @calls, 'check'; return 1 } );
    my $confirmed = 1;
    my $app       = Test::MockModule->new('App::BatchRelease')->redefine( confirm => sub ( $self, $plan ) { push @calls, 'confirm'; return $confirmed } );
    my @paths     = map { "$dir{$_}" } qw{Test-App Test-Base Test-Other};
    my $list      = <<"LIST";
in this order:
  1. Test-Base 0.002: not uploaded, CPAN has 0.001
       $dir{'Test-Base'}
  2. Test-App 1.000: never released; after Test-Base
       $dir{'Test-App'}
Skipped:
  Test-Other ($dir{'Test-Other'}): released: CPAN has 0.010, and the default branch has nothing after its tag
LIST

    my ( $out, $err, $status ) = capture { App::BatchRelease->new->run( qw{--author tester}, '--dry-run', '--credentials', @paths ) };
    is( [ $status, $out ], [ 0, "${testing}Would release, $list" ], 'a dry run runs the release tests of each, in order, then lists what it would release, in order, with the state and the requirements of each, and what it would skip, with why, and ex...
    is( [ \@released, \@calls ], [ [], [qw{test test}] ], 'and releases nothing, reads no credentials and asks nothing, even with --credentials' );

    @calls = ();
    ( $out, $err, $status ) = capture { App::BatchRelease->new->run( qw{--author tester}, '--credentials', @paths ) };
    is( $status,    0,                                                         'a run where all went well exits 0' );
    is( \@calls,    [qw{load test test confirm ask check}],                    'the credentials block is read first, then the release tests run, then the list is confirmed, then the credentials are asked for and checked, all before the first release'...
    is( \@released, [ [ 'Test-Base', 'hunter2' ], [ 'Test-App', 'hunter2' ] ], 'each release, in order, with the same credentials' );
    my $summary = <<"SUMMARY";

Summary
Released (2):
  Test-Base 0.002
  Test-App 1.000
Skipped (1):
  Test-Other ($dir{'Test-Other'}): released: CPAN has 0.010, and the default branch has nothing after its tag
Failed (0):
SUMMARY
    is( $out, "${testing}To release, $list\n== Releasing Test-Base from $dir{'Test-Base'}\n\n== Releasing Test-App from $dir{'Test-App'}\n$summary", 'the release tests, the list, each release, then the summary' );

    ( @calls, @released ) = ();
    $confirmed = 0;
    ( $out, $err, $status ) = capture { App::BatchRelease->new->run( qw{--author tester}, @paths ) };
    is( [ $status, \@calls, \@released ], [ 0, [qw{test test confirm}], [] ],                     'not confirmed: nothing is asked or released' );
    is( $out,                             "${testing}To release, ${list}Nothing was released.\n", 'and it says so' );

    @calls = ();
    $release->redefine( release => sub ( $root, $credentials, %options ) { return { error => 'the release exited 1', built => undef } } );
    ( $out, $err, $status ) = capture { App::BatchRelease->new->run( qw{--author tester}, '--yes', '--skip-login-check', @paths ) };
    is( [ $status, \@calls ], [ 1, [qw{test test ask}] ], 'with --yes nothing is confirmed, with --skip-login-check nothing is checked, and a failure exits 1' );

    @calls = ();
    $release->redefine( test => sub ( $root, %options ) { push @calls, 'test'; return { error => 'the release tests exited 1', built => undef } } );
    ( $out, $err, $status ) = capture { App::BatchRelease->new->run( qw{--author tester}, @paths ) };
    is( [ $status, \@calls ], [ 0, ['test'] ], 'release tests that fail: the batch has nothing left, so nothing is confirmed, asked or released, and that is no failure' );
    is(
        $out, <<"LIST",

== Testing Test-Base from $dir{'Test-Base'}
Nothing to release.
Skipped:
  Test-Other ($dir{'Test-Other'}): released: CPAN has 0.010, and the default branch has nothing after its tag
  Test-Base ($dir{'Test-Base'}): its release tests failed: the release tests exited 1
  Test-App ($dir{'Test-App'}): requires Test-Base, which failed
LIST



( run in 1.473 second using v1.01-cache-2.11-cpan-f5f21f1076c )