v5-0001-Add-read_file_ends-helper-to-Utils.pm.patch
text/x-patch
Filename: v5-0001-Add-read_file_ends-helper-to-Utils.pm.patch
Type: text/x-patch
Part: 0
From f3a4bbd092b545a519a4f28281ab14ffadb95fec Mon Sep 17 00:00:00 2001
From: Nazir Bilal Yavuz <byavuz81@gmail.com>
Date: Thu, 4 Dec 2025 14:45:45 +0300
Subject: [PATCH v5 1/2] Add read_file_ends() helper to Utils.pm
The read_file_ends() function reads lines from a file, from both head
and tail directions. It takes how many lines to read as a 'line_count'
argument. If the 'PG_TEST_FILE_READ_LINES' environment variable is set,
it overrides the 'line_count'.
Suggested-by: Michael Paquier <michael@paquier.xyz>
Discussion: https://postgr.es/m/CAN55FZ1D6KXvjSs7YGsDeadqCxNF3UUhjRAfforzzP0k-cE%3DbA%40mail.gmail.com
---
src/test/perl/PostgreSQL/Test/Utils.pm | 52 ++++++++++++++++++++++++++
doc/src/sgml/regress.sgml | 8 ++++
2 files changed, 60 insertions(+)
diff --git a/src/test/perl/PostgreSQL/Test/Utils.pm b/src/test/perl/PostgreSQL/Test/Utils.pm
index 85d36a3171e..0291c43102f 100644
--- a/src/test/perl/PostgreSQL/Test/Utils.pm
+++ b/src/test/perl/PostgreSQL/Test/Utils.pm
@@ -68,6 +68,7 @@ our @EXPORT = qw(
slurp_file
append_to_file
string_replace_file
+ read_file_ends
check_mode_recursive
chmod_recursive
check_pg_config
@@ -588,6 +589,57 @@ sub string_replace_file
return;
}
+=pod
+
+=item read_file_ends(filename, line_count)
+
+Return $line_count lines from the head and tail of a given file.
+
+If the [2 * $line_count] is more than size of the file, return all lines of
+the given file in the head variable.
+
+If the PG_TEST_FILE_READ_LINES environment variable is set, use it instead of
+the line_count variable.
+
+=cut
+
+sub read_file_ends
+{
+ my ($filename, $line_count) = @_;
+ my (@head, @tail);
+
+ # Use the PG_TEST_FILE_READ_LINES environment variable if it is set
+ if (defined $ENV{PG_TEST_FILE_READ_LINES})
+ {
+ $line_count = $ENV{PG_TEST_FILE_READ_LINES};
+ }
+
+ return ([], 0, [], 0, 0, $line_count) if $line_count <= 0;
+
+ open my $fh, '<', $filename or die "couldn't open file: $filename\n";
+ my @lines = <$fh>;
+ close $fh;
+
+ chomp @lines;
+
+ my $total = scalar @lines;
+
+ # If the file is small, return all lines in head variable
+ if (2 * $line_count >= $total)
+ {
+ @head = @lines;
+ @tail = ();
+ return (\@head, scalar(@head), \@tail, scalar(@tail), 1, $line_count);
+ }
+
+ @head = @lines[ 0 .. $line_count - 1 ];
+ @tail = @lines[ $total - $line_count .. $total - 1 ];
+
+ return (\@head, scalar(@head), \@tail, scalar(@tail), 0, $line_count);
+}
+
+
+
=pod
=item check_mode_recursive(dir, expected_dir_mode, expected_file_mode, ignore_list)
diff --git a/doc/src/sgml/regress.sgml b/doc/src/sgml/regress.sgml
index fd1e142d559..5fed320c470 100644
--- a/doc/src/sgml/regress.sgml
+++ b/doc/src/sgml/regress.sgml
@@ -546,6 +546,14 @@ make check EXTRA_TESTS=numeric_big
can run <command>diff</command> yourself, if you prefer.)
</para>
+ <para>
+ For certain tests, the environment variable
+ <envar>PG_TEST_FILE_READ_LINES</envar> can be set to limit the number of
+ lines read from the output files. This is useful when test output files
+ are large or contain unnecessary content, allowing the test framework to
+ read only a specified number of lines for comparison.
+ </para>
+
<para>
If for some reason a particular platform generates a <quote>failure</quote>
for a given test, but inspection of the output convinces you that
--
2.51.0