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