| 1 | #!./perl -w |
| 2 | |
| 3 | BEGIN { |
| 4 | chdir 't'; |
| 5 | @INC = '../lib'; |
| 6 | @OrigINC = @INC; |
| 7 | } |
| 8 | |
| 9 | use Test::More tests => 13; |
| 10 | use Config; |
| 11 | use File::Spec; |
| 12 | use File::Path; |
| 13 | |
| 14 | #set up files and directories |
| 15 | my @lib_dir; |
| 16 | my $Lib_Dir; |
| 17 | my $Arch_Dir; |
| 18 | my $Auto_Dir; |
| 19 | my $Module; |
| 20 | BEGIN { |
| 21 | # lib.pm is documented to only work with Unix filepaths. |
| 22 | @lib_dir = qw(stuff moo); |
| 23 | $Lib_Dir = join "/", @lib_dir; |
| 24 | $Arch_Dir = join "/", @lib_dir, $Config{archname}; |
| 25 | |
| 26 | # create the auto/ directory and a module |
| 27 | $Auto_Dir = File::Spec->catdir(@lib_dir, $Config{archname},'auto'); |
| 28 | $Module = File::Spec->catfile(@lib_dir, 'Yup.pm'); |
| 29 | |
| 30 | mkpath [$Auto_Dir]; |
| 31 | |
| 32 | open(MOD, ">$Module") || DIE $!; |
| 33 | print MOD <<'MODULE'; |
| 34 | package Yup; |
| 35 | $Plan = 9; |
| 36 | return '42'; |
| 37 | MODULE |
| 38 | |
| 39 | close MOD; |
| 40 | } |
| 41 | |
| 42 | END { |
| 43 | # cleanup the auto/ directory we created. |
| 44 | rmtree([$lib_dir[0]]); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | use lib $Lib_Dir; |
| 49 | use lib $Lib_Dir; |
| 50 | |
| 51 | BEGIN { use_ok('Yup') } |
| 52 | |
| 53 | BEGIN { |
| 54 | if ($^O eq 'MacOS') { |
| 55 | for ($Lib_Dir, $Arch_Dir) { |
| 56 | tr|/|:|; |
| 57 | $_ .= ":" unless /:$/; |
| 58 | $_ = ":$_" unless /^:/; # we know this path is relative |
| 59 | } |
| 60 | } |
| 61 | is( $INC[1], $Lib_Dir, 'lib adding at end of @INC' ); |
| 62 | print "# \@INC == @INC\n"; |
| 63 | is( $INC[0], $Arch_Dir, ' auto/ dir in front of that' ); |
| 64 | is( grep(/^\Q$Lib_Dir\E$/, @INC), 1, ' no duplicates' ); |
| 65 | |
| 66 | # Yes, %INC uses Unixy filepaths. |
| 67 | # Not on Mac OS, it doesn't ... it never has, at least. |
| 68 | my $path = join("/",$Lib_Dir, 'Yup.pm'); |
| 69 | if ($^O eq 'MacOS') { |
| 70 | $path = $Lib_Dir . 'Yup.pm'; |
| 71 | } |
| 72 | is( $INC{'Yup.pm'}, $path, '%INC set properly' ); |
| 73 | |
| 74 | is( eval { do 'Yup.pm' }, 42, 'do() works' ); |
| 75 | ok( eval { require Yup; }, ' require()' ); |
| 76 | ok( eval "use Yup; 1;", ' use()' ); |
| 77 | is( $@, '' ); |
| 78 | |
| 79 | is_deeply(\@OrigINC, \@lib::ORIG_INC, '@lib::ORIG_INC' ); |
| 80 | } |
| 81 | |
| 82 | no lib $Lib_Dir; |
| 83 | |
| 84 | unlike( do { eval 'use lib $Config{installsitelib};'; $@ || '' }, |
| 85 | qr/::Config is read-only/, 'lib handles readonly stuff' ); |
| 86 | |
| 87 | BEGIN { |
| 88 | is( grep(/stuff/, @INC), 0, 'no lib' ); |
| 89 | ok( !do 'Yup.pm', ' do() effected' ); |
| 90 | } |