From 9af435039575fa76dcb710f40d87d3e792e2f6d2 Mon Sep 17 00:00:00 2001 From: Nazir Bilal Yavuz Date: Fri, 18 Jul 2025 10:34:28 +0300 Subject: [PATCH v3 1/3] Add trim_file() helper to Utils.pm The trim_file() function trims lines from a file, either from the head or the tail, based on the provided direction. It takes how many lines to trim as a 'line_count' argument but if 'PG_TEST_FILE_TRIM_LINES' environment variable is set, it overrides the 'line_count'. Suggested-by: Michael Paquier Discussion: https://postgr.es/m/CAN55FZ1D6KXvjSs7YGsDeadqCxNF3UUhjRAfforzzP0k-cE%3DbA%40mail.gmail.com --- src/test/perl/PostgreSQL/Test/Utils.pm | 53 ++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/test/perl/PostgreSQL/Test/Utils.pm b/src/test/perl/PostgreSQL/Test/Utils.pm index 85d36a3171e..42ada64d5b7 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 + trim_file check_mode_recursive chmod_recursive check_pg_config @@ -590,6 +591,58 @@ sub string_replace_file =pod +=item trim_file(filename, direction, line_count) + +Return lines from the head or tail of given file. + +=cut + +sub trim_file +{ + my ($filename, $direction, $line_count) = @_; + my @lines; + + if ($direction ne 'tail' && $direction ne 'head') + { + die + "trim_file(_, direction, _) direction should be either 'tail' or 'head'."; + } + + my $temp_env_var = $ENV{PG_TEST_FILE_TRIM_LINES}; + if (defined $temp_env_var) + { + $line_count = $temp_env_var; + } + if ($line_count <= 0) + { + return; + } + + open my $fh, '<', $filename or die "couldn't open file: $filename\n"; + + if ($direction eq 'head') + { + while (my $line = <$fh>) + { + push @lines, $line; + last if $. == $line_count; + } + } + elsif ($direction eq 'tail') + { + while (my $line = <$fh>) + { + push @lines, $line; + shift @lines if @lines > $line_count; + } + } + + close $fh; + return @lines; +} + +=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, -- 2.50.0