add option to reset @INC to defaults at top of fatpacked script
[p5sagit/App-FatPacker.git] / t / pack.t
CommitLineData
edff3fdc 1#!perl
2use strict;
3use warnings FATAL => 'all';
4use Test::More qw(no_plan);
5use File::Basename;
6use File::Copy;
7use File::Path;
55044309 8use File::Temp qw/tempdir/;
2cf9fee2 9use File::Spec;
8a3a89c9 10use Cwd;
edff3fdc 11
12BEGIN { use_ok "App::FatPacker", "" }
13
55044309 14my $keep = $ENV{'FATPACKER_KEEP_TESTDIR'};
15my $tempdir = tempdir($keep ? (CLEANUP => 0) : (CLEANUP => 1));
edff3fdc 16mkpath([<$tempdir/{lib,fatlib}/t/mod>]);
17
18for(<t/mod/*.pm>) {
19 copy $_, "$tempdir/lib/$_" or die "copy failed: $!";
20}
21
8a3a89c9 22my $cwd = getcwd;
edff3fdc 23chdir $tempdir;
24
25my $fp = App::FatPacker->new;
26my $temp_fh = File::Temp->new;
27select $temp_fh;
a03c42fa 28$fp->script_command_file([qw/--core-only/]);
edff3fdc 29print "1;\n";
30select STDOUT;
31close $temp_fh;
32
2cf9fee2 33# make sure we don't pick up things from our created dir
34chdir File::Spec->tmpdir;
35
a03c42fa 36my $guts = do { local (@ARGV,$/) = "$temp_fh"; <> };
37like( $guts, qr/\QBEGIN { use Config\E/, "saw core-only preamble" );
38
edff3fdc 39# Packed, now try using it:
40require $temp_fh;
41
42{
43 require t::mod::a;
44 no warnings 'once';
2cf9fee2 45 ok $t::mod::a::foo eq 'bar', "packed script works";
edff3fdc 46}
8a3a89c9 47
5a64a6aa 48{
49
50 ok ref $INC[0], "\$INC[0] is a reference";
51 ok $INC[0]->can( "files" ), "\$INC[0] has a files method";
52
53 my @files = sort $INC[0]->files;
54
15bd679e 55 is_deeply( \@files, [
56 't/mod/a.pm',
57 't/mod/b.pm',
58 't/mod/c.pm',
59 't/mod/cond.pm',
60 ], "\$INC[0]->files returned the files" );
5a64a6aa 61
62}
63
64
2cf9fee2 65if (my $testwith = $ENV{'FATPACKER_TESTWITH'}) {
66 for my $perl (split ' ', $testwith) {
67 my $out = system $perl, '-e',
68 q{alarm 5; require $ARGV[0]; require t::mod::a; exit($t::mod::a::foo eq 'bar' ? 0 : 1)}, $temp_fh;
69 ok !$out, "packed script works with $perl";
5a64a6aa 70
71 $out = system $perl, '-e',
72 q{alarm 5; require $ARGV[0]; exit( (sort $INC[0]->files)[0] eq 't/mod/a.pm' ? 0 : 1 )}, $temp_fh;
73 ok !$out, "\$INC[0]->files works with $perl";
74
2cf9fee2 75 }
76}
55044309 77