From 2f403265509b66879d2d3396a9da4a6ba0dded6d Mon Sep 17 00:00:00 2001
From: Jacob Champion <jchampion@timescale.com>
Date: Fri, 23 Jun 2023 13:30:20 -0700
Subject: [PATCH v2 1/2] Test::Utils: honor PG_TEST_NOCLEAN for tempdirs

---
 doc/src/sgml/regress.sgml              | 2 ++
 src/test/perl/PostgreSQL/Test/Utils.pm | 6 +++---
 src/test/perl/README                   | 5 +++--
 3 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/doc/src/sgml/regress.sgml b/doc/src/sgml/regress.sgml
index 88a43b8961..675db86e4d 100644
--- a/doc/src/sgml/regress.sgml
+++ b/doc/src/sgml/regress.sgml
@@ -818,6 +818,8 @@ make check PROVE_TESTS='t/001_test1.pl t/003_test3.pl'
 <programlisting>
 PG_TEST_NOCLEAN=1 make -C src/bin/pg_dump check
 </programlisting>
+    This environment variable also prevents the test's temporary directories
+    from being removed.
    </para>
 
    <para>
diff --git a/src/test/perl/PostgreSQL/Test/Utils.pm b/src/test/perl/PostgreSQL/Test/Utils.pm
index a27fac83d2..e5a08d7b1a 100644
--- a/src/test/perl/PostgreSQL/Test/Utils.pm
+++ b/src/test/perl/PostgreSQL/Test/Utils.pm
@@ -272,7 +272,7 @@ sub all_tests_passing
 
 Securely create a temporary directory inside C<$tmp_check>, like C<mkdtemp>,
 and return its name.  The directory will be removed automatically at the
-end of the tests.
+end of the tests, unless the PG_TEST_NOCLEAN envvar is provided.
 
 If C<prefix> is given, the new directory is templated as C<${prefix}_XXXX>.
 Otherwise the template is C<tmp_test_XXXX>.
@@ -286,7 +286,7 @@ sub tempdir
 	return File::Temp::tempdir(
 		$prefix . '_XXXX',
 		DIR => $tmp_check,
-		CLEANUP => 1);
+		CLEANUP => not defined $ENV{'PG_TEST_NOCLEAN'});
 }
 
 =pod
@@ -301,7 +301,7 @@ name, to avoid path length issues.
 sub tempdir_short
 {
 
-	return File::Temp::tempdir(CLEANUP => 1);
+	return File::Temp::tempdir(CLEANUP => not defined $ENV{'PG_TEST_NOCLEAN'});
 }
 
 =pod
diff --git a/src/test/perl/README b/src/test/perl/README
index 6ddee42a10..43f6431f6e 100644
--- a/src/test/perl/README
+++ b/src/test/perl/README
@@ -31,8 +31,9 @@ some lesser number of seconds.
 
 Data directories will also be left behind for analysis when a test fails;
 they are named according to the test filename.  But if the environment
-variable PG_TEST_NOCLEAN is set, data directories will be retained
-regardless of test status.
+variable PG_TEST_NOCLEAN is set, those directories will be retained
+regardless of test status.  This environment variable also prevents the
+test's temporary directories from being removed.
 
 
 Writing tests
-- 
2.25.1

