fish fixes
[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 {
ecc3a2ee 32 name => 'fish',
33 },
34 {
57de2993 35 name => 'cmd.exe',
1d89f85b 36 opt => '/Q /D /C',
3b7ffd31 37 ext => 'bat',
38 perl => qq{@"$^X"},
1d89f85b 39 skip => $^O eq 'cygwin',
3b7ffd31 40 },
41 {
57de2993 42 name => 'powershell.exe',
1d89f85b 43 opt => '-NoProfile -ExecutionPolicy Unrestricted -File',
3b7ffd31 44 ext => 'ps1',
45 perl => qq{& '$^X'},
1d89f85b 46 skip => $^O eq 'cygwin',
3b7ffd31 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 }
1d89f85b 61 elsif ($shell->{skip} || !$shell->{shell}) {
edcdb011 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";
e5cbb984 86 my $bin_path = local::lib->install_base_bin_path($ll_dir);
87 like $env->{PATH}, qr/^\Q$bin_path$sep$root\E(?:$|\Q$sep\E)/,
57987bd1 88 "$shell->{name}: activate PATH";
89 is $env->{PERL5LIB}, local::lib->install_base_perl_path($ll_dir),
90 "$shell->{name}: activate PERL5LIB";
3b7ffd31 91
92 $ENV{$_} = $env->{$_} for qw(PATH PERL5LIB PERL_LOCAL_LIB_ROOT);
93 $env = call_ll($shell, '--deactivate', "$ll");
94
57987bd1 95 is $env->{PERL_LOCAL_LIB_ROOT}, undef,
96 "$shell->{name}: deactivate root";
e5cbb984 97 like $env->{PATH}, qr/^\Q$root\E(?:$|\Q$sep\E)/,
57987bd1 98 "$shell->{name}: deactivate PATH";
99 is $env->{PERL5LIB}, undef,
100 "$shell->{name}: deactivate PERL5LIB";
3b7ffd31 101}
102
103sub call_ll {
104 my ($info, @options) = @_;
105 my $option = @options ? '='.join(',', @options) : '';
106
107 local $ENV{SHELL} = $info->{shell};
108
94ff4026 109 my $script
110 = `"$^X" $extra_lib -Mlocal::lib$option` . "\n"
111 . qq{$info->{perl} -Mt::lib::ENVDumper -e1\n};
112
19e23dc8 113 my $file = File::Temp->new(
114 TEMPLATE => 'll-test-script-XXXXX',
115 TMPDIR => 1,
116 SUFFIX => '.'.$info->{ext},
3b7ffd31 117 );
94ff4026 118 print { $file } $script;
119 close $file;
3b7ffd31 120
121 my $opt = $info->{opt} ? "$info->{opt} " : '';
94ff4026 122 my $cmd = qq{"$info->{shell}" $opt"$file"};
123 my $out = `$cmd`;
3b7ffd31 124 if ($?) {
94ff4026 125 diag "script:\n$script";
126 diag "running:\n$cmd";
3b7ffd31 127 die "failed with code: $?";
128 }
129 my $VAR1;
130 eval $out or die "bad output: $@";
131 $VAR1;
132}