git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH v3 3/3] t3700: add a test_mode_in_index helper function
@ 2016-07-30 20:13 Ingo Brückl
  2016-08-01 21:25 ` Junio C Hamano
  0 siblings, 1 reply; 2+ messages in thread
From: Ingo Brückl @ 2016-07-30 20:13 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano

The case statement to check the file mode of a staged file appears
a number of times.

Simplify the test by utilizing a test_mode_in_index helper function.

Signed-off-by: Ingo Brückl <ib@wupperonline.de>
---
 t/t3700-add.sh | 54 ++++++++++++++++++++++--------------------------------
 1 file changed, 22 insertions(+), 32 deletions(-)

diff --git a/t/t3700-add.sh b/t/t3700-add.sh
index 1fa5dfd..7b98483 100755
--- a/t/t3700-add.sh
+++ b/t/t3700-add.sh
@@ -7,6 +7,20 @@ test_description='Test of git add, including the -- option.'

 . ./test-lib.sh

+# Test the file mode "$1" of the file "$2" in the index.
+test_mode_in_index () {
+	case "$(git ls-files --stage "$2")" in
+		$1\ *"$2")
+			echo pass
+			;;
+		*)
+			echo fail
+			git ls-files --stage "$2"
+			return 1
+			;;
+	esac
+}
+
 test_expect_success \
     'Test of git add' \
     'touch foo && git add foo'
@@ -25,18 +39,12 @@ test_expect_success \
 	 echo foo >xfoo1 &&
 	 chmod 755 xfoo1 &&
 	 git add xfoo1 &&
-	 case "$(git ls-files --stage xfoo1)" in
-	 100644" "*xfoo1) echo pass;;
-	 *) echo fail; git ls-files --stage xfoo1; (exit 1);;
-	 esac'
+	 test_mode_in_index 100644 xfoo1'

 test_expect_success 'git add: filemode=0 should not get confused by symlink' '
 	rm -f xfoo1 &&
 	test_ln_s_add foo xfoo1 &&
-	case "$(git ls-files --stage xfoo1)" in
-	120000" "*xfoo1) echo pass;;
-	*) echo fail; git ls-files --stage xfoo1; (exit 1);;
-	esac
+	test_mode_in_index 120000 xfoo1
 '

 test_expect_success \
@@ -45,28 +53,19 @@ test_expect_success \
 	 echo foo >xfoo2 &&
 	 chmod 755 xfoo2 &&
 	 git update-index --add xfoo2 &&
-	 case "$(git ls-files --stage xfoo2)" in
-	 100644" "*xfoo2) echo pass;;
-	 *) echo fail; git ls-files --stage xfoo2; (exit 1);;
-	 esac'
+	 test_mode_in_index 100644 xfoo2'

 test_expect_success 'git add: filemode=0 should not get confused by symlink' '
 	rm -f xfoo2 &&
 	test_ln_s_add foo xfoo2 &&
-	case "$(git ls-files --stage xfoo2)" in
-	120000" "*xfoo2) echo pass;;
-	*) echo fail; git ls-files --stage xfoo2; (exit 1);;
-	esac
+	test_mode_in_index 120000 xfoo2
 '

 test_expect_success \
 	'git update-index --add: Test that executable bit is not used...' \
 	'git config core.filemode 0 &&
 	 test_ln_s_add xfoo2 xfoo3 &&	# runs git update-index --add
-	 case "$(git ls-files --stage xfoo3)" in
-	 120000" "*xfoo3) echo pass;;
-	 *) echo fail; git ls-files --stage xfoo3; (exit 1);;
-	 esac'
+	 test_mode_in_index 120000 xfoo3'

 test_expect_success '.gitignore test setup' '
 	echo "*.ig" >.gitignore &&
@@ -336,15 +335,9 @@ test_expect_success 'git add --chmod=[+-]x stages correctly' '
 	rm -f foo1 &&
 	echo foo >foo1 &&
 	git add --chmod=+x foo1 &&
-	case "$(git ls-files --stage foo1)" in
-	100755" "*foo1) echo pass;;
-	*) echo fail; git ls-files --stage foo1; (exit 1);;
-	esac &&
+	test_mode_in_index 100755 foo1 &&
 	git add --chmod=-x foo1 &&
-	case "$(git ls-files --stage foo1)" in
-	100644" "*foo1) echo pass;;
-	*) echo fail; git ls-files --stage foo1; (exit 1);;
-	esac
+	test_mode_in_index 100644 foo1
 '

 test_expect_success POSIXPERM,SYMLINKS 'git add --chmod=+x with symlinks' '
@@ -353,10 +346,7 @@ test_expect_success POSIXPERM,SYMLINKS 'git add --chmod=+x with symlinks' '
 	rm -f foo2 &&
 	echo foo >foo2 &&
 	git add --chmod=+x foo2 &&
-	case "$(git ls-files --stage foo2)" in
-	100755" "*foo2) echo pass;;
-	*) echo fail; git ls-files --stage foo2; (exit 1);;
-	esac
+	test_mode_in_index 100755 foo2
 '

 test_done
--
2.9.2

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v3 3/3] t3700: add a test_mode_in_index helper function
  2016-07-30 20:13 [PATCH v3 3/3] t3700: add a test_mode_in_index helper function Ingo Brückl
@ 2016-08-01 21:25 ` Junio C Hamano
  0 siblings, 0 replies; 2+ messages in thread
From: Junio C Hamano @ 2016-08-01 21:25 UTC (permalink / raw)
  To: Ingo Brückl; +Cc: git

Ingo Brückl <ib@wupperonline.de> writes:

Ingo Brückl <ib@wupperonline.de> writes:

> The case statement to check the file mode of a staged file appears
> a number of times.
>
> Simplify the test by utilizing a test_mode_in_index helper function.
>
> Signed-off-by: Ingo Brückl <ib@wupperonline.de>
> ---
>  t/t3700-add.sh | 54 ++++++++++++++++++++++--------------------------------
>  1 file changed, 22 insertions(+), 32 deletions(-)

Nice.

> diff --git a/t/t3700-add.sh b/t/t3700-add.sh
> index 1fa5dfd..7b98483 100755
> --- a/t/t3700-add.sh
> +++ b/t/t3700-add.sh
> @@ -7,6 +7,20 @@ test_description='Test of git add, including the -- option.'
>
>  . ./test-lib.sh
>
> +# Test the file mode "$1" of the file "$2" in the index.
> +test_mode_in_index () {
> +	case "$(git ls-files --stage "$2")" in
> +		$1\ *"$2")
> +			echo pass
> +			;;
> +		*)
> +			echo fail
> +			git ls-files --stage "$2"
> +			return 1
> +			;;
> +	esac
> +}

This case/esac is misindented, but no need to resend; I can fix it
up trivially while queuing the patches.  It may be both easier to
read and more robust to tweak the pattern like this, though:

# Test the file mode "$1" of the file "$2" in the index.
test_mode_in_index () {
	case "$(git ls-files --stage "$2")" in
	"$1 "*"	$2")
		echo pass
		;;
	*)
		echo fail
		git ls-files --stage "$2"
		return 1
		;;
	esac
}

Thanks.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-08-01 21:41 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-07-30 20:13 [PATCH v3 3/3] t3700: add a test_mode_in_index helper function Ingo Brückl
2016-08-01 21:25 ` Junio C Hamano

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).