Carp::Always should not be a dep
[p5sagit/local-lib.git] / t / stackable.t
1 use strict;
2 use warnings;
3 use Test::More;
4 use File::Temp qw(tempdir);
5 use File::Spec;
6 use Cwd;
7 use Config;
8
9 plan tests => 24;
10
11 use local::lib ();
12
13 sub 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
28 my $dir1 = mk_temp_dir('test_local_lib-XXXXX');
29 my $dir2 = mk_temp_dir('test_local_lib-XXXXX');
30
31 my ($dir1_arch, $dir2_arch) = map { File::Spec->catfile($_, qw'lib perl5', $Config{archname}) } $dir1, $dir2;
32 note $dir1_arch;
33 note $dir2_arch;
34
35
36 my $prev_active = () = local::lib->active_paths;
37
38 local::lib->import($dir1);
39 is +() = local::lib->active_paths, $prev_active + 1, 'one active path';
40 like $ENV{PERL_LOCAL_LIB_ROOT}, qr/\Q$dir1/, 'added one dir in root';
41 like $ENV{PERL5LIB}, qr/\Q$dir1/, 'added one dir in lib';
42 note $ENV{PERL5LIB};
43 unlike $ENV{PERL5LIB}, qr/\Q$dir1_arch/, 'no arch in PERL5LIB';
44 like $ENV{PERL_MM_OPT}, qr/\Q$dir1/, 'first path is installation target';
45
46 local::lib->import($dir1);
47 is +() = local::lib->active_paths, $prev_active + 1, 'still one active path after adding it twice';
48
49 local::lib->import($dir2);
50 is +() = local::lib->active_paths, $prev_active + 2, 'two active paths';
51 like $ENV{PERL_LOCAL_LIB_ROOT}, qr/\Q$dir2/, 'added another dir in root';
52 like $ENV{PERL5LIB}, qr/\Q$dir2/, 'added another dir in lib';
53 unlike $ENV{PERL5LIB}, qr/\Q$dir2_arch/, 'no arch in PERL5LIB';
54 like $ENV{PERL_LOCAL_LIB_ROOT}, qr/\Q$dir1/, 'first dir is still in root';
55 like $ENV{PERL5LIB}, qr/\Q$dir1/, 'first dir is still in lib';
56 unlike $ENV{PERL5LIB}, qr/\Q$dir1_arch/, 'no arch in PERL5LIB';
57 like $ENV{PERL_MM_OPT}, qr/\Q$dir2/, 'second path is installation target';
58
59 local::lib->import($dir1);
60 my @active = local::lib->active_paths;
61 is @active, $prev_active + 2, 'still two active dirs after re-adding first';
62 is $active[-1], $dir1, 'first dir was re-added on top';
63 like $ENV{PERL_MM_OPT}, qr/\Q$dir1/, 'first path is installation target again';
64
65 local::lib->import('--deactivate', $dir2);
66 unlike $ENV{PERL_LOCAL_LIB_ROOT}, qr/\Q$dir2/, 'second dir was removed from root';
67 unlike $ENV{PERL5LIB}, qr/\Q$dir2/, 'second dir was removed from lib';
68 unlike $ENV{PERL5LIB}, qr/\Q$dir2_arch/, 'no arch in PERL5LIB';
69 like $ENV{PERL_LOCAL_LIB_ROOT}, qr/\Q$dir1/, q{first dir didn't go away from root};
70 like $ENV{PERL5LIB}, qr/\Q$dir1/, q{first dir didn't go away from lib};
71 unlike $ENV{PERL5LIB}, qr/\Q$dir1_arch/, 'no arch in PERL5LIB';
72 like $ENV{PERL_MM_OPT}, qr/\Q$dir1/, 'first dir stays installation target';