aboutsummaryrefslogtreecommitdiff
path: root/src/interfaces/ecpg/test
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2021-01-11 13:43:19 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2021-01-11 13:43:19 -0500
commit800d93f314b0f7c10193e48b259f87800cb85d84 (patch)
tree2a4bd15b5916882d869857ae93a2e85c55297daf /src/interfaces/ecpg/test
parentffa2e4670123124b92f037d335a1e844c3782d3f (diff)
downloadpostgresql-800d93f314b0f7c10193e48b259f87800cb85d84.tar.gz
postgresql-800d93f314b0f7c10193e48b259f87800cb85d84.zip
Allow pg_regress.c wrappers to postprocess test result files.
Add an optional callback to regression_main() that, if provided, is invoked on each test output file before we try to compare it to the expected-result file. The main and isolation test programs don't need this (yet). In pg_regress_ecpg, add a filter that eliminates target-host details from "could not connect" error reports. This filter doesn't do anything as of this commit, but it will be needed by the next one. In the long run we might want to provide some more general, perhaps pattern-based, filtering mechanism for test output. For now, this will solve the immediate problem. Discussion: https://postgr.es/m/BN6PR05MB3492948E4FD76C156E747E8BC9160@BN6PR05MB3492.namprd05.prod.outlook.com
Diffstat (limited to 'src/interfaces/ecpg/test')
-rw-r--r--src/interfaces/ecpg/test/pg_regress_ecpg.c95
1 files changed, 88 insertions, 7 deletions
diff --git a/src/interfaces/ecpg/test/pg_regress_ecpg.c b/src/interfaces/ecpg/test/pg_regress_ecpg.c
index 31dd507ea0a..8d43fd65bad 100644
--- a/src/interfaces/ecpg/test/pg_regress_ecpg.c
+++ b/src/interfaces/ecpg/test/pg_regress_ecpg.c
@@ -23,13 +23,16 @@
#include "lib/stringinfo.h"
+/*
+ * Create a filtered copy of sourcefile, removing any path
+ * appearing in #line directives; for example, replace
+ * #line x "./../bla/foo.h" with #line x "foo.h".
+ * This is needed because the path part can vary depending
+ * on compiler, platform, build options, etc.
+ */
static void
-ecpg_filter(const char *sourcefile, const char *outfile)
+ecpg_filter_source(const char *sourcefile, const char *outfile)
{
- /*
- * Create a filtered copy of sourcefile, replacing #line x
- * "./../bla/foo.h" with #line x "foo.h"
- */
FILE *s,
*t;
StringInfoData linebuf;
@@ -77,6 +80,66 @@ ecpg_filter(const char *sourcefile, const char *outfile)
}
/*
+ * Remove the details of "could not connect to ...: " error messages
+ * in a test result file, since the target host/pathname and/or port
+ * can vary. Rewrite the result file in-place.
+ *
+ * At some point it might be interesting to unify this with
+ * ecpg_filter_source, but building a general pattern matcher
+ * is no fun, nor does it seem desirable to introduce a
+ * dependency on an external one.
+ */
+static void
+ecpg_filter_stderr(const char *resultfile, const char *tmpfile)
+{
+ FILE *s,
+ *t;
+ StringInfoData linebuf;
+
+ s = fopen(resultfile, "r");
+ if (!s)
+ {
+ fprintf(stderr, "Could not open file %s for reading\n", resultfile);
+ exit(2);
+ }
+ t = fopen(tmpfile, "w");
+ if (!t)
+ {
+ fprintf(stderr, "Could not open file %s for writing\n", tmpfile);
+ exit(2);
+ }
+
+ initStringInfo(&linebuf);
+
+ while (pg_get_line_buf(s, &linebuf))
+ {
+ char *p1 = strstr(linebuf.data, "could not connect to ");
+
+ if (p1)
+ {
+ char *p2 = strstr(p1, ": ");
+
+ if (p2)
+ {
+ memmove(p1 + 17, p2, strlen(p2) + 1);
+ /* we don't bother to fix up linebuf.len */
+ }
+ }
+ fputs(linebuf.data, t);
+ }
+
+ pfree(linebuf.data);
+ fclose(s);
+ fclose(t);
+ if (rename(tmpfile, resultfile) != 0)
+ {
+ fprintf(stderr, "Could not overwrite file %s with %s\n",
+ resultfile, tmpfile);
+ exit(2);
+ }
+}
+
+/*
* start an ecpg test process for specified file (including redirection),
* and return process ID
*/
@@ -139,7 +202,7 @@ ecpg_start_test(const char *testname,
add_stringlist_item(expectfiles, expectfile_source);
add_stringlist_item(tags, "source");
- ecpg_filter(insource, outfile_source);
+ ecpg_filter_source(insource, outfile_source);
snprintf(cmd, sizeof(cmd),
"\"%s\" >\"%s\" 2>\"%s\"",
@@ -168,6 +231,21 @@ ecpg_start_test(const char *testname,
}
static void
+ecpg_postprocess_result(const char *filename)
+{
+ int nlen = strlen(filename);
+
+ /* Only stderr files require filtering, at the moment */
+ if (nlen > 7 && strcmp(filename + nlen - 7, ".stderr") == 0)
+ {
+ char *tmpfile = psprintf("%s.tmp", filename);
+
+ ecpg_filter_stderr(filename, tmpfile);
+ pfree(tmpfile);
+ }
+}
+
+static void
ecpg_init(int argc, char *argv[])
{
/* nothing to do here at the moment */
@@ -176,5 +254,8 @@ ecpg_init(int argc, char *argv[])
int
main(int argc, char *argv[])
{
- return regression_main(argc, argv, ecpg_init, ecpg_start_test);
+ return regression_main(argc, argv,
+ ecpg_init,
+ ecpg_start_test,
+ ecpg_postprocess_result);
}