allow trailing PATH entries in shell test
[p5sagit/local-lib.git] / t / shell.t
CommitLineData
3b7ffd31 1use strict;
2use warnings;
3use Test::More;
4use File::Spec;
57987bd1 5use File::Basename qw(dirname);
3b7ffd31 6use File::Temp ();
7use Config;
8use local::lib ();
9
10my @paths = File::Spec->path;
3b7ffd31 11sub which {
12 my $shell = shift;
57de2993 13 my ($full) =
14 grep { -x }
15 map { File::Spec->catfile( $_, $shell) }
16 File::Spec->path;
17 return $full;
3b7ffd31 18}
19
57987bd1 20my $extra_lib = '-I"' . dirname(dirname($INC{'local/lib.pm'})) . '"';
3b7ffd31 21
22my @shells;
23for my $shell (
24 {
25 name => 'sh',
26 },
27 {
28 name => 'csh',
19e23dc8 29 opt => '-f',
3b7ffd31 30 },
31 {
57de2993 32 name => 'cmd.exe',
1d89f85b 33 opt => '/Q /D /C',
3b7ffd31 34 ext => 'bat',
35 perl => qq{@"$^X"},
1d89f85b 36 skip => $^O eq 'cygwin',
3b7ffd31 37 },
38 {
57de2993 39 name => 'powershell.exe',
1d89f85b 40 opt => '-NoProfile -ExecutionPolicy Unrestricted -File',
3b7ffd31 41 ext => 'ps1',
42 perl => qq{& '$^X'},
1d89f85b 43 skip => $^O eq 'cygwin',
3b7ffd31 44 },
45) {
46 my $name = $shell->{name};
edcdb011 47 $shell->{shell} = which($name);
3b7ffd31 48 $shell->{ext} ||= $name;
49 $shell->{perl} ||= qq{"$^X"};
edcdb011 50 if (@ARGV) {
51 next
52 if !grep {$_ eq $name} @ARGV;
53 if (!$shell->{shell}) {
54 warn "unable to find executable for $name";
55 next;
56 }
57 }
1d89f85b 58 elsif ($shell->{skip} || !$shell->{shell}) {
edcdb011 59 next;
60 }
3b7ffd31 61 push @shells, $shell;
62}
63
64if (!@shells) {
65 plan skip_all => 'no supported shells found';
66}
57987bd1 67plan tests => 6*@shells;
3b7ffd31 68
69my $sep = $Config{path_sep};
70
71my $root = File::Spec->rootdir;
72for my $shell (@shells) {
73 my $ll = File::Temp->newdir();
74 my $ll_dir = local::lib->normalize_path("$ll");
75 local $ENV{PERL_LOCAL_LIB_ROOT};
3b7ffd31 76 delete $ENV{PERL_LOCAL_LIB_ROOT};
57987bd1 77 local $ENV{PATH} = $root;
78 local $ENV{PERL5LIB};
79 delete $ENV{PERL5LIB};
3b7ffd31 80 my $env = call_ll($shell, "$ll");
57987bd1 81 is $env->{PERL_LOCAL_LIB_ROOT}, $ll_dir,
82 "$shell->{name}: activate root";
e5cbb984 83 my $bin_path = local::lib->install_base_bin_path($ll_dir);
84 like $env->{PATH}, qr/^\Q$bin_path$sep$root\E(?:$|\Q$sep\E)/,
57987bd1 85 "$shell->{name}: activate PATH";
86 is $env->{PERL5LIB}, local::lib->install_base_perl_path($ll_dir),
87 "$shell->{name}: activate PERL5LIB";
3b7ffd31 88
89 $ENV{$_} = $env->{$_} for qw(PATH PERL5LIB PERL_LOCAL_LIB_ROOT);
90 $env = call_ll($shell, '--deactivate', "$ll");
91
57987bd1 92 is $env->{PERL_LOCAL_LIB_ROOT}, undef,
93 "$shell->{name}: deactivate root";
e5cbb984 94 like $env->{PATH}, qr/^\Q$root\E(?:$|\Q$sep\E)/,
57987bd1 95 "$shell->{name}: deactivate PATH";
96 is $env->{PERL5LIB}, undef,
97 "$shell->{name}: deactivate PERL5LIB";
3b7ffd31 98}
99
100sub call_ll {
101 my ($info, @options) = @_;
102 my $option = @options ? '='.join(',', @options) : '';
103
104 local $ENV{SHELL} = $info->{shell};
105
94ff4026 106 my $script
107 = `"$^X" $extra_lib -Mlocal::lib$option` . "\n"
108 . qq{$info->{perl} -Mt::lib::ENVDumper -e1\n};
109
19e23dc8 110 my $file = File::Temp->new(
111 TEMPLATE => 'll-test-script-XXXXX',
112 TMPDIR => 1,
113 SUFFIX => '.'.$info->{ext},
3b7ffd31 114 );
94ff4026 115 print { $file } $script;
116 close $file;
3b7ffd31 117
118 my $opt = $info->{opt} ? "$info->{opt} " : '';
94ff4026 119 my $cmd = qq{"$info->{shell}" $opt"$file"};
120 my $out = `$cmd`;
3b7ffd31 121 if ($?) {
94ff4026 122 diag "script:\n$script";
123 diag "running:\n$cmd";
3b7ffd31 124 die "failed with code: $?";
125 }
126 my $VAR1;
127 eval $out or die "bad output: $@";
128 $VAR1;
129}