This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
syntax check a directory: set the test process locale too
[perl5.git] / t / run / switches.t
CommitLineData
8a73d5dd
RGS
1#!./perl -w
2
feafb1eb 3# Tests for the command-line switches:
fed0ca7f 4# -0, -c, -l, -s, -m, -M, -V, -v, -h, -i, -E and all unknown
48eaf804 5# Some switches have their own tests, see MANIFEST.
8a73d5dd
RGS
6
7BEGIN {
8 chdir 't' if -d 't';
9 @INC = '../lib';
10}
11
768fd157 12BEGIN { require "./test.pl"; }
8a73d5dd 13
1044e8d2 14plan(tests => 115);
8a73d5dd 15
a09a0aa2 16use Config;
91514b78 17use Errno qw(EACCES EISDIR);
2e3d3246 18use POSIX qw(setlocale LC_ALL);
a09a0aa2 19
b734d6c9 20# due to a bug in VMS's piping which makes it impossible for runperl()
e54d2dfa
JH
21# to emulate echo -n (ie. stdin always winds up with a newline), these
22# tests almost totally fail.
b734d6c9
MS
23$TODO = "runperl() unable to emulate echo -n due to pipe bug" if $^O eq 'VMS';
24
8a73d5dd
RGS
25my $r;
26my @tmpfiles = ();
a29d0261 27END { unlink_all @tmpfiles }
8a73d5dd
RGS
28
29# Tests for -0
30
31$r = runperl(
32 switches => [ '-0', ],
33 stdin => 'foo\0bar\0baz\0',
34 prog => 'print qq(<$_>) while <>',
35);
36is( $r, "<foo\0><bar\0><baz\0>", "-0" );
37
38$r = runperl(
39 switches => [ '-l', '-0', '-p' ],
40 stdin => 'foo\0bar\0baz\0',
41 prog => '1',
42);
43is( $r, "foo\nbar\nbaz\n", "-0 after a -l" );
44
45$r = runperl(
46 switches => [ '-0', '-l', '-p' ],
47 stdin => 'foo\0bar\0baz\0',
48 prog => '1',
49);
50is( $r, "foo\0bar\0baz\0", "-0 before a -l" );
51
52$r = runperl(
53 switches => [ sprintf("-0%o", ord 'x') ],
54 stdin => 'fooxbarxbazx',
55 prog => 'print qq(<$_>) while <>',
56);
57is( $r, "<foox><barx><bazx>", "-0 with octal number" );
58
59$r = runperl(
60 switches => [ '-00', '-p' ],
61 stdin => 'abc\ndef\n\nghi\njkl\nmno\n\npq\n',
62 prog => 's/\n/-/g;$_.=q(/)',
63);
64is( $r, 'abc-def--/ghi-jkl-mno--/pq-/', '-00 (paragraph mode)' );
65
66$r = runperl(
67 switches => [ '-0777', '-p' ],
68 stdin => 'abc\ndef\n\nghi\njkl\nmno\n\npq\n',
69 prog => 's/\n/-/g;$_.=q(/)',
70);
71is( $r, 'abc-def--ghi-jkl-mno--pq-/', '-0777 (slurp mode)' );
72
7f9e821f
RGS
73$r = runperl(
74 switches => [ '-066' ],
0fbedf1d 75 prog => 'BEGIN { print qq{($/)} } print qq{[$/]}',
7f9e821f
RGS
76);
77is( $r, "(\066)[\066]", '$/ set at compile-time' );
78
8a73d5dd
RGS
79# Tests for -c
80
2d90ac95 81my $filename = tempfile();
8a73d5dd 82SKIP: {
b734d6c9
MS
83 local $TODO = ''; # this one works on VMS
84
8a73d5dd
RGS
85 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
86 print $f <<'SWTEST';
87BEGIN { print "block 1\n"; }
88CHECK { print "block 2\n"; }
89INIT { print "block 3\n"; }
90 print "block 4\n";
91END { print "block 5\n"; }
92SWTEST
d1e4d418 93 close $f or die "Could not close: $!";
8a73d5dd
RGS
94 $r = runperl(
95 switches => [ '-c' ],
96 progfile => $filename,
97 stderr => 1,
98 );
99 # Because of the stderr redirection, we can't tell reliably the order
100 # in which the output is given
101 ok(
102 $r =~ /$filename syntax OK/
103 && $r =~ /\bblock 1\b/
104 && $r =~ /\bblock 2\b/
105 && $r !~ /\bblock 3\b/
106 && $r !~ /\bblock 4\b/
107 && $r !~ /\bblock 5\b/,
108 '-c'
109 );
8a73d5dd
RGS
110}
111
1044e8d2
JK
112{
113 my $tempdir = tempfile;
114 mkdir $tempdir, 0700 or die "Can't mkdir '$tempdir': $!";
cdd06489 115
5e4d35e6
RS
116 local $ENV{'LC_ALL'} = 'C'; # Keep the test simple: expect English
117 local $ENV{LANGUAGE} = 'C';
2e3d3246 118 setlocale(LC_ALL, "C");
5e4d35e6 119
cdd06489
RS
120 # Win32 won't let us open the directory, so we never get to die with
121 # EISDIR, which happens after open.
91514b78 122 my $error = do { local $! = $^O eq 'MSWin32' ? EACCES : EISDIR; "$!" };
1044e8d2
JK
123 like(
124 runperl( switches => [ '-c' ], args => [ $tempdir ], stderr => 1),
5e4d35e6 125 qr/Can't open perl script.*$tempdir.*\Q$error/s,
1044e8d2
JK
126 "RT \#61362: Cannot syntax-check a directory"
127 );
128 rmdir $tempdir or die "Can't rmdir '$tempdir': $!";
129}
130
8a73d5dd
RGS
131# Tests for -l
132
133$r = runperl(
134 switches => [ sprintf("-l%o", ord 'x') ],
135 prog => 'print for qw/foo bar/'
136);
137is( $r, 'fooxbarx', '-l with octal number' );
138
139# Tests for -s
140
141$r = runperl(
142 switches => [ '-s' ],
143 prog => 'for (qw/abc def ghi/) {print defined $$_ ? $$_ : q(-)}',
144 args => [ '--', '-abc=2', '-def', ],
145);
146is( $r, '21-', '-s switch parsing' );
147
2d90ac95 148$filename = tempfile();
8a73d5dd
RGS
149SKIP: {
150 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
151 print $f <<'SWTEST';
59e235cb
GA
152#!perl -s
153BEGIN { print $x,$y; exit }
154SWTEST
155 close $f or die "Could not close: $!";
156 $r = runperl(
157 progfile => $filename,
158 args => [ '-x=foo -y' ],
159 );
160 is( $r, 'foo1', '-s on the shebang line' );
59e235cb
GA
161}
162
163# Bug ID 20011106.084
2d90ac95 164$filename = tempfile();
59e235cb
GA
165SKIP: {
166 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
167 print $f <<'SWTEST';
f0b2cf55
YST
168#!perl -sn
169BEGIN { print $x; exit }
8a73d5dd 170SWTEST
d1e4d418 171 close $f or die "Could not close: $!";
8a73d5dd 172 $r = runperl(
8a73d5dd
RGS
173 progfile => $filename,
174 args => [ '-x=foo' ],
175 );
59e235cb 176 is( $r, 'foo', '-sn on the shebang line' );
8a73d5dd
RGS
177}
178
179# Tests for -m and -M
180
2d90ac95
NC
181my $package = tempfile();
182$filename = "$package.pm";
8a73d5dd
RGS
183SKIP: {
184 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!",4 );
2d90ac95
NC
185 print $f <<"SWTESTPM";
186package $package;
187sub import { print map "<\$_>", \@_ }
8a73d5dd
RGS
1881;
189SWTESTPM
d1e4d418 190 close $f or die "Could not close: $!";
8a73d5dd 191 $r = runperl(
2d90ac95 192 switches => [ "-M$package" ],
8a73d5dd
RGS
193 prog => '1',
194 );
2d90ac95 195 is( $r, "<$package>", '-M' );
8a73d5dd 196 $r = runperl(
2d90ac95 197 switches => [ "-M$package=foo" ],
8a73d5dd
RGS
198 prog => '1',
199 );
2d90ac95 200 is( $r, "<$package><foo>", '-M with import parameter' );
8a73d5dd 201 $r = runperl(
2d90ac95 202 switches => [ "-m$package" ],
8a73d5dd
RGS
203 prog => '1',
204 );
b734d6c9
MS
205
206 {
207 local $TODO = ''; # this one works on VMS
208 is( $r, '', '-m' );
209 }
8a73d5dd 210 $r = runperl(
2d90ac95 211 switches => [ "-m$package=foo,bar" ],
8a73d5dd
RGS
212 prog => '1',
213 );
2d90ac95 214 is( $r, "<$package><foo><bar>", '-m with import parameters' );
8a73d5dd 215 push @tmpfiles, $filename;
0544e6df 216
3012b817
CB
217 {
218 local $TODO = ''; # these work on VMS
219
0544e6df
RB
220 is( runperl( switches => [ '-MTie::Hash' ], stderr => 1, prog => 1 ),
221 '', "-MFoo::Bar allowed" );
222
2d90ac95 223 like( runperl( switches => [ "-M:$package" ], stderr => 1,
3d7a9343 224 prog => 'die q{oops}' ),
0544e6df
RB
225 qr/Invalid module name [\w:]+ with -M option\b/,
226 "-M:Foo not allowed" );
227
228 like( runperl( switches => [ '-mA:B:C' ], stderr => 1,
3d7a9343 229 prog => 'die q{oops}' ),
0544e6df
RB
230 qr/Invalid module name [\w:]+ with -m option\b/,
231 "-mFoo:Bar not allowed" );
232
233 like( runperl( switches => [ '-m-A:B:C' ], stderr => 1,
3d7a9343 234 prog => 'die q{oops}' ),
0544e6df
RB
235 qr/Invalid module name [\w:]+ with -m option\b/,
236 "-m-Foo:Bar not allowed" );
237
238 like( runperl( switches => [ '-m-' ], stderr => 1,
3d7a9343 239 prog => 'die q{oops}' ),
0544e6df
RB
240 qr/Module name required with -m option\b/,
241 "-m- not allowed" );
242
243 like( runperl( switches => [ '-M-=' ], stderr => 1,
3d7a9343 244 prog => 'die q{oops}' ),
0544e6df
RB
245 qr/Module name required with -M option\b/,
246 "-M- not allowed" );
3012b817 247 } # disable TODO on VMS
8a73d5dd 248}
bda1f081
FC
249is runperl(stderr => 1, prog => '#!perl -m'),
250 qq 'Too late for "-m" option at -e line 1.\n', '#!perl -m';
251is runperl(stderr => 1, prog => '#!perl -M'),
252 qq 'Too late for "-M" option at -e line 1.\n', '#!perl -M';
e54d2dfa
JH
253
254# Tests for -V
255
256{
257 local $TODO = ''; # these ones should work on VMS
258
259 # basic perl -V should generate significant output.
ceda46a1 260 # we don't test actual format too much since it could change
e54d2dfa
JH
261 like( runperl( switches => ['-V'] ), qr/(\n.*){20}/,
262 '-V generates 20+ lines' );
263
ceda46a1
JH
264 like( runperl( switches => ['-V'] ),
265 qr/\ASummary of my perl5 .*configuration:/,
266 '-V looks okay' );
267
e54d2dfa
JH
268 # lookup a known config var
269 chomp( $r=runperl( switches => ['-V:osname'] ) );
270 is( $r, "osname='$^O';", 'perl -V:osname');
271
272 # lookup a nonexistent var
273 chomp( $r=runperl( switches => ['-V:this_var_makes_switches_test_fail'] ) );
274 is( $r, "this_var_makes_switches_test_fail='UNKNOWN';",
275 'perl -V:unknown var');
276
277 # regexp lookup
278 # platforms that don't like this quoting can either skip this test
279 # or fix test.pl _quote_args
280 $r = runperl( switches => ['"-V:i\D+size"'] );
281 # should be unlike( $r, qr/^$|not found|UNKNOWN/ );
282 like( $r, qr/^(?!.*(not found|UNKNOWN))./, 'perl -V:re got a result' );
283
284 # make sure each line we got matches the re
285 ok( !( grep !/^i\D+size=/, split /^/, $r ), '-V:re correct' );
286}
ceda46a1
JH
287
288# Tests for -v
289
290{
291 local $TODO = ''; # these ones should work on VMS
46807d8e
YO
292 # there are definitely known build configs where this test will fail
293 # DG/UX comes to mind. Maybe we should remove these special cases?
ceda46a1 294 my $v = sprintf "%vd", $^V;
ded326e4
DG
295 my $ver = $Config{PERL_VERSION};
296 my $rel = $Config{PERL_SUBVERSION};
ceda46a1 297 like( runperl( switches => ['-v'] ),
ded326e4 298 qr/This is perl 5, version \Q$ver\E, subversion \Q$rel\E \(v\Q$v\E(?:[-*\w]+| \([^)]+\))?\) built for \Q$Config{archname}\E.+Copyright.+Larry Wall.+Artistic License.+GNU General Public License/s,
ceda46a1
JH
299 '-v looks okay' );
300
301}
b8e3af44
JH
302
303# Tests for -h
304
305{
306 local $TODO = ''; # these ones should work on VMS
307
308 like( runperl( switches => ['-h'] ),
a09a0aa2 309 qr/Usage: .+(?i:perl(?:$Config{_exe})?).+switches.+programfile.+arguments/,
b8e3af44
JH
310 '-h looks okay' );
311
312}
313
fed0ca7f 314# Tests for switches which do not exist
b8e3af44 315
96889982 316foreach my $switch (split //, "ABbGgHJjKkLNOoPQqRrYyZz123456789_")
b8e3af44
JH
317{
318 local $TODO = ''; # these ones should work on VMS
319
fed0ca7f 320 like( runperl( switches => ["-$switch"], stderr => 1,
3d7a9343 321 prog => 'die q{oops}' ),
fed0ca7f
NC
322 qr/\QUnrecognized switch: -$switch (-h will show valid options)./,
323 "-$switch correctly unknown" );
b8e3af44 324
b7e077d0
FC
325 # [perl #104288]
326 like( runperl( stderr => 1, prog => "#!perl -$switch" ),
327 qr/^Unrecognized switch: -$switch \(-h will show valid (?x:
328 )options\) at -e line 1\./,
329 "-$switch unrecognised on #! line" );
330}
331
332# Tests for unshebangable switches
333for (qw( e f x E S V )) {
334 $r = runperl(
335 stderr => 1,
336 prog => "#!perl -$_",
337 );
338 is $r, "Can't emulate -$_ on #! line at -e line 1.\n","-$_ on #! line";
b8e3af44 339}
feafb1eb
JH
340
341# Tests for -i
342
343{
344 local $TODO = ''; # these ones should work on VMS
345
a29d0261 346 sub do_i_unlink { unlink_all("file", "file.bak") }
feafb1eb
JH
347
348 open(FILE, ">file") or die "$0: Failed to create 'file': $!";
349 print FILE <<__EOF__;
350foo yada dada
351bada foo bing
352king kong foo
353__EOF__
354 close FILE;
355
356 END { do_i_unlink() }
357
358 runperl( switches => ['-pi.bak'], prog => 's/foo/bar/', args => ['file'] );
359
360 open(FILE, "file") or die "$0: Failed to open 'file': $!";
361 chomp(my @file = <FILE>);
362 close FILE;
363
364 open(BAK, "file.bak") or die "$0: Failed to open 'file': $!";
365 chomp(my @bak = <BAK>);
366 close BAK;
367
368 is(join(":", @file),
369 "bar yada dada:bada bar bing:king kong bar",
370 "-i new file");
371 is(join(":", @bak),
372 "foo yada dada:bada foo bing:king kong foo",
373 "-i backup file");
82f96200
JL
374
375 my $out1 = runperl(
376 switches => ['-i.bak -p'],
377 prog => 'exit',
378 stderr => 1,
379 stdin => "1\n",
380 );
381 is(
382 $out1,
383 "-i used with no filenames on the command line, reading from STDIN.\n",
384 "warning when no files given"
385 );
386 my $out2 = runperl(
387 switches => ['-i.bak -p'],
388 prog => 'exit',
389 stderr => 1,
390 stdin => "1\n",
391 args => ['file'],
392 );
393 is($out2, "", "no warning when files given");
feafb1eb 394}
bc9b29db
RH
395
396# Tests for -E
397
3012b817
CB
398$TODO = ''; # the -E tests work on VMS
399
bc9b29db
RH
400$r = runperl(
401 switches => [ '-E', '"say q(Hello, world!)"']
402);
403is( $r, "Hello, world!\n", "-E say" );
404
405
406$r = runperl(
bc9b29db
RH
407 switches => [ '-E', '"undef ~~ undef and say q(Hello, world!)"']
408);
409is( $r, "Hello, world!\n", "-E ~~" );
410
411$r = runperl(
412 switches => [ '-E', '"given(undef) {when(undef) { say q(Hello, world!)"}}']
413);
414is( $r, "Hello, world!\n", "-E given" );
9f639728
FR
415
416$r = runperl(
f21873d1 417 switches => [ '-nE', q("} END { say q/affe/") ],
9f639728
FR
418 stdin => 'zomtek',
419);
420is( $r, "affe\n", '-E works outside of the block created by -n' );
d3133c89 421
53eb19dd
SM
422$r = runperl(
423 switches => [ '-E', q("*{'bar'} = sub{}; print 'Hello, world!',qq|\n|;")]
424);
425is( $r, "Hello, world!\n", "-E does not enable strictures" );
426
d3133c89
NC
427# RT #30660
428
429$filename = tempfile();
430SKIP: {
431 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
432 print $f <<'SWTEST';
433#!perl -w -iok
434print "$^I\n";
435SWTEST
436 close $f or die "Could not close: $!";
437 $r = runperl(
438 progfile => $filename,
439 );
440 like( $r, qr/ok/, 'Spaces on the #! line (#30660)' );
441}