From d728f54e5c4f9f2dec92a57ece528b43859800f7 Mon Sep 17 00:00:00 2001
From: Jelte Fennema-Nio <postgres@jeltef.nl>
Date: Mon, 26 Jan 2026 00:15:30 +0100
Subject: [PATCH v1 1/5] meson: Include pg_regress diffs in meson output

Whenever pg_regress fails there's an indirection to actually get to the
failure reason. When running tests locally it's somewhat okay because I
can copy paste the filename, but in CI I have to manually traverse the
directory structure by clicking and scrolling a bunch of time.

This change adds an option to pg_regress to print the diffs so that they
become part of the TAP output. So meson can actually display them.
---
 meson.build                   |  1 +
 src/test/regress/pg_regress.c | 41 ++++++++++++++++++++++++++++-------
 2 files changed, 34 insertions(+), 8 deletions(-)

diff --git a/meson.build b/meson.build
index df907b62da3..bbe855a3cb5 100644
--- a/meson.build
+++ b/meson.build
@@ -3738,6 +3738,7 @@ foreach test_dir : tests
         '--bindir', '',
         '--dlpath', test_dir['bd'],
         '--max-concurrent-tests=20',
+        '--print-diffs',
         '--dbname', dbname,
       ] + t.get('regress_args', [])
 
diff --git a/src/test/regress/pg_regress.c b/src/test/regress/pg_regress.c
index b5c0cb647a8..d057573af57 100644
--- a/src/test/regress/pg_regress.c
+++ b/src/test/regress/pg_regress.c
@@ -119,6 +119,7 @@ static char *dlpath = PKGLIBDIR;
 static char *user = NULL;
 static _stringlist *extraroles = NULL;
 static char *config_auth_datadir = NULL;
+static bool print_diffs = false;
 
 /* internal variables */
 static const char *progname;
@@ -1414,6 +1415,7 @@ results_differ(const char *testname, const char *resultsfile, const char *defaul
 	int			best_line_count;
 	int			i;
 	int			l;
+	long		startpos;
 	const char *platform_expectfile;
 
 	/*
@@ -1521,22 +1523,40 @@ results_differ(const char *testname, const char *resultsfile, const char *defaul
 	 * append to the diffs summary file.
 	 */
 
-	/* Write diff header */
-	difffile = fopen(difffilename, "a");
+	difffile = fopen(difffilename, "a+");
 	if (difffile)
 	{
+		fseek(difffile, 0, SEEK_END);
+		startpos = ftell(difffile);
 		fprintf(difffile,
 				"diff %s %s %s\n",
 				pretty_diff_opts, best_expect_file, resultsfile);
+		fflush(difffile);
+
+		/* Run diff */
+		snprintf(cmd, sizeof(cmd),
+				 "diff %s \"%s\" \"%s\" >> \"%s\"",
+				 pretty_diff_opts, best_expect_file, resultsfile, difffilename);
+		run_diff(cmd, difffilename);
+
+		/* Emit diff as TAP diagnostics if requested */
+		if (print_diffs)
+		{
+			char		line[1024];
+
+			fseek(difffile, startpos, SEEK_SET);
+			while (fgets(line, sizeof(line), difffile))
+			{
+				size_t		len = strlen(line);
+
+				if (len > 0 && line[len - 1] == '\n')
+					line[len - 1] = '\0';
+				diag("%s", line);
+			}
+		}
 		fclose(difffile);
 	}
 
-	/* Run diff */
-	snprintf(cmd, sizeof(cmd),
-			 "diff %s \"%s\" \"%s\" >> \"%s\"",
-			 pretty_diff_opts, best_expect_file, resultsfile, difffilename);
-	run_diff(cmd, difffilename);
-
 	unlink(diff);
 	return true;
 }
@@ -2044,6 +2064,7 @@ help(void)
 	printf(_("      --outputdir=DIR           place output files in DIR (default \".\")\n"));
 	printf(_("      --schedule=FILE           use test ordering schedule from FILE\n"));
 	printf(_("                                (can be used multiple times to concatenate)\n"));
+	printf(_("      --print-diffs             print diffs to stdout on failure\n"));
 	printf(_("      --temp-instance=DIR       create a temporary instance in DIR\n"));
 	printf(_("      --use-existing            use an existing installation\n"));
 	printf(_("  -V, --version                 output version information, then exit\n"));
@@ -2096,6 +2117,7 @@ regression_main(int argc, char *argv[],
 		{"config-auth", required_argument, NULL, 24},
 		{"max-concurrent-tests", required_argument, NULL, 25},
 		{"expecteddir", required_argument, NULL, 26},
+		{"print-diffs", no_argument, NULL, 27},
 		{NULL, 0, NULL, 0}
 	};
 
@@ -2224,6 +2246,9 @@ regression_main(int argc, char *argv[],
 			case 26:
 				expecteddir = pg_strdup(optarg);
 				break;
+			case 27:
+				print_diffs = true;
+				break;
 			default:
 				/* getopt_long already emitted a complaint */
 				pg_log_error_hint("Try \"%s --help\" for more information.",

base-commit: a9bdb63bba8a631cd4797393307eecf5fcde9167
-- 
2.52.0

