better diagnostics on taint test, and a failing case
[p5sagit/local-lib.git] / t / taint-mode.t
1 #
2 # t/taint-mode.t: checks that local::lib sets up @INC correctly when
3 # included in a script that has taint mode on, and is executing in an
4 # environment in which local::lib has already been loaded.
5 #
6
7 use strict;
8 use warnings;
9 use Test::More tests => 3;
10 use File::Temp 'tempfile';
11 use Cwd;
12 use File::Spec;
13 use IPC::Open3;
14
15 use lib 't/lib'; use TempDir;
16
17 my @INC_CLEAN = @INC;
18
19 my $dir1 = mk_temp_dir('used_in_taint-XXXXX');
20 my $dir2 = mk_temp_dir('not_used_in_taint-XXXXX');
21
22 # Set up local::lib environment using our temp dir
23 require local::lib;
24 local::lib->import($dir1);
25 local::lib->import($dir2);
26
27 # Create a script that has taint mode turned on, and tries to use a
28 # local lib to the same temp dir.
29 my ($fh, $filename) = tempfile('test_local_lib-XXXXX', DIR => Cwd::abs_path('t'), UNLINK => 1);
30
31 print $fh <<"EOM";
32 #!/usr/bin/perl -T
33 use strict; use warnings;
34 use local::lib "\Q$dir1\E";
35 print "\$_\\n" for \@INC;
36 EOM
37 close $fh;
38
39 open my $in, '<', File::Spec->devnull;
40 my $pid = open3($in, my $out, undef, $^X, map("-I$_", @INC_CLEAN), '-T', $filename);
41 my @libs = <$out>;
42 s/[\r\n]*\z// for @libs;
43 close $out;
44 waitpid $pid, 0;
45 is $?, 0, 'test script ran without error';
46
47 my $dir1_lib = local::lib->install_base_perl_path($dir1);
48 ok grep($_ eq $dir1_lib, @libs),
49   'local::lib used in taint script added to @INC'
50   or diag "searched for '$dir1_lib' in: ", explain \@libs;
51
52 my $dir2_lib = local::lib->install_base_perl_path($dir2);
53 ok !grep($_ eq $dir2_lib, @libs),
54   'local::lib not used used in taint script not added to @INC'
55   or diag "searched for '$dir2_lib' in: ", explain \@libs;