This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Refactor podcheck.t to slurp files into scalars, instead of an array of lines.
[perl5.git] / t / io / inplace.t
old mode 100755 (executable)
new mode 100644 (file)
index 477add1..ed85dac
@@ -1,13 +1,23 @@
 #!./perl
+use strict;
+require './test.pl';
 
-$^I = '.bak';
+$^I = $^O eq 'VMS' ? '_bak' : '.bak';
 
-# $RCSfile: inplace.t,v $$Revision: 4.1 $$Date: 92/08/07 18:27:29 $
+plan( tests => 6 );
 
-print "1..2\n";
+my @tfiles     = (tempfile(), tempfile(), tempfile());
+my @tfiles_bak = map "$_$^I", @tfiles;
+
+END { unlink_all(@tfiles_bak); }
+
+for my $file (@tfiles) {
+    runperl( prog => 'print qq(foo\n);', 
+             args => ['>', $file] );
+}
+
+@ARGV = @tfiles;
 
-@ARGV = ('.a','.b','.c');
-`echo foo | tee .a .b .c`;
 while (<>) {
     s/foo/bar/;
 }
@@ -15,7 +25,68 @@ continue {
     print;
 }
 
-if (`cat .a .b .c` eq "bar\nbar\nbar\n") {print "ok 1\n";} else {print "not ok 1\n";}
-if (`cat .a.bak .b.bak .c.bak` eq "foo\nfoo\nfoo\n") {print "ok 2\n";} else {print "not ok 2\n";}
+is ( runperl( prog => 'print<>;', args => \@tfiles ), 
+     "bar\nbar\nbar\n", 
+     "file contents properly replaced" );
 
-unlink '.a', '.b', '.c', '.a.bak', '.b.bak', '.c.bak';
+is ( runperl( prog => 'print<>;', args => \@tfiles_bak ), 
+     "foo\nfoo\nfoo\n", 
+     "backup file contents stay the same" );
+
+SKIP:
+{
+    # based on code, dosish and epoc systems can't do no-backup inplace
+    # edits
+    $^O =~ /^(MSWin32|cygwin|uwin|dos|epoc|os2)$/
+       and skip("Can't inplace edit without backups on $^O", 4);
+    
+    our @ifiles = ( tempfile(), tempfile(), tempfile() );
+    
+    {
+       for my $file (@ifiles) {
+           runperl( prog => 'print qq(bar\n);',
+                    args => [ '>', $file ] );
+       }
+       
+       local $^I = '';
+    local @ARGV = @ifiles;
+       
+       while (<>) {
+           print "foo$_";
+       }
+       
+       is(scalar(@ARGV), 0, "consumed ARGV");
+       
+#      runperl may quote its arguments, so don't expect to be able
+#      to reuse things you send it.
+
+       my @my_ifiles = @ifiles;
+       is( runperl( prog => 'print<>;', args => \@my_ifiles ),
+           "foobar\nfoobar\nfoobar\n",
+           "normal inplace edit");
+    }
+    
+    # test * equivalence RT #70802
+    {
+       for my $file (@ifiles) {
+           runperl( prog => 'print qq(bar\n);',
+                    args => [ '>', $file ] );
+       }
+       
+       local $^I = '*';
+       local @ARGV = @ifiles;
+       
+       while (<>) {
+           print "foo$_";
+       }
+       
+       is(scalar(@ARGV), 0, "consumed ARGV");
+       
+       my @my_ifiles = @ifiles;
+       is( runperl( prog => 'print<>;', args => \@my_ifiles ),
+           "foobar\nfoobar\nfoobar\n",
+           "normal inplace edit");
+    }
+    
+    END { unlink_all(@ifiles); }
+}