4 use lib $ENV{PERL_CORE} ? '../lib/Module/Build/t/lib' : 't/lib';
5 use MBTest tests => 65;
9 my $tmp = MBTest->tmpdir;
12 my $dist = DistGen->new( dir => $tmp );
15 chdir( $dist->dirname ) or die "Can't chdir to '@{[$dist->dirname]}': $!";
17 #########################
22 # Here we make sure actions are only called once per dispatch()
24 my $mb = Module::Build->subclass
26 code => "sub ACTION_loop { die 'recursed' if \$::x++; shift->depends_on('loop'); }"
27 )->new( module_name => $dist->name );
30 $mb->dispatch('loop');
33 $mb->dispatch('realclean');
35 # Make sure the subclass can be subclassed
36 my $build2class = ref($mb)->subclass
38 code => "sub ACTION_loop2 {}",
41 can_ok( $build2class, 'ACTION_loop' );
42 can_ok( $build2class, 'ACTION_loop2' );
45 { # Make sure globbing works in filenames
46 $dist->add_file( 'script', <<'---' );
48 print "Hello, World!\n";
52 $mb->test_files('*t*');
53 my $files = $mb->test_files;
54 ok grep {$_ eq 'script'} @$files;
55 ok grep {$_ eq File::Spec->catfile('t', 'basic.t')} @$files;
56 ok !grep {$_ eq 'Build.PL' } @$files;
58 # Make sure order is preserved
59 $mb->test_files('foo', 'bar');
60 $files = $mb->test_files;
62 is $files->[0], 'foo';
63 is $files->[1], 'bar';
65 $dist->remove_file( 'script' );
66 $dist->regen( clean => 1 );
71 # Make sure we can add new kinds of stuff to the build sequence
73 $dist->add_file( 'test.foo', "content\n" );
76 my $mb = Module::Build->new( module_name => $dist->name,
77 foo_files => {'test.foo', 'lib/test.foo'} );
80 $mb->add_build_element('foo');
81 $mb->add_build_element('foo');
82 is_deeply $mb->build_elements, [qw(PL support pm xs pod script foo)],
83 'The foo element should be in build_elements only once';
85 $mb->dispatch('build');
86 ok -e File::Spec->catfile($mb->blib, 'lib', 'test.foo');
88 $mb->dispatch('realclean');
90 # revert distribution to a pristine state
91 $dist->remove_file( 'test.foo' );
92 $dist->regen( clean => 1 );
99 use vars qw($VERSION @ISA);
100 @ISA = qw(Module::Build);
103 # Add a new property.
104 ok(__PACKAGE__->add_property('foo'));
105 # Add a new property with a default value.
106 ok(__PACKAGE__->add_property('bar', 'hey'));
107 # Add a hash property.
108 ok(__PACKAGE__->add_property('hash', {}));
111 # Catch an exception adding an existing property.
112 eval { __PACKAGE__->add_property('module_name')};
113 like "$@", qr/already exists/;
119 use vars qw($VERSION @ISA);
120 @ISA = qw(Module::Build);
123 # Add a new property with a different default value than MBSub has.
124 ok(__PACKAGE__->add_property('bar', 'yow'));
129 ok my $mb = MBSub->new( module_name => $dist->name );
130 isa_ok $mb, 'Module::Build';
132 ok $mb->valid_property('foo');
133 can_ok $mb, 'module_name';
135 # Check foo property.
141 # Check bar property.
147 # Check hash property.
149 module_name => $dist->name,
150 hash => { foo => 'bar', bin => 'foo'}
154 isa_ok $mb->hash, 'HASH';
155 is $mb->hash->{foo}, 'bar';
156 is $mb->hash->{bin}, 'foo';
158 # Check hash property passed via the command-line.
164 ok $mb = MBSub->new( module_name => $dist->name );
168 isa_ok $mb->hash, 'HASH';
169 is $mb->hash->{foo}, 'bar';
170 is $mb->hash->{bin}, 'foo';
172 # Make sure that a different subclass with the same named property has a
174 ok $mb = MBSub2->new( module_name => $dist->name );
175 isa_ok $mb, 'Module::Build';
176 isa_ok $mb, 'MBSub2';
177 ok $mb->valid_property('bar');
183 # Test the meta_add and meta_merge stuff
184 ok my $mb = Module::Build->new(
185 module_name => $dist->name,
187 meta_add => {foo => 'bar'},
188 conflicts => {'Foo::Barxx' => 0},
191 $mb->prepare_metadata( \%data );
192 is $data{foo}, 'bar';
194 $mb->meta_merge(foo => 'baz');
195 $mb->prepare_metadata( \%data );
196 is $data{foo}, 'baz';
198 $mb->meta_merge(conflicts => {'Foo::Fooxx' => 0});
199 $mb->prepare_metadata( \%data );
200 is_deeply $data{conflicts}, {'Foo::Barxx' => 0, 'Foo::Fooxx' => 0};
202 $mb->meta_add(conflicts => {'Foo::Bazxx' => 0});
203 $mb->prepare_metadata( \%data );
204 is_deeply $data{conflicts}, {'Foo::Bazxx' => 0, 'Foo::Fooxx' => 0};
208 # Test interactive prompting
211 local $ENV{PERL_MM_USE_DEFAULT};
214 local *{Module::Build::_readline} = sub { 'y' };
216 ok my $mb = Module::Build->new(
217 module_name => $dist->name,
221 eval{ $mb->prompt() };
222 like $@, qr/called without a prompt/, 'prompt() requires a prompt';
225 like $@, qr/called without a prompt/, 'y_n() requires a prompt';
227 eval{ $mb->y_n('Prompt?', 'invalid default') };
228 like $@, qr/Invalid default/, "y_n() requires a default of 'y' or 'n'";
231 $ENV{PERL_MM_USE_DEFAULT} = 1;
233 eval{ $mb->y_n('Is this a question?') };
234 print "\n"; # fake <enter> because the prompt prints before the checks
236 'Do not allow default-less y_n() for unattended builds';
238 eval{ $ans = $mb->prompt('Is this a question?') };
239 print "\n"; # fake <enter> because the prompt prints before the checks
241 'Do not allow default-less prompt() for unattended builds';
244 # When running Test::Smoke under a cron job, STDIN will be closed which
245 # will fool our _is_interactive() method causing various failures.
247 local *{Module::Build::_is_interactive} = sub { 1 };
249 $ENV{PERL_MM_USE_DEFAULT} = 0;
251 $ans = $mb->prompt('Is this a question?');
252 print "\n"; # fake <enter> after input
253 is $ans, 'y', "prompt() doesn't require default for interactive builds";
255 $ans = $mb->y_n('Say yes');
256 print "\n"; # fake <enter> after input
257 ok $ans, "y_n() doesn't require default for interactive build";
261 *{Module::Build::_readline} = sub { '' };
263 $ans = $mb->prompt("Is this a question");
264 is $ans, '', "default for prompt() without a default is ''";
266 $ans = $mb->prompt("Is this a question", 'y');
267 is $ans, 'y', " prompt() with a default";
269 $ans = $mb->y_n("Is this a question", 'y');
270 ok $ans, " y_n() with a default";
272 my @ans = $mb->prompt("Is this a question", undef);
273 is_deeply([@ans], [undef], " prompt() with undef() default");
279 chdir( $cwd ) or die "Can''t chdir to '$cwd': $!";