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