2 # Copyright (C) 2009, Parrot Foundation.
4 # init/hints/darwin-01.t
9 use File::Temp qw( tempdir );
11 plan( skip_all => 'only needs testing on Darwin' ) unless $^O =~ /darwin/i;
14 use lib qw( lib t/configure/testlib );
15 use_ok('config::init::hints');
16 use_ok('config::init::hints::darwin');
17 use Parrot::Configure::Options qw( process_options );
18 use Parrot::Configure::Step::Test;
19 use Parrot::Configure::Test qw(
20 test_step_constructor_and_description
22 use IO::CaptureOutput qw | capture |;
25 my ($args, $step_list_ref) = process_options(
32 my $conf = Parrot::Configure::Step::Test->new;
33 $conf->include_config_results( $args );
35 ##### Tests of some internal subroutines #####
37 ##### _precheck() #####
39 my $problematic_flag = 'ccflags';
40 my $stored = $conf->data->get($problematic_flag);
43 $conf->options->set( verbose => undef );
44 my ($stdout, $stderr);
46 sub { init::hints::darwin::_precheck(
47 $conf, $problematic_flag, $stored
52 ok( ! $stdout, "_precheck(): Non-verbose mode produced no output" );
56 $conf->options->set( verbose => 1 );
57 my ($stdout, $stderr);
59 sub { init::hints::darwin::_precheck(
60 $conf, $problematic_flag, $stored
65 ok( $stdout, "_precheck(): Verbose mode produced output" );
66 like($stdout, qr/Checking\s+$problematic_flag/,
67 "_precheck(): Got expected verbose output" );
68 like($stdout, qr/Pre-check:\s+$stored/,
69 "_precheck(): Got expected verbose output" );
73 $conf->options->set( verbose => 1 );
74 my ($stdout, $stderr);
76 sub { init::hints::darwin::_precheck(
77 $conf, $problematic_flag, q{}
82 ok( $stdout, "_precheck(): Verbose mode produced output" );
83 like($stdout, qr/Checking\s+$problematic_flag/,
84 "_precheck(): Got expected verbose output" );
85 like($stdout, qr/Pre-check:\s+\(nil\)/,
86 "_precheck(): Got expected verbose output" );
87 $conf->options->set( verbose => undef );
90 ##### _strip_arch_flags_engine #####
94 architectures => [ qw( i386 ppc64 ppc x86_64 ) ],
97 my $stored = q{-someflag -arch i386 -someotherflag -arch ppc};
98 my $flag = q{ccflags};
99 $flagsref = init::hints::darwin::_strip_arch_flags_engine(
100 $defaults{architectures}, $stored, $flagsref, $flag
104 qr{-someflag -someotherflag},
105 "_strip_arch_flags_engine(): '-arch' flags and extra whitespace removed",
111 ##### _postcheck #####
114 $conf->options->set( verbose => undef );
115 my $flag = 'ccflags';
116 my $flagsref = { 'ccflags' => 'my ccflag' };
117 my ($stdout, $stderr);
120 sub { init::hints::darwin::_postcheck(
121 $conf, $flagsref, $flag
126 ok( ! $stdout, "_postcheck(): Non-verbose mode produced no output" );
128 $conf->options->set( verbose => 1 );
130 sub { init::hints::darwin::_postcheck(
131 $conf, $flagsref, $flag
136 ok( $stdout, "_postcheck(): Verbose mode produced output" );
137 like($stdout, qr/Post-check:\s+$flagsref->{$flag}/,
138 "_postcheck(): Got expected verbose output" );
140 $conf->options->set( verbose => 1 );
141 $flagsref = { 'ccflags' => undef };
143 sub { init::hints::darwin::_postcheck(
144 $conf, $flagsref, $flag
149 ok( $stdout, "_postcheck(): Verbose mode produced output" );
150 like($stdout, qr/Post-check:\s+\(nil\)/,
151 "_postcheck(): Got expected verbose output" );
154 ##### _strip_arch_flags #####
158 problem_flags => [ qw( ccflags ldflags ) ],
159 architectures => [ qw( i386 ppc64 ppc x86_64 ) ],
162 my $flag = q{ccflags};
163 my $oldflag = $conf->data->get( $flag );
165 my $stored = q{-someflag -arch i386 -someotherflag -arch ppc};
166 $conf->data->set( $flag => $stored );
168 $flagsref = init::hints::darwin::_strip_arch_flags($conf, 0);
169 like($flagsref->{$flag},
170 qr/-someflag -someotherflag/,
171 "_strip_arch_flags(): '-arch' flags and extra whitespace removed",
174 $conf->options->set( verbose => 1 );
175 my ($stdout, $stderr);
178 $flagsref = init::hints::darwin::_strip_arch_flags($conf);
183 like($flagsref->{$flag},
184 qr/-someflag -someotherflag/,
185 "_strip_arch_flags(): '-arch' flags and extra whitespace removed",
189 qr/Stripping -arch flags due to Apple multi-architecture build problems:/,
190 "_strip_arch_flags(): Got expected verbose output",
194 $conf->data->set( $flag => undef );
195 $flagsref = init::hints::darwin::_strip_arch_flags($conf, 0);
196 is( $flagsref->{$flag}, q{},
197 "_strip_arch_flags(): Got empty flag when expected" );
199 $conf->data->set( $flag => $oldflag );
202 ##### _strip_ldl_as_needed #####
205 my $oldflag = $conf->data->get( 'libs ' );
206 my ( $major, $libs );
209 local $init::hints::darwin::defaults{uname} = $major;
210 $conf->data->set( libs => '-somelib -ldl -someotherlib' );
211 $libs = init::hints::darwin::_strip_ldl_as_needed(
212 $conf->data->get( 'libs' )
214 like( $libs, qr/-somelib\s+-someotherlib/,
215 "_strip_ldl_as_needed(): '-ldl' stripped as expected" );
218 local $init::hints::darwin::defaults{uname} = $major;
219 $conf->data->set( libs => '-somelib -ldl -someotherlib' );
220 $libs = init::hints::darwin::_strip_ldl_as_needed(
221 $conf->data->get( 'libs' )
223 like( $libs, qr/-somelib\s+-ldl\s+-someotherlib/,
224 "_strip_ldl_as_needed(): '-ldl' not stripped as expected in older darwin" );
226 $conf->data->set( libs => $oldflag );
229 ##### _set_deployment_environment() #####
232 my $predicted = '10.99';
233 local $ENV{'MACOSX_DEPLOYMENT_TARGET'} = undef;
235 local $init::hints::darwin::defaults{sw_vers} = qq{$predicted.11};
237 init::hints::darwin::_set_deployment_environment();
238 is($ENV{'MACOSX_DEPLOYMENT_TARGET'}, $predicted,
239 "_set_deployment_environment(): MACOSX_DEPLOYMENT_TARGET set as expected");
242 ##### _probe_for_fink() #####
245 my $tdir = tempdir( CLEANUP => 1 );
246 chdir $tdir or die "Unable to change to temporary directory: $!";
248 local $init::hints::darwin::defaults{fink_conf} = qq{$tdir/fink.conf};
249 ok( ! defined( init::hints::darwin::_probe_for_fink( $conf ) ),
250 "_probe_for_fink(): returned undefined value for no config file" );
251 $conf->options->set( 'verbose' => 1 );
253 my ($stdout, $stderr);
255 sub { init::hints::darwin::_probe_for_fink( $conf ); },
259 like( $stdout, qr/Fink configuration file not located/,
260 "Got expected verbose output when Fink config not located" );
262 $conf->options->set( 'verbose' => 0 );
264 my $fink_conf_file = q{fink.conf};
265 open my $CONF, '>', $fink_conf_file
266 or die "Unable to open $fink_conf_file for writing: $!";
267 print $CONF "Hello, world, but no Fink info\n";
268 close $CONF or die "Unable to close $fink_conf_file after writing: $!";
269 ok( ! defined( init::hints::darwin::_probe_for_fink( $conf ) ),
270 "_probe_for_fink(): returned undefined value for defective config file" );
272 $conf->options->set( 'verbose' => 1 );
274 my ($stdout, $stderr);
276 sub { init::hints::darwin::_probe_for_fink( $conf ); },
280 like( $stdout, qr/Fink configuration file defective/,
281 "Got expected verbose output when Fink config was defective" );
283 $conf->options->set( 'verbose' => 0 );
285 my $other = qq{$tdir/other_fink.conf};
286 local $init::hints::darwin::defaults{fink_conf} = $other;
287 $fink_conf_file = $other;
288 open my $OCONF, '>', $fink_conf_file
289 or die "Unable to open $fink_conf_file for writing: $!";
290 print $OCONF "Basepath: /tmp/foobar\n";
291 close $OCONF or die "Unable to close $fink_conf_file after writing: $!";
292 ok( ! defined( init::hints::darwin::_probe_for_fink( $conf ) ),
293 "_probe_for_fink(): returned undefined value for missing directories" );
295 $conf->options->set( 'verbose' => 1 );
297 my ($stdout, $stderr);
299 sub { init::hints::darwin::_probe_for_fink( $conf ); },
303 like( $stdout, qr/Could not locate Fink directories/,
304 "Got expected verbose output when Fink directories were missing" );
306 $conf->options->set( 'verbose' => 0 );
308 chdir $cwd or die "Unable to change back to starting directory: $!";
311 ##### _probe_for_macports() #####
314 my $tdir = tempdir( CLEANUP => 1 );
315 chdir $tdir or die "Unable to change to temporary directory: $!";
317 local $init::hints::darwin::defaults{ports_base_dir} = qq{$tdir/foobar};
318 ok( ! defined( init::hints::darwin::_probe_for_macports( $conf ) ),
319 "_probe_for_macports(): returned undefined value for no config file" );
321 $conf->options->set( 'verbose' => 1 );
323 my ($stdout, $stderr);
325 sub { init::hints::darwin::_probe_for_macports( $conf ); },
329 like( $stdout, qr/Could not locate Macports directories/,
330 "Got expected verbose output when Macports directories not found" );
332 $conf->options->set( 'verbose' => 0 );
335 ##### _probe_for_libraries() #####
338 $conf->options->set( 'darwin_no_fink' => 1 );
339 $conf->options->set( 'verbose' => 0 );
340 my $lib_dir = $conf->data->get('build_dir') . "/blib/lib";
342 $flagsref->{ldflags} .= " -L$lib_dir";
343 $flagsref->{ccflags} .= " -pipe -fno-common -Wno-long-double ";
344 $flagsref->{linkflags} .= " -undefined dynamic_lookup";
345 my %state_before = map { $_ => $flagsref->{$_} } keys %{ $flagsref };
346 ok( ! defined ( init::hints::darwin::_probe_for_libraries(
347 $conf, $flagsref, 'fink')
348 ), "_probe_for_libraries() returned undef as expected" );
349 is_deeply( $flagsref, { %state_before },
350 "No change in flags, as expected" );
352 $conf->options->set( 'darwin_no_fink' => 0 );
355 ##### _add_to_flags() #####
358 my ( $addl_flags_ref, $flagsref, $title, $verbose );
359 $addl_flags_ref = undef;
362 $conf->options->set( 'verbose' => undef );
363 ok( init::hints::darwin::_add_to_flags(
364 $conf, $addl_flags_ref, $flagsref, $title
365 ), "_add_to_flags(): returned true value when no probes found" );
367 $conf->options->set( 'verbose' => 1 );
369 my ($stdout, $stderr);
371 sub { init::hints::darwin::_add_to_flags(
372 $conf, $addl_flags_ref, $flagsref, $title
377 like( $stdout, qr/Probe for $title unsuccessful/,
378 "_add_to_flags(): got expected verbose output when probe unsuccessful" );
382 ldflags => "-Lfink_lib_dir",
383 ccflags => "-Lfink_include_dir",
384 linkflags => "-Lfink_lib_dir",
386 my $lib_dir = $conf->data->get('build_dir') . "/blib/lib";
388 $flagsref->{ldflags} = " -L$lib_dir";
389 $flagsref->{ccflags} = " -pipe -fno-common -Wno-long-double ";
390 $flagsref->{linkflags} = undef;
392 $conf->options->set( 'verbose' => undef );
393 my $rv = init::hints::darwin::_add_to_flags(
394 $conf, $addl_flags_ref, $flagsref, $title
396 is( $flagsref->{linkflags}, " $addl_flags_ref->{linkflags}",
397 "_add_to_flags(): flag added where not previously populated" );
400 pass("Completed all tests in $0");
402 ################### DOCUMENTATION ###################
406 init/hints/darwin-01.t - test init::hints::darwin
410 % prove t/steps/init/hints/darwin-01.t
414 The files in this directory test functionality used by F<Configure.pl>.
416 The tests in this file test init::hints::darwin.
424 config::init::hints::darwin, F<Configure.pl>.
430 # cperl-indent-level: 4
433 # vim: expandtab shiftwidth=4: