]> git.vpit.fr Git - perl/modules/VPIT-TestHelpers.git/blob - t/30-run_perl.t
Turn run_perl() into a feature
[perl/modules/VPIT-TestHelpers.git] / t / 30-run_perl.t
1 #!perl
2
3 use strict;
4 use warnings;
5
6 use VPIT::TestHelpers 'run_perl' => [ 'VTH' ];
7
8 use Test::More tests => 5 + 2;
9
10 use Config;
11 use File::Temp;
12
13 my $filename = '/tmp/run_perl_test';
14
15 my $guard = VPIT::TestHelpers::Guard->new(sub { unlink $filename });
16
17 my $code = "open my \$fh, q[>], q[$filename] or die \$!; for my \$key (sort keys %ENV) { print \$fh qq[\$key:\$ENV{\$key}\n] } close \$fh";
18
19 my $ld_path = $Config::Config{ldlibpthname};
20
21 my %expected_vars = (
22  'MSWin32' => [ qw<SystemRoot> ],
23  'android' => [                ],
24  'cygwin'  => [ qw<PATH>       ],
25  'darwin'  => [                ],
26  'linux'   => [                ],
27 );
28 push @$_, $ld_path for values %expected_vars;
29
30 my @all_vars = do {
31  my %uniq;
32  grep !$uniq{$_}++, map @$_, values %expected_vars;
33 };
34 push @all_vars, 'DUMMY';
35
36 my @ignored_vars = qw<
37  VERSIONER_PERL_PREFER_32_BIT
38  VERSIONER_PERL_VERSION
39  __CF_USER_TEXT_ENCODING
40 >;
41
42 for my $os (qw<MSWin32 android darwin cygwin linux>) {
43  1 while unlink $filename;
44
45  local $^O  = $os;
46  local %ENV;
47
48  @ENV{@all_vars} = @all_vars;
49
50  run_perl $code;
51
52  my %subprocess_env;
53
54  open my $fh, '<', $filename;
55  if ($fh) {
56   my @lines = <$fh>;
57   close $fh;
58   chomp for @lines;
59   %subprocess_env = map { split /:/, $_, 2 } @lines;
60  }
61
62  delete @subprocess_env{@ignored_vars};
63
64  my $got = join "\n", sort keys %subprocess_env;
65  my $exp = join "\n", sort @{ $expected_vars{$os} || [] };
66
67  is $got, $exp, "run_perl preserving $os ENV vars";
68 }
69
70 {
71  local $@;
72  eval {
73   run_perl 'print "hello\n"';
74  };
75  like $@, qr/^Double quotes in evaluated code are not portable/,
76           'croak on double quotes';
77 }
78
79 {
80  my $msg = VTH_RUN_PERL_FAILED;
81  like $msg, qr/^Could not execute/, 'RUN_PERL_FAILED error message';
82 }