From 4e2c5123e249ef3278da8d5260bbe1e4799988b2 Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horiguchi.kyotaro@lab.ntt.co.jp>
Date: Tue, 1 Mar 2016 16:09:25 +0900
Subject: [PATCH 1/2] Change behavior of PostgresNode.pm and TestLib.pm

Modify PostgresNode.new to use a temporary directory with the name
after the node name instead of tmp_dirXXXX. Modify PostgresNode.psql
to allow callers to get stderr output. Make TestLib to preserve the
temporary directory if a test has been failed. Modify TestLib.tempdir
to allow callers to prefix the tempdir name with an arbitrary prefix.
---
 src/test/perl/PostgresNode.pm |  9 +++++++--
 src/test/perl/TestLib.pm      | 10 +++++++++-
 2 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/src/test/perl/PostgresNode.pm b/src/test/perl/PostgresNode.pm
index a8e6f0c..ad27361 100644
--- a/src/test/perl/PostgresNode.pm
+++ b/src/test/perl/PostgresNode.pm
@@ -115,7 +115,7 @@ sub new
 	my $self   = {
 		_port     => $pgport,
 		_host     => $pghost,
-		_basedir  => TestLib::tempdir,
+		_basedir  => TestLib::tempdir($name),
 		_name     => $name,
 		_logfile  => "$TestLib::log_path/${testname}_${name}.log" };
 
@@ -805,10 +805,15 @@ sub psql
 		print "#### Begin standard error\n";
 		print $stderr;
 		print "#### End standard error\n";
+		if (wantarray)
+		{
+			chomp $stderr;
+			$stderr =~ s/\r//g if $Config{osname} eq 'msys';
+		}
 	}
 	chomp $stdout;
 	$stdout =~ s/\r//g if $Config{osname} eq 'msys';
-	return $stdout;
+	return wantarray ? ($stdout, $stderr) : $stdout;
 }
 
 =pod
diff --git a/src/test/perl/TestLib.pm b/src/test/perl/TestLib.pm
index 3d11cbb..da0e617 100644
--- a/src/test/perl/TestLib.pm
+++ b/src/test/perl/TestLib.pm
@@ -107,13 +107,21 @@ INIT
 	autoflush TESTLOG 1;
 }
 
+END
+{
+	# Preserve temporary directory for this test if failure
+	$File::Temp::KEEP_ALL = 1 unless Test::More->builder->is_passing;
+}
+
 #
 # Helper functions
 #
 sub tempdir
 {
+	my ($prefix) = @_;
+	$prefix = "tmp_test" if (!$prefix);
 	return File::Temp::tempdir(
-		'tmp_testXXXX',
+		$prefix.'_XXXX',
 		DIR => $tmp_check,
 		CLEANUP => 1);
 }
-- 
1.8.3.1

