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