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