git@vger.kernel.org mailing list mirror (one of many)
 help / Atom feed
From: Jeff King <peff@peff.net>
To: Junio C Hamano <gitster@pobox.com>
Cc: Alexander Shopov <ash@kambanaria.org>,
	git@vger.kernel.org, martin.agren@gmail.com, bmwill@google.com,
	sandals@crustytoothpaste.net, worldhello.net@gmail.com,
	j6t@kdbg.org, sunshine@sunshineco.com, pclouds@gmail.com
Subject: [PATCH] t0002: simplify error checking
Date: Sat, 10 Feb 2018 06:31:29 -0500
Message-ID: <20180210113128.GA11191@sigill.intra.peff.net> (raw)
In-Reply-To: <20180209193039.GA15554@sigill.intra.peff.net>

On Fri, Feb 09, 2018 at 02:30:39PM -0500, Jeff King wrote:

> Yes, I think so, but we may want to avoid this anti-pattern (since
> usually "! test_i18ngrep" is a sign of something wrong. It seems like
> these tests are doing more manual reporting work than is necessary, and
> could just be relying on helpers to report errors.
> 
> Something like the patch below, though I'm not sure if we'd want to
> leave it as "grep" (if applying on master), or have "test_i18ngrep" in
> the preimage (if basing on top of Alexander's patch).

Here's a version suitable for applying to master as an independent
cleanup. It will conflict with Alexander's patch, but the resolution is
pretty easy (take my side, but s/grep/test_i18ngrep/). I'm happy to do
it on top of his if that's easier.

-- >8 --
Subject: [PATCH] t0002: simplify error checking

This ancient test script does a lot of manual checking of
test conditions with "if" blocks. We can simplify this
by relying on helpers like test_must_fail.

Note that a failing "grep" call here won't produce any
verbose output, but that's OK. These days we rely on "-x" to
tell us about such commands. And in addition, these greps
are soon to be converted to test_i18ngrep (which is itself
soon learning to be more verbose).

Signed-off-by: Jeff King <peff@peff.net>
---
 t/t0002-gitfile.sh | 53 +++++++++-------------------------------------
 1 file changed, 10 insertions(+), 43 deletions(-)

diff --git a/t/t0002-gitfile.sh b/t/t0002-gitfile.sh
index 9670e8cbe6..fb8d094117 100755
--- a/t/t0002-gitfile.sh
+++ b/t/t0002-gitfile.sh
@@ -10,15 +10,6 @@ objpath() {
 	echo "$1" | sed -e 's|\(..\)|\1/|'
 }
 
-objck() {
-	p=$(objpath "$1")
-	if test ! -f "$REAL/objects/$p"
-	then
-		echo "Object not found: $REAL/objects/$p"
-		false
-	fi
-}
-
 test_expect_success 'initial setup' '
 	REAL="$(pwd)/.real" &&
 	mv .git "$REAL"
@@ -26,30 +17,14 @@ test_expect_success 'initial setup' '
 
 test_expect_success 'bad setup: invalid .git file format' '
 	echo "gitdir $REAL" >.git &&
-	if git rev-parse 2>.err
-	then
-		echo "git rev-parse accepted an invalid .git file"
-		false
-	fi &&
-	if ! grep "Invalid gitfile format" .err
-	then
-		echo "git rev-parse returned wrong error"
-		false
-	fi
+	test_must_fail git rev-parse 2>.err &&
+	grep "Invalid gitfile format" .err
 '
 
 test_expect_success 'bad setup: invalid .git file path' '
 	echo "gitdir: $REAL.not" >.git &&
-	if git rev-parse 2>.err
-	then
-		echo "git rev-parse accepted an invalid .git file path"
-		false
-	fi &&
-	if ! grep "Not a git repository" .err
-	then
-		echo "git rev-parse returned wrong error"
-		false
-	fi
+	test_must_fail git rev-parse 2>.err &&
+	grep "Not a git repository" .err
 '
 
 test_expect_success 'final setup + check rev-parse --git-dir' '
@@ -60,7 +35,7 @@ test_expect_success 'final setup + check rev-parse --git-dir' '
 test_expect_success 'check hash-object' '
 	echo "foo" >bar &&
 	SHA=$(cat bar | git hash-object -w --stdin) &&
-	objck $SHA
+	test_path_is_file "$REAL/objects/$(objpath $SHA)"
 '
 
 test_expect_success 'check cat-file' '
@@ -69,29 +44,21 @@ test_expect_success 'check cat-file' '
 '
 
 test_expect_success 'check update-index' '
-	if test -f "$REAL/index"
-	then
-		echo "Hmm, $REAL/index exists?"
-		false
-	fi &&
+	test_path_is_missing "$REAL/index" &&
 	rm -f "$REAL/objects/$(objpath $SHA)" &&
 	git update-index --add bar &&
-	if ! test -f "$REAL/index"
-	then
-		echo "$REAL/index not found"
-		false
-	fi &&
-	objck $SHA
+	test_path_is_file "$REAL/index" &&
+	test_path_is_file "$REAL/objects/$(objpath $SHA)"
 '
 
 test_expect_success 'check write-tree' '
 	SHA=$(git write-tree) &&
-	objck $SHA
+	test_path_is_file "$REAL/objects/$(objpath $SHA)"
 '
 
 test_expect_success 'check commit-tree' '
 	SHA=$(echo "commit bar" | git commit-tree $SHA) &&
-	objck $SHA
+	test_path_is_file "$REAL/objects/$(objpath $SHA)"
 '
 
 test_expect_success 'check rev-list' '
-- 
2.16.1.464.gc4bae515b7


  reply index

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-15  5:44 [PATCH 0/1] Marked end user messages for translation Alexander Shopov
2018-01-15  5:44 ` [PATCH 1/1] Mark messages for translations Alexander Shopov
2018-01-15  7:33   ` Johannes Sixt
     [not found]     ` <CAP6f5Mnn+pRdL6ihgwcqsTwyGy+EQfBbgPxkFmGLcWL-hJx-8g@mail.gmail.com>
     [not found]       ` <CAP6f5MnaMiqEMbGW_xj6X495jF=txpzeq+DEHOkz7VCg2D6D9w@mail.gmail.com>
2018-01-15  9:16         ` Alexander Shopov
2018-01-15  9:58   ` Eric Sunshine
2018-01-15 10:21   ` Duy Nguyen
2018-02-06  6:15 ` [PATCH 0/1] " Alexander Shopov
2018-02-06  6:15 ` [PATCH 1/1] " Alexander Shopov
2018-02-06  7:32   ` Eric Sunshine
2018-02-06  7:38     ` Jeff King
2018-02-09  7:44       ` [PATCH 0/1] " Alexander Shopov
2018-02-09  7:44       ` [PATCH 1/1] " Alexander Shopov
2018-02-09 18:20         ` Junio C Hamano
2018-02-09 19:15           ` Junio C Hamano
2018-02-09 19:30             ` Jeff King
2018-02-10 11:31               ` Jeff King [this message]
2018-02-12 15:03               ` Alexander Shopov
2018-02-12 15:15                 ` Jeff King
2018-02-13 13:19                   ` [PATCH 0/1] Mark messages for translation Alexander Shopov
2018-02-13 13:19                   ` [PATCH 1/1] Mark messages for translations Alexander Shopov

Reply instructions:

You may reply publically 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

  List information: http://vger.kernel.org/majordomo-info.html

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

  git send-email \
    --in-reply-to=20180210113128.GA11191@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=ash@kambanaria.org \
    --cc=bmwill@google.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=j6t@kdbg.org \
    --cc=martin.agren@gmail.com \
    --cc=pclouds@gmail.com \
    --cc=sandals@crustytoothpaste.net \
    --cc=sunshine@sunshineco.com \
    --cc=worldhello.net@gmail.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

git@vger.kernel.org mailing list mirror (one of many)

Archives are clonable:
	git clone --mirror https://public-inbox.org/git
	git clone --mirror http://ou63pmih66umazou.onion/git
	git clone --mirror http://czquwvybam4bgbro.onion/git
	git clone --mirror http://hjrcffqmbrq6wope.onion/git

Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.version-control.git
	nntp://ou63pmih66umazou.onion/inbox.comp.version-control.git
	nntp://czquwvybam4bgbro.onion/inbox.comp.version-control.git
	nntp://hjrcffqmbrq6wope.onion/inbox.comp.version-control.git
	nntp://news.gmane.org/gmane.comp.version-control.git

 note: .onion URLs require Tor: https://www.torproject.org/
       or Tor2web: https://www.tor2web.org/

AGPL code for this site: git clone https://public-inbox.org/ public-inbox