Skip to content

Commit

Permalink
Merge pull request #27 from paultcochrane/pr/avoid-echoing-password-t…
Browse files Browse the repository at this point in the history
…o-term

Avoid echoing password to terminal
  • Loading branch information
briandfoy committed Nov 13, 2017
2 parents fea5031 + 852732b commit 1467850
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 2 deletions.
5 changes: 4 additions & 1 deletion Makefile.PL
Expand Up @@ -68,14 +68,17 @@ my %WriteMakefile = (
'DateTime' => '0',
'IO::Null' => '0',
'Mojolicious' => '4.50',
'Term::ReadKey' => '0',
'URI' => '0',
},

'TEST_REQUIRES' => {
'Test::More' => '1',
'Test::Output' => '0',
'Test::Without::Module' => '0',
'Capture::Tiny' => '0',
'Capture::Tiny' => '0',
'File::Temp' => '0',
'Sub::Override' => '0',
},

'META_MERGE' => {
Expand Down
34 changes: 33 additions & 1 deletion lib/Module/Release.pm
Expand Up @@ -196,6 +196,7 @@ sub _set_defaults {
debug => $ENV{RELEASE_DEBUG} || 0,
local_file => undef,
remote_file => undef,
input_fh => *STDIN{IO},
output_fh => *STDOUT{IO},
debug_fh => *STDERR{IO},
null_fh => IO::Null->new(),
Expand Down Expand Up @@ -560,6 +561,16 @@ sub reset_perls {
}


=item input_fh
Return the value of input_fh.
=cut

sub input_fh {
return $_[0]->{input_fh};
}

=item output_fh
If quiet is off, return the value of output_fh. If output_fh is not
Expand Down Expand Up @@ -1290,7 +1301,17 @@ sub get_env_var {
return $pass if defined( $pass ) && length( $pass );

$self->_print( "$field is not set. Enter it now: " );
$pass = <>;
if ($field eq 'CPAN_PASS') {
# don't echo passwords to the screen
require Term::ReadKey;
local $| = 1;
Term::ReadKey::ReadMode('noecho');
$pass = $self->_slurp;
Term::ReadKey::ReadMode('restore');
}
else {
$pass = $self->_slurp;
}
chomp $pass;

return $pass if defined( $pass ) && length( $pass );
Expand All @@ -1313,6 +1334,17 @@ output_fh to a null filehandle, output goes nowhere.

sub _print { print { $_[0]->output_fh } @_[1..$#_] }

=item _slurp
Read a line from whatever is in input_fh and return it.
=cut

sub _slurp {
my $fh = $_[0]->input_fh;
return <$fh>;
}

=item _dashes()
Use this for a string representing a line in the output. Since it's a
Expand Down
71 changes: 71 additions & 0 deletions t/get_env_var.t
@@ -0,0 +1,71 @@
#!/usr/bin/env perl

use strict;
use warnings;

use Test::More tests => 10;
use Sub::Override;
use Capture::Tiny qw( capture );

use Module::Release;

BEGIN {
use File::Spec;
my $file = File::Spec->catfile( qw(. t lib setup_common.pl) );
require $file;
}

my $release = Module::Release->new;
$release->{'preset_field'} = 'preset';
is( $release->get_env_var('PRESET_FIELD'),
'preset', 'Preset field value returned' );

$ENV{'MOO'} = 'baa';
is( $release->get_env_var('MOO'),
'baa', 'Preset environment variable value returned' );

{
my $terminal_input =
Sub::Override->new( 'Module::Release::_slurp' => sub { "baa\n" } );
$ENV{'MOO'} = '';
my ( $stdout, $stderr, @result ) = capture { $release->get_env_var('MOO') };
is(
$stdout,
'MOO is not set. Enter it now: ',
'Empty environment variable prompts for value'
);
is( $result[0], 'baa', 'Variable read from input' );
}

{
my $terminal_input =
Sub::Override->new( 'Module::Release::_slurp' => sub { "\n" } );
$ENV{'MOO'} = undef;
$release->turn_debug_on;
my ( $stdout, $stderr, @result ) = capture { $release->get_env_var('MOO') };
is(
$stdout,
'MOO is not set. Enter it now: ',
'Undef environment variable prompts for value'
);
is(
$stderr,
"MOO not supplied. Aborting...\n",
"Error message about missing variable shown in debug mode"
);
}

{
my $terminal_input =
Sub::Override->new( 'Module::Release::_slurp' => sub { "s3cr3t\n" } );
$ENV{'CPAN_PASS'} = undef;
my ( $stdout, $stderr, @result ) = capture { $release->get_env_var('CPAN_PASS') };
is(
$stdout,
'CPAN_PASS is not set. Enter it now: ',
'Undef CPAN_PASS variable prompts for value'
);
is( $result[0], 's3cr3t', 'Variable password from input' );
}

# vim: expandtab shiftwidth=4
32 changes: 32 additions & 0 deletions t/input.t
@@ -0,0 +1,32 @@
#!/usr/bin/env perl

use strict;
use warnings;

use Test::More tests => 3;
use File::Temp qw(:seekable);

use Module::Release;

BEGIN {
use File::Spec;
my $file = File::Spec->catfile( qw(. t lib setup_common.pl) );
require $file;
}

my $release = Module::Release->new;
my $temp_fh = File::Temp->new;
$temp_fh->write("to be or not to be\n");
$temp_fh->flush();
$temp_fh->seek( 0, SEEK_SET );

$release->{input_fh} = $temp_fh;
my $input = $release->_slurp;

is(
$input,
"to be or not to be\n",
'_slurp returns content from input file handle'
);

# vim: expandtab shiftwidth=4

0 comments on commit 1467850

Please sign in to comment.