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