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