Update Module::Load::Conditional to CPAN version 0.38
[p5sagit/p5-mst-13.2.git] / cpan / Module-Build / t / basic.t
1 #!/usr/bin/perl -w
2
3 use strict;
4 use lib 't/lib';
5 use MBTest tests => 58;
6
7 blib_load('Module::Build');
8
9 my $tmp = MBTest->tmpdir;
10
11 use DistGen;
12 my $dist = DistGen->new( dir => $tmp );
13 $dist->regen;
14
15 $dist->chdir_in;
16
17 #########################
18
19
20 # Test object creation
21 {
22   my $mb = Module::Build->new( module_name => $dist->name );
23   ok $mb;
24   is $mb->module_name, $dist->name;
25   is $mb->build_class, 'Module::Build';
26   is $mb->dist_name, $dist->name;
27
28   $mb = Module::Build->new( dist_name => $dist->name, dist_version => 7 );
29   ok $mb;
30   ok $mb->module_name;  # Set via heuristics
31   is $mb->dist_name, $dist->name;
32 }
33
34 # Make sure actions are defined, and known_actions works as class method
35 {
36   my %actions = map {$_, 1} Module::Build->known_actions;
37   ok $actions{clean};
38   ok $actions{distdir};
39 }
40
41 # Test prerequisite checking
42 {
43   local @INC = (File::Spec->catdir( $dist->dirname, 'lib' ), @INC);
44   my $flagged = 0;
45   local $SIG{__WARN__} = sub { $flagged = 1 if $_[0] =~ /@{[$dist->name]}/};
46   my $mb = Module::Build->new(
47     module_name => $dist->name,
48     requires    => {$dist->name => 0},
49   );
50   ok ! $flagged;
51   ok ! $mb->prereq_failures;
52   $mb->dispatch('realclean');
53   $dist->clean;
54
55   $flagged = 0;
56   $mb = Module::Build->new(
57     module_name => $dist->name,
58     requires    => {$dist->name => 3.14159265},
59   );
60   ok $flagged;
61   ok $mb->prereq_failures;
62   ok $mb->prereq_failures->{requires}{$dist->name};
63   is $mb->prereq_failures->{requires}{$dist->name}{have}, 0.01;
64   is $mb->prereq_failures->{requires}{$dist->name}{need}, 3.14159265;
65
66   $mb->dispatch('realclean');
67   $dist->clean;
68
69   # Make sure check_installed_status() works as a class method
70   my $info = Module::Build->check_installed_status('File::Spec', 0);
71   ok $info->{ok};
72   is $info->{have}, $File::Spec::VERSION;
73
74   # Make sure check_installed_status() works with an advanced spec
75   $info = Module::Build->check_installed_status('File::Spec', '> 0');
76   ok $info->{ok};
77
78   # Use 2 lines for this, to avoid a "used only once" warning
79   local $Foo::Module::VERSION;
80   $Foo::Module::VERSION = '1.01_02';
81
82   $info = Module::Build->check_installed_status('Foo::Module', '1.01_02');
83   ok $info->{ok} or diag($info->{message});
84 }
85
86 {
87   # Make sure the correct warning message is generated when an
88   # optional prereq isn't installed
89   my $flagged = 0;
90   local $SIG{__WARN__} = sub { $flagged = 1 if $_[0] =~ /ModuleBuildNonExistent is not installed/};
91
92   my $mb = Module::Build->new(
93     module_name => $dist->name,
94     recommends  => {ModuleBuildNonExistent => 3},
95   );
96   ok $flagged;
97   $dist->clean;
98 }
99
100 # Test verbosity
101 {
102   my $mb = Module::Build->new(module_name => $dist->name);
103
104   $mb->add_to_cleanup('save_out');
105   # Use uc() so we don't confuse the current test output
106   like uc(stdout_of( sub {$mb->dispatch('test', verbose => 1)} )), qr/^OK \d/m;
107   like uc(stdout_of( sub {$mb->dispatch('test', verbose => 0)} )), qr/\.\. ?OK/;
108
109   $mb->dispatch('realclean');
110   $dist->clean;
111 }
112
113 # Make sure 'config' entries are respected on the command line, and that
114 # Getopt::Long specs work as expected.
115 {
116   use Config;
117   $dist->change_build_pl
118     ({
119       module_name => @{[$dist->name]},
120       license     => 'perl',
121       get_options => { foo => {},
122                        bar => { type    => '+'  },
123                        bat => { type    => '=s' },
124                        dee => { type    => '=s',
125                                 default => 'goo'
126                               },
127                      }
128      });
129
130   $dist->regen;
131   eval {Module::Build->run_perl_script('Build.PL', [], ['--nouse-rcfile', '--config', "foocakes=barcakes", '--foo', '--bar', '--bar', '-bat=hello', 'gee=whiz', '--any', 'hey', '--destdir', 'yo', '--verbose', '1'])};
132   is $@, '';
133
134   my $mb = Module::Build->resume;
135   ok $mb->valid_property('config');
136
137   is $mb->config('cc'), $Config{cc};
138   is $mb->config('foocakes'), 'barcakes';
139
140   # Test args().
141   is $mb->args('foo'), 1;
142   is $mb->args('bar'), 2, 'bar';
143   is $mb->args('bat'), 'hello', 'bat';
144   is $mb->args('gee'), 'whiz';
145   is $mb->args('any'), 'hey';
146   is $mb->args('dee'), 'goo';
147   is $mb->destdir, 'yo';
148   my %runtime = $mb->runtime_params;
149   is_deeply \%runtime,
150     {
151      verbose => 1,
152      destdir => 'yo',
153      use_rcfile => 0,
154      config => { foocakes => 'barcakes' },
155     };
156
157   ok my $argsref = $mb->args;
158   is $argsref->{foo}, 1;
159   $argsref->{doo} = 'hee';
160   is $mb->args('doo'), 'hee';
161   ok my %args = $mb->args;
162   is $args{foo}, 1;
163
164   # revert test distribution to pristine state because we modified a file
165   $dist->regen( clean => 1 );
166 }
167
168 # Test author stuff
169 {
170   my $mb = Module::Build->new(
171     module_name => $dist->name,
172     dist_author => 'Foo Meister <foo@example.com>',
173     build_class => 'My::Big::Fat::Builder',
174   );
175   ok $mb;
176   ok ref($mb->dist_author), 'dist_author converted to array if simple string';
177   is $mb->dist_author->[0], 'Foo Meister <foo@example.com>';
178   is $mb->build_class, 'My::Big::Fat::Builder';
179 }
180
181 # Test conversion of shell strings
182 {
183   my $mb = Module::Build->new(
184     module_name => $dist->name,
185     dist_author => 'Foo Meister <foo@example.com>',
186     extra_compiler_flags => '-I/foo -I/bar',
187     extra_linker_flags => '-L/foo -L/bar',
188   );
189   ok $mb;
190   is_deeply $mb->extra_compiler_flags, ['-I/foo', '-I/bar'], "Should split shell string into list";
191   is_deeply $mb->extra_linker_flags,   ['-L/foo', '-L/bar'], "Should split shell string into list";
192
193   # Try again with command-line args
194   eval {Module::Build->run_perl_script('Build.PL', [], ['--extra_compiler_flags', '-I/foo -I/bar',
195                                                         '--extra_linker_flags', '-L/foo -L/bar'])};
196   $mb = Module::Build->resume;
197   ok $mb;
198   is_deeply $mb->extra_compiler_flags, ['-I/foo', '-I/bar'], "Should split shell string into list";
199   is_deeply $mb->extra_linker_flags,   ['-L/foo', '-L/bar'], "Should split shell string into list";
200 }
201
202 # Test include_dirs.
203 {
204   ok my $mb = Module::Build->new(
205     module_name => $dist->name,
206     include_dirs => [qw(/foo /bar)],
207   );
208   is_deeply $mb->include_dirs, ['/foo', '/bar'], 'Should have include dirs';
209
210   # Try a string.
211   ok $mb = Module::Build->new(
212     module_name => $dist->name,
213     include_dirs => '/foo',
214   );
215   is_deeply $mb->include_dirs, ['/foo'], 'Should have string include dir';
216
217   # Try again with command-line args
218   eval { Module::Build->run_perl_script(
219       'Build.PL', [],
220       ['--include_dirs', '/foo', '--include_dirs', '/bar' ],
221   ) };
222
223   ok $mb = Module::Build->resume;
224   is_deeply $mb->include_dirs, ['/foo', '/bar'], 'Should have include dirs';
225
226   eval { Module::Build->run_perl_script(
227       'Build.PL', [],
228       ['--include_dirs', '/foo' ],
229   ) };
230
231   ok $mb = Module::Build->resume;
232   is_deeply $mb->include_dirs, ['/foo'], 'Should have single include dir';
233 }
234