X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fio%2Fopen.t;h=f08eed50a4ae06fd926de8a20a754b16e27aa02e;hb=0685228bc0e6f0ccd11501cab8ce8356eb5fd6bd;hp=f49ba10c4932217cd0b49b34cbcd0811a4265173;hpb=8e0fc1cdff8c4d07f11a5b0bd5056e1acbe2a68a;p=p5sagit%2Fp5-mst-13.2.git diff --git a/t/io/open.t b/t/io/open.t index f49ba10..f08eed5 100755 --- a/t/io/open.t +++ b/t/io/open.t @@ -8,9 +8,11 @@ BEGIN { $| = 1; use warnings; +use Config; $Is_VMS = $^O eq 'VMS'; +$Is_MacOS = $^O eq 'MacOS'; -plan tests => 95; +plan tests => 108; my $Perl = which_perl(); @@ -78,7 +80,7 @@ SKIP: { skip "open -| busted and noisy on VMS", 3 if $Is_VMS; ok( open(my $f, '-|', <; @@ -86,7 +88,9 @@ EOC ok( close($f), ' close' ); } -{ +SKIP: { + skip "Output for |- doesn't go to shell on MacOS", 5 if $Is_MacOS; + ok( open(my $f, '|-', <; @@ -179,7 +183,9 @@ EOC ok( close($f), ' close' ); } -{ +SKIP: { + skip "Output for |- doesn't go to shell on MacOS", 5 if $Is_MacOS; + ok( open(local $f, '|-', <, "ok\n", ' readline'); - ok( close F, ' close' ); + ok( close F, ' close' ); } for (1..2) { - ok( open(F, "-|", qq{$Perl -le "print 'ok'"}), 'open -|'); - is( scalar , "ok\n", ' readline'); + ok( open(F, "-|", qq{$Perl -le "print 'ok'"}), 'open -|'); + is( scalar , "ok\n", ' readline'); ok( close F, ' close' ); } } -# magic temporary file via 3 arg open with undef + +# other dupping techniques +{ + ok( open(my $stdout, ">&", \*STDOUT), 'dup \*STDOUT into lexical fh'); + ok( open(STDOUT, ">&", $stdout), 'restore dupped STDOUT from lexical fh'); + + { + use strict; # the below should not warn + ok( open(my $stdout, ">&", STDOUT), 'dup STDOUT into lexical fh'); + } + + # used to try to open a file [perl #17830] + ok( open(my $stdin, "<&", fileno STDIN), 'dup fileno(STDIN) into lexical fh') or _diag $!; +} + +SKIP: { + skip "This perl uses perlio", 1 if $Config{useperlio}; + skip "miniperl cannot be relied on to load %Errno" + if $ENV{PERL_CORE_MINITEST}; + # Force the reference to %! to be run time by writing ! as {"!"} + skip "This system doesn't understand EINVAL", 1 + unless exists ${"!"}{EINVAL}; + + no warnings 'io'; + ok(!open(F,'>',\my $s) && ${"!"}{EINVAL}, 'open(reference) raises EINVAL'); +} + +{ + ok( !eval { open F, "BAR", "QUUX" }, 'Unknown open() mode' ); + like( $@, qr/\QUnknown open() mode 'BAR'/, ' right error' ); +} + { - ok( open(my $x,"+<",undef), 'magic temp file via 3 arg open with undef'); - ok( defined fileno($x), ' fileno' ); + local $SIG{__WARN__} = sub { $@ = shift }; + + sub gimme { + my $tmphandle = shift; + my $line = scalar <$tmphandle>; + warn "gimme"; + return $line; + } + + open($fh0[0], "TEST"); + gimme($fh0[0]); + like($@, qr/<\$fh0\[...\]> line 1\./, "autoviv fh package aelem"); + + open($fh1{k}, "TEST"); + gimme($fh1{k}); + like($@, qr/<\$fh1{...}> line 1\./, "autoviv fh package helem"); - select $x; - ok( (print "ok\n"), ' print' ); + my @fh2; + open($fh2[0], "TEST"); + gimme($fh2[0]); + like($@, qr/<\$fh2\[...\]> line 1\./, "autoviv fh lexical aelem"); - select STDOUT; - ok( seek($x,0,0), ' seek' ); - is( scalar <$x>, "ok\n", ' readline' ); - ok( tell($x) >= 3, ' tell' ); + my %fh3; + open($fh3{k}, "TEST"); + gimme($fh3{k}); + like($@, qr/<\$fh3{...}> line 1\./, "autoviv fh lexical helem"); } + +SKIP: { + skip("These tests use perlio", 5) unless $Config{useperlio}; + my $w; + use warnings 'layer'; + local $SIG{__WARN__} = sub { $w = shift }; + + eval { open(F, ">>>", "afile") }; + like($w, qr/Invalid separator character '>' in PerlIO layer spec/, + "bad open (>>>) warning"); + like($@, qr/Unknown open\(\) mode '>>>'/, + "bad open (>>>) failure"); + + eval { open(F, ">:u", "afile" ) }; + like($w, qr/Unknown PerlIO layer "u"/, + 'bad layer ">:u" warning'); + eval { open(F, "<:u", "afile" ) }; + like($w, qr/Unknown PerlIO layer "u"/, + 'bad layer "<:u" warning'); + eval { open(F, ":c", "afile" ) }; + like($@, qr/Unknown open\(\) mode ':c'/, + 'bad layer ":c" failure'); +} + +# [perl #28986] "open m" crashes Perl + +fresh_perl_like('open m', qr/^Search pattern not terminated at/, + { stderr => 1 }, 'open m test'); + +fresh_perl_is( + 'sub f { open(my $fh, "xxx"); $fh = "f"; } f; f;print "ok"', + 'ok', { stderr => 1 }, + '#29102: Crash on assignment to lexical filehandle'); + +# [perl #31767] Using $1 as a filehandle via open $1, "file" doesn't raise +# an exception + +eval { open $99, "foo" }; +like($@, qr/Modification of a read-only value attempted/, "readonly fh");