diff options
| author | Michael Paquier <michael@paquier.xyz> | 2025-12-06 14:27:53 +0900 |
|---|---|---|
| committer | Michael Paquier <michael@paquier.xyz> | 2025-12-06 14:27:53 +0900 |
| commit | b93f4e2f98b384596a307b6ce54ce2affadc518a (patch) | |
| tree | 98e5bf776c93aa67fce7e79af6926d9f8f302bf4 /src/test/perl/PostgreSQL/Test | |
| parent | 6dfce8420e99d8cf41ffb7da698caee57fd73eb7 (diff) | |
Add PostgreSQL::Test::Cluster::read_head_tail() helper to PostgreSQL/Utils.pm
This function reads the lines from a file and filters its contents to
report its head and tail contents. The amount of contents to read from
a file can be tuned by the environment variable PG_TEST_FILE_READ_LINES,
that can be used to override the default of 50 lines. If the file whose
content is read has less lines than two times PG_TEST_FILE_READ_LINES,
the whole file is returned.
This will be used in a follow-up commit to limit the amount of
information reported by some of the TAP tests on failure, where we have
noticed that the contents reported by the buildfarm can be heavily
bloated in some cases, with the head and tail contents of a report being
able to provide enough information to be useful for debugging.
Author: Nazir Bilal Yavuz <byavuz81@gmail.com>
Co-authored-by: Michael Paquier <michael@paquier.xyz>
Discussion: https://postgr.es/m/CAN55FZ1D6KXvjSs7YGsDeadqCxNF3UUhjRAfforzzP0k-cE=bA@mail.gmail.com
Diffstat (limited to 'src/test/perl/PostgreSQL/Test')
| -rw-r--r-- | src/test/perl/PostgreSQL/Test/Utils.pm | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/test/perl/PostgreSQL/Test/Utils.pm b/src/test/perl/PostgreSQL/Test/Utils.pm index 85d36a3171e..0332d28916e 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_head_tail check_mode_recursive chmod_recursive check_pg_config @@ -590,6 +591,55 @@ sub string_replace_file =pod +=item read_head_tail(filename) + +Return lines from the head and the tail of a file. If the file is smaller +than the number of lines requested, all its contents are returned in @head, +leaving @tail empty. + +If the PG_TEST_FILE_READ_LINES environment variable is set, use it instead +of the default of 50 lines. + +=cut + +sub read_head_tail +{ + my $filename = shift; + my (@head, @tail); + my $line_count = 50; + + # Use PG_TEST_FILE_READ_LINES if set. + if (defined $ENV{PG_TEST_FILE_READ_LINES}) + { + $line_count = $ENV{PG_TEST_FILE_READ_LINES}; + } + + return ([], []) 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 enough, return all lines in @head. + if (2 * $line_count >= $total) + { + @head = @lines; + @tail = (); + return (\@head, \@tail); + } + + @head = @lines[ 0 .. $line_count - 1 ]; + @tail = @lines[ $total - $line_count .. $total - 1 ]; + + return (\@head, \@tail); +} + +=pod + =item check_mode_recursive(dir, expected_dir_mode, expected_file_mode, ignore_list) Check that all file/dir modes in a directory match the expected values, |
