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