correct output for fish shell
[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};
1730f48f 83 my $bin_path = local::lib->install_base_bin_path($ll_dir);
84 mkdir $bin_path;
3b7ffd31 85 my $env = call_ll($shell, "$ll");
57987bd1 86 is $env->{PERL_LOCAL_LIB_ROOT}, $ll_dir,
87 "$shell->{name}: activate root";
e5cbb984 88 like $env->{PATH}, qr/^\Q$bin_path$sep$root\E(?:$|\Q$sep\E)/,
57987bd1 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";
e5cbb984 98 like $env->{PATH}, qr/^\Q$root\E(?:$|\Q$sep\E)/,
57987bd1 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
94ff4026 110 my $script
111 = `"$^X" $extra_lib -Mlocal::lib$option` . "\n"
112 . qq{$info->{perl} -Mt::lib::ENVDumper -e1\n};
113
19e23dc8 114 my $file = File::Temp->new(
115 TEMPLATE => 'll-test-script-XXXXX',
116 TMPDIR => 1,
117 SUFFIX => '.'.$info->{ext},
3b7ffd31 118 );
94ff4026 119 print { $file } $script;
120 close $file;
3b7ffd31 121
122 my $opt = $info->{opt} ? "$info->{opt} " : '';
94ff4026 123 my $cmd = qq{"$info->{shell}" $opt"$file"};
124 my $out = `$cmd`;
3b7ffd31 125 if ($?) {
94ff4026 126 diag "script:\n$script";
127 diag "running:\n$cmd";
3b7ffd31 128 die "failed with code: $?";
129 }
130 my $VAR1;
131 eval $out or die "bad output: $@";
132 $VAR1;
133}