Refactor TAP test code for file comparisons into new routine in Utils.pm
authorMichael Paquier
Sun, 9 Feb 2025 07:52:33 +0000 (16:52 +0900)
committerMichael Paquier
Sun, 9 Feb 2025 07:52:33 +0000 (16:52 +0900)
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/[email protected]

src/bin/pg_combinebackup/t/002_compare_backups.pl
src/bin/pg_upgrade/t/002_pg_upgrade.pl
src/test/perl/PostgreSQL/Test/Utils.pm
src/test/recovery/t/027_stream_regress.pl

index ebd68bfb850860d1ae4b4b0a025ed2fe453b2210..2c7ca89b92f7f631d3e6d4d731a9dc90df0d91c3 100644 (file)
@@ -192,27 +192,12 @@ $pitr2->command_ok(
 
 # Compare the two dumps, there should be no differences other than
 # the tablespace paths.
-my $compare_res = compare_text(
+compare_files(
    $dump1, $dump2,
+   "contents of dumps match for both PITRs",
    sub {
        s{create tablespace .* location .*\btspitr\K[12]}{N}i for @_;
        return $_[0] ne $_[1];
    });
-note($dump1);
-note($dump2);
-is($compare_res, 0, "dumps are identical");
-
-# Provide more context if the dumps do not match.
-if ($compare_res != 0)
-{
-   my ($stdout, $stderr) =
-     run_command([ 'diff', '-u', $dump1, $dump2 ]);
-   print "=== diff of $dump1 and $dump2\n";
-   print "=== stdout ===\n";
-   print $stdout;
-   print "=== stderr ===\n";
-   print $stderr;
-   print "=== EOF ===\n";
-}
 
 done_testing();
index e49bff6454ae9af0fe75b3ad0e7d9212ed734678..68516fa486abc0a77fce9697d35b9425aba80f4a 100644 (file)
@@ -6,9 +6,8 @@ use warnings FATAL => 'all';
 
 use Cwd            qw(abs_path);
 use File::Basename qw(dirname);
-use File::Compare;
-use File::Find qw(find);
-use File::Path qw(rmtree);
+use File::Find     qw(find);
+use File::Path     qw(rmtree);
 
 use PostgreSQL::Test::Cluster;
 use PostgreSQL::Test::Utils;
@@ -515,20 +514,7 @@ my $dump1_filtered = filter_dump(1, $oldnode->pg_version, $dump1_file);
 my $dump2_filtered = filter_dump(0, $oldnode->pg_version, $dump2_file);
 
 # Compare the two dumps, there should be no differences.
-my $compare_res = compare($dump1_filtered, $dump2_filtered);
-is($compare_res, 0, 'old and new dumps match after pg_upgrade');
-
-# Provide more context if the dumps do not match.
-if ($compare_res != 0)
-{
-   my ($stdout, $stderr) =
-     run_command([ 'diff', '-u', $dump1_filtered, $dump2_filtered ]);
-   print "=== diff of $dump1_filtered and $dump2_filtered\n";
-   print "=== stdout ===\n";
-   print $stdout;
-   print "=== stderr ===\n";
-   print $stderr;
-   print "=== EOF ===\n";
-}
+compare_files($dump1_filtered, $dump2_filtered,
+   'old and new dumps match after pg_upgrade');
 
 done_testing();
index 9c83d93f79f260da6182480e55cb6cea61151740..efe0321a4efd2418922043229d8022bb82b38754 100644 (file)
@@ -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 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
index bab7b28084bc44cddec6028edde9560b2d623edc..0eac8f66a9c13415dfa90195b82fa3721d93bcd3 100644 (file)
@@ -120,8 +120,9 @@ command_ok(
        '--port' => $node_standby_1->port,
    ],
    'dump standby server');
-command_ok(
-   [ 'diff', $outputdir . '/primary.dump', $outputdir . '/standby.dump', ],
+compare_files(
+   $outputdir . '/primary.dump',
+   $outputdir . '/standby.dump',
    'compare primary and standby dumps');
 
 # Likewise for the catalogs of the regression database, after disabling
@@ -150,12 +151,9 @@ command_ok(
        'regression',
    ],
    'dump catalogs of standby server');
-command_ok(
-   [
-       'diff',
-       $outputdir . '/catalogs_primary.dump',
-       $outputdir . '/catalogs_standby.dump',
-   ],
+compare_files(
+   $outputdir . '/catalogs_primary.dump',
+   $outputdir . '/catalogs_standby.dump',
    'compare primary and standby catalog dumps');
 
 # Check some data from pg_stat_statements.