1#!/usr/bin/perl 2 3use strict; 4use warnings FATAL => 'all'; 5use English; 6 7use Config; 8use Test::More; 9use ExtUtils::PL2Bat; 10use Cwd qw/cwd/; 11 12my @test_vals = ( 0, 1, 2, 3, -1, -2, 65535, 65536, 65537, 47, 100, 200, 255, 256, 257, 258, 511, 512, 513, -255, -256, -20012001 ); 13 14plan($OSNAME eq 'MSWin32' ? ( tests => (($#test_vals+1)*5)+2 ) : ( skip_all => 'Only usable on Windows' )); 15 16# the method of execution of the test script is geared to cmd.exe so ensure 17# this is used in case the user have some non-standard shell. 18# E.g. TCC/4NT doesn't quite handle the invocation correctly producing errors. 19$ENV{COMSPEC} = "$ENV{SystemRoot}\\System32\\cmd.exe"; 20 21my $perl_in_fname = 'test_perl_source'; 22 23open my $out, '>', $perl_in_fname or die qq{Couldn't create source file ("$perl_in_fname"): $!}; 24print $out "#! perl -w\nexit \$ARGV[0];\n"; 25close $out; 26 27pl2bat(in => $perl_in_fname); 28 29my $batch_out_fname = $perl_in_fname.'.bat'; 30 31ok (-e "$batch_out_fname", qq{Executable file exists ("$batch_out_fname")}); 32 33my $int_max_8bit = 2**8; 34my $int_max_16bit = 2**16; 35 36my $path_with_cwd = construct_test_PATH(); 37 38foreach my $input_val ( @test_vals ) { 39 local $ENV{PATH} = $path_with_cwd; 40 my $qx_output = q//; 41 my $qx_retval = 0; 42 my $error_level = 0; 43 my $status = q//; 44 my $success = 1; 45 46 $success &&= eval { $qx_output = qx{"$batch_out_fname" $input_val}; $qx_retval = $CHILD_ERROR; $qx_retval != -1; }; 47 $qx_retval = ( $qx_retval > 0 ) ? ( $qx_retval >> 8 ) : $qx_retval; 48 49 $success &&= eval { $error_level = qx{"$batch_out_fname" $input_val & call echo ^%ERRORLEVEL^%}; 1; }; 50 $error_level =~ s/\r?\n$//msx; 51 52 $success &&= eval { $status = qx{"$batch_out_fname" $input_val && (echo PROCESS_SUCCESS) || (echo PROCESS_FAILURE)}; 1; }; 53 $status =~ s/\r?\n$//msx; 54 55 # (for qx/.../) post-call status values ($CHILD_ERROR) can be [ 0 ... 255 ]; values outside that range will be returned as `value % 256` 56 my $expected_qx_retval = ($input_val % $int_max_8bit); 57 58 # `exit $value` will set ERRORLEVEL to $value for values of [ -1, 0 ... 65535 ]; values outside that range will set ERRORLEVEL to `$value % 65536` 59 my $expected_error_level = ($input_val == -1) ? -1 : ($input_val % $int_max_16bit); 60 61 is $success, 1, qq{`"$batch_out_fname" $input_val` executed successfully}; 62 is $qx_output, q//, qq{qx/"$batch_out_fname" $input_val/ returns expected empty output}; # assure no extraneous output from BAT wrap 63 is $qx_retval, $expected_qx_retval, qq{qx/"$batch_out_fname" $input_val/ returns expected $CHILD_ERROR ($expected_qx_retval)}; 64 is $error_level, $expected_error_level, qq{"$batch_out_fname": `exit $input_val` set expected ERRORLEVEL ($expected_error_level)}; 65 is $status, (($input_val % $int_max_16bit) == 0) ? 'PROCESS_SUCCESS' : 'PROCESS_FAILURE', qq{`"$batch_out_fname" $input_val` process exit ($status) is correct}; 66} 67 68unlink $perl_in_fname, $batch_out_fname; 69 70# the test needs CWD in PATH to check the created .bat files, but under win2k 71# PATH must not be too long. so to keep any win2k smokers happy, we construct 72# a new PATH that contains the dirs which hold cmd.exe, perl.exe, and CWD 73 74sub construct_test_PATH { 75 my $perl_path = $^X; 76 my $cmd_path = $ENV{ComSpec} || `where cmd`; # where doesn't seem to work on all windows versions 77 $_ =~ s/[\\\/][^\\\/]+$// for $perl_path, $cmd_path; # strip executable names 78 79 my @path_fallbacks = grep /\Q$ENV{SystemRoot}\E|system32|winnt|windows/i, split $Config{path_sep}, $ENV{PATH}; 80 81 my $path_with_cwd = join $Config{path_sep}, @path_fallbacks, $cmd_path, $perl_path, cwd(); 82 83 my ($perl) = ( $^X =~ /[\\\/]([^\\]+)$/ ); # in case the perl executable name differs 84 note "using perl executable name: $perl"; 85 86 local $ENV{PATH} = $path_with_cwd; 87 my $test_out = `$perl -e 1 2>&1`; 88 is $test_out, "", "perl execution with temp path works" 89 or diag "make_executable.t tmp path: $path_with_cwd"; 90 diag "make_executable.t PATH likely did not contain cmd.exe" 91 if !defined $test_out; 92 93 return $path_with_cwd; 94} 95