All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Đoàn Trần Công Danh" <congdanhqx@gmail.com>
To: git@vger.kernel.org
Cc: "Đoàn Trần Công Danh" <congdanhqx@gmail.com>,
	"Eric Sunshine" <sunshine@sunshineco.com>,
	"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>,
	"Junio C Hamano" <gitster@pobox.com>
Subject: [PATCH 1/4] test-lib-functions: introduce test_line_count_cmd
Date: Sat, 12 Jun 2021 11:27:52 +0700	[thread overview]
Message-ID: <20210612042755.28342-2-congdanhqx@gmail.com> (raw)
In-Reply-To: <20210612042755.28342-1-congdanhqx@gmail.com>

In Git project, we have multiple occasions that requires checking number
of lines of text in stdout and/or stderr of a command. One of such
example is t6400, which checks number of files in various states.

Some of those commands are Git command, and we would like to check their
exit status.  In some of those checks, we pipe the stdout of those
commands to "wc -l" to check for line count, thus loosing the exit status.

Introduce a helper function to check for number of lines in stdout and
stderr from those commands.

This helper will create 2 temporary files in process, thus it may affect
output of some checks.

Signed-off-by: Đoàn Trần Công Danh <congdanhqx@gmail.com>
---

Notes:
    Theoretically, we could avoid those temporary files by this shenanigan:
    
    	! (
    	  test $(
    		(
    		  test $(
    			  ( "$@" || echo "'$*' run into failure" >&3) | wc -l
    			)
    			"$out_ops" "$out_val" ||
    		  echo "stdout: !$outop $outval '$*'" >&3
    		) 2>&1 | wc -l
    	  ) "$errop" "$errval" ||
    	  echo "stderr: !$errop $errval '$*'" >&3
    	) 3>&1 | grep .
    
    However, it looks too complicated.

 t/test-lib-functions.sh | 64 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 64 insertions(+)

diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
index b823c14027..85bb31ea4c 100644
--- a/t/test-lib-functions.sh
+++ b/t/test-lib-functions.sh
@@ -817,6 +817,70 @@ test_line_count () {
 	fi
 }
 
+# test_line_count_cmd checks the number of lines of captured stdout and/or
+# stderr of a command.
+#
+# NOTE: this helper function will create 2 temporary files named:
+# * test_line_count_cmd_.out; and
+# * test_line_count_cmd_.err
+#
+# And this helper function will remove those 2 files if the check is succeed.
+# In case of failure, those files will be preserved.
+test_line_count_cmd () {
+	local outop outval
+	local errop errval
+
+	while test $# -ge 3
+	do
+		case "$1" in
+		--out)
+			outop="$2"
+			outval="$3"
+			;;
+		--err)
+			errop="$2"
+			errval="$3"
+			;;
+		*)
+			break
+			;;
+		esac
+		shift 3
+	done &&
+	if test $# = 0 ||
+	   { test "x$1" = "x!" && test $# = 1 ; }
+	then
+		BUG "test_line_count_cmd: no command to be run"
+	fi &&
+	if test -z "$outop$errop"
+	then
+		BUG "test_line_count_cmd: check which stream?"
+	fi &&
+
+	if test "x$1" = "x!" 
+	then
+		shift &&
+		if "$@" >test_line_count_cmd_.out 2>test_line_count_cmd_.err
+		then
+			echo "error: '$@' succeed!"
+			return 1
+		fi
+	elif ! "$@" >test_line_count_cmd_.out 2>test_line_count_cmd_.err
+	then
+		echo "error: '$@' run into failure!"
+		return 1
+	fi &&
+	if test -n "$outop"
+	then
+		test_line_count "$outop" "$outval" test_line_count_cmd_.out
+	fi &&
+	if test -n "$errop"
+	then
+		test_line_count "$errop" "$errval" test_line_count_cmd_.err
+	fi &&
+	rm -f test_line_count_cmd_.out test_line_count_cmd_.err
+}
+
 test_file_size () {
 	test "$#" -ne 1 && BUG "1 param"
 	test-tool path-utils file-size "$1"
-- 
2.32.0.278.gd42b80f139


  reply	other threads:[~2021-06-12  4:29 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-12  4:27 [PATCH 0/4] t: new helper test_line_count_cmd Đoàn Trần Công Danh
2021-06-12  4:27 ` Đoàn Trần Công Danh [this message]
2021-06-13  3:10   ` [PATCH 1/4] test-lib-functions: introduce test_line_count_cmd Eric Sunshine
2021-06-13  7:36     ` Đoàn Trần Công Danh
2021-06-13 13:28   ` Ævar Arnfjörð Bjarmason
2021-06-13 16:37     ` Đoàn Trần Công Danh
2021-06-13 18:18     ` Phillip Wood
2021-06-13 21:42       ` Felipe Contreras
2021-06-13 23:43       ` Eric Sunshine
2021-06-14  2:56         ` Junio C Hamano
2021-06-24 23:19         ` Ævar Arnfjörð Bjarmason
2021-06-13 13:36   ` Ævar Arnfjörð Bjarmason
2021-06-14  3:01     ` Junio C Hamano
2021-06-15 15:40     ` Đoàn Trần Công Danh
2021-06-12  4:27 ` [PATCH 2/4] t6402: use find(1) builtin to filter instead of grep Đoàn Trần Công Danh
2021-06-12  4:27 ` [PATCH 3/4] t6400: use test_line_count_cmd to count # of lines in stdout Đoàn Trần Công Danh
2021-06-12  4:33   ` Bagas Sanjaya
2021-06-13  7:39     ` Đoàn Trần Công Danh
2021-06-13  3:39   ` Eric Sunshine
2021-06-13  7:42     ` Đoàn Trần Công Danh
2021-06-12  4:27 ` [PATCH 4/4] t6402: " Đoàn Trần Công Danh
2021-06-13  3:43   ` Eric Sunshine

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210612042755.28342-2-congdanhqx@gmail.com \
    --to=congdanhqx@gmail.com \
    --cc=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=sunshine@sunshineco.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.