Skip to content

Test oneliner without double escaping dollar sign #356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 89 additions & 27 deletions t/oneliner.t
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,117 @@ BEGIN {
chdir 't';

use Config;
use MakeMaker::Test::Utils;
use Test::More tests => 16;
use MakeMaker::Test::Utils qw(makefile_name make_run run which_perl);
use Test::More;
use File::Temp qw[tempdir];
use File::Spec;
use Cwd;

my $TB = Test::More->builder;
my $TB = Test::More->builder;
my $perl = which_perl;

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

my $mm = bless { NAME => "Foo", MAKE => $Config{make} }, 'MM';
isa_ok($mm, 'ExtUtils::MakeMaker');
isa_ok($mm, 'ExtUtils::MM_Any');
isa_ok( $mm, 'ExtUtils::MakeMaker' );
isa_ok( $mm, 'ExtUtils::MM_Any' );

my $make = make_run();

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');
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' );
try_oneliner( q{print 'foo'}, ['-l'], "foo\n", 'switches' );

# some DOS-specific things
try_oneliner(q{print " \" "}, [], q{ " }, 'single quote' );
try_oneliner(q{print " < \" "}, [], q{ < " }, 'bracket, then quote' );
try_oneliner(q{print " \" < "}, [], q{ " < }, 'quote, then bracket' );
try_oneliner(q{print " < \"\" < \" < \" < "}, [], q{ < "" < " < " < }, 'quotes and brackets mixed' );
try_oneliner(q{print " < \" | \" < | \" < \" < "}, [], q{ < " | " < | " < " < }, 'brackets, pipes and quotes' );
try_oneliner( q{print " \" "}, [], q{ " }, 'single quote' );
try_oneliner( q{print " < \" "}, [], q{ < " }, 'bracket, then quote' );
try_oneliner( q{print " \" < "}, [], q{ " < }, 'quote, then bracket' );
try_oneliner(
q{print " < \"\" < \" < \" < "}, [], q{ < "" < " < " < },
'quotes and brackets mixed'
);
try_oneliner(
q{print " < \" | \" < | \" < \" < "}, [],
q{ < " | " < | " < " < }, 'brackets, pipes and quotes'
);

# some examples from http://www.autohotkey.net/~deleyd/parameters/parameters.htm#CPP
try_oneliner(q{print q[ &<>^|()@ ! ]}, [], q{ &<>^|()@ ! }, 'example 8.1' );
try_oneliner(q{print q[ &<>^|@()!"&<>^|@()! ]}, [], q{ &<>^|@()!"&<>^|@()! }, 'example 8.2' );
try_oneliner(q{print q[ "&<>^|@() !"&<>^|@() !" ]}, [], q{ "&<>^|@() !"&<>^|@() !" }, 'example 8.3' );
try_oneliner(q{print q[ "C:\TEST A\" ]}, [], q{ "C:\TEST A\" }, 'example 8.4' );
try_oneliner(q{print q[ "C:\TEST %&^ A\" ]}, [], q{ "C:\TEST %&^ A\" }, 'example 8.5' );
try_oneliner( q{print q[ &<>^|()@ ! ]}, [], q{ &<>^|()@ ! }, 'example 8.1' );
try_oneliner(
q{print q[ &<>^|@()!"&<>^|@()! ]}, [],
q{ &<>^|@()!"&<>^|@()! }, 'example 8.2'
);
try_oneliner(
q{print q[ "&<>^|@() !"&<>^|@() !" ]}, [],
q{ "&<>^|@() !"&<>^|@() !" }, 'example 8.3'
);
try_oneliner(
q{print q[ "C:\TEST A\" ]}, [], q{ "C:\TEST A\" },
'example 8.4'
);
try_oneliner(
q{print q[ "C:\TEST %&^ A\" ]}, [], q{ "C:\TEST %&^ A\" },
'example 8.5'
);

# 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 $Makefile = makefile_name();

my $content = Makefile_template($cmd);
write_file( $Makefile, $content );

my $output = run(qq{$make -s all});

my $ok = $TB->is_eq( $output, $expect, $name )
|| $TB->diag("$Makefile:\n$content");

return $ok;
}

sub Makefile_template {
my ($RUN) = @_;
my $NOECHO = '@';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will not be correct on all platforms

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed you are right. This is kind a of a chicken egg issue: producing Makefile to test code which generate Makefile..
What would be your preference?

  • do we have some helpers we could use for this?
  • skipping some distro for this test?
  • going back to the unescape solution?
  • other?

thanks for your valuable input

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Skipping and unescaping are not viable approaches. Probably the way to go here is to be making a sandbox dir, then minimal Makefile.PL. Look in the XS tests for functions to make this easy.


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;
}