Update Module::Build to 0.3603
[p5sagit/p5-mst-13.2.git] / cpan / Module-Build / t / bundle_inc.t
1 # sample.t -- a sample test file for Module::Build
2
3 use strict;
4 use lib 't/lib';
5 use MBTest; # or 'no_plan'
6 use DistGen;
7 use Config;
8 use IO::File;
9 use File::Spec;
10 use ExtUtils::Packlist;
11 use ExtUtils::Installed;
12 use File::Path;
13
14 # Ensure any Module::Build modules are loaded from correct directory
15 blib_load('Module::Build');
16 blib_load('Module::Build::ConfigData');
17
18 if ( $ENV{PERL_CORE} ) {
19   plan skip_all => 'bundle_inc tests will never succeed in PERL_CORE';
20 }
21 elsif ( ! $ENV{MB_TEST_EXPERIMENTAL} ) {
22   plan skip_all => '$ENV{MB_TEST_EXPERIMENTAL} is not set';
23 }
24 elsif ( ! MBTest::check_EUI() ) {
25   plan skip_all => 'ExtUtils::Installed takes too long on your system';
26 }
27 elsif ( Module::Build::ConfigData->feature('inc_bundling_support') ) {
28   plan tests => 19;
29 } else {
30   plan skip_all => 'inc_bundling_support feature is not enabled';
31 }
32
33 # need to do a temp install of M::B being tested to ensure a packlist
34 # is available for bundling
35
36 my $current_mb = Module::Build->resume();
37 my $temp_install = MBTest->tmpdir();
38 my $arch = $Config{archname};
39 my $lib_path = File::Spec->catdir($temp_install,qw/lib perl5/);
40 my $arch_path = File::Spec->catdir( $lib_path, $arch );
41 mkpath ( $arch_path );
42 ok( -d $arch_path, "created temporary M::B pseudo-install directory");
43
44 unshift @INC, $lib_path, $arch_path;
45 local $ENV{PERL5LIB} = join( $Config{path_sep},
46   $lib_path, ($ENV{PERL5LIB} ? $ENV{PERL5LIB} : () )
47 );
48
49 # must uninst=0 so we don't try to remove an installed M::B!
50 stdout_of( sub { $current_mb->dispatch(
51       'install', install_base => $temp_install, uninst => 0
52     )
53   }
54 );
55
56 # create dist object in a temp directory
57 # enter the directory and generate the skeleton files
58 my $dist = DistGen->new( inc => 1 )->chdir_in->regen;
59
60 # get a Module::Build object and test with it
61 my $mb = $dist->new_from_context(); # quiet by default
62 isa_ok( $mb, "Module::Build" );
63 is( $mb->dist_name, "Simple", "dist_name is 'Simple'" );
64 is_deeply( $mb->bundle_inc, [ 'Module::Build' ],
65   "Module::Build is flagged for bundling"
66 );
67
68 # bundle stuff into distdir
69 stdout_stderr_of( sub { $mb->dispatch('distdir') } );
70
71 my $dist_inc = File::Spec->catdir($mb->dist_dir, 'inc');
72 ok( -e File::Spec->catfile( $dist_inc, 'latest.pm' ),
73   "dist_dir/inc/latest.pm created"
74 );
75
76 ok( -d File::Spec->catdir( $dist_inc, 'inc_Module-Build' ),
77   "dist_dir/inc/inc_Module_Build created"
78 );
79
80 my $mb_file =
81   File::Spec->catfile( $dist_inc, qw/inc_Module-Build Module Build.pm/ );
82
83 ok( -e $mb_file,
84   "dist_dir/inc/inc_Module_Build/Module/Build.pm created"
85 );
86
87 ok( -e File::Spec->catfile( $dist_inc, qw/inc_Module-Build Module Build Base.pm/ ),
88   "dist_dir/inc/inc_Module_Build/Module/Build/Base.pm created"
89 );
90
91 # Force bundled M::B to a higher version so it gets loaded
92 # This has failed on Win32 for no known reason, so we'll skip if
93 # we can't edit the file.
94
95 eval {
96   my $fh;
97   chmod 0666, $mb_file;
98   $fh = IO::File->new($mb_file, "<") or die "Could not read $mb_file: $!";
99   my $mb_code = do { local $/; <$fh> };
100   $mb_code =~ s{\$VERSION\s+=\s+\S+}{\$VERSION = 9999;};
101   $fh->close;
102   $fh = IO::File->new($mb_file, ">") or die "Could not write $mb_file: $!";
103   print {$fh} $mb_code;
104   $fh->close;
105 };
106
107 my $err = $@;
108 diag $@ if $@;
109 SKIP: {
110   skip "Couldn't adjust \$VERSION in bundled M::B for testing", 10
111     if $err;
112
113   # test the bundling in dist_dir
114   chdir $mb->dist_dir;
115
116   stdout_of( sub { Module::Build->run_perl_script('Build.PL',[],[]) } );
117   ok( -e 'MYMETA.yml', 'MYMETA was created' );
118
119   my $meta = IO::File->new('MYMETA.yml');
120   ok( $meta, "opened MYMETA.yml" );
121   ok( scalar( grep { /generated_by:.*9999/ } <$meta> ),
122     "dist_dir Build.PL loaded bundled Module::Build"
123   );
124   close $meta;
125
126   #--------------------------------------------------------------------------#
127   # test identification of dependencies
128   #--------------------------------------------------------------------------#
129
130   $dist->chdir_in;
131
132   $dist->add_file( 'mylib/Foo.pm', << 'HERE' );
133 package Foo;
134 our $VERSION = 1;
135 1;
136 HERE
137
138   $dist->add_file( 'mylib/Bar.pm', << 'HERE' );
139 package Bar;
140 use Foo;
141 our $VERSION = 42;
142 1;
143 HERE
144
145   $dist->change_file( 'Build.PL', << "HERE" );
146 use inc::latest 'Module::Build';
147 use inc::latest 'Foo';
148
149 Module::Build->new(
150   module_name => '$dist->{name}',
151   license => 'perl',
152 )->create_build_script;
153 HERE
154
155   $dist->regen( clean => 1 );
156
157   make_packlist($_,'mylib') for qw/Foo Bar/;
158
159   # get a Module::Build object and test with it
160   my $abs_mylib = File::Spec->rel2abs('mylib');
161
162
163   unshift @INC, $abs_mylib;
164   $mb = $dist->new_from_context(); # quiet by default
165   isa_ok( $mb, "Module::Build" );
166   is_deeply( [sort @{$mb->bundle_inc}], [ 'Foo', 'Module::Build' ],
167     "Module::Build and Foo are flagged for bundling"
168   );
169
170   my $output = stdout_stderr_of( sub { $mb->dispatch('distdir') } );
171
172   ok( -e File::Spec->catfile( $dist_inc, 'latest.pm' ),
173     "./inc/latest.pm created"
174   );
175
176   ok( -d File::Spec->catdir( $dist_inc, 'inc_Foo' ),
177     "dist_dir/inc/inc_Foo created"
178   );
179
180   $dist->change_file( 'Build.PL', << "HERE" );
181 use inc::latest 'Module::Build';
182 use inc::latest 'Bar';
183
184 Module::Build->new(
185   module_name => '$dist->{name}',
186   license => 'perl',
187 )->create_build_script;
188 HERE
189
190   $dist->regen( clean => 1 );
191   make_packlist($_,'mylib') for qw/Foo Bar/;
192
193   $mb = $dist->new_from_context(); # quiet by default
194   isa_ok( $mb, "Module::Build" );
195   is_deeply( [sort @{$mb->bundle_inc}], [ 'Bar', 'Module::Build' ],
196     "Module::Build and Bar are flagged for bundling"
197   );
198
199   $output = stdout_stderr_of( sub { $mb->dispatch('distdir') } );
200
201   ok( -e File::Spec->catfile( $dist_inc, 'latest.pm' ),
202     "./inc/latest.pm created"
203   );
204
205   ok( -d File::Spec->catdir( $dist_inc, 'inc_Bar' ),
206     "dist_dir/inc/inc_Bar created"
207   );
208 }
209
210
211 sub make_packlist {
212   my ($mod, $lib) = @_;
213   my $arch = $Config{archname};
214   (my $mod_path = $mod) =~ s{::}{/}g;
215   my $mod_file = File::Spec->catfile( $lib, "$mod_path\.pm" );
216   my $abs = File::Spec->rel2abs($mod_file);
217   my $packlist_path = File::Spec->catdir($lib, $arch, 'auto', $mod_path);
218   mkpath $packlist_path;
219   my $packlist = ExtUtils::Packlist->new;
220   $packlist->{$abs}++;
221   $packlist->write( File::Spec->catfile( $packlist_path, '.packlist' ));
222 }
223
224 # vim:ts=2:sw=2:et:sta:sts=2