1#!./perl -w 2 3# Tests for the command-line switches: 4# -0, -c, -l, -s, -m, -M, -V, -v, -h, -z, -i 5# Some switches have their own tests, see MANIFEST. 6 7BEGIN { 8 chdir 't' if -d 't'; 9 @INC = '../lib'; 10} 11 12require "./test.pl"; 13 14plan(tests => 26); 15 16use Config; 17 18# due to a bug in VMS's piping which makes it impossible for runperl() 19# to emulate echo -n (ie. stdin always winds up with a newline), these 20# tests almost totally fail. 21$TODO = "runperl() unable to emulate echo -n due to pipe bug" if $^O eq 'VMS'; 22 23my $r; 24my @tmpfiles = (); 25END { unlink @tmpfiles } 26 27# Tests for -0 28 29$r = runperl( 30 switches => [ '-0', ], 31 stdin => 'foo\0bar\0baz\0', 32 prog => 'print qq(<$_>) while <>', 33); 34is( $r, "<foo\0><bar\0><baz\0>", "-0" ); 35 36$r = runperl( 37 switches => [ '-l', '-0', '-p' ], 38 stdin => 'foo\0bar\0baz\0', 39 prog => '1', 40); 41is( $r, "foo\nbar\nbaz\n", "-0 after a -l" ); 42 43$r = runperl( 44 switches => [ '-0', '-l', '-p' ], 45 stdin => 'foo\0bar\0baz\0', 46 prog => '1', 47); 48is( $r, "foo\0bar\0baz\0", "-0 before a -l" ); 49 50$r = runperl( 51 switches => [ sprintf("-0%o", ord 'x') ], 52 stdin => 'fooxbarxbazx', 53 prog => 'print qq(<$_>) while <>', 54); 55is( $r, "<foox><barx><bazx>", "-0 with octal number" ); 56 57$r = runperl( 58 switches => [ '-00', '-p' ], 59 stdin => 'abc\ndef\n\nghi\njkl\nmno\n\npq\n', 60 prog => 's/\n/-/g;$_.=q(/)', 61); 62is( $r, 'abc-def--/ghi-jkl-mno--/pq-/', '-00 (paragraph mode)' ); 63 64$r = runperl( 65 switches => [ '-0777', '-p' ], 66 stdin => 'abc\ndef\n\nghi\njkl\nmno\n\npq\n', 67 prog => 's/\n/-/g;$_.=q(/)', 68); 69is( $r, 'abc-def--ghi-jkl-mno--pq-/', '-0777 (slurp mode)' ); 70 71$r = runperl( 72 switches => [ '-066' ], 73 prog => 'BEGIN { print qq{($/)} } print qq{[$/]}', 74); 75is( $r, "(\066)[\066]", '$/ set at compile-time' ); 76 77# Tests for -c 78 79my $filename = 'swctest.tmp'; 80SKIP: { 81 local $TODO = ''; # this one works on VMS 82 83 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" ); 84 print $f <<'SWTEST'; 85BEGIN { print "block 1\n"; } 86CHECK { print "block 2\n"; } 87INIT { print "block 3\n"; } 88 print "block 4\n"; 89END { print "block 5\n"; } 90SWTEST 91 close $f or die "Could not close: $!"; 92 $r = runperl( 93 switches => [ '-c' ], 94 progfile => $filename, 95 stderr => 1, 96 ); 97 # Because of the stderr redirection, we can't tell reliably the order 98 # in which the output is given 99 ok( 100 $r =~ /$filename syntax OK/ 101 && $r =~ /\bblock 1\b/ 102 && $r =~ /\bblock 2\b/ 103 && $r !~ /\bblock 3\b/ 104 && $r !~ /\bblock 4\b/ 105 && $r !~ /\bblock 5\b/, 106 '-c' 107 ); 108 push @tmpfiles, $filename; 109} 110 111# Tests for -l 112 113$r = runperl( 114 switches => [ sprintf("-l%o", ord 'x') ], 115 prog => 'print for qw/foo bar/' 116); 117is( $r, 'fooxbarx', '-l with octal number' ); 118 119# Tests for -s 120 121$r = runperl( 122 switches => [ '-s' ], 123 prog => 'for (qw/abc def ghi/) {print defined $$_ ? $$_ : q(-)}', 124 args => [ '--', '-abc=2', '-def', ], 125); 126is( $r, '21-', '-s switch parsing' ); 127 128# Bug ID 20011106.084 129$filename = 'swstest.tmp'; 130SKIP: { 131 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" ); 132 print $f <<'SWTEST'; 133#!perl -sn 134BEGIN { print $x; exit } 135SWTEST 136 close $f or die "Could not close: $!"; 137 $r = runperl( 138 progfile => $filename, 139 args => [ '-x=foo' ], 140 ); 141 is( $r, 'foo', '-s on the shebang line' ); 142 push @tmpfiles, $filename; 143} 144 145# Tests for -m and -M 146 147$filename = 'swtest.pm'; 148SKIP: { 149 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!",4 ); 150 print $f <<'SWTESTPM'; 151package swtest; 152sub import { print map "<$_>", @_ } 1531; 154SWTESTPM 155 close $f or die "Could not close: $!"; 156 $r = runperl( 157 switches => [ '-Mswtest' ], 158 prog => '1', 159 ); 160 is( $r, '<swtest>', '-M' ); 161 $r = runperl( 162 switches => [ '-Mswtest=foo' ], 163 prog => '1', 164 ); 165 is( $r, '<swtest><foo>', '-M with import parameter' ); 166 $r = runperl( 167 switches => [ '-mswtest' ], 168 prog => '1', 169 ); 170 171 { 172 local $TODO = ''; # this one works on VMS 173 is( $r, '', '-m' ); 174 } 175 $r = runperl( 176 switches => [ '-mswtest=foo,bar' ], 177 prog => '1', 178 ); 179 is( $r, '<swtest><foo><bar>', '-m with import parameters' ); 180 push @tmpfiles, $filename; 181} 182 183# Tests for -V 184 185{ 186 local $TODO = ''; # these ones should work on VMS 187 188 # basic perl -V should generate significant output. 189 # we don't test actual format too much since it could change 190 like( runperl( switches => ['-V'] ), qr/(\n.*){20}/, 191 '-V generates 20+ lines' ); 192 193 like( runperl( switches => ['-V'] ), 194 qr/\ASummary of my perl5 .*configuration:/, 195 '-V looks okay' ); 196 197 # lookup a known config var 198 chomp( $r=runperl( switches => ['-V:osname'] ) ); 199 is( $r, "osname='$^O';", 'perl -V:osname'); 200 201 # lookup a nonexistent var 202 chomp( $r=runperl( switches => ['-V:this_var_makes_switches_test_fail'] ) ); 203 is( $r, "this_var_makes_switches_test_fail='UNKNOWN';", 204 'perl -V:unknown var'); 205 206 # regexp lookup 207 # platforms that don't like this quoting can either skip this test 208 # or fix test.pl _quote_args 209 $r = runperl( switches => ['"-V:i\D+size"'] ); 210 # should be unlike( $r, qr/^$|not found|UNKNOWN/ ); 211 like( $r, qr/^(?!.*(not found|UNKNOWN))./, 'perl -V:re got a result' ); 212 213 # make sure each line we got matches the re 214 ok( !( grep !/^i\D+size=/, split /^/, $r ), '-V:re correct' ); 215} 216 217# Tests for -v 218 219{ 220 local $TODO = ''; # these ones should work on VMS 221 222 my $v = sprintf "%vd", $^V; 223 like( runperl( switches => ['-v'] ), 224 qr/This is perl, v$v built for $Config{archname}.+Copyright.+Larry Wall.+Artistic License.+GNU General Public License/s, 225 '-v looks okay' ); 226 227} 228 229# Tests for -h 230 231{ 232 local $TODO = ''; # these ones should work on VMS 233 234 like( runperl( switches => ['-h'] ), 235 qr/Usage: .+(?i:perl(?:$Config{_exe})?).+switches.+programfile.+arguments/, 236 '-h looks okay' ); 237 238} 239 240# Tests for -z (which does not exist) 241 242{ 243 local $TODO = ''; # these ones should work on VMS 244 245 like( runperl( switches => ['-z'], stderr => 1 ), 246 qr/\QUnrecognized switch: -z (-h will show valid options)./, 247 '-z correctly unknown' ); 248 249} 250 251# Tests for -i 252 253{ 254 local $TODO = ''; # these ones should work on VMS 255 256 sub do_i_unlink { 1 while unlink("file", "file.bak") } 257 258 open(FILE, ">file") or die "$0: Failed to create 'file': $!"; 259 print FILE <<__EOF__; 260foo yada dada 261bada foo bing 262king kong foo 263__EOF__ 264 close FILE; 265 266 END { do_i_unlink() } 267 268 runperl( switches => ['-pi.bak'], prog => 's/foo/bar/', args => ['file'] ); 269 270 open(FILE, "file") or die "$0: Failed to open 'file': $!"; 271 chomp(my @file = <FILE>); 272 close FILE; 273 274 open(BAK, "file.bak") or die "$0: Failed to open 'file': $!"; 275 chomp(my @bak = <BAK>); 276 close BAK; 277 278 is(join(":", @file), 279 "bar yada dada:bada bar bing:king kong bar", 280 "-i new file"); 281 is(join(":", @bak), 282 "foo yada dada:bada foo bing:king kong foo", 283 "-i backup file"); 284} 285