Update Module::Build to 0.3603
[p5sagit/p5-mst-13.2.git] / cpan / Module-Build / t / bundle_inc.t
CommitLineData
613f422f 1# sample.t -- a sample test file for Module::Build
2
3use strict;
4use lib 't/lib';
5use MBTest; # or 'no_plan'
6use DistGen;
7use Config;
8use IO::File;
9use File::Spec;
10use ExtUtils::Packlist;
53fc1c7e 11use ExtUtils::Installed;
613f422f 12use File::Path;
13
14# Ensure any Module::Build modules are loaded from correct directory
15blib_load('Module::Build');
16blib_load('Module::Build::ConfigData');
17
7f40cf6b 18if ( $ENV{PERL_CORE} ) {
19 plan skip_all => 'bundle_inc tests will never succeed in PERL_CORE';
20}
7dc9e1b4 21elsif ( ! $ENV{MB_TEST_EXPERIMENTAL} ) {
22 plan skip_all => '$ENV{MB_TEST_EXPERIMENTAL} is not set';
23}
53fc1c7e 24elsif ( ! MBTest::check_EUI() ) {
25 plan skip_all => 'ExtUtils::Installed takes too long on your system';
26}
7f40cf6b 27elsif ( Module::Build::ConfigData->feature('inc_bundling_support') ) {
53fc1c7e 28 plan tests => 19;
613f422f 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
36my $current_mb = Module::Build->resume();
37my $temp_install = MBTest->tmpdir();
38my $arch = $Config{archname};
39my $lib_path = File::Spec->catdir($temp_install,qw/lib perl5/);
40my $arch_path = File::Spec->catdir( $lib_path, $arch );
41mkpath ( $arch_path );
42ok( -d $arch_path, "created temporary M::B pseudo-install directory");
43
44unshift @INC, $lib_path, $arch_path;
45local $ENV{PERL5LIB} = join( $Config{path_sep},
53fc1c7e 46 $lib_path, ($ENV{PERL5LIB} ? $ENV{PERL5LIB} : () )
613f422f 47);
48
53fc1c7e 49# must uninst=0 so we don't try to remove an installed M::B!
50stdout_of( sub { $current_mb->dispatch(
51 'install', install_base => $temp_install, uninst => 0
52 )
53 }
54);
613f422f 55
56# create dist object in a temp directory
57# enter the directory and generate the skeleton files
58my $dist = DistGen->new( inc => 1 )->chdir_in->regen;
59
60# get a Module::Build object and test with it
61my $mb = $dist->new_from_context(); # quiet by default
62isa_ok( $mb, "Module::Build" );
63is( $mb->dist_name, "Simple", "dist_name is 'Simple'" );
64is_deeply( $mb->bundle_inc, [ 'Module::Build' ],
65 "Module::Build is flagged for bundling"
66);
67
53fc1c7e 68# bundle stuff into distdir
613f422f 69stdout_stderr_of( sub { $mb->dispatch('distdir') } );
70
71my $dist_inc = File::Spec->catdir($mb->dist_dir, 'inc');
72ok( -e File::Spec->catfile( $dist_inc, 'latest.pm' ),
53fc1c7e 73 "dist_dir/inc/latest.pm created"
613f422f 74);
75
76ok( -d File::Spec->catdir( $dist_inc, 'inc_Module-Build' ),
77 "dist_dir/inc/inc_Module_Build created"
78);
79
80my $mb_file =
81 File::Spec->catfile( $dist_inc, qw/inc_Module-Build Module Build.pm/ );
82
83ok( -e $mb_file,
84 "dist_dir/inc/inc_Module_Build/Module/Build.pm created"
85);
86
87ok( -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
08fc25ad 92# This has failed on Win32 for no known reason, so we'll skip if
93# we can't edit the file.
94
95eval {
96 my $fh;
53fc1c7e 97 chmod 0666, $mb_file;
08fc25ad 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
107my $err = $@;
108diag $@ if $@;
109SKIP: {
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',[],[]) } );
53fc1c7e 117 ok( -e 'MYMETA.yml', 'MYMETA was created' );
08fc25ad 118
119 my $meta = IO::File->new('MYMETA.yml');
53fc1c7e 120 ok( $meta, "opened MYMETA.yml" );
08fc25ad 121 ok( scalar( grep { /generated_by:.*9999/ } <$meta> ),
122 "dist_dir Build.PL loaded bundled Module::Build"
123 );
53fc1c7e 124 close $meta;
08fc25ad 125
126 #--------------------------------------------------------------------------#
127 # test identification of dependencies
128 #--------------------------------------------------------------------------#
129
130 $dist->chdir_in;
131
132 $dist->add_file( 'mylib/Foo.pm', << 'HERE' );
613f422f 133package Foo;
134our $VERSION = 1;
1351;
136HERE
137
08fc25ad 138 $dist->add_file( 'mylib/Bar.pm', << 'HERE' );
613f422f 139package Bar;
140use Foo;
141our $VERSION = 42;
1421;
143HERE
144
08fc25ad 145 $dist->change_file( 'Build.PL', << "HERE" );
613f422f 146use inc::latest 'Module::Build';
147use inc::latest 'Foo';
148
149Module::Build->new(
150 module_name => '$dist->{name}',
151 license => 'perl',
152)->create_build_script;
153HERE
154
08fc25ad 155 $dist->regen( clean => 1 );
613f422f 156
08fc25ad 157 make_packlist($_,'mylib') for qw/Foo Bar/;
613f422f 158
08fc25ad 159 # get a Module::Build object and test with it
160 my $abs_mylib = File::Spec->rel2abs('mylib');
613f422f 161
162
08fc25ad 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 );
613f422f 169
08fc25ad 170 my $output = stdout_stderr_of( sub { $mb->dispatch('distdir') } );
613f422f 171
08fc25ad 172 ok( -e File::Spec->catfile( $dist_inc, 'latest.pm' ),
173 "./inc/latest.pm created"
174 );
613f422f 175
08fc25ad 176 ok( -d File::Spec->catdir( $dist_inc, 'inc_Foo' ),
177 "dist_dir/inc/inc_Foo created"
178 );
613f422f 179
08fc25ad 180 $dist->change_file( 'Build.PL', << "HERE" );
613f422f 181use inc::latest 'Module::Build';
182use inc::latest 'Bar';
183
184Module::Build->new(
185 module_name => '$dist->{name}',
186 license => 'perl',
187)->create_build_script;
188HERE
189
08fc25ad 190 $dist->regen( clean => 1 );
191 make_packlist($_,'mylib') for qw/Foo Bar/;
613f422f 192
08fc25ad 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 );
613f422f 198
08fc25ad 199 $output = stdout_stderr_of( sub { $mb->dispatch('distdir') } );
613f422f 200
08fc25ad 201 ok( -e File::Spec->catfile( $dist_inc, 'latest.pm' ),
202 "./inc/latest.pm created"
203 );
613f422f 204
08fc25ad 205 ok( -d File::Spec->catdir( $dist_inc, 'inc_Bar' ),
206 "dist_dir/inc/inc_Bar created"
207 );
208}
613f422f 209
210
211sub 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