Add ExtUtils::Miniperl to the list of core modules for all versions >= 5.00504
[p5sagit/p5-mst-13.2.git] / lib / Module / Build / t / runthrough.t
1 #!/usr/bin/perl -w
2
3 use strict;
4 use lib $ENV{PERL_CORE} ? '../lib/Module/Build/t/lib' : 't/lib';
5 use MBTest tests => 32;
6
7 use_ok 'Module::Build';
8 ensure_blib('Module::Build');
9
10 use Module::Build::ConfigData;
11 my $have_yaml = Module::Build::ConfigData->feature('YAML_support');
12
13 #########################
14
15 my $tmp = MBTest->tmpdir;
16
17 use DistGen;
18 my $dist = DistGen->new( dir => $tmp );
19 $dist->remove_file( 't/basic.t' );
20 $dist->change_build_pl
21 ({
22   module_name => 'Simple',
23   scripts     => [ 'script' ],
24   license     => 'perl',
25   requires    => { 'File::Spec' => 0 },
26 });
27
28 $dist->add_file( 'script', <<'---' );
29 #!perl -w
30 print "Hello, World!\n";
31 ---
32 $dist->add_file( 'test.pl', <<'---' );
33 #!/usr/bin/perl
34
35 use Test;
36 plan tests => 2;
37
38 ok 1;
39
40 require Module::Build;
41 skip $ENV{PERL_CORE} && "no blib in core",
42   $INC{'Module/Build.pm'}, qr/blib/, 'Module::Build should be loaded from blib';
43
44 print "# Cwd: ", Module::Build->cwd, "\n";
45 print "# \@INC: (@INC)\n";
46 print "Done.\n";  # t/compat.t looks for this
47 ---
48 $dist->add_file( 'lib/Simple/Script.PL', <<'---' );
49 #!perl -w
50
51 my $filename = shift;
52 open FH, "> $filename" or die "Can't create $filename: $!";
53 print FH "Contents: $filename\n";
54 close FH;
55 ---
56 $dist->regen;
57
58 $dist->chdir_in;
59
60
61 #########################
62
63 my $mb = Module::Build->new_from_context;
64 ok $mb;
65 is $mb->license, 'perl';
66
67 # Make sure cleanup files added before create_build_script() get respected
68 $mb->add_to_cleanup('before_script');
69
70 eval {$mb->create_build_script};
71 is $@, '';
72 ok -e $mb->build_script;
73
74 my $dist_dir = 'Simple-0.01';
75
76 # VMS may or may not need to modify the name, vmsify will do this if
77 # the name looks like a UNIX directory.
78 if ($^O eq 'VMS') {
79    my @dist_dirs = File::Spec->splitdir(VMS::Filespec::vmsify($dist_dir.'/'));
80    $dist_dir = $dist_dirs[0];
81 }
82
83 is $mb->dist_dir, $dist_dir;
84
85 # The 'cleanup' file doesn't exist yet
86 ok grep {$_ eq 'before_script'} $mb->cleanup;
87
88 $mb->add_to_cleanup('save_out');
89
90 # The 'cleanup' file now exists
91 ok grep {$_ eq 'before_script'} $mb->cleanup;
92 ok grep {$_ eq 'save_out'     } $mb->cleanup;
93
94 {
95   # Make sure verbose=>1 works
96   my $all_ok = 1;
97   my $output = eval {
98     stdout_of( sub { $mb->dispatch('test', verbose => 1) } )
99   };
100   $all_ok &&= is($@, '');
101   $all_ok &&= like($output, qr/all tests successful/i);
102   
103   # This is the output of lib/Simple/Script.PL
104   $all_ok &&= ok(-e $mb->localize_file_path('lib/Simple/Script'));
105
106   unless ($all_ok) {
107     # We use diag() so Test::Harness doesn't get confused.
108     diag("vvvvvvvvvvvvvvvvvvvvv Simple/test.pl output vvvvvvvvvvvvvvvvvvvvv");
109     diag($output);
110     diag("^^^^^^^^^^^^^^^^^^^^^ Simple/test.pl output ^^^^^^^^^^^^^^^^^^^^^");
111   }
112 }
113
114 SKIP: {
115   skip( 'YAML_support feature is not enabled', 7 ) unless $have_yaml;
116
117   my $output = eval {
118     stdout_of( sub { $mb->dispatch('disttest') } )
119   };
120   is $@, '';
121   
122   # After a test, the distdir should contain a blib/ directory
123   ok -e File::Spec->catdir('Simple-0.01', 'blib');
124   
125   eval {$mb->dispatch('distdir')};
126   is $@, '';
127   
128   # The 'distdir' should contain a lib/ directory
129   ok -e File::Spec->catdir('Simple-0.01', 'lib');
130   
131   # The freshly run 'distdir' should never contain a blib/ directory, or
132   # else it could get into the tarball
133   ok ! -e File::Spec->catdir('Simple-0.01', 'blib');
134
135   # Make sure all of the above was done by the new version of Module::Build
136   my $fh = IO::File->new(File::Spec->catfile($dist->dirname, 'META.yml'));
137   my $contents = do {local $/; <$fh>};
138   $contents =~ /Module::Build version ([0-9_.]+)/m;
139   cmp_ok $1, '==', $mb->VERSION, "Check version used to create META.yml: $1 == " . $mb->VERSION;
140
141   SKIP: {
142     skip( "not sure if we can create a tarball on this platform", 1 )
143       unless $mb->check_installed_version('Archive::Tar', 0) ||
144              $mb->isa('Module::Build::Platform::Unix');
145
146     $mb->add_to_cleanup($mb->dist_dir . ".tar.gz");
147     eval {$mb->dispatch('dist')};
148     is $@, '';
149   }
150
151 }
152
153 {
154   # Make sure the 'script' file was recognized as a script.
155   my $scripts = $mb->script_files;
156   ok $scripts->{script};
157   
158   # Check that a shebang line is rewritten
159   my $blib_script = File::Spec->catfile( qw( blib script script ) );
160   ok -e $blib_script;
161   
162  SKIP: {
163     skip("We do not rewrite shebang on VMS", 1) if $^O eq 'VMS';
164     my $fh = IO::File->new($blib_script);
165     my $first_line = <$fh>;
166     isnt $first_line, "#!perl -w\n", "should rewrite the shebang line";
167   }
168 }
169
170 {
171   # Check PPD
172   $mb->dispatch('ppd', args => {codebase => '/path/to/codebase'});
173
174   my $ppd = slurp('Simple.ppd');
175
176   # This test is quite a hack since with XML you don't really want to
177   # do a strict string comparison, but absent an XML parser it's the
178   # best we can do.
179   is $ppd, <<'EOF';
180 <SOFTPKG NAME="Simple" VERSION="0,01,0,0">
181     <TITLE>Simple</TITLE>
182     <ABSTRACT>Perl extension for blah blah blah</ABSTRACT>
183     <AUTHOR>A. U. Thor, a.u.thor@a.galaxy.far.far.away</AUTHOR>
184     <IMPLEMENTATION>
185         <DEPENDENCY NAME="File-Spec" VERSION="0,0,0,0" />
186         <CODEBASE HREF="/path/to/codebase" />
187     </IMPLEMENTATION>
188 </SOFTPKG>
189 EOF
190 }
191
192
193 eval {$mb->dispatch('realclean')};
194 is $@, '';
195
196 ok ! -e $mb->build_script;
197 ok ! -e $mb->config_dir;
198 ok ! -e $mb->dist_dir;
199
200 $dist->remove;
201
202 SKIP: {
203   skip( 'Windows-only test', 4 ) unless $^O =~ /^MSWin/;
204
205   my $script_data = <<'---';
206 @echo off
207 echo Hello, World!
208 ---
209
210   $dist = DistGen->new( dir => $tmp );
211   $dist->change_build_pl({
212                           module_name => 'Simple',
213                           scripts     => [ 'bin/script.bat' ],
214                           license     => 'perl',
215                          });
216
217   $dist->add_file( 'bin/script.bat', $script_data );
218
219   $dist->regen;
220   $dist->chdir_in;
221
222   $mb = Module::Build->new_from_context;
223   ok $mb;
224
225   eval{ $mb->dispatch('build') };
226   is $@, '';
227
228   my $script_file = File::Spec->catfile( qw(blib script), 'script.bat' );
229   ok -f $script_file, "Native batch file copied to 'scripts'";
230
231   my $out = slurp( $script_file );
232   is $out, $script_data, '  unmodified by pl2bat';
233
234   $dist->remove;
235 }
236
237 # cleanup
238 $dist->remove;