warn about missing shells specified on the command line
[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',
e46466ba 38 opt => '/D /C',
3b7ffd31 39 ext => 'bat',
40 perl => qq{@"$^X"},
41 },
42 {
43 name => 'powershell',
e46466ba 44 opt => '-NoProfile -ExecutionPolicy Unrestricted',
3b7ffd31 45 ext => 'ps1',
46 perl => qq{& '$^X'},
47 },
48) {
49 my $name = $shell->{name};
edcdb011 50 $shell->{shell} = which($name);
3b7ffd31 51 $shell->{ext} ||= $name;
52 $shell->{perl} ||= qq{"$^X"};
edcdb011 53 if (@ARGV) {
54 next
55 if !grep {$_ eq $name} @ARGV;
56 if (!$shell->{shell}) {
57 warn "unable to find executable for $name";
58 next;
59 }
60 }
61 elsif (!$shell->{shell}) {
62 next;
63 }
3b7ffd31 64 push @shells, $shell;
65}
66
67if (!@shells) {
68 plan skip_all => 'no supported shells found';
69}
57987bd1 70plan tests => 6*@shells;
3b7ffd31 71
72my $sep = $Config{path_sep};
73
74my $root = File::Spec->rootdir;
75for my $shell (@shells) {
76 my $ll = File::Temp->newdir();
77 my $ll_dir = local::lib->normalize_path("$ll");
78 local $ENV{PERL_LOCAL_LIB_ROOT};
3b7ffd31 79 delete $ENV{PERL_LOCAL_LIB_ROOT};
57987bd1 80 local $ENV{PATH} = $root;
81 local $ENV{PERL5LIB};
82 delete $ENV{PERL5LIB};
3b7ffd31 83 my $env = call_ll($shell, "$ll");
57987bd1 84 is $env->{PERL_LOCAL_LIB_ROOT}, $ll_dir,
85 "$shell->{name}: activate root";
86 is $env->{PATH}, local::lib->install_base_bin_path($ll_dir)."$sep$root",
87 "$shell->{name}: activate PATH";
88 is $env->{PERL5LIB}, local::lib->install_base_perl_path($ll_dir),
89 "$shell->{name}: activate PERL5LIB";
3b7ffd31 90
91 $ENV{$_} = $env->{$_} for qw(PATH PERL5LIB PERL_LOCAL_LIB_ROOT);
92 $env = call_ll($shell, '--deactivate', "$ll");
93
57987bd1 94 is $env->{PERL_LOCAL_LIB_ROOT}, undef,
95 "$shell->{name}: deactivate root";
96 is $env->{PATH}, $root,
97 "$shell->{name}: deactivate PATH";
98 is $env->{PERL5LIB}, undef,
99 "$shell->{name}: deactivate PERL5LIB";
3b7ffd31 100}
101
102sub call_ll {
103 my ($info, @options) = @_;
104 my $option = @options ? '='.join(',', @options) : '';
105
106 local $ENV{SHELL} = $info->{shell};
107
19e23dc8 108 my $file = File::Temp->new(
109 TEMPLATE => 'll-test-script-XXXXX',
110 TMPDIR => 1,
111 SUFFIX => '.'.$info->{ext},
3b7ffd31 112 );
113
114 $file->print(scalar `"$^X" $extra_lib -Mlocal::lib$option` . "\n");
115 $file->print(qq{$info->{perl} -Mt::lib::ENVDumper -e1\n});
116 $file->close;
117
118 my $opt = $info->{opt} ? "$info->{opt} " : '';
119 my $out = `"$info->{shell}" $opt"$file"`;
120 if ($?) {
121 die "failed with code: $?";
122 }
123 my $VAR1;
124 eval $out or die "bad output: $@";
125 $VAR1;
126}