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