summaryrefslogtreecommitdiff
path: root/src/test/perl/PostgreSQL/Test/Utils.pm
diff options
context:
space:
mode:
authorMichael Paquier <michael@paquier.xyz>2025-02-09 16:52:33 +0900
committerMichael Paquier <michael@paquier.xyz>2025-02-09 16:52:33 +0900
commit169208092f5c98a6021b23b38f03a5d65f84ad96 (patch)
treedaf82ede81714635c4143d605eeccb3d42eecd91 /src/test/perl/PostgreSQL/Test/Utils.pm
parentecb8226af63dc8f1c0859977102764704368693b (diff)
Refactor TAP test code for file comparisons into new routine in Utils.pm
This unifies the output used should any differences be found in the files provided, information that 027_stream_regress did not show on failures. TAP tests of pg_combinebackup and pg_upgrade now rely on the refactored routine, reducing the dependency to the diff command. The callers of this routine can optionally specify a custom line-comparison function. There are a couple of tests that still use directly a diff command: 001_pg_bsd_indent, 017_shm and test_json_parser's 003. These rely on different properties and are left out for now. Extracted from a larger patch by the same author. Author: Ashutosh Bapat Discussion: https://postgr.es/m/Z6RQS-tMzGYjlA-H@paquier.xyz
Diffstat (limited to 'src/test/perl/PostgreSQL/Test/Utils.pm')
-rw-r--r--src/test/perl/PostgreSQL/Test/Utils.pm41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/test/perl/PostgreSQL/Test/Utils.pm b/src/test/perl/PostgreSQL/Test/Utils.pm
index 9c83d93f79f..efe0321a4ef 100644
--- a/src/test/perl/PostgreSQL/Test/Utils.pm
+++ b/src/test/perl/PostgreSQL/Test/Utils.pm
@@ -50,6 +50,7 @@ use Cwd;
use Exporter 'import';
use Fcntl qw(:mode :seek);
use File::Basename;
+use File::Compare;
use File::Find;
use File::Spec;
use File::stat qw(stat);
@@ -70,6 +71,7 @@ our @EXPORT = qw(
check_mode_recursive
chmod_recursive
check_pg_config
+ compare_files
dir_symlink
scan_server_header
system_or_bail
@@ -773,6 +775,45 @@ sub check_pg_config
=pod
+=item compare_files(file1, file2, testname)
+
+Check that two files match, printing the difference if any.
+
+C<line_comp_function> is an optional CODE reference to a line comparison
+function, passed down as-is to File::Compare::compare_text.
+
+=cut
+
+sub compare_files
+{
+ my ($file1, $file2, $testname, $line_comp_function) = @_;
+
+ # If nothing is given, all lines should be equal.
+ $line_comp_function = sub { $_[0] ne $_[1] }
+ unless defined $line_comp_function;
+
+ my $compare_res =
+ File::Compare::compare_text($file1, $file2, $line_comp_function);
+ is($compare_res, 0, $testname);
+
+ # Provide more context if the files do not match.
+ if ($compare_res != 0)
+ {
+ my ($stdout, $stderr) =
+ run_command([ 'diff', '-u', $file1, $file2 ]);
+ print "=== diff of $file1 and $file2\n";
+ print "=== stdout ===\n";
+ print $stdout;
+ print "=== stderr ===\n";
+ print $stderr;
+ print "=== EOF ===\n";
+ }
+
+ return;
+}
+
+=pod
+
=item dir_symlink(oldname, newname)
Portably create a symlink for a directory. On Windows this creates a junction