Skip to content

Commit c0319fa

Browse files
committed
Test oneliner using a temporary Makefile
Fix Perl-Toolchain-Gang#355 Double escaping '$' for Makefile usage is fine, but we should not use such a syntax while testing and trying to run the oneliner output. We could consider unescpaing '$$', '{{', '}}'... (for dmake) but a cleaner solution is to test the oneliner in a Makefile itself.
1 parent 570413d commit c0319fa

File tree

1 file changed

+51
-11
lines changed

1 file changed

+51
-11
lines changed

t/oneliner.t

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ chdir 't';
88

99
use Config;
1010
use MakeMaker::Test::Utils;
11-
use Test::More tests => 16;
11+
use Test::More;
12+
use File::Temp qw[tempdir];
1213
use File::Spec;
14+
use Cwd;
1315

1416
my $TB = Test::More->builder;
1517
my $perl = which_perl;
@@ -20,24 +22,21 @@ my $mm = bless { NAME => "Foo", MAKE => $Config{make} }, 'MM';
2022
isa_ok($mm, 'ExtUtils::MakeMaker');
2123
isa_ok($mm, 'ExtUtils::MM_Any');
2224

25+
my $MAKE = $mm->{MAKE} or die;
2326

24-
sub try_oneliner {
25-
my($code, $switches, $expect, $name) = @_;
26-
my $cmd = $mm->oneliner($code, $switches);
27-
$cmd =~ s{\$\(ABSPERLRUN\)}{$perl};
27+
my $tmpdir = tempdir( CLEANUP => 1 );
2828

29-
# VMS likes to put newlines at the end of commands if there isn't
30-
# one already.
31-
$expect =~ s/([^\n])\z/$1\n/ if $^O eq 'VMS';
29+
my $cwd = getcwd; END { chdir $cwd if defined $cwd } # so File::Temp can cleanup
3230

33-
$TB->is_eq(scalar `$cmd`, $expect, $name) || $TB->diag("oneliner:\n$cmd");
34-
}
31+
# run all these test from a temporary directory
32+
chdir($tmpdir) or die "Fail to change to tmp directory: $!";
3533

3634
# Lets see how it deals with quotes.
3735
try_oneliner(q{print "foo'o", ' bar"ar'}, [], q{foo'o bar"ar}, 'quotes');
3836

3937
# How about dollar signs?
40-
try_oneliner(q{$PATH = 'foo'; print $PATH},[], q{foo}, 'dollar signs' );
38+
try_oneliner(q{my $PATH = 'foo'; print $PATH},[], q{foo}, 'dollar signs' );
39+
try_oneliner(q{my %h = (1, 2); print $h{1}},[], q{2}, '%h and $h' );
4140

4241
# switches?
4342
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
5958
# XXX gotta rethink the newline test. The Makefile does newline
6059
# escaping, then the shell.
6160

61+
done_testing;
62+
exit;
63+
64+
sub try_oneliner {
65+
my($code, $switches, $expect, $name) = @_;
66+
my $cmd = $mm->oneliner($code, $switches);
67+
$cmd =~ s{\$\(ABSPERLRUN\)}{$perl};
68+
69+
# VMS likes to put newlines at the end of commands if there isn't
70+
# one already.
71+
$expect =~ s/([^\n])\z/$1\n/ if $^O eq 'VMS';
72+
73+
my $content = Makefile_template( $cmd );
74+
write_file('Makefile', $content);
75+
76+
my $output = qx{$MAKE all};
77+
78+
my $ok = $TB->is_eq($output, $expect, $name) || $TB->diag("oneliner:\n$cmd");
79+
80+
return $ok;
81+
}
82+
83+
sub Makefile_template {
84+
my ( $RUN ) = @_;
85+
my $NOECHO = '@';
86+
87+
return <<"MAKEFILE";
88+
all:
89+
${NOECHO} ${RUN}
90+
MAKEFILE
91+
}
92+
93+
sub write_file {
94+
my ( $f, $content ) = @_;
95+
96+
open( my $fh, '>', $f ) or die $!;
97+
print {$fh} $content or die $!;
98+
close $fh;
99+
100+
return;
101+
}

0 commit comments

Comments
 (0)