diff --git a/t/oneliner.t b/t/oneliner.t index e7c2621f2..efd373a72 100644 --- a/t/oneliner.t +++ b/t/oneliner.t @@ -8,8 +8,10 @@ chdir 't'; use Config; use MakeMaker::Test::Utils; -use Test::More tests => 16; +use Test::More; +use File::Temp qw[tempdir]; use File::Spec; +use Cwd; my $TB = Test::More->builder; my $perl = which_perl; @@ -20,24 +22,21 @@ my $mm = bless { NAME => "Foo", MAKE => $Config{make} }, 'MM'; isa_ok($mm, 'ExtUtils::MakeMaker'); isa_ok($mm, 'ExtUtils::MM_Any'); +my $MAKE = $mm->{MAKE} or die; -sub try_oneliner { - my($code, $switches, $expect, $name) = @_; - my $cmd = $mm->oneliner($code, $switches); - $cmd =~ s{\$\(ABSPERLRUN\)}{$perl}; +my $tmpdir = tempdir( CLEANUP => 1 ); - # VMS likes to put newlines at the end of commands if there isn't - # one already. - $expect =~ s/([^\n])\z/$1\n/ if $^O eq 'VMS'; +my $cwd = getcwd; END { chdir $cwd if defined $cwd } # so File::Temp can cleanup - $TB->is_eq(scalar `$cmd`, $expect, $name) || $TB->diag("oneliner:\n$cmd"); -} +# run all these test from a temporary directory +chdir($tmpdir) or die "Fail to change to tmp directory: $!"; # Lets see how it deals with quotes. try_oneliner(q{print "foo'o", ' bar"ar'}, [], q{foo'o bar"ar}, 'quotes'); # How about dollar signs? -try_oneliner(q{$PATH = 'foo'; print $PATH},[], q{foo}, 'dollar signs' ); +try_oneliner(q{my $PATH = 'foo'; print $PATH},[], q{foo}, 'dollar signs' ); +try_oneliner(q{my %h = (1, 2); print $h{1}},[], q{2}, '%h and $h' ); # switches? try_oneliner(q{print 'foo'}, ['-l'], "foo\n", 'switches' ); @@ -59,3 +58,44 @@ try_oneliner(q{print q[ "C:\TEST %&^ A\" ]}, [], q{ "C:\TEST %&^ A\" }, 'examp # XXX gotta rethink the newline test. The Makefile does newline # escaping, then the shell. +done_testing; +exit; + +sub try_oneliner { + my($code, $switches, $expect, $name) = @_; + my $cmd = $mm->oneliner($code, $switches); + $cmd =~ s{\$\(ABSPERLRUN\)}{$perl}; + + # VMS likes to put newlines at the end of commands if there isn't + # one already. + $expect =~ s/([^\n])\z/$1\n/ if $^O eq 'VMS'; + + my $content = Makefile_template( $cmd ); + write_file('Makefile', $content); + + my $output = qx{$MAKE all}; + + my $ok = $TB->is_eq($output, $expect, $name) || $TB->diag("oneliner:\n$cmd"); + + return $ok; +} + +sub Makefile_template { + my ( $RUN ) = @_; + my $NOECHO = '@'; + + return <<"MAKEFILE"; +all: + ${NOECHO} ${RUN} +MAKEFILE +} + +sub write_file { + my ( $f, $content ) = @_; + + open( my $fh, '>', $f ) or die $!; + print {$fh} $content or die $!; + close $fh; + + return; +} \ No newline at end of file