Re: [PATCH v2 2/2] t1006: ensure cat-file info isn't buffered by default

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Jun 18, 2024 at 09:30:41PM +0000, Eric Wong wrote:

> +script='
> +use warnings;
> +use strict;
> +use IPC::Open2;
> +my ($opt, $oid, $expect, @pfx) = @ARGV;
> +my @cmd = (qw(git cat-file), $opt);
> +my $pid = open2(my $out, my $in, @cmd) or die "open2: @cmd";
> +print $in @pfx, $oid, "\n" or die "print $!";
> +my $rvec = "";
> +vec($rvec, fileno($out), 1) = 1;
> +select($rvec, undef, undef, 30) or die "no response to `@pfx $oid` from @cmd";
> +my $info = <$out>;
> +chop($info) eq "\n" or die "no LF";
> +$info eq $expect or die "`$info` != `$expect`";
> +close $in or die "close in $!";
> +close $out or die "close out $!";
> +waitpid $pid, 0;
> +$? == 0 or die "\$?=$?";
> +'
> +
> +expect="$hello_oid blob $hello_size"
> +
> +test_expect_success PERL '--batch-check is unbuffered by default' '
> +	perl -e "$script" -- --batch-check $hello_oid "$expect"
> +'

We often use "perl -e" for one-liners, etc, but this is pretty big.
Maybe:

  cat >foo.pl <<-\EOF
  ...
  EOF
  perl foo.pl -- ...

would be more readable? To be clear I don't think there's anything
incorrect about your usage, but it would match the style of our suite a
bit better.

Likewise, it would be usual in our suite for the helper to do the
minimum that needs to be in perl, and use our normal functions for
things like comparing output (rather than taking its own "expect"
argument).

So maybe:

diff --git a/t/t1006-cat-file.sh b/t/t1006-cat-file.sh
index e12b221972..929d7a7579 100755
--- a/t/t1006-cat-file.sh
+++ b/t/t1006-cat-file.sh
@@ -1294,4 +1294,33 @@ test_expect_success 'batch-command flush without --buffer' '
 	grep "^fatal:.*flush is only for --buffer mode.*" err
 '
 
+# Copy a single line from stdin to the program specified
+# by @ARGV, and then wait for a response _without_ closing
+# the pipe.
+cat >run-and-wait.pl <<-\EOF
+use IPC::Open2;
+open2(my $out, my $in, @ARGV) or die "open2: @ARGV";
+print $in scalar(<STDIN>) or die "print $!";
+
+my $rvec = "";
+vec($rvec, fileno($out), 1) = 1;
+select($rvec, undef, undef, 30) or die "no response after 30 seconds";
+
+print scalar(<$out>);
+EOF
+
+test_expect_success PERL '--batch-check is unbuffered by default' '
+	echo "$hello_oid" |
+	perl run-and-wait.pl git cat-file --batch-check >out &&
+	echo "$hello_oid blob $hello_size" >expect &&
+	test_cmp expect out
+'
+
+test_expect_success PERL '--batch-command info is unbuffered by default' '
+	echo "info $hello_oid" |
+	perl run-and-wait.pl git cat-file --batch-command >out &&
+	echo "$hello_oid blob $hello_size" >expect &&
+	test_cmp expect out
+'
+
 test_done

I went for brevity above. Notably missing are:

  - the use of strict/warnings. I think we've shied away from these in
    the test suite because we want to run on any version of perl. In my
    experience most strict/warnings output is actually telling you about
    obvious garbage, but not always. IIRC perl got more strict about
    "()" around lists in some contexts a few years back, and code which
    used to be OK started generating warnings. OTOH, those warnings were
    probably a sign of problems-to-come, anyway. Without "FATAL",
    though, I think "use warnings" is not doing much good (nobody is
    ever going to see its output if the test isn't failing).

  - I dropped the close/waitpid. I guess maybe it is valuable to confirm
    that cat-file did not barf, but IMHO the important thing here is
    testing that it produced the single line of output we expected.

-Peff




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux