1 # sample.t -- a sample test file for Module::Build
5 use MBTest; # or 'no_plan'
10 use ExtUtils::Packlist;
13 # Ensure any Module::Build modules are loaded from correct directory
14 blib_load('Module::Build');
15 blib_load('Module::Build::ConfigData');
17 if ( $ENV{PERL_CORE} ) {
18 plan skip_all => 'bundle_inc tests will never succeed in PERL_CORE';
20 elsif ( Module::Build::ConfigData->feature('inc_bundling_support') ) {
23 plan skip_all => 'inc_bundling_support feature is not enabled';
26 # need to do a temp install of M::B being tested to ensure a packlist
27 # is available for bundling
29 my $current_mb = Module::Build->resume();
30 my $temp_install = MBTest->tmpdir();
31 my $arch = $Config{archname};
32 my $lib_path = File::Spec->catdir($temp_install,qw/lib perl5/);
33 my $arch_path = File::Spec->catdir( $lib_path, $arch );
34 mkpath ( $arch_path );
35 ok( -d $arch_path, "created temporary M::B pseudo-install directory");
37 unshift @INC, $lib_path, $arch_path;
38 local $ENV{PERL5LIB} = join( $Config{path_sep},
39 $lib_path, $arch_path, ($ENV{PERL5LIB} ? $ENV{PERL5LIB} : () )
42 stdout_of( sub { $current_mb->dispatch('install', install_base => $temp_install) } );
44 # create dist object in a temp directory
45 # enter the directory and generate the skeleton files
46 my $dist = DistGen->new( inc => 1 )->chdir_in->regen;
48 # get a Module::Build object and test with it
49 my $mb = $dist->new_from_context(); # quiet by default
50 isa_ok( $mb, "Module::Build" );
51 is( $mb->dist_name, "Simple", "dist_name is 'Simple'" );
52 is_deeply( $mb->bundle_inc, [ 'Module::Build' ],
53 "Module::Build is flagged for bundling"
56 # see what gets bundled
57 stdout_stderr_of( sub { $mb->dispatch('distdir') } );
59 my $dist_inc = File::Spec->catdir($mb->dist_dir, 'inc');
60 ok( -e File::Spec->catfile( $dist_inc, 'latest.pm' ),
61 "./inc/latest.pm created"
64 ok( -d File::Spec->catdir( $dist_inc, 'inc_Module-Build' ),
65 "dist_dir/inc/inc_Module_Build created"
69 File::Spec->catfile( $dist_inc, qw/inc_Module-Build Module Build.pm/ );
72 "dist_dir/inc/inc_Module_Build/Module/Build.pm created"
75 ok( -e File::Spec->catfile( $dist_inc, qw/inc_Module-Build Module Build Base.pm/ ),
76 "dist_dir/inc/inc_Module_Build/Module/Build/Base.pm created"
79 # Force bundled M::B to a higher version so it gets loaded
80 # This has failed on Win32 for no known reason, so we'll skip if
81 # we can't edit the file.
85 $fh = IO::File->new($mb_file, "<") or die "Could not read $mb_file: $!";
86 my $mb_code = do { local $/; <$fh> };
87 $mb_code =~ s{\$VERSION\s+=\s+\S+}{\$VERSION = 9999;};
89 $fh = IO::File->new($mb_file, ">") or die "Could not write $mb_file: $!";
97 skip "Couldn't adjust \$VERSION in bundled M::B for testing", 10
100 # test the bundling in dist_dir
103 stdout_of( sub { Module::Build->run_perl_script('Build.PL',[],[]) } );
105 my $meta = IO::File->new('MYMETA.yml');
106 ok( $meta, "found MYMETA.yml" );
107 ok( scalar( grep { /generated_by:.*9999/ } <$meta> ),
108 "dist_dir Build.PL loaded bundled Module::Build"
111 #--------------------------------------------------------------------------#
112 # test identification of dependencies
113 #--------------------------------------------------------------------------#
117 $dist->add_file( 'mylib/Foo.pm', << 'HERE' );
123 $dist->add_file( 'mylib/Bar.pm', << 'HERE' );
130 $dist->change_file( 'Build.PL', << "HERE" );
131 use inc::latest 'Module::Build';
132 use inc::latest 'Foo';
135 module_name => '$dist->{name}',
137 )->create_build_script;
140 $dist->regen( clean => 1 );
142 make_packlist($_,'mylib') for qw/Foo Bar/;
144 # get a Module::Build object and test with it
145 my $abs_mylib = File::Spec->rel2abs('mylib');
148 unshift @INC, $abs_mylib;
149 $mb = $dist->new_from_context(); # quiet by default
150 isa_ok( $mb, "Module::Build" );
151 is_deeply( [sort @{$mb->bundle_inc}], [ 'Foo', 'Module::Build' ],
152 "Module::Build and Foo are flagged for bundling"
155 my $output = stdout_stderr_of( sub { $mb->dispatch('distdir') } );
157 ok( -e File::Spec->catfile( $dist_inc, 'latest.pm' ),
158 "./inc/latest.pm created"
161 ok( -d File::Spec->catdir( $dist_inc, 'inc_Foo' ),
162 "dist_dir/inc/inc_Foo created"
165 $dist->change_file( 'Build.PL', << "HERE" );
166 use inc::latest 'Module::Build';
167 use inc::latest 'Bar';
170 module_name => '$dist->{name}',
172 )->create_build_script;
175 $dist->regen( clean => 1 );
176 make_packlist($_,'mylib') for qw/Foo Bar/;
178 $mb = $dist->new_from_context(); # quiet by default
179 isa_ok( $mb, "Module::Build" );
180 is_deeply( [sort @{$mb->bundle_inc}], [ 'Bar', 'Module::Build' ],
181 "Module::Build and Bar are flagged for bundling"
184 $output = stdout_stderr_of( sub { $mb->dispatch('distdir') } );
186 ok( -e File::Spec->catfile( $dist_inc, 'latest.pm' ),
187 "./inc/latest.pm created"
190 ok( -d File::Spec->catdir( $dist_inc, 'inc_Bar' ),
191 "dist_dir/inc/inc_Bar created"
197 my ($mod, $lib) = @_;
198 my $arch = $Config{archname};
199 (my $mod_path = $mod) =~ s{::}{/}g;
200 my $mod_file = File::Spec->catfile( $lib, "$mod_path\.pm" );
201 my $abs = File::Spec->rel2abs($mod_file);
202 my $packlist_path = File::Spec->catdir($lib, $arch, 'auto', $mod_path);
203 mkpath $packlist_path;
204 my $packlist = ExtUtils::Packlist->new;
206 $packlist->write( File::Spec->catfile( $packlist_path, '.packlist' ));
209 # vim:ts=2:sw=2:et:sta:sts=2