App-BatchRelease

 view release on metacpan or  search on metacpan

lib/App/BatchRelease.pm  view on Meta::CPAN

each one whose tests fail, and each one that requires it.  A dry run runs
them too.

=item 3. L</listing>

Prints both lists.  A dry run stops here.

=item 4. L</confirm>, then the credentials

Asks the person to confirm the list, then asks for the PAUSE username and
password, once, through L<App::BatchRelease::Credentials>.  Both happen
before the first release.

=item 5. L</release_all>

Releases each distribution of the list, in order, through
L<App::BatchRelease::Release>, skips each one that requires a distribution
whose release failed, and prints the summary.

=back

lib/App/BatchRelease/Credentials.pm  view on Meta::CPAN

package App::BatchRelease::Credentials 0.001;

# ABSTRACT: The PAUSE username and password, asked for once, before any build

use 5.040;
use warnings FATAL => 'all';
use re '/aasx';
use Mojo::Base -base, -signatures;

use Mojo::URL       ();
use Mojo::UserAgent ();

use App::BatchRelease::Prompt();


has 'username';
has 'password';
has from_stdin => 0;

# The names that a block may give.
our %KNOWN = map { $_ => 1 } qw{username password};

# Where check_with_pause asks.  The page answers 401 without a valid login.
our $PAUSE_LOGIN = 'https://pause.perl.org/pause/authenquery';


sub load ( $self, $fh = \*STDIN ) {
    $self->from_stdin(1);
    while ( my $line = <$fh> ) {
        chomp $line;
        last if $line eq q{};

lib/App/BatchRelease/Credentials.pm  view on Meta::CPAN

        $self->$name($value);
    }
    return $self;
}

# True for a credential that was given and is not empty.  PAUSE takes no empty one.
my sub filled ($value) { return ( $value // q{} ) ne q{} }


sub ask ($self) {
    return $self if filled( $self->username ) && filled( $self->password );
    my @at;
    if ( $self->from_stdin ) {
        @at = App::BatchRelease::Prompt::at_terminal('the PAUSE credentials');
    }
    elsif ( !App::BatchRelease::Prompt::interactive() ) {
        die "Nobody can type the PAUSE credentials: standard input is not a terminal.\nGive them on standard input with --credentials.\n";
    }
    if ( !filled( $self->username ) ) {
        $self->username( typed( App::BatchRelease::Prompt::ask( 'PAUSE username:', @at ), 'the PAUSE username' ) );
    }
    if ( !filled( $self->password ) ) {
        my $user = $self->username;
        $self->password( typed( App::BatchRelease::Prompt::ask( "PAUSE password for $user:", -echo => '*', @at ), 'the PAUSE password' ) );
    }
    return $self;
}


sub typed ( $answer, $what ) {
    die "Nothing was typed for $what: its input ended before an answer.\n" if !$answer;
    my $typed = "$answer";
    die "An empty answer for $what: PAUSE takes no empty one.\n" if $typed eq q{};
    return $typed;
}


sub check_with_pause ( $self, $ua = Mojo::UserAgent->new( max_redirects => 3 ) ) {
    my $url  = Mojo::URL->new($PAUSE_LOGIN)->userinfo( $self->username . q{:} . $self->password );
    my $tx   = $ua->get($url);
    my $code = $tx->res->code;
    die "PAUSE refused the username and password.\n" if ( $code // 0 ) == 401;
    return 1                                         if $code && $code < 400;
    warn "Could not check the PAUSE login, so the run goes on: " . ( $code ? "PAUSE answered $code" : 'no answer: ' . ( $tx->error->{message} // 'unknown' ) ) . "\n";
    return 0;
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

App::BatchRelease::Credentials - The PAUSE username and password, asked for once, before any build

=head1 VERSION

version 0.001

=head1 SYNOPSIS

    use 5.040;
    use App::BatchRelease::Credentials;

    my $credentials = App::BatchRelease::Credentials->new;
    $credentials->load(\*STDIN);      # only when asked to, with --credentials
    $credentials->ask;                # whatever the block did not give
    $credentials->check_with_pause;   # dies when PAUSE refuses them

=head1 DESCRIPTION

A batch uploads many distributions, and each upload needs the PAUSE username
and password.  This asks for them once, at the start, before any build, and
keeps them in memory for the rest of the run.  Nothing here writes them to a
file, puts them on a command line or in the environment.

=head2 A BLOCK ON STANDARD INPUT

A run that nobody watches cannot answer a prompt.  So, as C<trog-provisioner>'s
C<Trog::Credentials> does, the caller can give the credentials on standard
input, before anything runs, as a block of C<name: value> lines that ends at a
blank line or at the end of the input:

    username: TESTER
    password: correct horse battery staple

The names are C<username> and C<password>.  Any other name is an error, and
is not ignored, because a misspelled name that silently meant "ask for it"
makes a run that waits for an answer nobody gives.  Whitespace around the
value stays, because a password can contain some.

Standard input is the one channel that is not the process table, the
environment or a file.  Anybody can read the arguments of a process while it
lives, and anything that runs as the same user can read its environment, which
every child inherits.  A file has to be created, given its mode and deleted,
with no failure in between.  A pipe exists only while the two ends talk.

Nothing reads the block until something calls L</load>, which the option
C<--credentials> of C<cpan-batch-release> does.  A read that nobody asked for
hangs when the caller has nothing to give and never closes its end.

=head1 ATTRIBUTES

=head2 username

The PAUSE username.

=head2 password

The PAUSE password.

=head2 from_stdin

True when L</load> read the block, so standard input is spoken for, and
every later question goes to the terminal.

=head1 METHODS

=head2 load

C<($fh)>.  Reads the block from C<$fh>, default standard input, and keeps each
value under its name.

Returns an App::BatchRelease::Credentials object, this one.

    $credentials->load( \*STDIN );

It dies when a line is not
C<name: value>, or names something other than C<username> or C<password>.

=head2 ask

Asks for the username, and then for the password, masked, unless each is
already there.  If L</from_stdin>, it asks at F</dev/tty>.

Returns an App::BatchRelease::Credentials object, this one.

    $credentials->ask;

It dies when the input ends before an answer, when an answer is
empty, because PAUSE takes no empty username or password, and when nobody can
answer: when standard input is not a terminal and the block was not read, or
there is no terminal.

=head2 check_with_pause

C<($ua)>, default a new L<Mojo::UserAgent>.  Logs in to PAUSE at
C<$PAUSE_LOGIN> with the credentials, and changes nothing there.

Returns an integer: 1 when PAUSE takes them, and 0, after a warning, when
PAUSE gives no answer or another error, because the uploads can still work.

    $credentials->check_with_pause;

It dies when PAUSE answers 401, so that a mistyped password stops the run
before the first build, rather than after each build of the batch.

=head1 FUNCTIONS

=head2 typed

C<($answer, $what)>.

Returns a string, the text of an answer of L<App::BatchRelease::Prompt/ask>.

lib/App/BatchRelease/Prompt.pm  view on Meta::CPAN


=head2 ask

C<($message, %options)>.  Asks the question C<$message>.  C<%options> go to
L<IO::Prompter> unchanged.

Returns an object, what the person typed, as L<IO::Prompter/prompt> returns
it: false when the input ended before an answer, and true for any line, an
empty one too.

    my $answer = App::BatchRelease::Prompt::ask( 'PAUSE password:', -echo => '*' );

For example, C<-echo>
masks what the person types, and C<-in> and C<-out> ask somewhere other than
standard input.

L<IO::Prompter> reads from C<*ARGV>, so it dies with C<Can't open *ARGV> in a
program that has arguments, such as the paths of checkouts.  This makes
C<*ARGV> C<local> to the call.

If standard input is a terminal and the output is not, for example under

lib/App/BatchRelease/Release.pm  view on Meta::CPAN

    my $built = $zilla->built_in // return;
    return Mojo::File->new("$built")->to_abs->to_string;
}


sub hand_credentials ( $zilla, $credentials ) {
    my @uploaders = grep { $_->isa('Dist::Zilla::Plugin::UploadToCPAN') } @{ $zilla->plugins };
    for my $plugin (@uploaders) {
        my $meta = $plugin->meta;
        $meta->find_attribute_by_name('username')->set_value( $plugin, $credentials->username );
        $meta->find_attribute_by_name('password')->set_value( $plugin, $credentials->password );
    }
    return scalar @uploaders;
}

1;

__END__

=pod

lib/App/BatchRelease/Release.pm  view on Meta::CPAN

=head1 VERSION

version 0.001

=head1 SYNOPSIS

    use 5.040;
    use App::BatchRelease::Credentials;
    use App::BatchRelease::Release;

    my $credentials = App::BatchRelease::Credentials->new( username => 'TESTER', password => 'bogus' );
    my $outcome = App::BatchRelease::Release::release( '/bogus/Foo-Bar', $credentials );
    print defined $outcome->{error} ? "failed: $outcome->{error}\n" : "released from $outcome->{built}\n";

=head1 DESCRIPTION

L</release> does what C<dzil release> does in a checkout, with one change:
the C<[UploadToCPAN]> plugins of the distribution get the PAUSE username and
password from this program, so none of them asks for them, and none reads
F<~/.pause>.

L</test> does what C<dzil test --release --keep-build-dir> does, before the
batch, and uploads nothing.

=head2 HOW THE CREDENTIALS REACH THE UPLOAD PLUGIN

L<Dist::Zilla::Plugin::UploadToCPAN> takes its C<username> and C<password>
from the first of: its C<credentials_stash>, default C<%PAUSE>, as
F<~/.dzil/config.ini> sets it; F<~/.pause>; a prompt.  Both attributes are
lazy, and C<password> takes no value from F<dist.ini>.

This program runs the release in a child process, made with C<fork>, so the
child has the credentials in its memory from the parent, and nothing carries
them between the two.  The child builds the Dist::Zilla object as the
C<dzil release> command builds it, with L<Dist::Zilla::App>, so the global
configuration and its stashes still apply.  Then, before the release starts,
it sets the C<username> and C<password> of every plugin that is an
L<Dist::Zilla::Plugin::UploadToCPAN>, through the meta-object of the class.
A lazy attribute that has a value does not run its default, so neither a
stash, nor F<~/.pause>, nor a prompt is asked.  That holds whatever the
F<dist.ini> names as its stash.

A child also keeps the batch apart: each release and each test run loads the
plugins of its own distribution into a fresh process, and one that dies, or
changes the directory or the environment, takes only its own process with it.

=head1 FUNCTIONS

lib/App/BatchRelease/Release.pm  view on Meta::CPAN


Returns a string or undef: the absolute path of the directory of the build, or
undef when Dist::Zilla does not say.

    my $built = dzil( '/bogus/Foo-Bar', $credentials, 'release' );

It dies as the command dies.

=head2 hand_credentials

C<($zilla, $credentials)>.  Sets the C<username> and C<password> of every
plugin of the Dist::Zilla object C<$zilla> that is an
L<Dist::Zilla::Plugin::UploadToCPAN>, from C<$credentials>, an
L<App::BatchRelease::Credentials>.

Returns an integer, the count of such plugins.

    my $count = hand_credentials( $zilla, $credentials );

=head1 SEE ALSO

lib/App/BatchRelease/Source/Git.pm  view on Meta::CPAN

=item *

L</switch> and L</pull> change the branch that is checked out and the work
tree.  L<App::BatchRelease> calls them only with C<--pull-latest>, and never
on a dry run.

=back

None of them stashes, merges, rebases or resets.  The two that talk to a
remote run with C<GIT_TERMINAL_PROMPT=0>, so that git fails, rather than asks
for a password at the terminal.

Every command runs through L</git>, with a timeout, so a test can replace that
one method.

=head1 ATTRIBUTES

=head2 root

The directory of the checkout.  Required.

script/cpan-batch-release  view on Meta::CPAN


=head1 SYNOPSIS

    cpan-batch-release --author PAUSEID [--author PAUSEID]... [--dry-run]
                       [--conformant-only] [--credentials] [--yes]
                       [--skip-login-check] [--pull-latest] [--jobs N] CHECKOUT...

    cpan-batch-release --author TESTER --dry-run ~/src/*
    cpan-batch-release --author TESTER --dry-run --pull-latest ~/src/*
    cpan-batch-release --author TESTER --conformant-only ~/src/*
    printf 'username: %s\npassword: %s\n\n' "$PAUSE_USER" "$PAUSE_PASSWORD" \
        | cpan-batch-release --author TESTER --credentials --yes ~/src/*

=head1 DESCRIPTION

Each CHECKOUT is a directory.  For each one, C<cpan-batch-release> finds out
whether it is a Dist::Zilla distribution whose work is not all on CPAN, as
L<App::BatchRelease::State> says, and releases all such distributions in one
run, with C<dzil release> in each checkout.  It asks for the PAUSE username
and password once, before the first release, and uses them for every upload.

It looks at each checkout first.  It fetches each one, and changes no work
tree while it looks, unless C<--pull-latest> tells it to:

=over

=item *

A path that is not a directory, has no F<dist.ini>, or is not the top of a
git checkout is skipped.

script/cpan-batch-release  view on Meta::CPAN

Before it prints them, it runs the release tests of each distribution to
release, in that order, as C<dzil test --release> does, whether or not its
F<dist.ini> loads C<[TestRelease]>.  Each test run gets the F<lib/> of the
test build of each distribution of the batch that it requires.  A
distribution whose release tests fail is skipped, with the reason, and so is
each one that requires it.  The output of each test run shows as it runs.
When all have run, it removes the build of each one that passed, and keeps
the build of each one that failed under F<.build/> of its checkout.  A dry run
runs the release tests too, and then stops after the list.

Else it asks for confirmation, then for the PAUSE username and password, and
checks them with PAUSE.  Then it releases the distributions, one at a time, in
order.  The output of each C<dzil release> shows as it runs.  When a release
fails, the distributions of the batch that require it are skipped, and the
others are still released.  At the end it prints a summary of the
distributions released, skipped, with the reason, and failed, with the error.

=head1 OPTIONS

=over

script/cpan-batch-release  view on Meta::CPAN

=item --conformant-only

Skips each distribution that is not in full conformance: one for which
C<conformance check> of L<Dist::Conformance> would report a failure, or
could not run, as when the checkout has no F<.conformance.yml>.

=item --credentials

Reads the PAUSE credentials from standard input, before anything else, as a
block of C<name: value> lines that ends at a blank line or at the end of the
input.  The names are C<username> and C<password>.  A name that the block does
not give is asked for at F</dev/tty>, and so is the confirmation, because
standard input is spoken for.  See L<App::BatchRelease::Credentials>.

=item --yes, -y

Releases without asking for confirmation.  A run with no terminal needs it.

=item --skip-login-check

Does not check the credentials with PAUSE before the first build.

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


use App::BatchRelease::Credentials();

# A handle that reads the text, as standard input would.
sub input ($text) {
    open( my $fh, '<', \$text ) or die "open: $!";
    return $fh;
}

subtest load => sub {
    my $credentials = App::BatchRelease::Credentials->new->load( input("username: TESTER\npassword:  spaced out \n\nusername: AFTER\n") );
    is( [ $credentials->username, $credentials->password ], [ 'TESTER', ' spaced out ' ], 'each name: value, up to the blank line; the space after the colon goes, and the rest of the value stays' );
    ok( $credentials->from_stdin, 'standard input is spoken for' );

    is( App::BatchRelease::Credentials->new->load( input('password: x') )->password, 'x', 'the end of the input ends the block too' );
    like( dies { App::BatchRelease::Credentials->new->load( input("user: TESTER\n") ) }, qr{\AUnknown\ credential\ 'user'}, 'an unknown name dies, rather than leaving it to a prompt nobody answers' );
    my $error = dies { App::BatchRelease::Credentials->new->load( input("hunter2\n") ) };
    like( $error, qr{Expected\ 'name:\ value'}, 'a line that is not name: value dies' );
    unlike( $error, qr{hunter2}, 'and the message does not repeat the line, which may be a password' );
};

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

    ( $interactive, @answers ) = ( 1, 'TESTER', 'secret' );
    my $credentials = App::BatchRelease::Credentials->new->ask;
    is( [ $credentials->username, $credentials->password ], [qw{TESTER secret}],                                 'asks for both' );
    is( [ map { $_->[0] } @asked ],                         [ 'PAUSE username:', 'PAUSE password for TESTER:' ], 'the username first, then the password for it' );
    is( $asked[1][1],                                       { -echo => '*' },                                    'the password is masked' );

    @asked = ();
    ( $interactive, @answers ) = ( 0, 'secret' );
    $credentials = App::BatchRelease::Credentials->new( username => 'TESTER', from_stdin => 1 )->ask;
    is( $credentials->password, 'secret',                                                                       'only what the block did not give' );
    is( $asked[0][1],           { -echo => '*', -in => 'tty-in for the PAUSE credentials', -out => 'tty-out' }, 'at the terminal, because standard input carried the block' );

    @asked = ();
    App::BatchRelease::Credentials->new( username => 'TESTER', password => 'secret' )->ask;
    is( \@asked, [], 'nothing is asked when both are there' );

    $interactive = 0;
    like( dies { App::BatchRelease::Credentials->new->ask }, qr{\ANobody\ can\ type}, 'no terminal and no block: it dies at once, rather than wait' );

    ( $interactive, @answers ) = ( 1, q{} );
    like( dies { App::BatchRelease::Credentials->new->ask }, qr{input\ ended}, 'an input that ends before an answer dies' );
};

subtest typed => sub {

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

            return $c->render( text => 'gone', status => 503 ) if $seen eq 'Basic ' . Mojo::Util::b64_encode( 'TESTER:down', q{} );
            return $c->render( text => 'no',   status => 401 );
        }
    );
    my $ua     = Mojo::UserAgent->new;
    my $server = $ua->server;
    $server->app($fake);
    my $login = $server->url;
    local $App::BatchRelease::Credentials::PAUSE_LOGIN = $login->path('/pause/authenquery')->to_string;

    is( App::BatchRelease::Credentials->new( username => 'TESTER', password => 'right' )->check_with_pause($ua),          1,                                                        'PAUSE takes them' );
    is( $seen,                                                                                                            'Basic ' . Mojo::Util::b64_encode( 'TESTER:right', q{} ), 'sent as HTTP basic authentication' );
    is( dies { App::BatchRelease::Credentials->new( username => 'TESTER', password => 'wrong' )->check_with_pause($ua) }, "PAUSE refused the username and password.\n",             'a 401 stops the run' );
    like( warning { is( App::BatchRelease::Credentials->new( username => 'TESTER', password => 'down' )->check_with_pause($ua), 0, 'another error does not stop it' ) }, qr{PAUSE\ answered\ 503}, 'and warns' );
};

done_testing;

package Test::EmptyLine;
use overload 'bool' => sub { 1 }, q{""} => sub { q{} };

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


use App::BatchRelease::Credentials();
use App::BatchRelease::Release();

# Dist::Zilla reads no ~/.dzil/config.ini under DZIL_TESTING, and the empty
# home has no ~/.pause, so the user's own credentials cannot answer here.
my $home = File::Temp->newdir;
local $ENV{HOME}         = "$home";
local $ENV{DZIL_TESTING} = 1;

my $credentials = App::BatchRelease::Credentials->new( username => 'TESTER', password => 'hunter2' );

# A distribution whose only releaser is [UploadToCPAN], with its stash and
# its config file pointed at nothing, in a directory that the test removes.
sub distribution ( $extra = q{} ) {
    my $dir = File::Temp->newdir;
    Mojo::File->new( $dir, 'dist.ini' )->spew(<<"INI");
name = Test-Dist
version = 0.001
abstract = A distribution for a test
author = A. Tester <tester\@test.test>

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

    return $dir;
}

subtest 'dzil' => sub {
    my %uploaded;

    # Mocked through the symbol table, which these immutable Moose classes allow
    # only when they are mutable again.
    Dist::Zilla::Plugin::UploadToCPAN->meta->make_mutable;
    Dist::Zilla::Chrome::Term->meta->make_mutable;
    my $upload   = Test::MockModule->new('Dist::Zilla::Plugin::UploadToCPAN')->redefine( release => sub ( $self, $archive ) { %uploaded = ( user => $self->username, password => $self->password, archive => "$archive" ); return } );
    my $prompted = 0;
    my $chrome   = Test::MockModule->new('Dist::Zilla::Chrome::Term')->redefine( prompt_str => sub { $prompted++; return 'from a prompt' } );

    my $dir = distribution("credentials_stash = %Bogus\n");
    my $cwd = Cwd::getcwd();
    my $built;
    my $done = eval {
        capture { $built = App::BatchRelease::Release::dzil( "$dir", $credentials, 'release' ) };
        1;
    };
    my $error = $@;
    chdir $cwd                                              or die "chdir $cwd: $!";
    ok( $done, 'the release ran, as dzil release runs it' ) or diag $error;
    is( $built, Mojo::File->new( $dir, 'Test-Dist-0.001' )->realpath->to_string, 'it returns the absolute path of the build' );
    ok( -e "$built/lib/Test/Dist.pm", 'which has the modules that were released' );
    is( [ @uploaded{qw{user password}} ], [qw{TESTER hunter2}], 'the upload got the username and password of this program, whatever stash dist.ini names' );
    like( $uploaded{archive}, qr{Test-Dist-0[.]001[.]tar[.]gz\z}, 'and the archive of the build' );
    is( $prompted, 0, 'nothing was asked' );

    # The test of the throwaway distribution passes only under RELEASE_TESTING.
    my $tested = distribution("[MakeMaker]\n");
    my $t      = Mojo::File->new( $tested, 't' )->make_path;
    $t->child('release.t')->spew(qq{print \$ENV{RELEASE_TESTING} ? "1..1\\nok 1\\n" : "1..1\\nnot ok 1\\n";\n});
    my $kept;
    $done = eval {
        capture { $kept = App::BatchRelease::Release::dzil( "$tested", undef, qw{test --release --keep-build-dir} ) };

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

    chdir $cwd or die "chdir $cwd: $!";
    ok( !$done, 'release tests that fail make it die' );
};

subtest hand_credentials => sub {
    my $dir   = distribution("[UploadToCPAN / Second]\n");
    my $zilla = Dist::Zilla::Dist::Builder->from_config( { dist_root => "$dir", chrome => Dist::Zilla::Chrome::Term->new } );
    is( App::BatchRelease::Release::hand_credentials( $zilla, $credentials ), 2, 'every [UploadToCPAN] of the distribution' );
    my @uploaders = grep { $_->isa('Dist::Zilla::Plugin::UploadToCPAN') } @{ $zilla->plugins };
    my @given;
    for my $plugin (@uploaders) { push @given, [ $plugin->username, $plugin->password ] }
    is( \@given, [ [qw{TESTER hunter2}], [qw{TESTER hunter2}] ], 'each has both' );
};

subtest release => sub {
    my $outcome;
    my $mock = Test::MockModule->new('App::BatchRelease::Release')->redefine(
        dzil => sub ( $root, $given, @command ) {
            die "the build broke\n" if $root eq '/bogus/broken';
            kill 'KILL', $$ if $root eq '/bogus/killed';
            die "the child did not get the credentials\n" if $given->password ne 'hunter2';
            die "the child ran dzil @command\n"           if "@command" ne 'release';
            return '/bogus/fine/Test-Dist-0.001';
        }
    );
    my ( $out, $err ) = capture { $outcome = App::BatchRelease::Release::release( '/bogus/fine', $credentials ) };
    is( $outcome, { error => undef, built => '/bogus/fine/Test-Dist-0.001' }, 'a release that succeeds in the child has no error, and the child had the credentials from its parent and told it where it built' );
    ( $out, $err ) = capture { $outcome = App::BatchRelease::Release::release( '/bogus/broken', $credentials ) };
    is( $outcome, { error => 'the release exited 1', built => undef },      'a release that dies says so' );
    is( $err,     "The release of /bogus/broken failed: the build broke\n", 'and the child printed why' );
    ( $out, $err ) = capture { $outcome = App::BatchRelease::Release::release( '/bogus/killed', $credentials ) };

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

    $git->unmock('git');
    local @App::BatchRelease::Source::Git::GIT = ( $^X, '-e', 'print qq{out:@ARGV}; print STDERR q{trouble}; exit( $ARGV[-1] eq q{fail} ? 3 : 0 )', '--' );
    my $source = App::BatchRelease::Source::Git->new( root => '/bogus/Test-Dist' );
    is( scalar $source->git('ok'),     'out:-C /bogus/Test-Dist ok',                      'runs in the root, and gives the standard output' );
    is( [ $source->git('fail') ],      [ 3, 'out:-C /bogus/Test-Dist fail' ],             'in list context, the exit status and the output, and no death' );
    is( dies { $source->git('fail') }, "git fail in /bogus/Test-Dist: exit 3: trouble\n", 'in scalar context, a failure dies with the status and the standard error' );

    # The fake git fails, and says in its error whether it may prompt.
    local @App::BatchRelease::Source::Git::GIT = ( $^X, '-e', 'print STDERR qq{prompt=$ENV{GIT_TERMINAL_PROMPT}}; exit 1', '--' );
    local $ENV{GIT_TERMINAL_PROMPT} = 1;
    is( dies { $source->fetch('origin') }, "git fetch --quiet origin in /bogus/Test-Dist: exit 1: prompt=0\n",               'fetch runs git with GIT_TERMINAL_PROMPT=0, so it fails rather than ask for a password' );
    is( dies { $source->pull },            "git pull --quiet --no-rebase --ff-only in /bogus/Test-Dist: exit 1: prompt=0\n", 'so does pull' );
    is( $ENV{GIT_TERMINAL_PROMPT},         1,                                                                                'and the environment of the program is as it was' );
};

done_testing;

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

    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'}



( run in 3.101 seconds using v1.01-cache-2.11-cpan-f5f21f1076c )