From 2ad4759ae2b0b7ce66b127209e8057dc975995c9 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Thu, 30 Jun 2022 18:28:17 -0700
Subject: [PATCH v10 13/16] meson: prereq: regress: allow to specify director
 containing expected files

This is needed for ecpg tests, so tests don't have to be generated in the
source directory.

Author:
Reviewed-By:
Discussion: https://postgr.es/m/
Backpatch:
---
 src/interfaces/ecpg/test/pg_regress_ecpg.c | 34 +++++++++++++++++++---
 src/test/regress/pg_regress.c              |  7 +++++
 src/test/regress/pg_regress.h              |  1 +
 3 files changed, 38 insertions(+), 4 deletions(-)

diff --git a/src/interfaces/ecpg/test/pg_regress_ecpg.c b/src/interfaces/ecpg/test/pg_regress_ecpg.c
index f920af4560c..721fa6f17eb 100644
--- a/src/interfaces/ecpg/test/pg_regress_ecpg.c
+++ b/src/interfaces/ecpg/test/pg_regress_ecpg.c
@@ -164,7 +164,7 @@ ecpg_start_test(const char *testname,
 	char	   *appnameenv;
 
 	snprintf(inprg, sizeof(inprg), "%s/%s", inputdir, testname);
-	snprintf(insource, sizeof(insource), "%s.c", testname);
+	snprintf(insource, sizeof(insource), "%s/%s.c", inputdir, testname);
 
 	/* make a version of the test name that has dashes in place of slashes */
 	initStringInfo(&testname_dash);
@@ -177,13 +177,39 @@ ecpg_start_test(const char *testname,
 
 	snprintf(expectfile_stdout, sizeof(expectfile_stdout),
 			 "%s/expected/%s.stdout",
-			 outputdir, testname_dash.data);
+			 expecteddir, testname_dash.data);
+	if (!file_exists(expectfile_stdout))
+		snprintf(expectfile_stdout, sizeof(expectfile_stdout),
+				 "%s/expected/%s.stdout",
+				 outputdir, testname_dash.data);
+	if (!file_exists(expectfile_stdout))
+		snprintf(expectfile_stdout, sizeof(expectfile_stdout),
+				 "%s/expected/%s.stdout",
+				 inputdir, testname_dash.data);
+
 	snprintf(expectfile_stderr, sizeof(expectfile_stderr),
 			 "%s/expected/%s.stderr",
-			 outputdir, testname_dash.data);
+			 expecteddir, testname_dash.data);
+	if (!file_exists(expectfile_stderr))
+		snprintf(expectfile_stderr, sizeof(expectfile_stderr),
+				 "%s/expected/%s.stderr",
+				 outputdir, testname_dash.data);
+	if (!file_exists(expectfile_stderr))
+		snprintf(expectfile_stderr, sizeof(expectfile_stderr),
+				 "%s/expected/%s.stderr",
+				 inputdir, testname_dash.data);
+
 	snprintf(expectfile_source, sizeof(expectfile_source),
 			 "%s/expected/%s.c",
-			 outputdir, testname_dash.data);
+			 expecteddir, testname_dash.data);
+	if (!file_exists(expectfile_source))
+		snprintf(expectfile_source, sizeof(expectfile_source),
+				 "%s/expected/%s.c",
+				 outputdir, testname_dash.data);
+	if (!file_exists(expectfile_source))
+		snprintf(expectfile_source, sizeof(expectfile_source),
+				 "%s/expected/%s.c",
+				 inputdir, testname_dash.data);
 
 	snprintf(outfile_stdout, sizeof(outfile_stdout),
 			 "%s/results/%s.stdout",
diff --git a/src/test/regress/pg_regress.c b/src/test/regress/pg_regress.c
index 982801e029d..3bab881340d 100644
--- a/src/test/regress/pg_regress.c
+++ b/src/test/regress/pg_regress.c
@@ -76,6 +76,7 @@ _stringlist *dblist = NULL;
 bool		debug = false;
 char	   *inputdir = ".";
 char	   *outputdir = ".";
+char       *expecteddir = ".";
 char	   *bindir = PGBINDIR;
 char	   *launcher = NULL;
 static _stringlist *loadextension = NULL;
@@ -1989,6 +1990,7 @@ help(void)
 	printf(_("      --debug                   turn on debug mode in programs that are run\n"));
 	printf(_("      --dlpath=DIR              look for dynamic libraries in DIR\n"));
 	printf(_("      --encoding=ENCODING       use ENCODING as the encoding\n"));
+	printf(_("      --expecteddir=DIR         take expected files from DIR (default \".\")\n"));
 	printf(_("  -h, --help                    show this help, then exit\n"));
 	printf(_("      --inputdir=DIR            take input files from DIR (default \".\")\n"));
 	printf(_("      --launcher=CMD            use CMD as launcher of psql\n"));
@@ -2052,6 +2054,7 @@ regression_main(int argc, char *argv[],
 		{"load-extension", required_argument, NULL, 22},
 		{"config-auth", required_argument, NULL, 24},
 		{"max-concurrent-tests", required_argument, NULL, 25},
+		{"expecteddir", required_argument, NULL, 26},
 		{NULL, 0, NULL, 0}
 	};
 
@@ -2181,6 +2184,9 @@ regression_main(int argc, char *argv[],
 			case 25:
 				max_concurrent_tests = atoi(optarg);
 				break;
+			case 26:
+				expecteddir = pg_strdup(optarg);
+				break;
 			default:
 				/* getopt_long already emitted a complaint */
 				fprintf(stderr, _("\nTry \"%s -h\" for more information.\n"),
@@ -2220,6 +2226,7 @@ regression_main(int argc, char *argv[],
 
 	inputdir = make_absolute_path(inputdir);
 	outputdir = make_absolute_path(outputdir);
+	expecteddir = make_absolute_path(expecteddir);
 	dlpath = make_absolute_path(dlpath);
 
 	/*
diff --git a/src/test/regress/pg_regress.h b/src/test/regress/pg_regress.h
index 2143ee0e727..d8772fec8ed 100644
--- a/src/test/regress/pg_regress.h
+++ b/src/test/regress/pg_regress.h
@@ -53,6 +53,7 @@ extern _stringlist *dblist;
 extern bool debug;
 extern char *inputdir;
 extern char *outputdir;
+extern char *expecteddir;
 extern char *launcher;
 
 extern const char *basic_diff_opts;
-- 
2.37.0.3.g30cc8d0f14

