BATsh

 view release on metacpan or  search on metacpan

t/0007-extcmd-env.t  view on Meta::CPAN

######################################################################
#
# 0007-extcmd-env.t  Regression: external commands must be portable
#                    across cmd.exe (Windows) AND /bin/sh (Unix/BSD),
#                    plus static guards against the two known hazards.
#
# BACKGROUND
#   BATsh runs an external command through a real shell:
#     - Windows : system STRING -> cmd.exe
#     - Unix/BSD: system STRING -> /bin/sh -c
#   A shelled-out Perl one-liner must therefore satisfy BOTH shells at
#   once.  Two distinct foot-guns were observed:
#
#   (A) Unix vector -- a dollar default-variable token inside the
#       one-liner is expanded by /bin/sh using the environment variable
#       "_" (the path/last-arg the shell exports), which is
#       unpredictable on CPAN smokers.  This produced random failures
#       such as "Bareword found where operator expected ... 1EERDtQcrK".
#
#   (B) Windows vector -- cmd.exe does NOT treat single quotes as
#       quoting.  A one-liner wrapped in single quotes is split on
#       whitespace, so Perl receives a broken fragment and dies with
#       "Can't find string terminator".
#
#   The portable form satisfies both: wrap the code in DOUBLE quotes
#   and use NO dollar sign the shell would expand, e.g.
#       perl -ne "print uc"
#   (the default variable is used implicitly by uc, so no token leaks).
#
# THIS TEST
#   EE01/EE02 run the pipeline and here-document patterns under several
#             hostile values of the environment variable "_" (the Unix
#             vector) and confirm correct output -- this passes on every
#             OS, and on Unix it actively defeats vector (A).
#   EE03      clean-environment baseline.
#   EE04      static guard for vector (B): no inline Perl may be wrapped
#             in single quotes (cmd.exe-unsafe).  Runs on any OS, so a
#             Windows run still catches a Unix-introduced regression and
#             vice versa.
#   EE05      static guard for vector (A): no double-quoted inline Perl
#             may contain a shell-expandable dollar token.
#
# COMPATIBILITY: Perl 5.005_03 and later
#
######################################################################
use strict;
BEGIN { if ($] < 5.006 && !defined(&warnings::import)) {
        $INC{'warnings.pm'} = 'stub'; eval 'package warnings; sub import {}' } }
use warnings; local $^W = 1;
BEGIN { pop @INC if $INC[-1] eq '.' }
use FindBin ();
use File::Spec ();
use lib "$FindBin::Bin/../lib";

eval { require BATsh } or die "Cannot load BATsh: $@";

# Portability: EE01/EE02/EE03 shell out to a bareword "perl".  On a CPAN
# smoker the perl under test is frequently NOT on PATH as "perl"
# (perlbrew/plenv, or perl invoked by absolute path), which would turn
# these regression checks into false failures.  Prepend the directory of
# the running interpreter ($^X) to PATH so "perl" resolves to it.  Done
# by environment (not by embedding $^X in the command), so a Win32 path
# with backslashes never reaches SH-mode quote/escape processing.  Must
# precede the first init(): init() snapshots %ENV into STORE and
# sync_to_env() later copies STORE back to %ENV.
{
    my ($pvol, $pdirs) = File::Spec->splitpath($^X);
    my $perldir = File::Spec->catpath($pvol, $pdirs, '');
    if (length $perldir) {
        my $sep = ($^O =~ /^(?:MSWin32|dos|os2)$/) ? ';' : ':';
        $ENV{'PATH'} = (defined($ENV{'PATH'}) && length($ENV{'PATH'}))
                     ? "$perldir$sep$ENV{'PATH'}" : $perldir;
    }
}
BATsh::Env::init();

# Hostile values that, with the old code, broke the generated Perl on
# Unix by leaking through /bin/sh's expansion of the environment "_".
my @HOSTILE = ('1EERDtQcrK', 'abc/test', 'bar/t', '9zz/b');

my @tests = (



( run in 1.448 second using v1.01-cache-2.11-cpan-364913b4093 )