2 # Copyright (C) 2001-2005, Parrot Foundation.
8 use Test::More tests => 31;
11 use File::Basename qw(basename dirname);
12 use File::Temp 0.13 qw/ tempfile /;
14 use lib qw( lib t/configure/testlib );
15 use IO::CaptureOutput qw | capture |;
16 use Tie::Filehandle::Preempt::Stdin;
18 BEGIN { use Parrot::Configure::Utils; }
20 Parrot::Configure::Utils->import(@Parrot::Configure::Utils::EXPORT_OK);
21 can_ok( __PACKAGE__, @Parrot::Configure::Utils::EXPORT_OK );
24 my ( @prompts, $object, $cc, $nonexistent, $command );
28 is( integrate( undef, undef ), undef, "integrate(undef, undef)" );
29 is( integrate( undef, 1 ), 1, "integrate(undef, 1)" );
30 is( integrate( 1, undef ), 1, "integrate(1, undef)" );
31 is( integrate( 1, 2 ), 2, "integrate(1, 1)" );
32 is( integrate( 1, q{ } ), 1, 'integrate(1, [empty string])' );
35 # Tests in t/configure/1??-inter-*.t do a good job of testing prompt().
36 # They leave only one condition to be tested here.
39 $object = tie *STDIN, 'Tie::Filehandle::Preempt::Stdin', @prompts;
40 can_ok( 'Tie::Filehandle::Preempt::Stdin', ('READLINE') );
41 isa_ok( $object, 'Tie::Filehandle::Preempt::Stdin' );
46 capture ( sub { $rv = prompt( "What C compiler do you want to use?", $cc ) },
48 ok( $stdout, "prompts were captured" );
49 is( $rv, $cc, "Empty response to prompt led to expected return value" );
54 # file_checksum(), not exported
57 eval { my $sum = Parrot::Configure::Utils::file_checksum($nonexistent); };
59 $@, qr/Can't open $nonexistent/, #'
60 "Got expected error message when trying to get checksum on non-existent file"
64 my ( $tmpfile, $fname ) = tempfile( UNLINK => 1 );
65 print $tmpfile "foo" x 1000;
67 is( Parrot::Configure::Utils::file_checksum($fname),
68 '324000', "file_checksum() returns correct checksum" );
72 my ( $tmpfile, $fname ) = tempfile( UNLINK => 1 );
73 my $str = 'Do not print this line';
74 print $tmpfile "foo" x 500;
76 print $tmpfile "$str\n";
77 print $tmpfile "foo" x 500;
79 my $ignore_pattern = qr/$str/;
80 my $csum = Parrot::Configure::Utils::file_checksum( $fname, $ignore_pattern );
81 is( $csum, '324010', "file_checksum() returns correct checksum" );
87 my ( $fromfile, $fromfname ) = tempfile( UNLINK => 1 );
88 my ( $tofile, $tofname ) = tempfile( UNLINK => 1 );
89 print $fromfile "foo" x 1000;
92 ok( copy_if_diff( $fromfname, $tofname ), "copy_if_diff() true return status" );
93 is( Parrot::Configure::Utils::file_checksum($tofname),
94 '324000', "copy_if_diff() copied differing files" );
98 my ( $fromfile, $fromfname ) = tempfile( UNLINK => 1 );
99 my ( $tofile, $tofname ) = tempfile( UNLINK => 1 );
100 print $fromfile "foo" x 1000;
102 print $tofile "foo" x 1000;
105 ok( !defined( copy_if_diff( $fromfname, $tofname ) ), "copy_if_diff() true return undef" );
111 my ( $fromfile, $fromfname ) = tempfile( UNLINK => 1 );
112 my ( $tofile, $tofname ) = tempfile( UNLINK => 1 );
113 print $fromfile "foo" x 1000;
117 # redirect STDERR to avoid warnings
118 my $redir = File::Spec->devnull;
120 # copy file descriptors
121 open *OLDERR, ">&", "STDERR";
125 ok( move_if_diff( $fromfname, $tofname ), "move_if_diff() true return status" );
126 ok( !-e $fromfname, "move_if_diff() moved differing file" );
128 # redirect STDERR for the test below
130 open *STDERR, '<', $redir;
132 ok( -e $tofname, "move_if_diff() moved differing file" );
136 open *STDERR, ">&", "OLDERR";
141 my %tf_params = ( UNLINK => 1, );
142 $tf_params{SUFFIX} = '.exe' if (
143 ( $^O eq 'MSWin32' ) ||
146 my ( $tmpfile, $fname ) = tempfile(%tf_params);
148 local $ENV{PATH} = dirname($fname);
150 my $prog = basename($fname);
152 is( check_progs($prog), $prog, "check_progs() returns the proper program" )
156 my %tf_params = ( UNLINK => 1, );
157 $tf_params{SUFFIX} = '.exe' if (
158 ( $^O eq 'MSWin32' ) ||
161 my ( $tmpfile, $fname ) = tempfile(%tf_params);
163 local $ENV{PATH} = dirname($fname);
165 my $prog = basename($fname);
167 is( check_progs( [$prog] ),
168 $prog, "check_progs() returns the proper program when passed an array ref" )
172 my $cmd = 'someboguscommand';
173 ok( !check_progs( [$cmd] ), "check_progs() returns undef in scalar context on failure" );
174 ok( !check_progs($cmd), "check_progs() returns undef in scalar context on failure" );
175 is_deeply( [ check_progs( [$cmd] ) ],
176 [], "check_progs() returns () in list context on failure" );
177 is_deeply( [ check_progs($cmd) ], [], "check_progs() returns () in list context on failure" );
181 my %tf_params = ( UNLINK => 1, );
182 $tf_params{SUFFIX} = '.exe' if (
183 ( $^O eq 'MSWin32' ) ||
186 my ( $tmpfile, $fname ) = tempfile(%tf_params);
188 local $ENV{PATH} = dirname($fname);
190 my $prog = basename($fname);
194 capture ( sub { is( check_progs( $prog, $verbose ),
195 $prog, "check_progs() returns the proper program" ) }, \$stdout );
196 like( $stdout, qr/checking for program/, "Got expected verbose output" );
203 capture ( sub { $prog = check_progs(
204 [ 'gmake', 'mingw32-make', 'nmake', 'make' ], $verbose) }, \$stdout );
205 ok( defined($prog), "check_progs() returned a 'make' program" );
206 like( $stdout, qr/checking for program/s, "Got expected verbose output" );
207 like( $stdout, qr/$prog(\.EXE)? is executable/s,
208 "Got expected verbose output for executable program" );
211 # _slurp(), not exported
214 my ( $tmpfile, $fname ) = tempfile( UNLINK => 1 );
215 print $tmpfile "foo" x 1000;
217 is( Parrot::Configure::Utils::_slurp($fname), "foo" x 1000, "_slurp() slurped the file" );
220 ################### DOCUMENTATION ###################
224 t/configure/033-step.t - tests Parrot::Configure::Utils
228 prove t/configure/033-step.t
232 Regression tests for the L<Parrot::Configure::Utils> module.
238 # cperl-indent-level: 4
241 # vim: expandtab shiftwidth=4: