Update Module::Load::Conditional to CPAN version 0.38
[p5sagit/p5-mst-13.2.git] / cpan / Module-Build / t / runthrough.t
1 #!/usr/bin/perl -w
2
3 use strict;
4 use lib 't/lib';
5 use MBTest tests => 29;
6
7 blib_load('Module::Build');
8 blib_load('Module::Build::ConfigData');
9 my $have_yaml = Module::Build::ConfigData->feature('YAML_support');
10
11 #########################
12
13 my $tmp = MBTest->tmpdir;
14
15 use DistGen;
16 my $dist = DistGen->new();
17 $dist->change_build_pl
18 ({
19   module_name => 'Simple',
20   scripts     => [ 'script' ],
21   license     => 'perl',
22   requires    => { 'File::Spec' => 0 },
23 });
24
25 $dist->add_file( 'MANIFEST.SKIP', <<'---' );
26 ^MYMETA.yml$
27 ---
28 $dist->add_file( 'script', <<'---' );
29 #!perl -w
30 print "Hello, World!\n";
31 ---
32 $dist->add_file( 'lib/Simple/Script.PL', <<'---' );
33 #!perl -w
34
35 my $filename = shift;
36 open FH, "> $filename" or die "Can't create $filename: $!";
37 print FH "Contents: $filename\n";
38 close FH;
39 ---
40 $dist->regen;
41
42 $dist->chdir_in;
43
44
45 #########################
46
47 my $mb = Module::Build->new_from_context;
48 ok $mb;
49 is $mb->license, 'perl';
50
51 # Make sure cleanup files added before create_build_script() get respected
52 $mb->add_to_cleanup('before_script');
53
54 eval {$mb->create_build_script};
55 is $@, '';
56 ok -e $mb->build_script;
57
58 my $dist_dir = 'Simple-0.01';
59
60 # VMS in traditional mode needs the $dist_dir name to not have a '.' in it
61 # as this is a directory delimiter.  In extended character set mode the dot
62 # is permitted for Unix format file specifications.
63 if ($^O eq 'VMS') {
64     my $Is_VMS_noefs = 1;
65     my $vms_efs = 0;
66     if (eval 'require VMS::Feature') {
67         $vms_efs = VMS::Feature::current("efs_charset");
68     } else {
69         my $efs_charset = $ENV{'DECC$EFS_CHARSET'} || '';
70         $vms_efs = $efs_charset =~ /^[ET1]/i;
71     }
72     $Is_VMS_noefs = 0 if $vms_efs;
73     if ($Is_VMS_noefs) {
74         $dist_dir = 'Simple-0_01';
75     }
76 }
77
78 is $mb->dist_dir, $dist_dir;
79
80 # The 'cleanup' file doesn't exist yet
81 ok grep {$_ eq 'before_script'} $mb->cleanup;
82
83 $mb->add_to_cleanup('save_out');
84
85 # The 'cleanup' file now exists
86 ok grep {$_ eq 'before_script'} $mb->cleanup;
87 ok grep {$_ eq 'save_out'     } $mb->cleanup;
88
89 {
90   # Make sure verbose=>1 works
91   my $all_ok = 1;
92   my $output = eval {
93     stdout_of( sub { $mb->dispatch('test', verbose => 1) } )
94   };
95   $all_ok &&= is($@, '');
96   $all_ok &&= like($output, qr/all tests successful/i);
97
98   # This is the output of lib/Simple/Script.PL
99   $all_ok &&= ok(-e $mb->localize_file_path('lib/Simple/Script'));
100
101   unless ($all_ok) {
102     # We use diag() so Test::Harness doesn't get confused.
103     diag("vvvvvvvvvvvvvvvvvvvvv Simple/t/basic.t output vvvvvvvvvvvvvvvvvvvvv");
104     diag($output);
105     diag("^^^^^^^^^^^^^^^^^^^^^ Simple/t/basic.t output ^^^^^^^^^^^^^^^^^^^^^");
106   }
107 }
108
109 SKIP: {
110   skip( 'YAML_support feature is not enabled', 7 ) unless $have_yaml;
111
112   my $output = eval {
113     stdout_of( sub { $mb->dispatch('disttest') } )
114   };
115   is $@, '';
116
117   # After a test, the distdir should contain a blib/ directory
118   ok -e File::Spec->catdir('Simple-0.01', 'blib');
119
120   eval {$mb->dispatch('distdir')};
121   is $@, '';
122
123   # The 'distdir' should contain a lib/ directory
124   ok -e File::Spec->catdir('Simple-0.01', 'lib');
125
126   # The freshly run 'distdir' should never contain a blib/ directory, or
127   # else it could get into the tarball
128   ok ! -e File::Spec->catdir('Simple-0.01', 'blib');
129
130   # Make sure all of the above was done by the new version of Module::Build
131   my $fh = IO::File->new(File::Spec->catfile($dist->dirname, 'META.yml'));
132   my $contents = do {local $/; <$fh>};
133   $contents =~ /Module::Build version ([0-9_.]+)/m;
134   cmp_ok $1, '==', $mb->VERSION, "Check version used to create META.yml: $1 == " . $mb->VERSION;
135
136   SKIP: {
137     skip( "Archive::Tar 1.08+ not installed", 1 )
138       unless eval { require Archive::Tar && Archive::Tar->VERSION(1.08); 1 };
139     $mb->add_to_cleanup($mb->dist_dir . ".tar.gz");
140     eval {$mb->dispatch('dist')};
141     is $@, '';
142   }
143
144 }
145
146 {
147   # Make sure the 'script' file was recognized as a script.
148   my $scripts = $mb->script_files;
149   ok $scripts->{script};
150
151   # Check that a shebang line is rewritten
152   my $blib_script = File::Spec->catfile( qw( blib script script ) );
153   ok -e $blib_script;
154
155  SKIP: {
156     skip("We do not rewrite shebang on VMS", 1) if $^O eq 'VMS';
157     my $fh = IO::File->new($blib_script);
158     my $first_line = <$fh>;
159     isnt $first_line, "#!perl -w\n", "should rewrite the shebang line";
160   }
161 }
162
163
164 eval {$mb->dispatch('realclean')};
165 is $@, '';
166
167 ok ! -e $mb->build_script;
168 ok ! -e $mb->config_dir;
169 ok ! -e $mb->dist_dir;
170
171 SKIP: {
172   skip( 'Windows-only test', 4 ) unless $^O =~ /^MSWin/;
173
174   my $script_data = <<'---';
175 @echo off
176 echo Hello, World!
177 ---
178
179   $dist = DistGen->new();
180   $dist->change_build_pl({
181                           module_name => 'Simple',
182                           scripts     => [ 'bin/script.bat' ],
183                           license     => 'perl',
184                          });
185
186   $dist->add_file( 'bin/script.bat', $script_data );
187
188   $dist->regen;
189   $dist->chdir_in;
190
191   $mb = Module::Build->new_from_context;
192   ok $mb;
193
194   eval{ $mb->dispatch('build') };
195   is $@, '';
196
197   my $script_file = File::Spec->catfile( qw(blib script), 'script.bat' );
198   ok -f $script_file, "Native batch file copied to 'scripts'";
199
200   my $out = slurp( $script_file );
201   is $out, $script_data, '  unmodified by pl2bat';
202
203 }
204