cbceb647b705735527b3b883f6db95467c77fc5b
[p5sagit/local-lib.git] / t / install.t
1 use strict;
2 use warnings;
3 use Test::More;
4 BEGIN { plan skip_all => "Install Capture::Tiny to test installation"
5   unless eval { require Capture::Tiny; 1 } }
6 use Capture::Tiny qw(capture_merged);
7 use File::Spec;
8 use File::Path qw(mkpath);
9 use Cwd;
10 use Config;
11
12 use lib 't/lib'; use TempDir;
13
14 use local::lib ();
15
16 delete @ENV{
17   'PERL_MM_OPT',
18   'PERL_MB_OPT',
19   'PERL_LOCAL_LIB_ROOT',
20   grep /^MAKE/, keys %ENV
21 };
22
23 my @dirs = (
24   'plain',
25   'with space',
26   'with\backslash',
27   'with space\and-bs',
28 );
29
30 my %dist_types = (
31   EUMM => sub {
32     open my $fh, '>', 'Makefile.PL' or die "can't create Makefile.PL: $!";
33     print $fh 'use ExtUtils::MakeMaker; WriteMakefile( NAME => "EUMM" );';
34     close $fh;
35     system($^X, 'Makefile.PL') && die "Makefile.PL failed";
36     system($Config{make}, 'install') && die "$Config{make} install failed";
37   },
38   MB => sub {
39     open my $fh, '>', 'Build.PL' or die "can't create Build.PL: $!";
40     print $fh <<END_BUILD;
41 use Module::Build;
42 Module::Build->new(
43   module_name       => "MB",
44   dist_version      => 1,
45   license           => "perl",
46 )->create_build_script;
47 END_BUILD
48     close $fh;
49     system($^X, 'Build.PL') && die "Build.PL failed";
50     system($^X, 'Build', 'install') && die "Build install failed";
51   },
52 );
53
54 plan tests => @dirs * keys(%dist_types) * 2;
55
56 my $orig_dir = cwd;
57 for my $dir_base (@dirs) {
58   for my $dist_type (sort keys %dist_types) {
59     chdir $orig_dir;
60     my $temp = mk_temp_dir("install-$dist_type-XXXXX");
61     my $ll_dir = "$dist_type-$dir_base";
62     my $ll = "$temp/$ll_dir";
63     mkpath(File::Spec->canonpath($ll));
64
65     local::lib->import($ll);
66
67     my $dist_dir = mk_temp_dir("source-$dist_type-XXXXX");
68     chdir $dist_dir;
69     mkdir 'lib';
70     open my $fh, '>', "lib/$dist_type.pm";
71     print $fh '1;';
72     close $fh;
73
74     my $output = capture_merged { eval {
75       $dist_types{$dist_type}->();
76     } };
77     is $@, '', "installed $dist_type into '$ll_dir'"
78       or diag $output;
79
80     my $dest_dir = local::lib->install_base_perl_path($ll);
81     my $file = File::Spec->catfile($dest_dir, "$dist_type.pm");
82     (my $short_file = $file) =~ s/^\Q$ll/$ll_dir/;
83     ok(
84       -e $file,
85       "$dist_type - $dir_base - $dist_type.pm installed as '$short_file'",
86     ) or diag 'Files in ' . $dest_dir . ":\n", join("\n", do {
87       my $dh;
88       (opendir $dh, $dest_dir) ? readdir $dh : "doesn't exist";
89     });
90   }
91 }