This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Alternate backup file rename test for VMS.
[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';
7620c903 10 require Config; import Config;
8a73d5dd
RGS
11}
12
629eeaee 13BEGIN { require "./test.pl"; require "./loc_tools.pl"; }
8a73d5dd 14
7896dde7 15plan(tests => 137);
8a73d5dd 16
a09a0aa2
JH
17use Config;
18
b734d6c9 19# due to a bug in VMS's piping which makes it impossible for runperl()
e54d2dfa
JH
20# to emulate echo -n (ie. stdin always winds up with a newline), these
21# tests almost totally fail.
b734d6c9
MS
22$TODO = "runperl() unable to emulate echo -n due to pipe bug" if $^O eq 'VMS';
23
8a73d5dd
RGS
24my $r;
25my @tmpfiles = ();
a29d0261 26END { unlink_all @tmpfiles }
8a73d5dd
RGS
27
28# Tests for -0
29
30$r = runperl(
31 switches => [ '-0', ],
32 stdin => 'foo\0bar\0baz\0',
33 prog => 'print qq(<$_>) while <>',
34);
35is( $r, "<foo\0><bar\0><baz\0>", "-0" );
36
37$r = runperl(
38 switches => [ '-l', '-0', '-p' ],
39 stdin => 'foo\0bar\0baz\0',
40 prog => '1',
41);
42is( $r, "foo\nbar\nbaz\n", "-0 after a -l" );
43
44$r = runperl(
45 switches => [ '-0', '-l', '-p' ],
46 stdin => 'foo\0bar\0baz\0',
47 prog => '1',
48);
49is( $r, "foo\0bar\0baz\0", "-0 before a -l" );
50
51$r = runperl(
52 switches => [ sprintf("-0%o", ord 'x') ],
53 stdin => 'fooxbarxbazx',
54 prog => 'print qq(<$_>) while <>',
55);
56is( $r, "<foox><barx><bazx>", "-0 with octal number" );
57
58$r = runperl(
59 switches => [ '-00', '-p' ],
60 stdin => 'abc\ndef\n\nghi\njkl\nmno\n\npq\n',
61 prog => 's/\n/-/g;$_.=q(/)',
62);
63is( $r, 'abc-def--/ghi-jkl-mno--/pq-/', '-00 (paragraph mode)' );
64
65$r = runperl(
66 switches => [ '-0777', '-p' ],
67 stdin => 'abc\ndef\n\nghi\njkl\nmno\n\npq\n',
68 prog => 's/\n/-/g;$_.=q(/)',
69);
70is( $r, 'abc-def--ghi-jkl-mno--pq-/', '-0777 (slurp mode)' );
71
7f9e821f
RGS
72$r = runperl(
73 switches => [ '-066' ],
0fbedf1d 74 prog => 'BEGIN { print qq{($/)} } print qq{[$/]}',
7f9e821f
RGS
75);
76is( $r, "(\066)[\066]", '$/ set at compile-time' );
77
8a73d5dd
RGS
78# Tests for -c
79
2d90ac95 80my $filename = tempfile();
8a73d5dd 81SKIP: {
b734d6c9
MS
82 local $TODO = ''; # this one works on VMS
83
8a73d5dd
RGS
84 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
85 print $f <<'SWTEST';
86BEGIN { print "block 1\n"; }
87CHECK { print "block 2\n"; }
88INIT { print "block 3\n"; }
89 print "block 4\n";
90END { print "block 5\n"; }
91SWTEST
d1e4d418 92 close $f or die "Could not close: $!";
8a73d5dd
RGS
93 $r = runperl(
94 switches => [ '-c' ],
95 progfile => $filename,
96 stderr => 1,
97 );
98 # Because of the stderr redirection, we can't tell reliably the order
99 # in which the output is given
100 ok(
101 $r =~ /$filename syntax OK/
102 && $r =~ /\bblock 1\b/
103 && $r =~ /\bblock 2\b/
104 && $r !~ /\bblock 3\b/
105 && $r !~ /\bblock 4\b/
106 && $r !~ /\bblock 5\b/,
107 '-c'
108 );
8a73d5dd
RGS
109}
110
ae7bdbc5 111SKIP: {
629eeaee 112 skip 'locales not available', 1 unless locales_enabled('LC_ALL');
ae7bdbc5 113
1044e8d2
JK
114 my $tempdir = tempfile;
115 mkdir $tempdir, 0700 or die "Can't mkdir '$tempdir': $!";
cdd06489 116
5e4d35e6
RS
117 local $ENV{'LC_ALL'} = 'C'; # Keep the test simple: expect English
118 local $ENV{LANGUAGE} = 'C';
2e3d3246 119 setlocale(LC_ALL, "C");
5e4d35e6 120
cdd06489
RS
121 # Win32 won't let us open the directory, so we never get to die with
122 # EISDIR, which happens after open.
dbab6833
FC
123 require Errno;
124 import Errno qw(EACCES EISDIR);
125 my $error = do {
126 local $! = $^O eq 'MSWin32' ? &EACCES : &EISDIR; "$!"
127 };
1044e8d2
JK
128 like(
129 runperl( switches => [ '-c' ], args => [ $tempdir ], stderr => 1),
5e4d35e6 130 qr/Can't open perl script.*$tempdir.*\Q$error/s,
1044e8d2
JK
131 "RT \#61362: Cannot syntax-check a directory"
132 );
133 rmdir $tempdir or die "Can't rmdir '$tempdir': $!";
134}
135
8a73d5dd
RGS
136# Tests for -l
137
138$r = runperl(
139 switches => [ sprintf("-l%o", ord 'x') ],
140 prog => 'print for qw/foo bar/'
141);
142is( $r, 'fooxbarx', '-l with octal number' );
143
144# Tests for -s
145
146$r = runperl(
147 switches => [ '-s' ],
148 prog => 'for (qw/abc def ghi/) {print defined $$_ ? $$_ : q(-)}',
149 args => [ '--', '-abc=2', '-def', ],
150);
151is( $r, '21-', '-s switch parsing' );
152
2d90ac95 153$filename = tempfile();
8a73d5dd
RGS
154SKIP: {
155 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
156 print $f <<'SWTEST';
59e235cb
GA
157#!perl -s
158BEGIN { print $x,$y; exit }
159SWTEST
160 close $f or die "Could not close: $!";
161 $r = runperl(
162 progfile => $filename,
163 args => [ '-x=foo -y' ],
164 );
165 is( $r, 'foo1', '-s on the shebang line' );
59e235cb
GA
166}
167
ee95e30c 168# Bug ID 20011106.084 (#7876)
2d90ac95 169$filename = tempfile();
59e235cb
GA
170SKIP: {
171 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
172 print $f <<'SWTEST';
f0b2cf55
YST
173#!perl -sn
174BEGIN { print $x; exit }
8a73d5dd 175SWTEST
d1e4d418 176 close $f or die "Could not close: $!";
8a73d5dd 177 $r = runperl(
8a73d5dd
RGS
178 progfile => $filename,
179 args => [ '-x=foo' ],
180 );
59e235cb 181 is( $r, 'foo', '-sn on the shebang line' );
8a73d5dd
RGS
182}
183
184# Tests for -m and -M
185
2d90ac95
NC
186my $package = tempfile();
187$filename = "$package.pm";
8a73d5dd
RGS
188SKIP: {
189 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!",4 );
2d90ac95
NC
190 print $f <<"SWTESTPM";
191package $package;
192sub import { print map "<\$_>", \@_ }
8a73d5dd
RGS
1931;
194SWTESTPM
d1e4d418 195 close $f or die "Could not close: $!";
8a73d5dd 196 $r = runperl(
3d7c117d 197 switches => [ "-I.", "-M$package" ],
8a73d5dd
RGS
198 prog => '1',
199 );
2d90ac95 200 is( $r, "<$package>", '-M' );
8a73d5dd 201 $r = runperl(
3d7c117d 202 switches => [ "-I.", "-M$package=foo" ],
8a73d5dd
RGS
203 prog => '1',
204 );
2d90ac95 205 is( $r, "<$package><foo>", '-M with import parameter' );
8a73d5dd 206 $r = runperl(
2d90ac95 207 switches => [ "-m$package" ],
8a73d5dd
RGS
208 prog => '1',
209 );
b734d6c9
MS
210
211 {
212 local $TODO = ''; # this one works on VMS
213 is( $r, '', '-m' );
214 }
8a73d5dd 215 $r = runperl(
3d7c117d 216 switches => [ "-I.", "-m$package=foo,bar" ],
8a73d5dd
RGS
217 prog => '1',
218 );
2d90ac95 219 is( $r, "<$package><foo><bar>", '-m with import parameters' );
8a73d5dd 220 push @tmpfiles, $filename;
0544e6df 221
3012b817
CB
222 {
223 local $TODO = ''; # these work on VMS
224
0544e6df
RB
225 is( runperl( switches => [ '-MTie::Hash' ], stderr => 1, prog => 1 ),
226 '', "-MFoo::Bar allowed" );
227
2d90ac95 228 like( runperl( switches => [ "-M:$package" ], stderr => 1,
3d7a9343 229 prog => 'die q{oops}' ),
0544e6df
RB
230 qr/Invalid module name [\w:]+ with -M option\b/,
231 "-M:Foo not allowed" );
232
233 like( runperl( switches => [ '-mA:B:C' ], stderr => 1,
3d7a9343 234 prog => 'die q{oops}' ),
0544e6df
RB
235 qr/Invalid module name [\w:]+ with -m option\b/,
236 "-mFoo:Bar not allowed" );
237
238 like( runperl( switches => [ '-m-A:B:C' ], stderr => 1,
3d7a9343 239 prog => 'die q{oops}' ),
0544e6df
RB
240 qr/Invalid module name [\w:]+ with -m option\b/,
241 "-m-Foo:Bar 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" );
247
248 like( runperl( switches => [ '-M-=' ], stderr => 1,
3d7a9343 249 prog => 'die q{oops}' ),
0544e6df
RB
250 qr/Module name required with -M option\b/,
251 "-M- not allowed" );
3012b817 252 } # disable TODO on VMS
8a73d5dd 253}
bda1f081
FC
254is runperl(stderr => 1, prog => '#!perl -m'),
255 qq 'Too late for "-m" option at -e line 1.\n', '#!perl -m';
256is runperl(stderr => 1, prog => '#!perl -M'),
257 qq 'Too late for "-M" option at -e line 1.\n', '#!perl -M';
e54d2dfa
JH
258
259# Tests for -V
260
261{
262 local $TODO = ''; # these ones should work on VMS
263
264 # basic perl -V should generate significant output.
ceda46a1 265 # we don't test actual format too much since it could change
e54d2dfa
JH
266 like( runperl( switches => ['-V'] ), qr/(\n.*){20}/,
267 '-V generates 20+ lines' );
268
ceda46a1
JH
269 like( runperl( switches => ['-V'] ),
270 qr/\ASummary of my perl5 .*configuration:/,
271 '-V looks okay' );
272
e54d2dfa
JH
273 # lookup a known config var
274 chomp( $r=runperl( switches => ['-V:osname'] ) );
275 is( $r, "osname='$^O';", 'perl -V:osname');
276
277 # lookup a nonexistent var
278 chomp( $r=runperl( switches => ['-V:this_var_makes_switches_test_fail'] ) );
279 is( $r, "this_var_makes_switches_test_fail='UNKNOWN';",
280 'perl -V:unknown var');
281
282 # regexp lookup
283 # platforms that don't like this quoting can either skip this test
284 # or fix test.pl _quote_args
285 $r = runperl( switches => ['"-V:i\D+size"'] );
286 # should be unlike( $r, qr/^$|not found|UNKNOWN/ );
287 like( $r, qr/^(?!.*(not found|UNKNOWN))./, 'perl -V:re got a result' );
288
289 # make sure each line we got matches the re
290 ok( !( grep !/^i\D+size=/, split /^/, $r ), '-V:re correct' );
291}
ceda46a1
JH
292
293# Tests for -v
294
295{
296 local $TODO = ''; # these ones should work on VMS
e7ce59ca
AC
297 # There may be build configs where this test will fail; DG/UX was one,
298 # but we no longer support it. Maybe we should remove these special cases?
a64c093f
TC
299 SKIP:
300 {
301 skip "Win32 miniperl produces a default archname in -v", 1
302 if $^O eq 'MSWin32' && is_miniperl;
303 my $v = sprintf "%vd", $^V;
304 my $ver = $Config{PERL_VERSION};
305 my $rel = $Config{PERL_SUBVERSION};
306 like( runperl( switches => ['-v'] ),
307 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,
308 '-v looks okay' );
309 }
ceda46a1 310}
b8e3af44
JH
311
312# Tests for -h
313
314{
315 local $TODO = ''; # these ones should work on VMS
316
317 like( runperl( switches => ['-h'] ),
a09a0aa2 318 qr/Usage: .+(?i:perl(?:$Config{_exe})?).+switches.+programfile.+arguments/,
b8e3af44
JH
319 '-h looks okay' );
320
321}
322
fed0ca7f 323# Tests for switches which do not exist
b8e3af44 324
96889982 325foreach my $switch (split //, "ABbGgHJjKkLNOoPQqRrYyZz123456789_")
b8e3af44
JH
326{
327 local $TODO = ''; # these ones should work on VMS
328
fed0ca7f 329 like( runperl( switches => ["-$switch"], stderr => 1,
3d7a9343 330 prog => 'die q{oops}' ),
fed0ca7f
NC
331 qr/\QUnrecognized switch: -$switch (-h will show valid options)./,
332 "-$switch correctly unknown" );
b8e3af44 333
b7e077d0
FC
334 # [perl #104288]
335 like( runperl( stderr => 1, prog => "#!perl -$switch" ),
336 qr/^Unrecognized switch: -$switch \(-h will show valid (?x:
337 )options\) at -e line 1\./,
338 "-$switch unrecognised on #! line" );
339}
340
341# Tests for unshebangable switches
342for (qw( e f x E S V )) {
343 $r = runperl(
344 stderr => 1,
345 prog => "#!perl -$_",
346 );
347 is $r, "Can't emulate -$_ on #! line at -e line 1.\n","-$_ on #! line";
b8e3af44 348}
feafb1eb
JH
349
350# Tests for -i
351
8200f806 352SKIP:
feafb1eb
JH
353{
354 local $TODO = ''; # these ones should work on VMS
355
c0a22fcc 356 sub do_i_unlink { unlink_all("tmpswitches", "tmpswitches.bak") }
feafb1eb 357
c0a22fcc 358 open(FILE, ">tmpswitches") or die "$0: Failed to create 'tmpswitches': $!";
929344c0 359 my $yada = <<__EOF__;
feafb1eb
JH
360foo yada dada
361bada foo bing
362king kong foo
363__EOF__
929344c0 364 print FILE $yada;
feafb1eb
JH
365 close FILE;
366
367 END { do_i_unlink() }
368
c0a22fcc 369 runperl( switches => ['-pi.bak'], prog => 's/foo/bar/', args => ['tmpswitches'] );
feafb1eb 370
c0a22fcc 371 open(FILE, "tmpswitches") or die "$0: Failed to open 'tmpswitches': $!";
feafb1eb
JH
372 chomp(my @file = <FILE>);
373 close FILE;
374
c0a22fcc 375 open(BAK, "tmpswitches.bak") or die "$0: Failed to open 'tmpswitches.bak': $!";
feafb1eb
JH
376 chomp(my @bak = <BAK>);
377 close BAK;
378
379 is(join(":", @file),
380 "bar yada dada:bada bar bing:king kong bar",
381 "-i new file");
382 is(join(":", @bak),
383 "foo yada dada:bada foo bing:king kong foo",
384 "-i backup file");
82f96200
JL
385
386 my $out1 = runperl(
387 switches => ['-i.bak -p'],
388 prog => 'exit',
389 stderr => 1,
390 stdin => "1\n",
391 );
392 is(
393 $out1,
394 "-i used with no filenames on the command line, reading from STDIN.\n",
395 "warning when no files given"
396 );
397 my $out2 = runperl(
398 switches => ['-i.bak -p'],
399 prog => 'exit',
400 stderr => 1,
401 stdin => "1\n",
c0a22fcc 402 args => ['tmpswitches'],
82f96200
JL
403 );
404 is($out2, "", "no warning when files given");
5f92e204 405
c0a22fcc 406 open my $f, ">", "tmpswitches" or die "$0: failed to create 'tmpswitches': $!";
5f92e204
TC
407 print $f "foo\nbar\n";
408 close $f;
409
410 # a backup extension is no longer required on any platform
411 my $out3 = runperl(
412 switches => [ '-i', '-p' ],
413 prog => 's/foo/quux/',
414 stderr => 1,
c0a22fcc 415 args => [ 'tmpswitches' ],
5f92e204
TC
416 );
417 is($out3, "", "no warnings/errors without backup extension");
c0a22fcc 418 open $f, "<", "tmpswitches" or die "$0: cannot open 'tmpswitches': $!";
5f92e204
TC
419 chomp(my @out4 = <$f>);
420 close $f;
421 is(join(":", @out4), "quux:bar", "correct output without backup extension");
422
8200f806
TC
423 eval { require File::Spec; 1 }
424 or skip "Cannot load File::Spec - miniperl?", 20;
425
c0a22fcc
Z
426 -d "tmpinplace" or mkdir("tmpinplace")
427 or die "Cannot mkdir 'tmpinplace': $!";
428 my $work = File::Spec->catfile("tmpinplace", "foo");
929344c0
TC
429
430 # exit or die should leave original content in file
431 for my $inplace (qw/-i -i.bak/) {
432 for my $prog (qw/die exit/) {
433 open my $fh, ">", $work or die "$0: failed to open '$work': $!";
434 print $fh $yada;
435 close $fh or die "Failed to close: $!";
436 my $out = runperl (
437 switches => [ $inplace, '-n' ],
438 prog => "print q(foo\n); $prog",
439 stderr => 1,
440 args => [ $work ],
441 );
442 open my $in, "<", $work or die "$0: failed to open '$work': $!";
443 my $data = do { local $/; <$in> };
444 close $in;
445 is ($data, $yada, "check original content still in file");
446 unlink $work;
447 }
448 }
449
450 # test that path parsing is correct
5f92e204
TC
451 open $f, ">", $work or die "Cannot create $work: $!";
452 print $f "foo\nbar\n";
453 close $f;
454
455 my $out4 = runperl
456 (
457 switches => [ "-i", "-p" ],
458 prog => 's/foo/bar/',
459 stderr => 1,
460 args => [ $work ],
461 );
462 is ($out4, "", "no errors or warnings");
463 open $f, "<", $work or die "Cannot open $work: $!";
464 chomp(my @file4 = <$f>);
465 close $f;
466 is(join(":", @file4), "bar:bar", "check output");
467
977bd6d6
TC
468 SKIP:
469 {
470 # this needs to match how ARGV_USE_ATFUNCTIONS is defined in doio.c
471 skip "Not enough *at functions", 3
472 unless $Config{d_unlinkat} && $Config{d_renameat} && $Config{d_fchmodat}
473 && ($Config{d_dirfd} || $Config{d_dir_dd_fd})
b416949b 474 && $Config{d_linkat}
977bd6d6 475 && $Config{ccflags} !~ /-DNO_USE_ATFUNCTIONS\b/;
184f90dc
TC
476 my ($osvers) = ($Config{osvers} =~ /^(\d+(?:\.\d+)?)/);
477 skip "NetBSD 6 libc defines at functions, but they're incomplete", 3
478 if $^O eq "netbsd" && $osvers < 7;
977bd6d6 479 fresh_perl_is(<<'CODE', "ok\n", { },
c0a22fcc 480@ARGV = ("tmpinplace/foo");
977bd6d6
TC
481$^I = "";
482while (<>) {
483 chdir "..";
484 print "xx\n";
485}
486print "ok\n";
487CODE
488 "chdir while in-place editing");
489 ok(open(my $fh, "<", $work), "open out file");
490 is(scalar <$fh>, "xx\n", "file successfully saved after chdir");
491 close $fh;
492 }
493
8ab93df0
TC
494 SKIP:
495 {
496 skip "Need threads and full perl", 3
497 if !$Config{useithreads} || is_miniperl();
498 fresh_perl_is(<<'CODE', "ok\n", { stderr => 1 },
499use threads;
500use strict;
c0a22fcc 501@ARGV = ("tmpinplace/foo");
8ab93df0
TC
502$^I = "";
503while (<>) {
504 threads->create(sub { })->join;
505 print "yy\n";
506}
507print "ok\n";
508CODE
509 "threads while in-place editing");
510 ok(open(my $fh, "<", $work), "open out file");
511 is(scalar <$fh>, "yy\n", "file successfully saved after chdir");
512 close $fh;
513 }
514
05df5c88
TC
515 SKIP:
516 {
517 skip "Need fork", 3 if !$Config{d_fork};
518 open my $fh, ">", $work
519 or die "Cannot open $work: $!";
520 # we want only a single line for this test, otherwise
521 # it attempts to close the file twice
522 print $fh "foo\n";
523 close $fh or die "Cannot close $work: $!";
524 fresh_perl_is(<<'CODE', "ok\n", { stderr => 1 },
525use strict;
c0a22fcc 526@ARGV = ("tmpinplace/foo");
05df5c88
TC
527$^I = "";
528while (<>) {
529 my $pid = fork;
530 if (defined $pid && !$pid) {
531 # child
532 close ARGVOUT or die "Cannot close in child\n"; # this shouldn't do ARGVOUT magic
533 exit 0;
534 }
535 wait;
536 print "yy\n";
537 close ARGVOUT or die "Cannot close in parent\n"; # this should
538}
539print "ok\n";
540CODE
541 "fork while in-place editing");
542 ok(open($fh, "<", $work), "open out file");
184f90dc 543 is(scalar <$fh>, "yy\n", "file successfully saved after fork");
05df5c88
TC
544 close $fh;
545 }
546
83419aa9
TC
547 {
548 # test we handle the rename to the backup failing
a33b278e
CB
549 if ($^O eq 'VMS') {
550 # make it fail by creating a .bak file with a version than which no higher can be created
551 # can't make a directory because foo.bak and foo^.bak.DIR do not conflict.
552 open my $fh, '>', "$work.bak;32767" or die "Cannot make mask backup file: $!";
553 close $fh or die "Failed to close: $!";
554 }
555 else {
556 # make it fail by creating a directory of the backup name
557 mkdir "$work.bak" or die "Cannot make mask backup directory: $!";
558 }
83419aa9 559 fresh_perl_like(<<'CODE', qr/Can't rename/, { stderr => 1 }, "fail backup rename");
c0a22fcc 560@ARGV = ("tmpinplace/foo");
83419aa9
TC
561$^I = ".bak";
562while (<>) {
563 print;
564}
565print "ok\n";
566CODE
a33b278e
CB
567 if ($^O eq 'VMS') {
568 1 while unlink "$work.bak";
569 }
570 else {
571 rmdir "$work.bak" or die "Cannot remove mask backup directory: $!";
572 }
83419aa9
TC
573 }
574
84dbe61c
TC
575 {
576 # test with absolute paths, this was failing on FreeBSD 11ish due
577 # to a bug in renameat()
578 my $abs_work = File::Spec->rel2abs($work);
579 fresh_perl_is(<<'CODE', "",
580while (<>) {
581 print;
582}
583CODE
584 { stderr => 1, args => [ $abs_work ], switches => [ "-i" ] },
585 "abs paths");
586 }
587
5f92e204
TC
588 # we now use temp files for in-place editing, make sure we didn't leave
589 # any behind in the above test
c0a22fcc 590 opendir my $d, "tmpinplace" or die "Cannot opendir tmpinplace: $!";
a33b278e 591 my @names = grep !/^\.\.?$/ && $_ ne 'foo' && $_ ne 'foo.', readdir $d;
5f92e204
TC
592 closedir $d;
593 is(scalar(@names), 0, "no extra files")
594 or diag "Found @names, expected none";
929344c0 595
bb082417
TC
596 # the following tests might leave work files behind
597
dddabd86
TC
598 # this test can leave the work file in the directory, since making
599 # the directory non-writable also prevents removing the work file
600 SKIP:
601 {
602 # test we handle the rename of the work to the original failing
603 # make it fail by removing write perms from the directory
604 # but first check that doesn't prevent writing
c0a22fcc
Z
605 chmod 0500, "tmpinplace";
606 my $check = File::Spec->catfile("tmpinplace", "check");
dddabd86
TC
607 my $canwrite = open my $fh, ">", $check;
608 unlink $check;
c0a22fcc
Z
609 chmod 0700, "tmpinplace" or die "Cannot make tmpinplace writable again: $!";
610 skip "Cannot make tmpinplace read only", 1
dddabd86 611 if $canwrite;
8779d80a 612 fresh_perl_like(<<'CODE', qr/failed to rename/, { stderr => 1 }, "fail final rename");
c0a22fcc 613@ARGV = ("tmpinplace/foo");
dddabd86
TC
614$^I = "";
615while (<>) {
c0a22fcc 616 chmod 0500, "tmpinplace";
dddabd86
TC
617 print;
618}
619print "ok\n";
620CODE
c0a22fcc 621 chmod 0700, "tmpinplace" or die "Cannot make tmpinplace writable again: $!";
dddabd86
TC
622 }
623
bb082417
TC
624 SKIP:
625 {
626 # this needs to reverse match how ARGV_USE_ATFUNCTIONS is defined in doio.c
627 skip "Testing without *at functions", 1
628 if $Config{d_unlinkat} && $Config{d_renameat} && $Config{d_fchmodat}
629 && ($Config{d_dirfd} || $Config{d_dir_dd_fd})
b416949b 630 && $Config{d_linkat}
bb082417 631 && $Config{ccflags} !~ /-DNO_USE_ATFUNCTIONS\b/;
c0a22fcc
Z
632 fresh_perl_like(<<'CODE', qr/^Cannot complete in-place edit of tmpinplace\/foo: .* - line 5, <> line \d+\./, { },
633@ARGV = ("tmpinplace/foo");
bb082417
TC
634$^I = "";
635while (<>) {
636 chdir "..";
637 print "xx\n";
638}
639print "ok\n";
640CODE
641 "chdir while in-place editing (no at-functions)");
642 }
643
dddabd86
TC
644 unlink $work;
645
c0a22fcc 646 opendir $d, "tmpinplace" or die "Cannot opendir tmpinplace: $!";
bb082417 647 @names = grep !/^\.\.?$/ && !/foo$/aai, readdir $d;
dddabd86
TC
648 closedir $d;
649
5a39f542 650 # clean up in case the above failed
c0a22fcc 651 unlink map File::Spec->catfile("tmpinplace", $_), @names;
5a39f542 652
c0a22fcc 653 rmdir "tmpinplace";
feafb1eb 654}
bc9b29db
RH
655
656# Tests for -E
657
3012b817
CB
658$TODO = ''; # the -E tests work on VMS
659
bc9b29db
RH
660$r = runperl(
661 switches => [ '-E', '"say q(Hello, world!)"']
662);
663is( $r, "Hello, world!\n", "-E say" );
664
665
666$r = runperl(
7896dde7
Z
667 switches => [ '-E', '"no warnings q{experimental::smartmatch}; undef ~~ undef and say q(Hello, world!)"']
668);
669is( $r, "Hello, world!\n", "-E ~~" );
670
671$r = runperl(
672 switches => [ '-E', '"no warnings q{experimental::smartmatch}; given(undef) {when(undef) { say q(Hello, world!)"}}']
bc9b29db
RH
673);
674is( $r, "Hello, world!\n", "-E given" );
9f639728
FR
675
676$r = runperl(
f21873d1 677 switches => [ '-nE', q("} END { say q/affe/") ],
9f639728
FR
678 stdin => 'zomtek',
679);
680is( $r, "affe\n", '-E works outside of the block created by -n' );
d3133c89 681
53eb19dd
SM
682$r = runperl(
683 switches => [ '-E', q("*{'bar'} = sub{}; print 'Hello, world!',qq|\n|;")]
684);
685is( $r, "Hello, world!\n", "-E does not enable strictures" );
686
d3133c89
NC
687# RT #30660
688
689$filename = tempfile();
690SKIP: {
691 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
692 print $f <<'SWTEST';
693#!perl -w -iok
694print "$^I\n";
695SWTEST
696 close $f or die "Could not close: $!";
697 $r = runperl(
698 progfile => $filename,
699 );
700 like( $r, qr/ok/, 'Spaces on the #! line (#30660)' );
701}