1 # sample.t -- a sample test file for Module::Build
5 use MBTest; # or 'no_plan'
10 use ExtUtils::Packlist;
11 use ExtUtils::Installed;
14 # Ensure any Module::Build modules are loaded from correct directory
15 blib_load('Module::Build');
16 blib_load('Module::Build::ConfigData');
18 if ( $ENV{PERL_CORE} ) {
19 plan skip_all => 'bundle_inc tests will never succeed in PERL_CORE';
21 elsif ( ! $ENV{MB_TEST_EXPERIMENTAL} ) {
22 plan skip_all => '$ENV{MB_TEST_EXPERIMENTAL} is not set';
24 elsif ( ! MBTest::check_EUI() ) {
25 plan skip_all => 'ExtUtils::Installed takes too long on your system';
27 elsif ( Module::Build::ConfigData->feature('inc_bundling_support') ) {
30 plan skip_all => 'inc_bundling_support feature is not enabled';
33 # need to do a temp install of M::B being tested to ensure a packlist
34 # is available for bundling
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");
44 unshift @INC, $lib_path, $arch_path;
45 local $ENV{PERL5LIB} = join( $Config{path_sep},
46 $lib_path, ($ENV{PERL5LIB} ? $ENV{PERL5LIB} : () )
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
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;
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"
68 # bundle stuff into distdir
69 stdout_stderr_of( sub { $mb->dispatch('distdir') } );
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"
76 ok( -d File::Spec->catdir( $dist_inc, 'inc_Module-Build' ),
77 "dist_dir/inc/inc_Module_Build created"
81 File::Spec->catfile( $dist_inc, qw/inc_Module-Build Module Build.pm/ );
84 "dist_dir/inc/inc_Module_Build/Module/Build.pm created"
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"
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.
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;};
102 $fh = IO::File->new($mb_file, ">") or die "Could not write $mb_file: $!";
103 print {$fh} $mb_code;
110 skip "Couldn't adjust \$VERSION in bundled M::B for testing", 10
113 # test the bundling in dist_dir
116 stdout_of( sub { Module::Build->run_perl_script('Build.PL',[],[]) } );
117 ok( -e 'MYMETA.yml', 'MYMETA was created' );
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"
126 #--------------------------------------------------------------------------#
127 # test identification of dependencies
128 #--------------------------------------------------------------------------#
132 $dist->add_file( 'mylib/Foo.pm', << 'HERE' );
138 $dist->add_file( 'mylib/Bar.pm', << 'HERE' );
145 $dist->change_file( 'Build.PL', << "HERE" );
146 use inc::latest 'Module::Build';
147 use inc::latest 'Foo';
150 module_name => '$dist->{name}',
152 )->create_build_script;
155 $dist->regen( clean => 1 );
157 make_packlist($_,'mylib') for qw/Foo Bar/;
159 # get a Module::Build object and test with it
160 my $abs_mylib = File::Spec->rel2abs('mylib');
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"
170 my $output = stdout_stderr_of( sub { $mb->dispatch('distdir') } );
172 ok( -e File::Spec->catfile( $dist_inc, 'latest.pm' ),
173 "./inc/latest.pm created"
176 ok( -d File::Spec->catdir( $dist_inc, 'inc_Foo' ),
177 "dist_dir/inc/inc_Foo created"
180 $dist->change_file( 'Build.PL', << "HERE" );
181 use inc::latest 'Module::Build';
182 use inc::latest 'Bar';
185 module_name => '$dist->{name}',
187 )->create_build_script;
190 $dist->regen( clean => 1 );
191 make_packlist($_,'mylib') for qw/Foo Bar/;
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"
199 $output = stdout_stderr_of( sub { $mb->dispatch('distdir') } );
201 ok( -e File::Spec->catfile( $dist_inc, 'latest.pm' ),
202 "./inc/latest.pm created"
205 ok( -d File::Spec->catdir( $dist_inc, 'inc_Bar' ),
206 "dist_dir/inc/inc_Bar created"
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;
221 $packlist->write( File::Spec->catfile( $packlist_path, '.packlist' ));
224 # vim:ts=2:sw=2:et:sta:sts=2