# -*- perl -*-
use strict;
use Benchmark;
use Test::More tests => 10;
select(STDERR); $| = 1; select(STDOUT); $| = 1;
use ClearCase::Argv qw(chdir);
use File::Temp qw(tempfile);
use File::Slurp;

ClearCase::Argv->summary;      # start keeping stats
ClearCase::Argv->ipc(1);
my $ct = ClearCase::Argv->new({autochomp=>1});
my @cmd = qw(des -s /@@ .); # -s in order to avoid timestamps
my ($outfh, $tmpout) = tempfile();
my ($errfh, $tmperr) = tempfile();
open(SAVEOUT, ">&STDOUT");
open(SAVEERR, ">&STDERR");
# Test 1
*STDOUT = $outfh;
*STDERR = $errfh;
my $rc = $ct->argv(@cmd)->system;
close(STDOUT);
close(STDERR);
open(STDOUT, ">&SAVEOUT");
open(STDERR, ">&SAVEERR");
my @expout = read_file($tmpout);
my @experr = read_file($tmperr);
ok (($rc and @expout and @experr), 'system: ret code, stdout and stderr.');
write_file($tmpout, ());
write_file($tmperr, ());
# Test 2
open(STDOUT, ">$tmpout");
open(STDERR, ">$tmperr");
$rc = $ct->argv(@cmd)->stderr(0)->system;
close(STDOUT);
close(STDERR);
open(STDOUT, ">&SAVEOUT");
open(STDERR, ">&SAVEERR");
my @lastout = read_file($tmpout);
ok (($rc and (@lastout eq @expout) and !read_file($tmperr)),
    'system: stderr ignored, stdout ok.');
write_file($tmpout, ());
write_file($tmperr, ());
# Test 3
open(STDOUT, ">$tmpout");
open(STDERR, ">$tmperr");
$rc = $ct->argv(@cmd)->stdout(0)->system;
close(STDOUT);
close(STDERR);
open(STDOUT, ">&SAVEOUT");
open(STDERR, ">&SAVEERR");
my @lasterr = read_file($tmperr);
ok (($rc and (@lasterr eq @experr) and !read_file($tmpout)),
    'system: stdout ignored, stderr ok.');
write_file($tmpout, ());
write_file($tmperr, ());
# Test 4
my $err = $ct->argv(@cmd)->stdout(0)->qx;
ok ($err, 'qx: stdout ignored, stderr ok.');
# Test 5
@lastout = $ct->argv(@cmd)->stderr(0)->qx;
ok (@lastout eq @expout, 'qx: stderr ignored, stdout ok.');
TODO: {
  eval { require ClearCase::CtCmd };
  local $TODO = "redirection not supported in fork model" if $@;
# Test 6 -- same thing for ctcmd mode, or what emulates it
$ct->ctcmd(2); #Gives a warning if CtCmd not installed
open(STDOUT, ">$tmpout");
open(STDERR, ">$tmperr");
$rc = $ct->argv(@cmd)->system;
close(STDOUT);
close(STDERR);
open(STDOUT, ">&SAVEOUT");
open(STDERR, ">&SAVEERR");
@expout = read_file($tmpout);
@experr = read_file($tmperr);
ok (($rc and @expout and @experr), 'system: ret code, stdout and stderr.');
write_file($tmpout, ());
write_file($tmperr, ());
# Test 7
open(STDOUT, ">$tmpout");
open(STDERR, ">$tmperr");
$rc = $ct->argv(@cmd)->stderr(0)->system;
close(STDOUT);
close(STDERR);
open(STDOUT, ">&SAVEOUT");
open(STDERR, ">&SAVEERR");
@lastout = read_file($tmpout);
ok (($rc and (@lastout eq @expout) and !read_file($tmperr)),
    'system: stderr ignored, stdout ok.');
write_file($tmpout, ());
write_file($tmperr, ());
# Test 8
open(STDOUT, ">$tmpout");
open(STDERR, ">$tmperr");
$rc = $ct->argv(@cmd)->stdout(0)->system;
close(STDOUT);
close(STDERR);
open(STDOUT, ">&SAVEOUT");
open(STDERR, ">&SAVEERR");
@lasterr = read_file($tmperr);
ok (($rc and (@lasterr eq @experr) and !read_file($tmpout)),
    'system: stdout ignored, stderr ok.');
write_file($tmpout, ());
write_file($tmperr, ());
# Test 9
$err = $ct->argv(@cmd)->stdout(0)->qx;
ok ($err, 'qx: stdout ignored, stderr ok.');
# Test 10
@lastout = $ct->argv(@cmd)->stderr(0)->qx;
ok (@lastout eq @expout, 'qx: stderr ignored, stdout ok.');
}
unlink $tmpout, $tmperr;
print STDERR "\n", ClearCase::Argv->summary;   # print out the stats we kept
