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 ( ! MBTest::check_EUI() ) {
22 plan skip_all => 'ExtUtils::Installed takes too long on your system';
24 elsif ( Module::Build::ConfigData->feature('inc_bundling_support') ) {
27 plan skip_all => 'inc_bundling_support feature is not enabled';
30 # need to do a temp install of M::B being tested to ensure a packlist
31 # is available for bundling
33 my $current_mb = Module::Build->resume();
34 my $temp_install = MBTest->tmpdir();
35 my $arch = $Config{archname};
36 my $lib_path = File::Spec->catdir($temp_install,qw/lib perl5/);
37 my $arch_path = File::Spec->catdir( $lib_path, $arch );
38 mkpath ( $arch_path );
39 ok( -d $arch_path, "created temporary M::B pseudo-install directory");
41 unshift @INC, $lib_path, $arch_path;
42 local $ENV{PERL5LIB} = join( $Config{path_sep},
43 $lib_path, ($ENV{PERL5LIB} ? $ENV{PERL5LIB} : () )
46 # must uninst=0 so we don't try to remove an installed M::B!
47 stdout_of( sub { $current_mb->dispatch(
48 'install', install_base => $temp_install, uninst => 0
53 # create dist object in a temp directory
54 # enter the directory and generate the skeleton files
55 my $dist = DistGen->new( inc => 1 )->chdir_in->regen;
57 # get a Module::Build object and test with it
58 my $mb = $dist->new_from_context(); # quiet by default
59 isa_ok( $mb, "Module::Build" );
60 is( $mb->dist_name, "Simple", "dist_name is 'Simple'" );
61 is_deeply( $mb->bundle_inc, [ 'Module::Build' ],
62 "Module::Build is flagged for bundling"
65 # bundle stuff into distdir
66 stdout_stderr_of( sub { $mb->dispatch('distdir') } );
68 my $dist_inc = File::Spec->catdir($mb->dist_dir, 'inc');
69 ok( -e File::Spec->catfile( $dist_inc, 'latest.pm' ),
70 "dist_dir/inc/latest.pm created"
73 ok( -d File::Spec->catdir( $dist_inc, 'inc_Module-Build' ),
74 "dist_dir/inc/inc_Module_Build created"
78 File::Spec->catfile( $dist_inc, qw/inc_Module-Build Module Build.pm/ );
81 "dist_dir/inc/inc_Module_Build/Module/Build.pm created"
84 ok( -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"
88 # Force bundled M::B to a higher version so it gets loaded
89 # This has failed on Win32 for no known reason, so we'll skip if
90 # we can't edit the file.
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;};
99 $fh = IO::File->new($mb_file, ">") or die "Could not write $mb_file: $!";
100 print {$fh} $mb_code;
107 skip "Couldn't adjust \$VERSION in bundled M::B for testing", 10
110 # test the bundling in dist_dir
113 stdout_of( sub { Module::Build->run_perl_script('Build.PL',[],[]) } );
114 ok( -e 'MYMETA.yml', 'MYMETA was created' );
116 my $meta = IO::File->new('MYMETA.yml');
117 ok( $meta, "opened MYMETA.yml" );
118 ok( scalar( grep { /generated_by:.*9999/ } <$meta> ),
119 "dist_dir Build.PL loaded bundled Module::Build"
123 #--------------------------------------------------------------------------#
124 # test identification of dependencies
125 #--------------------------------------------------------------------------#
129 $dist->add_file( 'mylib/Foo.pm', << 'HERE' );
135 $dist->add_file( 'mylib/Bar.pm', << 'HERE' );
142 $dist->change_file( 'Build.PL', << "HERE" );
143 use inc::latest 'Module::Build';
144 use inc::latest 'Foo';
147 module_name => '$dist->{name}',
149 )->create_build_script;
152 $dist->regen( clean => 1 );
154 make_packlist($_,'mylib') for qw/Foo Bar/;
156 # get a Module::Build object and test with it
157 my $abs_mylib = File::Spec->rel2abs('mylib');
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"
167 my $output = stdout_stderr_of( sub { $mb->dispatch('distdir') } );
169 ok( -e File::Spec->catfile( $dist_inc, 'latest.pm' ),
170 "./inc/latest.pm created"
173 ok( -d File::Spec->catdir( $dist_inc, 'inc_Foo' ),
174 "dist_dir/inc/inc_Foo created"
177 $dist->change_file( 'Build.PL', << "HERE" );
178 use inc::latest 'Module::Build';
179 use inc::latest 'Bar';
182 module_name => '$dist->{name}',
184 )->create_build_script;
187 $dist->regen( clean => 1 );
188 make_packlist($_,'mylib') for qw/Foo Bar/;
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"
196 $output = stdout_stderr_of( sub { $mb->dispatch('distdir') } );
198 ok( -e File::Spec->catfile( $dist_inc, 'latest.pm' ),
199 "./inc/latest.pm created"
202 ok( -d File::Spec->catdir( $dist_inc, 'inc_Bar' ),
203 "dist_dir/inc/inc_Bar created"
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;
218 $packlist->write( File::Spec->catfile( $packlist_path, '.packlist' ));
221 # vim:ts=2:sw=2:et:sta:sts=2