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