Skip to content

Commit

Permalink
replace the old exec test with new, mock-based testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
gugod committed Jan 31, 2012
1 parent 2c5bda7 commit f9ecf02
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 133 deletions.
66 changes: 0 additions & 66 deletions t/command-exec--with.t

This file was deleted.

125 changes: 58 additions & 67 deletions t/command-exec.t
Original file line number Diff line number Diff line change
Expand Up @@ -6,70 +6,61 @@ use lib $FindBin::Bin;
use App::perlbrew;
require 'test_helpers.pl';

use Test::More;
use Path::Class;
use File::Temp qw( tempdir );

my $root = dir($App::perlbrew::PERLBREW_ROOT);

my @perls = qw( yak needs shave );
my %exe = (
# this enables "perlbrew exec perl -e '...'"
perl => {
# interpolate $^X to specify current perl and avoid infinite recursion
content => qq[#!/bin/sh\nPERLBREW_TEST_PERL="\$0" exec $^X "\$@";\n],
# printing $^X is not helpful because these exe's are shell scripts that call the same perl
args => [ qw( exec perl -e ), 'open FH, ">>", shift and print FH "=$ENV{PERLBREW_TEST_PERL}\n" and close FH' ],
output => join('', sort map { "=$root/perls/$_/bin/perl\n" } @perls),
},
# also test an exe that isn't "perl" (like a script installed by a module)
brewed_app => {
content => qq[#!/bin/sh\necho \$0 >> \$1\n],
args => [ qw( exec brewed_app ) ],
output => join('', sort map { "$root/perls/$_/bin/brewed_app\n" } @perls),
},
# test something outside the $perl/bin/ to ensure that the environment is setup correctly
# NOTE: this script may need to change if the usage of these perlbrew vars changes
test_env => {
content => '', # don't create a file for this one
args => [ qw( exec sh -c ), 'echo "$PERLBREW_PERL--$PERLBREW_PATH" >> $0' ],
output => join('', sort map { "$_--$root/bin:$root/perls/$_/bin\n" } @perls),
},
);

close STDOUT;
close STDERR;

# build a fake root with some fake perls (most of this was modified from stuff found in t/installation.t)
foreach my $name ( @perls ) {
my $bin = $root->subdir("perls", $name, "bin");
App::perlbrew::mkpath($bin) for @perls;

while ( my ($script, $data) = each %exe ) {
next unless $data->{content};
my $path = $bin->file($script);
io($path)->print( $data->{content} );
chmod 0755, $path;
}
}

# exec each script
while ( my ($script, $data) = each %exe ) {
# we need a file to which the subprocesses can append
my $file = $root->file("test-exec-output.$script");

my $app = App::perlbrew->new(@{ $data->{args} }, $file->stringify);
$app->run;

# $file should have output in it
if ( -e $file ) {
# get output from all execs (ensure same order as above)
my $output = do { open(my $fh, '<', $file); join '', sort <$fh>; };
is $output, $data->{output}, 'correct exec output';
}
else {
ok 0, 'output file does not exist';
}
}

done_testing;
use Test::Spec;

mock_perlbrew_install("perl-5.12.3");
mock_perlbrew_install("perl-5.12.4");
mock_perlbrew_install("perl-5.14.1");
mock_perlbrew_install("perl-5.14.2");

describe 'perlbrew exec perl -E "say 42"' => sub {
it "invokes all perls" => sub {
my $app = App::perlbrew->new(qw(exec perl -E), "say 42");

my @perls = $app->installed_perls;

$app->expects("do_system")->exactly(4)->returns(
sub {
my ($self, @args) = @_;

is_deeply \@args, ["perl", "-E", "say 42"];

my ($perlbrew_bin_path, $perlbrew_perl_bin_path, @paths) = split(":", $ENV{PATH});

my $perl_installation = shift @perls;

is $perlbrew_bin_path, file($App::perlbrew::PERLBREW_ROOT, "bin");
is $perlbrew_perl_bin_path, file($App::perlbrew::PERLBREW_ROOT, "perls", $perl_installation->{name}, "bin"), "perls/". $perl_installation->{name} . "/bin";

return 0;
}
);

$app->run;
};
};

describe 'perlbrew exec --with perl-5.12.3 perl -E "say 42"' => sub {
it "invokes perl-5.12.3/bin/perl" => sub {
my $app = App::perlbrew->new(qw(exec --with perl-5.12.3 perl -E), "say 42");

$app->expects("do_system")->returns(
sub {
my ($self, @args) = @_;

is_deeply \@args, ["perl", "-E", "say 42"];

my ($perlbrew_bin_path, $perlbrew_perl_bin_path, @paths) = split(":", $ENV{PATH});

is $perlbrew_bin_path, file($App::perlbrew::PERLBREW_ROOT, "bin");
is $perlbrew_perl_bin_path, file($App::perlbrew::PERLBREW_ROOT, "perls", "perl-5.12.3", "bin");

return 0;
}
);

$app->run;
};
};

runtests unless caller;

0 comments on commit f9ecf02

Please sign in to comment.