Skip to content

Commit ca37cb2

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 ca37cb2

File tree

1 file changed

+54
-12
lines changed

1 file changed

+54
-12
lines changed

t/oneliner.t

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ BEGIN {
77
chdir 't';
88

99
use Config;
10-
use MakeMaker::Test::Utils;
11-
use Test::More tests => 16;
10+
use MakeMaker::Test::Utils qw(makefile_name make_run run which_perl);
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 = make_run();
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,46 @@ 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 $Makefile = makefile_name();
74+
75+
my $content = Makefile_template( $cmd );
76+
write_file($Makefile, $content);
77+
78+
my $output = run(qq{$make -s all});
79+
80+
my $ok = $TB->is_eq($output, $expect, $name) || $TB->diag("$Makefile:\n$output");
81+
82+
return $ok;
83+
}
84+
85+
sub Makefile_template {
86+
my ( $RUN ) = @_;
87+
my $NOECHO = '@';
88+
89+
return <<"MAKEFILE";
90+
all:
91+
${NOECHO} ${RUN}
92+
MAKEFILE
93+
}
94+
95+
sub write_file {
96+
my ( $f, $content ) = @_;
97+
98+
open( my $fh, '>', $f ) or die $!;
99+
print {$fh} $content or die $!;
100+
close $fh;
101+
102+
return;
103+
}

0 commit comments

Comments
 (0)