Skip to content

Commit 9a150dc

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 9a150dc

File tree

1 file changed

+89
-27
lines changed

1 file changed

+89
-27
lines changed

t/oneliner.t

Lines changed: 89 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,55 +7,117 @@ 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

14-
my $TB = Test::More->builder;
16+
my $TB = Test::More->builder;
1517
my $perl = which_perl;
1618

1719
BEGIN { use_ok('ExtUtils::MM') }
1820

1921
my $mm = bless { NAME => "Foo", MAKE => $Config{make} }, 'MM';
20-
isa_ok($mm, 'ExtUtils::MakeMaker');
21-
isa_ok($mm, 'ExtUtils::MM_Any');
22+
isa_ok( $mm, 'ExtUtils::MakeMaker' );
23+
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;
30+
END { chdir $cwd if defined $cwd } # so File::Temp can cleanup
3231

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

3635
# Lets see how it deals with quotes.
37-
try_oneliner(q{print "foo'o", ' bar"ar'}, [], q{foo'o bar"ar}, 'quotes');
36+
try_oneliner( q{print "foo'o", ' bar"ar'}, [], q{foo'o bar"ar}, 'quotes' );
3837

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

4242
# switches?
43-
try_oneliner(q{print 'foo'}, ['-l'], "foo\n", 'switches' );
43+
try_oneliner( q{print 'foo'}, ['-l'], "foo\n", 'switches' );
4444

4545
# some DOS-specific things
46-
try_oneliner(q{print " \" "}, [], q{ " }, 'single quote' );
47-
try_oneliner(q{print " < \" "}, [], q{ < " }, 'bracket, then quote' );
48-
try_oneliner(q{print " \" < "}, [], q{ " < }, 'quote, then bracket' );
49-
try_oneliner(q{print " < \"\" < \" < \" < "}, [], q{ < "" < " < " < }, 'quotes and brackets mixed' );
50-
try_oneliner(q{print " < \" | \" < | \" < \" < "}, [], q{ < " | " < | " < " < }, 'brackets, pipes and quotes' );
46+
try_oneliner( q{print " \" "}, [], q{ " }, 'single quote' );
47+
try_oneliner( q{print " < \" "}, [], q{ < " }, 'bracket, then quote' );
48+
try_oneliner( q{print " \" < "}, [], q{ " < }, 'quote, then bracket' );
49+
try_oneliner(
50+
q{print " < \"\" < \" < \" < "}, [], q{ < "" < " < " < },
51+
'quotes and brackets mixed'
52+
);
53+
try_oneliner(
54+
q{print " < \" | \" < | \" < \" < "}, [],
55+
q{ < " | " < | " < " < }, 'brackets, pipes and quotes'
56+
);
5157

5258
# some examples from http://www.autohotkey.net/~deleyd/parameters/parameters.htm#CPP
53-
try_oneliner(q{print q[ &<>^|()@ ! ]}, [], q{ &<>^|()@ ! }, 'example 8.1' );
54-
try_oneliner(q{print q[ &<>^|@()!"&<>^|@()! ]}, [], q{ &<>^|@()!"&<>^|@()! }, 'example 8.2' );
55-
try_oneliner(q{print q[ "&<>^|@() !"&<>^|@() !" ]}, [], q{ "&<>^|@() !"&<>^|@() !" }, 'example 8.3' );
56-
try_oneliner(q{print q[ "C:\TEST A\" ]}, [], q{ "C:\TEST A\" }, 'example 8.4' );
57-
try_oneliner(q{print q[ "C:\TEST %&^ A\" ]}, [], q{ "C:\TEST %&^ A\" }, 'example 8.5' );
59+
try_oneliner( q{print q[ &<>^|()@ ! ]}, [], q{ &<>^|()@ ! }, 'example 8.1' );
60+
try_oneliner(
61+
q{print q[ &<>^|@()!"&<>^|@()! ]}, [],
62+
q{ &<>^|@()!"&<>^|@()! }, 'example 8.2'
63+
);
64+
try_oneliner(
65+
q{print q[ "&<>^|@() !"&<>^|@() !" ]}, [],
66+
q{ "&<>^|@() !"&<>^|@() !" }, 'example 8.3'
67+
);
68+
try_oneliner(
69+
q{print q[ "C:\TEST A\" ]}, [], q{ "C:\TEST A\" },
70+
'example 8.4'
71+
);
72+
try_oneliner(
73+
q{print q[ "C:\TEST %&^ A\" ]}, [], q{ "C:\TEST %&^ A\" },
74+
'example 8.5'
75+
);
5876

5977
# XXX gotta rethink the newline test. The Makefile does newline
6078
# escaping, then the shell.
6179

80+
done_testing;
81+
exit;
82+
83+
sub try_oneliner {
84+
my ( $code, $switches, $expect, $name ) = @_;
85+
my $cmd = $mm->oneliner( $code, $switches );
86+
$cmd =~ s{\$\(ABSPERLRUN\)}{$perl};
87+
88+
# VMS likes to put newlines at the end of commands if there isn't
89+
# one already.
90+
$expect =~ s/([^\n])\z/$1\n/ if $^O eq 'VMS';
91+
92+
my $Makefile = makefile_name();
93+
94+
my $content = Makefile_template($cmd);
95+
write_file( $Makefile, $content );
96+
97+
my $output = run(qq{$make -s all});
98+
99+
my $ok = $TB->is_eq( $output, $expect, $name )
100+
|| $TB->diag("$Makefile:\n$content");
101+
102+
return $ok;
103+
}
104+
105+
sub Makefile_template {
106+
my ($RUN) = @_;
107+
my $NOECHO = '@';
108+
109+
return <<"MAKEFILE";
110+
all:
111+
${NOECHO} ${RUN}
112+
MAKEFILE
113+
}
114+
115+
sub write_file {
116+
my ( $f, $content ) = @_;
117+
118+
open( my $fh, '>', $f ) or die $!;
119+
print {$fh} $content or die $!;
120+
close $fh;
121+
122+
return;
123+
}

0 commit comments

Comments
 (0)