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