Carp::Always should not be a dep
[p5sagit/local-lib.git] / t / stackable.t
CommitLineData
b0e4e8db 1use strict;
2use warnings;
3use Test::More;
4use File::Temp qw(tempdir);
b4d420ef 5use File::Spec;
b0e4e8db 6use Cwd;
b4d420ef 7use Config;
b0e4e8db 8
b4d420ef 9plan tests => 24;
b0e4e8db 10
f694b8aa 11use local::lib ();
12
c1ed5f4b 13sub mk_temp_dir
14{
15 my $name_template = shift;
16
17 my $path = tempdir($name_template, DIR => Cwd::abs_path('t'), CLEANUP => 1);
18 local::lib->ensure_dir_structure_for($path);
19 # On Win32 the path where the distribution is built usually contains
20 # spaces. This is a problem for some parts of the CPAN toolchain, so
21 # local::lib uses the GetShortPathName trick do get an alternate
22 # representation of the path that doesn't constain spaces.
23 return ($^O eq 'MSWin32')
24 ? Win32::GetShortPathName($path)
25 : $path
26}
27
28my $dir1 = mk_temp_dir('test_local_lib-XXXXX');
29my $dir2 = mk_temp_dir('test_local_lib-XXXXX');
b0e4e8db 30
b4d420ef 31my ($dir1_arch, $dir2_arch) = map { File::Spec->catfile($_, qw'lib perl5', $Config{archname}) } $dir1, $dir2;
32note $dir1_arch;
33note $dir2_arch;
34
35
a20a4a55 36my $prev_active = () = local::lib->active_paths;
37
b0e4e8db 38local::lib->import($dir1);
a20a4a55 39is +() = local::lib->active_paths, $prev_active + 1, 'one active path';
b0e4e8db 40like $ENV{PERL_LOCAL_LIB_ROOT}, qr/\Q$dir1/, 'added one dir in root';
41like $ENV{PERL5LIB}, qr/\Q$dir1/, 'added one dir in lib';
b4d420ef 42note $ENV{PERL5LIB};
43unlike $ENV{PERL5LIB}, qr/\Q$dir1_arch/, 'no arch in PERL5LIB';
6369ed6d 44like $ENV{PERL_MM_OPT}, qr/\Q$dir1/, 'first path is installation target';
45
46local::lib->import($dir1);
a20a4a55 47is +() = local::lib->active_paths, $prev_active + 1, 'still one active path after adding it twice';
b0e4e8db 48
49local::lib->import($dir2);
a20a4a55 50is +() = local::lib->active_paths, $prev_active + 2, 'two active paths';
b0e4e8db 51like $ENV{PERL_LOCAL_LIB_ROOT}, qr/\Q$dir2/, 'added another dir in root';
52like $ENV{PERL5LIB}, qr/\Q$dir2/, 'added another dir in lib';
b4d420ef 53unlike $ENV{PERL5LIB}, qr/\Q$dir2_arch/, 'no arch in PERL5LIB';
b0e4e8db 54like $ENV{PERL_LOCAL_LIB_ROOT}, qr/\Q$dir1/, 'first dir is still in root';
55like $ENV{PERL5LIB}, qr/\Q$dir1/, 'first dir is still in lib';
b4d420ef 56unlike $ENV{PERL5LIB}, qr/\Q$dir1_arch/, 'no arch in PERL5LIB';
6369ed6d 57like $ENV{PERL_MM_OPT}, qr/\Q$dir2/, 'second path is installation target';
58
59local::lib->import($dir1);
60my @active = local::lib->active_paths;
a20a4a55 61is @active, $prev_active + 2, 'still two active dirs after re-adding first';
6369ed6d 62is $active[-1], $dir1, 'first dir was re-added on top';
63like $ENV{PERL_MM_OPT}, qr/\Q$dir1/, 'first path is installation target again';
b0e4e8db 64
6369ed6d 65local::lib->import('--deactivate', $dir2);
66unlike $ENV{PERL_LOCAL_LIB_ROOT}, qr/\Q$dir2/, 'second dir was removed from root';
67unlike $ENV{PERL5LIB}, qr/\Q$dir2/, 'second dir was removed from lib';
b4d420ef 68unlike $ENV{PERL5LIB}, qr/\Q$dir2_arch/, 'no arch in PERL5LIB';
6369ed6d 69like $ENV{PERL_LOCAL_LIB_ROOT}, qr/\Q$dir1/, q{first dir didn't go away from root};
70like $ENV{PERL5LIB}, qr/\Q$dir1/, q{first dir didn't go away from lib};
b4d420ef 71unlike $ENV{PERL5LIB}, qr/\Q$dir1_arch/, 'no arch in PERL5LIB';
6369ed6d 72like $ENV{PERL_MM_OPT}, qr/\Q$dir1/, 'first dir stays installation target';