5 use MBTest tests => 64;
7 blib_load('Module::Build');
9 my $tmp = MBTest->tmpdir;
12 my $dist = DistGen->new( dir => $tmp );
17 #########################
19 # Here we make sure actions are only called once per dispatch()
21 my $mb = Module::Build->subclass
23 code => "sub ACTION_loop { die 'recursed' if \$::x++; shift->depends_on('loop'); }"
24 )->new( module_name => $dist->name );
27 $mb->dispatch('loop');
30 $mb->dispatch('realclean');
32 # Make sure the subclass can be subclassed
33 my $build2class = ref($mb)->subclass
35 code => "sub ACTION_loop2 {}",
38 can_ok( $build2class, 'ACTION_loop' );
39 can_ok( $build2class, 'ACTION_loop2' );
42 { # Make sure globbing works in filenames
43 $dist->add_file( 'script', <<'---' );
45 print "Hello, World!\n";
49 $mb->test_files('*t*');
50 my $files = $mb->test_files;
51 ok grep {$_ eq 'script'} @$files;
52 my $t_basic_t = File::Spec->catfile('t', 'basic.t');
53 $t_basic_t = VMS::Filespec::vmsify($t_basic_t) if $^O eq 'VMS';
54 ok grep {$_ eq $t_basic_t} @$files;
55 ok !grep {$_ eq 'Build.PL' } @$files;
57 # Make sure order is preserved
58 $mb->test_files('foo', 'bar');
59 $files = $mb->test_files;
61 is $files->[0], 'foo';
62 is $files->[1], 'bar';
64 $dist->remove_file( 'script' );
65 $dist->regen( clean => 1 );
70 # Make sure we can add new kinds of stuff to the build sequence
72 $dist->add_file( 'test.foo', "content\n" );
75 my $mb = Module::Build->new( module_name => $dist->name,
76 foo_files => {'test.foo', 'lib/test.foo'} );
79 $mb->add_build_element('foo');
80 $mb->add_build_element('foo');
81 is_deeply $mb->build_elements, [qw(PL support pm xs share_dir pod script foo)],
82 'The foo element should be in build_elements only once';
84 $mb->dispatch('build');
85 ok -e File::Spec->catfile($mb->blib, 'lib', 'test.foo');
87 $mb->dispatch('realclean');
89 # revert distribution to a pristine state
90 $dist->remove_file( 'test.foo' );
91 $dist->regen( clean => 1 );
98 use vars qw($VERSION @ISA);
99 @ISA = qw(Module::Build);
102 # Add a new property.
103 ok(__PACKAGE__->add_property('foo'));
104 # Add a new property with a default value.
105 ok(__PACKAGE__->add_property('bar', 'hey'));
106 # Add a hash property.
107 ok(__PACKAGE__->add_property('hash', {}));
110 # Catch an exception adding an existing property.
111 eval { __PACKAGE__->add_property('module_name')};
112 like "$@", qr/already exists/;
118 use vars qw($VERSION @ISA);
119 @ISA = qw(Module::Build);
122 # Add a new property with a different default value than MBSub has.
123 ok(__PACKAGE__->add_property('bar', 'yow'));
128 ok my $mb = MBSub->new( module_name => $dist->name );
129 isa_ok $mb, 'Module::Build';
131 ok $mb->valid_property('foo');
132 can_ok $mb, 'module_name';
134 # Check foo property.
140 # Check bar property.
146 # Check hash property.
148 module_name => $dist->name,
149 hash => { foo => 'bar', bin => 'foo'}
153 isa_ok $mb->hash, 'HASH';
154 is $mb->hash->{foo}, 'bar';
155 is $mb->hash->{bin}, 'foo';
157 # Check hash property passed via the command-line.
163 ok $mb = MBSub->new( module_name => $dist->name );
167 isa_ok $mb->hash, 'HASH';
168 is $mb->hash->{foo}, 'bar';
169 is $mb->hash->{bin}, 'foo';
171 # Make sure that a different subclass with the same named property has a
173 ok $mb = MBSub2->new( module_name => $dist->name );
174 isa_ok $mb, 'Module::Build';
175 isa_ok $mb, 'MBSub2';
176 ok $mb->valid_property('bar');
182 # Test the meta_add and meta_merge stuff
183 ok my $mb = Module::Build->new(
184 module_name => $dist->name,
186 meta_add => {foo => 'bar'},
187 conflicts => {'Foo::Barxx' => 0},
189 my $data = $mb->get_metadata;
190 is $data->{foo}, 'bar';
192 $mb->meta_merge(foo => 'baz');
193 $data = $mb->get_metadata;
194 is $data->{foo}, 'baz';
196 $mb->meta_merge(conflicts => {'Foo::Fooxx' => 0});
197 $data = $mb->get_metadata;
198 is_deeply $data->{conflicts}, {'Foo::Barxx' => 0, 'Foo::Fooxx' => 0};
200 $mb->meta_add(conflicts => {'Foo::Bazxx' => 0});
201 $data = $mb->get_metadata;
202 is_deeply $data->{conflicts}, {'Foo::Bazxx' => 0, 'Foo::Fooxx' => 0};
206 # Test interactive prompting
209 local $ENV{PERL_MM_USE_DEFAULT};
212 local *{Module::Build::_readline} = sub { 'y' };
214 ok my $mb = Module::Build->new(
215 module_name => $dist->name,
219 eval{ $mb->prompt() };
220 like $@, qr/called without a prompt/, 'prompt() requires a prompt';
223 like $@, qr/called without a prompt/, 'y_n() requires a prompt';
225 eval{ $mb->y_n('Prompt?', 'invalid default') };
226 like $@, qr/Invalid default/, "y_n() requires a default of 'y' or 'n'";
229 $ENV{PERL_MM_USE_DEFAULT} = 1;
231 eval{ $mb->y_n('Is this a question?') };
232 print "\n"; # fake <enter> because the prompt prints before the checks
234 'Do not allow default-less y_n() for unattended builds';
236 eval{ $ans = $mb->prompt('Is this a question?') };
237 print "\n"; # fake <enter> because the prompt prints before the checks
239 'Do not allow default-less prompt() for unattended builds';
242 # When running Test::Smoke under a cron job, STDIN will be closed which
243 # will fool our _is_interactive() method causing various failures.
245 local *{Module::Build::_is_interactive} = sub { 1 };
247 $ENV{PERL_MM_USE_DEFAULT} = 0;
249 $ans = $mb->prompt('Is this a question?');
250 print "\n"; # fake <enter> after input
251 is $ans, 'y', "prompt() doesn't require default for interactive builds";
253 $ans = $mb->y_n('Say yes');
254 print "\n"; # fake <enter> after input
255 ok $ans, "y_n() doesn't require default for interactive build";
259 *{Module::Build::_readline} = sub { '' };
261 $ans = $mb->prompt("Is this a question");
262 is $ans, '', "default for prompt() without a default is ''";
264 $ans = $mb->prompt("Is this a question", 'y');
265 is $ans, 'y', " prompt() with a default";
267 $ans = $mb->y_n("Is this a question", 'y');
268 ok $ans, " y_n() with a default";
270 my @ans = $mb->prompt("Is this a question", undef);
271 is_deeply([@ans], [undef], " prompt() with undef() default");