From 7e6c31c73d14f8e50d13ad7ce4aa1aa167193afd Mon Sep 17 00:00:00 2001
From: Nazir Bilal Yavuz <byavuz81@gmail.com>
Date: Fri, 19 Jul 2024 09:48:47 +0300
Subject: [PATCH v1 1/2] Make PG_TEST_EXTRA env variable override its Meson
 configure option

Since PG_TEST_EXTRA configure option is set while configuring Meson,
each change on PG_TEST_EXTRA needs reconfigure and this is costly. So,
first try to use PG_TEST_EXTRA from environment. If it does not exist,
then try to use it from its configure option in Meson builds.
---
 doc/src/sgml/installation.sgml |  6 ++++--
 meson.build                    | 11 ++++++-----
 meson_options.txt              |  2 +-
 src/tools/testwrap             |  6 ++++++
 4 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/doc/src/sgml/installation.sgml b/doc/src/sgml/installation.sgml
index 4784834ab9f..a78ef8cb78b 100644
--- a/doc/src/sgml/installation.sgml
+++ b/doc/src/sgml/installation.sgml
@@ -3132,8 +3132,10 @@ ninja install
       <listitem>
        <para>
         Enable test suites which require special software to run.  This option
-        accepts arguments via a whitespace-separated list.  See <xref
-        linkend="regress-additional"/> for details.
+        accepts arguments via a whitespace-separated list.  Please note that
+        this configure option is overridden by PG_TEST_EXTRA environment
+        variable if it exists.  See <xref linkend="regress-additional"/> for
+        details.
        </para>
       </listitem>
      </varlistentry>
diff --git a/meson.build b/meson.build
index 5387bb6d5fd..e0b04b01756 100644
--- a/meson.build
+++ b/meson.build
@@ -3224,11 +3224,6 @@ test_env.set('PG_REGRESS', pg_regress.full_path())
 test_env.set('REGRESS_SHLIB', regress_module.full_path())
 test_env.set('INITDB_TEMPLATE', test_initdb_template)
 
-# Test suites that are not safe by default but can be run if selected
-# by the user via the whitespace-separated list in variable PG_TEST_EXTRA.
-# Export PG_TEST_EXTRA so it can be checked in individual tap tests.
-test_env.set('PG_TEST_EXTRA', get_option('PG_TEST_EXTRA'))
-
 # Add the temporary installation to the library search path on platforms where
 # that works (everything but windows, basically). On windows everything
 # library-like gets installed into bindir, solving that issue.
@@ -3292,6 +3287,12 @@ foreach test_dir : tests
     testwrap,
     '--basedir', meson.build_root(),
     '--srcdir', test_dir['sd'],
+    # Test suites that are not safe by default but can be run if selected
+    # by the user via the whitespace-separated list in variable PG_TEST_EXTRA.
+    # Export PG_TEST_EXTRA so it can be checked in individual tap tests.
+    # This configure option is overridden by PG_TEST_EXTRA environment variable
+    # if it exists.
+    '--pg_test_extra', get_option('PG_TEST_EXTRA'),
   ]
 
   foreach kind, v : test_dir
diff --git a/meson_options.txt b/meson_options.txt
index 246cecf3827..e1d55311702 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -47,7 +47,7 @@ option('injection_points', type: 'boolean', value: false,
   description: 'Enable injection points')
 
 option('PG_TEST_EXTRA', type: 'string', value: '',
-  description: 'Enable selected extra tests')
+  description: 'Enable selected extra tests, please note that this configure option is overridden by PG_TEST_EXTRA environment variable if it exists')
 
 option('atomics', type: 'boolean', value: true,
   description: 'Use atomic operations')
diff --git a/src/tools/testwrap b/src/tools/testwrap
index 9a270beb72d..0dca4c26f2b 100755
--- a/src/tools/testwrap
+++ b/src/tools/testwrap
@@ -13,6 +13,7 @@ parser.add_argument('--basedir', help='base directory of test', type=str)
 parser.add_argument('--testgroup', help='test group', type=str)
 parser.add_argument('--testname', help='test name', type=str)
 parser.add_argument('--skip', help='skip test (with reason)', type=str)
+parser.add_argument('--pg_test_extra', help='extra tests', type=str)
 parser.add_argument('test_command', nargs='*')
 
 args = parser.parse_args()
@@ -41,6 +42,11 @@ env_dict = {**os.environ,
             'TESTDATADIR': os.path.join(testdir, 'data'),
             'TESTLOGDIR': os.path.join(testdir, 'log')}
 
+# If PG_TEST_EXTRA is not set in the environment, then look for its Meson
+# configure option.
+if "PG_TEST_EXTRA" not in os.environ and args.pg_test_extra:
+    env_dict["PG_TEST_EXTRA"] = args.pg_test_extra
+
 sp = subprocess.Popen(args.test_command, env=env_dict, stdout=subprocess.PIPE)
 # Meson categorizes a passing TODO test point as bad
 # (https://github.com/mesonbuild/meson/issues/13183).  Remove the TODO
-- 
2.45.2

