From bc660c423731a3b68fb9c1e61a3ab8812dc8d55d Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Fri, 21 Jul 2006 00:24:04 +0000
Subject: [PATCH] Ah, I finally realize why Magnus wanted to add a --bindir
 option to pg_regress: there's no other way to cope with testing a relocated
 installation.  Seems better to call it --psqldir though, since the only thing
 we need to find in that case is psql.  It'd be better if we could use
 find_other_exec, but that's not happening unless we are willing to install
 pg_regress alongside psql, which seems unlikely to happen.

---
 src/makefiles/pgxs.mk         |  7 ++++--
 src/pl/plperl/GNUmakefile     |  6 +++--
 src/pl/plpython/Makefile      |  6 +++--
 src/pl/tcl/Makefile           |  6 +++--
 src/test/regress/GNUmakefile  | 11 +++++----
 src/test/regress/pg_regress.c | 43 +++++++++++++++++++++++++----------
 6 files changed, 55 insertions(+), 24 deletions(-)

diff --git a/src/makefiles/pgxs.mk b/src/makefiles/pgxs.mk
index a26622922bd..1a0fa54c6ef 100644
--- a/src/makefiles/pgxs.mk
+++ b/src/makefiles/pgxs.mk
@@ -1,6 +1,6 @@
 # PGXS: PostgreSQL extensions makefile
 
-# $PostgreSQL: pgsql/src/makefiles/pgxs.mk,v 1.8 2006/07/19 02:37:00 tgl Exp $ 
+# $PostgreSQL: pgsql/src/makefiles/pgxs.mk,v 1.9 2006/07/21 00:24:04 tgl Exp $ 
 
 # This file contains generic rules to build many kinds of simple
 # extension modules.  You only need to set a few variables and include
@@ -210,6 +210,9 @@ ifndef REGRESS_OPTS
 REGRESS_OPTS = --dbname=$(CONTRIB_TESTDB)
 endif
 
+# where to find psql for running the tests
+PSQLDIR = $(bindir)
+
 # When doing a VPATH build, must copy over the test .sql and .out
 # files so that the driver script can find them.  We have to use an
 # absolute path for the targets, because otherwise make will try to
@@ -235,7 +238,7 @@ endif
 
 # against installed postmaster
 installcheck: submake
-	$(top_builddir)/src/test/regress/pg_regress $(REGRESS_OPTS) $(REGRESS)
+	$(top_builddir)/src/test/regress/pg_regress --psqldir=$(PSQLDIR) $(REGRESS_OPTS) $(REGRESS)
 
 # in-tree test doesn't work yet (no way to install my shared library)
 #check: all submake
diff --git a/src/pl/plperl/GNUmakefile b/src/pl/plperl/GNUmakefile
index 73f36bd44ac..11583763ff9 100644
--- a/src/pl/plperl/GNUmakefile
+++ b/src/pl/plperl/GNUmakefile
@@ -1,5 +1,5 @@
 # Makefile for PL/Perl
-# $PostgreSQL: pgsql/src/pl/plperl/GNUmakefile,v 1.27 2006/07/19 02:37:00 tgl Exp $
+# $PostgreSQL: pgsql/src/pl/plperl/GNUmakefile,v 1.28 2006/07/21 00:24:04 tgl Exp $
 
 subdir = src/pl/plperl
 top_builddir = ../../..
@@ -38,6 +38,8 @@ SHLIB_LINK = $(perl_embed_ldflags) $(BE_DLLLIBS)
 
 REGRESS_OPTS = --dbname=$(PL_TESTDB) --load-language=plperl
 REGRESS = plperl plperl_trigger plperl_shared plperl_elog
+# where to find psql for running the tests
+PSQLDIR = $(bindir)
 
 include $(top_srcdir)/src/Makefile.shlib
 
@@ -84,7 +86,7 @@ uninstall:
 	rm -f '$(DESTDIR)$(pkglibdir)/plperl$(DLSUFFIX)'
 
 installcheck: submake
-	$(top_builddir)/src/test/regress/pg_regress $(REGRESS_OPTS) $(REGRESS)
+	$(top_builddir)/src/test/regress/pg_regress --psqldir=$(PSQLDIR) $(REGRESS_OPTS) $(REGRESS)
 
 .PHONY: submake
 submake:
diff --git a/src/pl/plpython/Makefile b/src/pl/plpython/Makefile
index 5823e205dcd..9d02f4d7f41 100644
--- a/src/pl/plpython/Makefile
+++ b/src/pl/plpython/Makefile
@@ -1,4 +1,4 @@
-# $PostgreSQL: pgsql/src/pl/plpython/Makefile,v 1.25 2006/07/19 02:37:00 tgl Exp $
+# $PostgreSQL: pgsql/src/pl/plpython/Makefile,v 1.26 2006/07/21 00:24:04 tgl Exp $
 
 subdir = src/pl/plpython
 top_builddir = ../../..
@@ -60,6 +60,8 @@ SHLIB_LINK = $(BE_DLLLIBS) $(python_libspec) $(python_additional_libs)
 
 REGRESS_OPTS = --dbname=$(PL_TESTDB) --load-language=plpythonu
 REGRESS = plpython_schema plpython_populate plpython_function plpython_test plpython_error plpython_drop
+# where to find psql for running the tests
+PSQLDIR = $(bindir)
 
 include $(top_srcdir)/src/Makefile.shlib
 
@@ -103,7 +105,7 @@ uninstall:
 	rm -f '$(DESTDIR)$(pkglibdir)/plpython$(DLSUFFIX)'
 
 installcheck: submake
-	$(top_builddir)/src/test/regress/pg_regress $(REGRESS_OPTS) $(REGRESS)
+	$(top_builddir)/src/test/regress/pg_regress --psqldir=$(PSQLDIR) $(REGRESS_OPTS) $(REGRESS)
 
 .PHONY: submake
 submake:
diff --git a/src/pl/tcl/Makefile b/src/pl/tcl/Makefile
index a474c71e2d9..6fc6cc272ff 100644
--- a/src/pl/tcl/Makefile
+++ b/src/pl/tcl/Makefile
@@ -2,7 +2,7 @@
 #
 # Makefile for the pltcl shared object
 #
-# $PostgreSQL: pgsql/src/pl/tcl/Makefile,v 1.49 2006/07/19 02:37:00 tgl Exp $
+# $PostgreSQL: pgsql/src/pl/tcl/Makefile,v 1.50 2006/07/21 00:24:04 tgl Exp $
 #
 #-------------------------------------------------------------------------
 
@@ -42,6 +42,8 @@ OBJS = pltcl.o
 
 REGRESS_OPTS = --dbname=$(PL_TESTDB) --load-language=pltcl
 REGRESS = pltcl_setup pltcl_queries
+# where to find psql for running the tests
+PSQLDIR = $(bindir)
 
 include $(top_srcdir)/src/Makefile.shlib
 
@@ -90,7 +92,7 @@ uninstall:
 	$(MAKE) -C modules $@
 
 installcheck: submake
-	$(top_builddir)/src/test/regress/pg_regress $(REGRESS_OPTS) $(REGRESS)
+	$(top_builddir)/src/test/regress/pg_regress --psqldir=$(PSQLDIR) $(REGRESS_OPTS) $(REGRESS)
 
 .PHONY: submake
 submake:
diff --git a/src/test/regress/GNUmakefile b/src/test/regress/GNUmakefile
index cb22a1c55c6..f2319d2a79b 100644
--- a/src/test/regress/GNUmakefile
+++ b/src/test/regress/GNUmakefile
@@ -6,7 +6,7 @@
 # Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
 # Portions Copyright (c) 1994, Regents of the University of California
 #
-# $PostgreSQL: pgsql/src/test/regress/GNUmakefile,v 1.61 2006/07/20 01:16:57 tgl Exp $
+# $PostgreSQL: pgsql/src/test/regress/GNUmakefile,v 1.62 2006/07/21 00:24:04 tgl Exp $
 #
 #-------------------------------------------------------------------------
 
@@ -19,6 +19,9 @@ contribdir := $(top_builddir)/contrib
 # port number for temp-installation test postmaster
 TEMP_PORT = 5$(DEF_PGPORT)
 
+# where to find psql for testing an existing installation
+PSQLDIR = $(bindir)
+
 # default encoding
 MULTIBYTE = SQL_ASCII
 
@@ -150,12 +153,12 @@ check: all
 installcheck: all
 	-rm -rf ./testtablespace
 	mkdir ./testtablespace
-	./pg_regress --schedule=$(srcdir)/serial_schedule --multibyte=$(MULTIBYTE) --load-language=plpgsql $(NOLOCALE)
+	./pg_regress --psqldir=$(PSQLDIR) --schedule=$(srcdir)/serial_schedule --multibyte=$(MULTIBYTE) --load-language=plpgsql $(NOLOCALE)
 
 installcheck-parallel: all
 	-rm -rf ./testtablespace
 	mkdir ./testtablespace
-	./pg_regress --schedule=$(srcdir)/parallel_schedule --multibyte=$(MULTIBYTE) --load-language=plpgsql $(MAXCONNOPT) $(NOLOCALE)
+	./pg_regress --psqldir=$(PSQLDIR) --schedule=$(srcdir)/parallel_schedule --multibyte=$(MULTIBYTE) --load-language=plpgsql $(MAXCONNOPT) $(NOLOCALE)
 
 
 # old interfaces follow...
@@ -165,7 +168,7 @@ runtest: installcheck
 runtest-parallel: installcheck-parallel
 
 bigtest:
-	./pg_regress --schedule=$(srcdir)/serial_schedule --multibyte=$(MULTIBYTE) --load-language=plpgsql $(NOLOCALE) numeric_big 
+	./pg_regress --psqldir=$(PSQLDIR) --schedule=$(srcdir)/serial_schedule --multibyte=$(MULTIBYTE) --load-language=plpgsql $(NOLOCALE) numeric_big 
 
 bigcheck:
 	./pg_regress --temp-install=./tmp_check --top-builddir=$(top_builddir) --temp-port=$(TEMP_PORT) --schedule=$(srcdir)/parallel_schedule --multibyte=$(MULTIBYTE) --load-language=plpgsql $(MAXCONNOPT) $(NOLOCALE) numeric_big
diff --git a/src/test/regress/pg_regress.c b/src/test/regress/pg_regress.c
index f99231c907a..ebc03114b53 100644
--- a/src/test/regress/pg_regress.c
+++ b/src/test/regress/pg_regress.c
@@ -3,7 +3,7 @@
  * pg_regress --- regression test driver
  *
  * This is a C implementation of the previous shell script for running
- * the regression tests, and should be highly compatible with it.
+ * the regression tests, and should be mostly compatible with it.
  * Initial author of C translation: Magnus Hagander
  *
  * This code is released under the terms of the PostgreSQL License.
@@ -11,7 +11,7 @@
  * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/test/regress/pg_regress.c,v 1.10 2006/07/20 16:25:30 tgl Exp $
+ * $PostgreSQL: pgsql/src/test/regress/pg_regress.c,v 1.11 2006/07/21 00:24:04 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -52,11 +52,11 @@ typedef struct _resultmap
 } _resultmap;
 
 /*
- * Values obtained from pg_config_paths.h and Makefile.  (It might seem
- * tempting to get the paths via get_share_path() and friends, but that's
- * not going to work because pg_regress is typically not executed from an
- * installed bin directory.  In any case, for our purposes the configured
- * paths are what we want anyway.)
+ * Values obtained from pg_config_paths.h and Makefile.  The PG installation
+ * paths are only used in temp_install mode: we use these strings to find
+ * out where "make install" will put stuff under the temp_install directory.
+ * In non-temp_install mode, the only thing we need is the location of psql,
+ * which we expect to find in psqldir, or in the PATH if psqldir isn't given.
  */
 static char *bindir = PGBINDIR;
 static char *libdir = LIBDIR;
@@ -85,6 +85,7 @@ static char *temp_install = NULL;
 static char *top_builddir = NULL;
 static int temp_port = 65432;
 static bool nolocale = false;
+static char *psqldir = NULL;
 static char *hostname = NULL;
 static int port = -1;
 static char *user = NULL;
@@ -499,6 +500,9 @@ initialize_environment(void)
 		sprintf(tmp, "%s/install/%s", temp_install, datadir);
 		datadir = tmp;
 
+		/* psql will be installed into temp-install bindir */
+		psqldir = bindir;
+
 		/*
 		 * Set up shared library paths to include the temp install.
 		 *
@@ -539,7 +543,8 @@ initialize_environment(void)
 
 		/*
 		 * On Windows, it seems to be necessary to adjust PATH even in
-		 * this case.
+		 * this case.  (XXX really?  If so, what if installation has
+		 * been relocated?)
 		 */
 #ifdef WIN32
 		add_to_path("PATH", ';', libdir);
@@ -600,8 +605,11 @@ psql_command(const char *database, const char *query, ...)
 
 	/* And now we can build and execute the shell command */
 	snprintf(psql_cmd, sizeof(psql_cmd),
-			 SYSTEMQUOTE "\"%s/psql\" -X -c \"%s\" \"%s\"" SYSTEMQUOTE,
-			 bindir, query_escaped, database);
+			 SYSTEMQUOTE "\"%s%spsql\" -X -c \"%s\" \"%s\"" SYSTEMQUOTE,
+			 psqldir ? psqldir : "",
+			 psqldir ? "/" : "",
+			 query_escaped,
+			 database);
 
 	if (system(psql_cmd) != 0)
 	{
@@ -699,8 +707,12 @@ psql_start_test(const char *testname)
 			 outputdir, testname);
 
 	snprintf(psql_cmd, sizeof(psql_cmd),
-			 SYSTEMQUOTE "\"%s/psql\" -X -a -q -d \"%s\" < \"%s\" > \"%s\" 2>&1" SYSTEMQUOTE,
-			 bindir, dbname, infile, outfile);
+			 SYSTEMQUOTE "\"%s%spsql\" -X -a -q -d \"%s\" < \"%s\" > \"%s\" 2>&1" SYSTEMQUOTE,
+			 psqldir ? psqldir : "",
+			 psqldir ? "/" : "",
+			 dbname,
+			 infile,
+			 outfile);
 
 	pid = spawn_process(psql_cmd);
 
@@ -1267,6 +1279,7 @@ help(void)
 	printf(_("  --host=HOST               use postmaster running on HOST\n"));
 	printf(_("  --port=PORT               use postmaster running at PORT\n"));
 	printf(_("  --user=USER               connect as USER\n"));
+	printf(_("  --psqldir=DIR             use psql in DIR (default: find in PATH)\n"));
 	printf(_("\n"));
 	printf(_("The exit status is 0 if all tests passed, 1 if some tests failed, and 2\n"));
 	printf(_("if the tests could not be run for some reason.\n"));
@@ -1301,6 +1314,7 @@ main(int argc, char *argv[])
 		{"host", required_argument, NULL, 13},
 		{"port", required_argument, NULL, 14},
 		{"user", required_argument, NULL, 15},
+		{"psqldir", required_argument, NULL, 16},
 		{NULL, 0, NULL, 0}
 	};
 
@@ -1388,6 +1402,11 @@ main(int argc, char *argv[])
 			case 15:
 				user = strdup(optarg);
 				break;
+			case 16:
+				/* "--psqldir=" should mean to use PATH */
+				if (strlen(optarg))
+					psqldir = strdup(optarg);
+				break;
 			default:
 				/* getopt_long already emitted a complaint */
 				fprintf(stderr, _("\nTry \"%s -h\" for more information.\n"),
-- 
GitLab