1#!perl 2use strict; 3use Capture::Tiny qw/capture/; 4use IO::All; 5use App::perlbrew; 6use File::Temp qw( tempdir ); 7 8$App::perlbrew::PERLBREW_ROOT = tempdir( CLEANUP => 1 ); 9$App::perlbrew::PERLBREW_HOME = tempdir( CLEANUP => 1 ); 10$ENV{PERLBREW_ROOT} = $App::perlbrew::PERLBREW_ROOT; 11my $DESTDIR = tempdir( CLEANUP => 1 ); 12 13use Test::More; 14 15## setup 16 17App::Perlbrew::Path 18 ->new($ENV{PERLBREW_ROOT}) 19 ->rmpath; 20 21## mock 22 23no warnings 'redefine'; 24 25sub App::perlbrew::do_system { 26 my ($self, $cmd) = @_; 27 if ($cmd =~ /sitelib/) { 28 print "sitelib='$ENV{PERLBREW_ROOT}/perls/perl-5.14.2/lib/site_perl/5.14.2';\n"; 29 print "installprefix='$ENV{PERLBREW_ROOT}/perls/perl-5.14.2';\n"; 30 print "installstyle='lib';\n"; 31 return 1; 32 } 33 elsif ($cmd =~ /Configure/) { 34 # pretend to succeed 35 return 1; 36 } 37 else { 38 # fail to run 39 $? = 1<<8; 40 $! = "Could not run '$cmd'"; 41 return 0; 42 } 43} 44 45sub App::perlbrew::do_install_release { 46 my ($self, $dist) = @_; 47 my ($dist_name, $dist_version) = $dist =~ m/^(.*)-([\d.]+(?:-RC\d+)?)$/; 48 49 my $name = $dist; 50 $name = $self->{as} if $self->{as}; 51 52 my $root = App::Perlbrew::Path->new ($DESTDIR, $ENV{PERLBREW_ROOT}); 53 my $installation_dir = $root->child("perls", $name); 54 $installation_dir->mkpath; 55 $root->child("perls", $name, "bin")->mkpath; 56 57 my $perl = $root->child("perls", $name, "bin")->child("perl"); 58 io($perl)->print("#!/bin/sh\nperl \"\$@\";\n"); 59 chmod 0755, $perl; 60 61 # fake the install 62 $self->do_install_this("/tmp/fake-src/perl-5.14.2", $dist_version, $dist); 63} 64 65use warnings; 66 67## main 68 69note "PERLBREW_ROOT set to $ENV{PERLBREW_ROOT}"; 70note "DESTDIR set to $DESTDIR"; 71 72subtest "No perls yet installed" => sub { 73 my $app = App::perlbrew->new; 74 my @installed = grep { !$_->{is_external} } $app->installed_perls; 75 is 0+@installed, 0, "no perls installed"; 76}; 77 78subtest "--destdir option can be set" => sub { 79 my $app = App::perlbrew->new('install', 'perl-5.14.2', 80 '--destdir=/tmp/foo' 81 ); 82 83 is join(' ', $app->args), join(' ', qw(install perl-5.14.2)), "post-option args correct"; 84 is $app->{destdir}, '/tmp/foo', '--destdir set as expected'; 85}; 86 87subtest "mock installing" => sub { 88 my $sitefile = File::Temp->new; 89 print $sitefile "use strict;\n"; 90 close $sitefile; 91 my $app = App::perlbrew->new('install', 'perl-5.14.2', 92 "--destdir=$DESTDIR", "--sitecustomize=$sitefile" 93 ); 94 my ($output,$error) = capture { $app->run }; 95 96 my @installed = grep { !$_->{is_external} } $app->installed_perls; 97 is 0+@installed, 0, "found 0 installed perl (as it's installed in DESTDIR)"; 98 99 my $root = App::Perlbrew::Path->new ($DESTDIR, $ENV{PERLBREW_ROOT}); 100 my $perldir = $root->child("perls", "perl-5.14.2"); 101 my $installedsite = $perldir->child('lib', 'site_perl', '5.14.2', 'sitecustomize.pl'); 102 ok( -f $installedsite, "sitecustomize.pl installed in DESTDIR" ); 103 104 my $guts = do { local (@ARGV, $/) = $installedsite; <> }; 105 is( $guts, "use strict;\n", "sitecustomize.pl contents correct in DESTDIR" ); 106}; 107 108done_testing; 109# vim: ts=4 sts=4 sw=4 et: 110