[pve-devel] [RFC qemu-server 3/3] t/cfg2cmd: replace is_deeply with diff

Wolfgang Bumiller w.bumiller at proxmox.com
Fri Dec 7 10:31:19 CET 2018


(The diff() sub is copied from our pve-common's network
interfaces test scripts)

Signed-off-by: Wolfgang Bumiller <w.bumiller at proxmox.com>
---
 test/run_config2command_tests.pl | 36 +++++++++++++++++++++++++++++++++++-
 1 file changed, 35 insertions(+), 1 deletion(-)

diff --git a/test/run_config2command_tests.pl b/test/run_config2command_tests.pl
index 3fe4855..0e37c17 100755
--- a/test/run_config2command_tests.pl
+++ b/test/run_config2command_tests.pl
@@ -125,6 +125,38 @@ $qemu_server_config->mock(
     },
 );
 
+sub diff($$) {
+    my ($a, $b) = @_;
+    return if $a eq $b;
+
+    my ($ra, $wa) = POSIX::pipe();
+    my ($rb, $wb) = POSIX::pipe();
+    my $ha = IO::Handle->new_from_fd($wa, 'w');
+    my $hb = IO::Handle->new_from_fd($wb, 'w');
+
+    open my $diffproc, '-|', 'diff', '-up', "/dev/fd/$ra", "/dev/fd/$rb"
+	or die "failed to run program 'diff': $!";
+    POSIX::close($ra);
+    POSIX::close($rb);
+
+    open my $f1, '<', \$a;
+    open my $f2, '<', \$b;
+    my ($line1, $line2);
+    do {
+	$ha->print($line1) if defined($line1 = <$f1>);
+	$hb->print($line2) if defined($line2 = <$f2>);
+    } while (defined($line1 // $line2));
+    close $f1;
+    close $f2;
+    close $ha;
+    close $hb;
+
+    local $/ = undef;
+    my $diff = <$diffproc>;
+    close $diffproc;
+    die "files differ:\n$diff";
+}
+
 sub do_test($) {
     my ($config_fn) = @_;
 
@@ -156,7 +188,9 @@ sub do_test($) {
 	# comment out for easier debugging
 	#file_set_contents("$cmd_fn.tmp", $cmdline);
 
-	is_deeply($cmd, $cmd_expected, "$testname")
+	my $exp = join("\n", @$cmd_expected);
+	my $got = join("\n", @$cmd);
+	diff($exp, $got);
     } else {
 	file_set_contents($cmd_fn, $cmdline);
     }
-- 
2.11.0





More information about the pve-devel mailing list