Skip to content

Commit 817a0df

Browse files
author
Chris White
committed
Added z_noexit_on_help; unique script pkg. names
Both changes support the move of testing to Capture::Tiny. --z_noexit_on_help prevents pod2usage() from exiting the test process early, and unique script package names keep the scripts from separate calls to Text::PerlPP::Main in separate packages.
1 parent 4d75926 commit 817a0df

File tree

3 files changed

+64
-30
lines changed

3 files changed

+64
-30
lines changed

lib/Text/PerlPP.pm

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use warnings;
1111

1212
use Getopt::Long 2.5 qw(GetOptionsFromArray);
1313
use Pod::Usage;
14+
use Data::Dumper;
1415

1516
# === Constants ===========================================================
1617

@@ -58,6 +59,7 @@ use constant OB_STARTLINE => 2;
5859

5960
# Internals
6061
my $Package = ''; # package name for the generated script
62+
my $PackageNum = 0; # make sure each run has a unique package name
6163
my $RootSTDOUT;
6264
my $WorkingDir = '.';
6365
my %Opts; # Parsed command-line options
@@ -616,6 +618,10 @@ my %CMDLINE_OPTS = (
616618
SETS => ['s','|set:s%'], # Extra data in %S, without text substitution
617619
# --usage reserved
618620
PRINT_VERSION => ['v','|version+'],
621+
622+
# Special-case for testing --- don't exit on --help &c.
623+
NOEXIT_ON_HELP => ['z_noexit_on_help'],
624+
619625
# -? reserved
620626
);
621627

@@ -645,22 +651,42 @@ sub parse_command_line {
645651
# small POD below, which links to `perldoc perlpp`.
646652

647653
# Get options
654+
my $ok =
648655
GetOptionsFromArray($lrArgs, $hrOptsOut, # destination hash
649656
'usage|?', 'h|help', 'man', # options we handle here
650-
map { $_->[0] . $_->[1] } values %CMDLINE_OPTS, # options strs
651-
)
652-
or pod2usage(-verbose => 0, -exitval => EXIT_PARAM_ERR, %docs);
653-
# unknown opt
654-
655-
# Help, if requested
656-
pod2usage(-verbose => 0, -exitval => EXIT_PROC_ERR, %docs) if have('usage');
657-
pod2usage(-verbose => 1, -exitval => EXIT_PROC_ERR, %docs) if have('h');
658-
pod2usage(-verbose => 2, -exitval => EXIT_PROC_ERR, %docs) if have('man');
657+
map { $_->[0] . ($_->[1]//'') } values %CMDLINE_OPTS, # options strs
658+
);
659+
my $noexit_on_help =
660+
$hrOptsOut->{ $CMDLINE_OPTS{NOEXIT_ON_HELP}->[0] } // false;
661+
662+
if($noexit_on_help) { # Report help during testing
663+
# unknown opt --- error out. false => processing should terminate.
664+
pod2usage(-verbose => 0, -exitval => 'NOEXIT', %docs), return false unless $ok;
665+
666+
# Help, if requested
667+
pod2usage(-verbose => 0, -exitval => 'NOEXIT', %docs), return false if have('usage');
668+
pod2usage(-verbose => 1, -exitval => 'NOEXIT', %docs), return false if have('h');
669+
pod2usage(-verbose => 2, -exitval => 'NOEXIT', %docs), return false if have('man');
670+
671+
} else { # Normal usage
672+
# unknown opt --- error out
673+
pod2usage(-verbose => 0, -exitval => EXIT_PARAM_ERR, %docs) unless $ok;
674+
675+
# Help, if requested
676+
pod2usage(-verbose => 0, -exitval => EXIT_PROC_ERR, %docs) if have('usage');
677+
pod2usage(-verbose => 1, -exitval => EXIT_PROC_ERR, %docs) if have('h');
678+
pod2usage(-verbose => 2, -exitval => EXIT_PROC_ERR, %docs) if have('man');
679+
}
659680

660681
# Map the option names from GetOptions back to the internal names we use,
661682
# e.g., $hrOptsOut->{EVAL} from $hrOptsOut->{e}.
662683
my %revmap = map { $CMDLINE_OPTS{$_}->[0] => $_ } keys %CMDLINE_OPTS;
684+
#say "revmap ", Dumper(\%revmap);
685+
#say "hrOptsOut ", Dumper($hrOptsOut);
663686
for my $optname (keys %$hrOptsOut) {
687+
#say "\nOptname $optname";
688+
#say "Value $hrOptsOut->{$optname}";
689+
#say "Revmap $revmap{$optname}";
664690
$hrOptsOut->{ $revmap{$optname} } = $hrOptsOut->{ $optname };
665691
}
666692

@@ -672,13 +698,16 @@ sub parse_command_line {
672698
# Process other arguments. TODO? support multiple input filenames?
673699
$hrOptsOut->{INPUT_FILENAME} = $ARGV[0] // "";
674700

701+
return true; # Go ahead and run
675702
} #parse_command_line()
676703

677704
# === Main ================================================================
678705

679706
sub Main {
680707
my $lrArgv = shift // [];
681-
parse_command_line $lrArgv, \%Opts;
708+
unless(parse_command_line $lrArgv, \%Opts) {
709+
return EXIT_OK; # TODO report param err vs. proc err?
710+
}
682711

683712
if($Opts{PRINT_VERSION}) {
684713
print "PerlPP version $Text::PerlPP::VERSION\n";
@@ -694,6 +723,7 @@ sub Main {
694723
$Package =~ s/^.*?([a-z_][a-z_0-9.]*).pl?$/$1/i;
695724
$Package =~ s/[^a-z0-9_]/_/gi;
696725
# $Package is not the whole name, so can start with a number.
726+
$Package .= $PackageNum++;
697727

698728
StartOB(); # Output from here on will be included in the generated script
699729

t/03-cmdline.t

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,20 @@ my @testcases=(
1313
['--version','',qr/\bversion\b/],
1414

1515
# Debug output
16-
['-d','',qr/^package PPP_;/m],
16+
['-d','',qr/^package PPP_[0-9]*;/m],
1717
['-d', '<?= 2+2 ?>', qr{print\s+2\+2\s*;}],
1818
['--debug', '<?= 2+2 ?>', qr{print\s+2\+2\s*;}],
1919
['-E', '<?= 2+2 ?>', qr{print\s+2\+2\s*;}],
2020

2121
# Usage
22-
['-h', '', qr/^Usage/],
23-
['--help', '', qr/^Usage/],
22+
['-h --z_noexit_on_help', '', qr/^Usage/],
23+
['--help --z_noexit_on_help', '', qr/^Usage/],
2424

2525
# Eval at start of file
26-
['-e \'my $foo=42;\'','<?= $foo ?>', qr/^42$/],
26+
[['-e', 'my $foo=42;'],'<?= $foo ?>', qr/^42$/],
27+
# TODO RESUME HERE: break down the command line arguments since the
28+
# shell is no longer parsing them. (Note that they will have to be
29+
# re-quoted for use by the packed test.)
2730
['--eval \'my $foo=42;\'','<?= $foo ?>', qr/^42$/],
2831
['-d -e \'my $foo=42;\'','<?= $foo ?>', qr/^my \$foo=42;/m],
2932
['--debug --eval \'my $foo=42;\'','<?= $foo ?>', qr/^print\s+\$foo\s*;/m],
@@ -115,16 +118,16 @@ for my $lrTest (@testcases) {
115118
my ($opts, $testin, $out_re, $err_re) = @$lrTest;
116119

117120
my ($out, $err);
118-
diag "$opts", " <<<'", $testin, "'\n";
121+
diag $opts, " <<<'", $testin, "'\n";
119122
run_perlpp $opts, \$testin, \$out, \$err;
120-
diag "Done running";
123+
#diag "Done running";
121124

122125
if(defined $out_re) {
123-
diag "checking output";
126+
#diag "checking output";
124127
like($out, $out_re);
125128
}
126129
if(defined $err_re) {
127-
diag "checking stderr";
130+
#diag "checking stderr";
128131
like($err, $err_re);
129132
}
130133
print STDERR "$err\n";

t/lib/PerlPPTest.pm

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ sub run_perlpp {
3131
my $retval;
3232

3333
$lrArgs = [split(' ', $lrArgs)] if ref $lrArgs ne 'ARRAY';
34+
say STDERR "args: ", Dumper($lrArgs);
3435

3536
if($ENV{PERLPP_PERLOPTS}) {
3637
say STDERR "# running external perl";
@@ -41,23 +42,23 @@ sub run_perlpp {
4142
# TODO figure out $?, retval, &c.
4243

4344
} else {
44-
say STDERR "# running perlpp internal";
45-
say STDERR "# redirecting stdin";
45+
#say STDERR "# running perlpp internal";
46+
#say STDERR "# redirecting stdin";
4647
open local(*STDIN), '<', $refStdin or die $!;
47-
say STDERR "# redirected stdin";
48+
#say STDERR "# redirected stdin";
4849

4950
my @result;
50-
say STDERR "# before capture";
51+
#say STDERR "# before capture";
5152
eval {
52-
($$refStdout, $$refStderr, @result) = capture {
53-
# Thanks to http://www.perlmonks.org/bare/?node_id=289391 by Zaxo
54-
say STDERR "# running perlpp";
55-
my $result = Text::PerlPP::Main($lrArgs);
56-
say STDERR "# done running perlpp";
57-
$result;
58-
};
53+
($$refStdout, $$refStderr, @result) = capture {
54+
# Thanks to http://www.perlmonks.org/bare/?node_id=289391 by Zaxo
55+
say STDERR "# running perlpp";
56+
my $result = Text::PerlPP::Main($lrArgs);
57+
say STDERR "# done running perlpp";
58+
$result;
59+
};
5960
} or die "Capture failed: " . $@;
60-
say STDERR "# after capture";
61+
#say STDERR "# after capture";
6162
close STDIN;
6263
$retval = $result[0] if @result;
6364
}

0 commit comments

Comments
 (0)