File: shell.t

package info (click to toggle)
liblocal-lib-perl 2.000014-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 308 kB
  • ctags: 72
  • sloc: perl: 727; makefile: 22
file content (251 lines) | stat: -rw-r--r-- 6,041 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
use strict;
use warnings;
use Test::More;
use File::Spec;
use File::Basename qw(dirname);
use File::Temp ();
use File::Path;
use Config;
use local::lib ();
use IPC::Open3 qw(open3);

my @ext = $^O eq 'MSWin32' ? (split /\Q$Config{path_sep}/, $ENV{PATHEXT}) : ();
sub which {
  my $shell = shift;
  my ($full) =
    grep { -x }
    map { my $x = $_; $x, map { $x . $_ } @ext }
    map { File::Spec->catfile( $_, $shell) }
    File::Spec->path;
  return $full;
}

BEGIN {
    *quote_literal =
      $^O ne 'MSWin32'
        ? sub { $_[0] }
        : sub {
          my ($text) = @_;
          $text =~ s{(\\*)(?="|\z)}{$1$1}g;
          $text =~ s{"}{\\"}g;
          $text = qq{"$text"};
          return $text;
        };
}

my %shell_path;
{
  my @shell_paths;
  if (open my $fh, '<', '/etc/shells') {
    my @lines = <$fh>;
    s/^\s+//, s/\s+$// for @lines;
    @shell_paths = grep { length && !/^#/ } @lines;
  }
  %shell_path =
    map { m{[\\/]([^\\/]+)$} ? ($1 => $_) : () }
    grep { defined && -x }
    ( '/bin/sh', '/bin/csh', $ENV{'ComSpec'}, @shell_paths );
}

my @extra_lib = ('-I' . dirname(dirname($INC{'local/lib.pm'})));
my $nul = File::Spec->devnull;

my @shells;
for my $shell (
  {
    name => 'sh',
    test => '-c "exit 0"',
  },
  {
    name => 'dash',
    test => '-c "exit 0"',
  },
  {
    name => 'bash',
    test => '-c "exit 0"',
  },
  {
    name => 'zsh',
    test => '-c "exit 0"',
  },
  {
    name => 'ksh',
    test => '-c "exit 0"',
  },
  {
    name => 'csh',
    test => '-c "exit 0"',
    opt => '-f',
  },
  {
    name => 'tcsh',
    test => '-c "exit 0"',
    opt => '-f',
  },
  {
    name => 'fish',
    test => '-c "exit 0"',
  },
  {
    name => 'cmd.exe',
    opt => '/Q /D /C',
    test => '/Q /D /C "exit 0"',
    ext => 'bat',
    perl => qq{@"$^X"},
    skip => $^O ne 'MSWin32',
  },
  {
    name => 'powershell.exe',
    shell => which('powershell.exe'),
    opt => '-NoProfile -ExecutionPolicy Unrestricted -File',
    test => '-NoProfile -Command "exit 0"',
    ext => 'ps1',
    perl => qq{& '$^X'},
    skip => $^O ne 'MSWin32',
  },
) {
  my $name = $shell->{name};
  $shell->{shell} ||= $shell_path{$name};
  $shell->{ext}   ||= $name;
  $shell->{perl}  ||= qq{"$^X"};
  if (@ARGV) {
    next
      if !grep {$_ eq $name} @ARGV;
    my $exec = $shell->{shell} ||= which($name);
    if (!$exec) {
      warn "unable to find executable for $name";
      next;
    }
  }
  elsif ($shell->{skip} || !$shell->{shell}) {
    next;
  }
  elsif ($shell->{test}) {
    no warnings 'exec';
    if (system "$shell->{shell} $shell->{test} > $nul 2> $nul") {
      diag "$name seems broken, skipping";
      next;
    }
  }
  push @shells, $shell;
}

if (!@shells) {
  plan skip_all => 'no supported shells found';
}
my @vars = qw(PATH PERL5LIB PERL_LOCAL_LIB_ROOT PERL_MM_OPT PERL_MB_OPT);
my @strings = (
  'string',
  'with space',
  'with"quote',
  "with'squote",
  'with\\bslash',
  'with%per%cent',
);

plan tests => @shells * (@vars * 2 + @strings * 2);

my $sep = $Config{path_sep};

my $root = File::Spec->rootdir;
for my $shell (@shells) {
  my $ll = local::lib->normalize_path(File::Temp::tempdir(CLEANUP => 1));
  local $ENV{$_}
    for @vars;
  delete $ENV{$_}
    for @vars;
  $ENV{PATH} = $root;
  my $bin_path = local::lib->install_base_bin_path($ll);
  mkdir $bin_path;
  my $env = call_ll($shell, $ll);
  is $env->{PERL_LOCAL_LIB_ROOT}, $ll,
    "$shell->{name}: activate root";
  like $env->{PATH}, qr/^\Q$bin_path$sep\E/,
    "$shell->{name}: activate PATH";
  is $env->{PERL5LIB}, local::lib->install_base_perl_path($ll),
    "$shell->{name}: activate PERL5LIB";
  my %install_opts = local::lib->installer_options_for($ll);
  for my $var (qw(PERL_MM_OPT PERL_MB_OPT)) {
    is $env->{$var}, $install_opts{$var},
      "$shell->{name}: activate $var";
  }

  $ENV{$_} = $env->{$_} for @vars;
  $env = call_ll($shell, '--deactivate', "$ll");

  unlike $env->{PATH}, qr/^\Q$bin_path$sep\E/,
    "$shell->{name}: deactivate PATH";
  for my $var (grep { $_ ne 'PATH' } @vars) {
    is $env->{$var}, undef,
      "$shell->{name}: deactivate $var";
  }

  my $shelltype = do {
    local $ENV{SHELL} = $shell->{shell};
    local::lib->guess_shelltype;
  };
  for my $string (@strings) {
    local $TODO = "$shell->{name}: can't quote strings with percents"
      if $shell->{name} eq 'cmd.exe' && $string =~ /%/;

    local $ENV{LL_TEST};
    delete $ENV{LL_TEST};
    my $script = local::lib->_build_env_string($shelltype, [
      LL_TEST => $string,
    ]);
    my $env = call_shell($shell, $script);
    is $env->{LL_TEST}, $string, "$shell->{name}: can quote [$string]";

    local $TODO = "$shell->{name}: can't test strings with double quotes"
      if $shell->{name} eq 'cmd.exe' && $string =~ /"/;

    $ENV{LL_TEST} = 'pre';
    $script = local::lib->_build_env_string($shelltype, [
      LL_TEST => [\"LL_TEST", $string],
    ]);
    $env = call_shell($shell, $script);
    is $env->{LL_TEST}, "pre$sep$string",
      "$shell->{name}: can append [$string]";
  }
}

sub call_ll {
  my ($info, @options) = @_;
  local $ENV{SHELL} = $info->{shell};

  open my $in, '<', File::Spec->devnull;
  open my $err, '>', File::Spec->devnull;
  open3 $in, my $out, $err,
    $^X, @extra_lib, '-Mlocal::lib', '-', '--no-create',
    map { quote_literal($_) } @options
    or die "blah";
  my $script = do { local $/; <$out> };
  close $out;
  call_shell($info, $script);
}

sub call_shell {
  my ($info, $script) = @_;
  $script .= "\n" . qq{$info->{perl} -Mt::lib::ENVDumper -e1\n};

  my ($fh, $file) = File::Temp::tempfile(
    'll-test-script-XXXXX',
    DIR      => File::Spec->tmpdir,
    SUFFIX   => '.'.$info->{ext},
    UNLINK   => 1,
  );
  print { $fh } $script;
  close $fh;

  my $opt = $info->{opt} ? "$info->{opt} " : '';
  my $cmd = qq{"$info->{shell}" $opt"$file"};
  my $output = `$cmd`;
  if ($?) {
    diag "script:\n$script";
    diag "running:\n$cmd";
    diag "failed with code: $?";
    return {};
  }
  my $env = eval $output or die "bad output: $@";
  $env;
}