threads / patch / 30252

patchtests: add tests for the bash prompt functions in the completion script

Subject: [PATCH] tests: add tests for the bash prompt functions in the completion script

## tl;dr

14 messages between Apr 15, 2012 and Apr 17, 2012. Diffs are folded; open one to read it.

replies: 13people: 4as markdown or json

SZEDER Gábor· Apr 15, 2012, 20:31 UTC · lore

The tests cover the discovery of the .git directory in the __gitdir() function in different scenarios, and the prompt itself, i.e. branch name, detached heads, operations (rebase, merge, cherry-pick, bisect), and status indicators (dirty, stash, untracked files; but not the upstream status).

Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
---
Goes on top of fc/completion-tests (b8574ba7).
 t/t9902-completion.sh |  436 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 436 insertions(+)
Show changes to t/t9902-completion.sh +436 −0
diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index 3bbec79b..6edbaa72 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -114,4 +114,440 @@ test_expect_success 'double dash "git checkout"' '
 	test_completion "git checkout --"
 '
 
+actual="$TRASH_DIRECTORY/actual"
+
+test_expect_success 'setup for prompt tests' '
+	mkdir -p subdir/subsubdir &&
+	git init otherrepo &&
+	echo 1 > file &&
+	git add file &&
+	test_tick &&
+	git commit -m initial &&
+	git tag -a -m msg1 t1 &&
+	git checkout -b b1 &&
+	echo 2 > file &&
+	git commit -m "second b1" file &&
+	echo 3 > file &&
+	git commit -m "third b1" file &&
+	git tag -a -m msg2 t2 &&
+	git checkout -b b2 master &&
+	echo 0 > file &&
+	git commit -m "second b2" file &&
+	git checkout master
+'
+
+test_expect_success 'gitdir - from command line (through $__git_dir)' '
+	echo "$TRASH_DIRECTORY/otherrepo/.git" > expected &&
+	(
+		__git_dir="$TRASH_DIRECTORY/otherrepo/.git" &&
+		__gitdir > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'gitdir - repo as argument' '
+	echo "otherrepo/.git" > expected &&
+	__gitdir "otherrepo" > "$actual" &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'gitdir - remote as argument' '
+	echo "remote" > expected &&
+	__gitdir "remote" > "$actual" &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'gitdir - .git directory in cwd' '
+	echo ".git" > expected &&
+	__gitdir > "$actual" &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'gitdir - .git directory in parent' '
+	echo "$TRASH_DIRECTORY/.git" > expected &&
+	(
+		cd subdir &&
+		__gitdir > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'gitdir - cwd is a .git directory' '
+	echo "." > expected &&
+	(
+		cd .git &&
+		__gitdir > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'gitdir - parent is a .git directory' '
+	echo "$TRASH_DIRECTORY/.git" > expected &&
+	(
+		cd .git/refs &&
+		__gitdir > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_failure 'gitdir - $GIT_DIR set while .git directory in cwd' '
+	echo "$TRASH_DIRECTORY/otherrepo/.git" > expected &&
+	(
+		GIT_DIR="$TRASH_DIRECTORY/otherrepo/.git" &&
+		export GIT_DIR &&
+		__gitdir > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'gitdir - $GIT_DIR set while .git directory in parent' '
+	echo "$TRASH_DIRECTORY/otherrepo/.git" > expected &&
+	(
+		GIT_DIR="$TRASH_DIRECTORY/otherrepo/.git" &&
+		export GIT_DIR &&
+		cd subdir &&
+		__gitdir > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'gitdir - gitfile in cwd' '
+	echo "$TRASH_DIRECTORY/otherrepo/.git" > expected &&
+	echo "gitdir: $TRASH_DIRECTORY/otherrepo/.git" > subdir/.git &&
+	test_when_finished "rm -f subdir/.git" &&
+	(
+		cd subdir &&
+		__gitdir > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'gitdir - gitfile in parent' '
+	echo "$TRASH_DIRECTORY/otherrepo/.git" > expected &&
+	echo "gitdir: $TRASH_DIRECTORY/otherrepo/.git" > subdir/.git &&
+	test_when_finished "rm -f subdir/.git" &&
+	(
+		cd subdir/subsubdir &&
+		__gitdir > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success SYMLINKS 'gitdir - resulting path avoids symlinks' '
+	echo "$TRASH_DIRECTORY/otherrepo/.git" >expected &&
+	mkdir otherrepo/dir &&
+	test_when_finished "rm -rf otherrepo/dir" &&
+	ln -s otherrepo/dir link &&
+	test_when_finished "rm -f link" &&
+	(
+		cd link &&
+		__gitdir > "$actual"
+	) &&
+	test_cmp expected actual
+'
+
+test_expect_success 'gitdir - not a git repository' '
+	(
+		cd subdir/subsubdir &&
+		GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY" &&
+		export GIT_CEILING_DIRECTORIES &&
+		test_must_fail __gitdir
+	)
+'
+
+test_expect_success 'prompt - branch name' '
+	printf " (master)" > expected &&
+	__git_ps1 > "$actual" &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - detached head' '
+	printf " ((%s...))" $(git log -1 --format="%h" b1^) > expected &&
+	git checkout b1^ &&
+	test_when_finished "git checkout master" &&
+	__git_ps1 > "$actual" &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - describe detached head - contains' '
+	printf " ((t2~1))" > expected &&
+	git checkout b1^ &&
+	test_when_finished "git checkout master" &&
+	(
+		GIT_PS1_DESCRIBE_STYLE=contains &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - describe detached head - branch' '
+	printf " ((b1~1))" > expected &&
+	git checkout b1^ &&
+	test_when_finished "git checkout master" &&
+	(
+		GIT_PS1_DESCRIBE_STYLE=branch &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - describe detached head - describe' '
+	printf " ((t1-1-g%s))" $(git log -1 --format="%h" b1^) > expected &&
+	git checkout b1^ &&
+	test_when_finished "git checkout master" &&
+	(
+		GIT_PS1_DESCRIBE_STYLE=describe &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - describe detached head - default' '
+	printf " ((t2))" > expected &&
+	git checkout --detach b1 &&
+	test_when_finished "git checkout master" &&
+	__git_ps1 > "$actual" &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - inside .git directory' '
+	printf " (GIT_DIR!)" > expected &&
+	(
+		cd .git &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - deep inside .git directory' '
+	printf " (GIT_DIR!)" > expected &&
+	(
+		cd .git/refs &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - inside bare repository' '
+	printf " (BARE:master)" > expected &&
+	git init --bare bare.git &&
+	test_when_finished "rm -rf bare.git" &&
+	(
+		cd bare.git &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - interactive rebase' '
+	printf " (b1|REBASE-i)" > expected
+	echo "#!$SHELL_PATH" >fake_editor.sh &&
+	cat >>fake_editor.sh <<\EOF &&
+echo "edit $(git log -1 --format="%h")" > "$1"
+EOF
+	test_when_finished "rm -f fake_editor.sh" &&
+	chmod a+x fake_editor.sh &&
+	test_set_editor "$TRASH_DIRECTORY/fake_editor.sh" &&
+	git checkout b1 &&
+	test_when_finished "git checkout master" &&
+	git rebase -i HEAD^ &&
+	test_when_finished "git rebase --abort"
+	__git_ps1 > "$actual" &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - rebase merge' '
+	printf " (b2|REBASE-m)" > expected &&
+	git checkout b2 &&
+	test_when_finished "git checkout master" &&
+	test_must_fail git rebase --merge b1 b2 &&
+	test_when_finished "git rebase --abort" &&
+	__git_ps1 > "$actual" &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - rebase' '
+	printf " ((t2)|REBASE)" > expected &&
+	git checkout b2 &&
+	test_when_finished "git checkout master" &&
+	test_must_fail git rebase b1 b2 &&
+	test_when_finished "git rebase --abort" &&
+	__git_ps1 > "$actual" &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - merge' '
+	printf " (b1|MERGING)" > expected &&
+	git checkout b1 &&
+	test_when_finished "git checkout master" &&
+	test_must_fail git merge b2 &&
+	test_when_finished "git reset --hard" &&
+	__git_ps1 > "$actual" &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - cherry-pick' '
+	printf " (master|CHERRY-PICKING)" > expected &&
+	test_must_fail git cherry-pick b1 &&
+	test_when_finished "git reset --hard" &&
+	__git_ps1 > "$actual" &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - bisect' '
+	printf " (master|BISECTING)" > expected &&
+	git bisect start &&
+	test_when_finished "git bisect reset" &&
+	__git_ps1 > "$actual" &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - dirty status indicator - clean' '
+	printf " (master)" > expected &&
+	(
+		GIT_PS1_SHOWDIRTYSTATE=y &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - dirty status indicator - dirty worktree' '
+	printf " (master *)" > expected &&
+	echo "dirty" > file &&
+	test_when_finished "git reset --hard" &&
+	(
+		GIT_PS1_SHOWDIRTYSTATE=y &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - dirty status indicator - dirty index' '
+	printf " (master +)" > expected &&
+	echo "dirty" > file &&
+	test_when_finished "git reset --hard" &&
+	git add -u &&
+	(
+		GIT_PS1_SHOWDIRTYSTATE=y &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - dirty status indicator - dirty index and worktree' '
+	printf " (master *+)" > expected &&
+	echo "dirty index" > file &&
+	test_when_finished "git reset --hard" &&
+	git add -u &&
+	echo "dirty worktree" > file &&
+	(
+		GIT_PS1_SHOWDIRTYSTATE=y &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - dirty status indicator - before root commit' '
+	printf " (master #)" > expected &&
+	(
+		GIT_PS1_SHOWDIRTYSTATE=y &&
+		cd otherrepo &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - dirty status indicator - disabled by config' '
+	printf " (master)" > expected &&
+	echo "dirty" > file &&
+	test_when_finished "git reset --hard" &&
+	test_config bash.showDirtyState false &&
+	(
+		GIT_PS1_SHOWDIRTYSTATE=y &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - dirty status indicator - not shown inside .git directory' '
+	printf " (GIT_DIR!)" > expected &&
+	echo "dirty" > file &&
+	test_when_finished "git reset --hard" &&
+	(
+		GIT_PS1_SHOWDIRTYSTATE=y &&
+		cd .git &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - stash status indicator - no stash' '
+	printf " (master)" > expected &&
+	(
+		GIT_PS1_SHOWSTASHSTATE=y &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - stash status indicator - stash' '
+	printf " (master $)" > expected &&
+	echo 2 >file &&
+	git stash &&
+	test_when_finished "git stash drop" &&
+	(
+		GIT_PS1_SHOWSTASHSTATE=y &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - stash status indicator - not shown inside .git directory' '
+	printf " (GIT_DIR!)" > expected &&
+	echo 2 >file &&
+	git stash &&
+	test_when_finished "git stash drop" &&
+	(
+		GIT_PS1_SHOWSTASHSTATE=y &&
+		cd .git &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - untracked files status indicator - no untracked files' '
+	printf " (master)" > expected &&
+	(
+		GIT_PS1_SHOWUNTRACKEDFILES=y &&
+		cd otherrepo &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - untracked files status indicator - untracked files' '
+	printf " (master %%)" > expected &&
+	(
+		GIT_PS1_SHOWUNTRACKEDFILES=y &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - untracked files status indicator - not shown inside .git directory' '
+	printf " (GIT_DIR!)" > expected &&
+	(
+		GIT_PS1_SHOWUNTRACKEDFILES=y &&
+		cd .git &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - format string starting with dash' '
+	printf -- "-master" > expected &&
+	__git_ps1 "-%s" > "$actual" &&
+	test_cmp expected "$actual"
+'
+
 test_done
-- 
1.7.10.177.g26c39
Felipe Contreras· Apr 15, 2012, 20:56 UTC · re: SZEDER Gábor · lore

Re: [PATCH] tests: add tests for the bash prompt functions in the completion script

On Sun, Apr 15, 2012 at 11:31 PM, SZEDER Gábor <szeder@ira.uka.de> wrote:
Show 13 quoted lines
> The tests cover the discovery of the .git directory in the __gitdir()
> function in different scenarios, and the prompt itself, i.e. branch
> name, detached heads, operations (rebase, merge, cherry-pick, bisect),
> and status indicators (dirty, stash, untracked files; but not the
> upstream status).
>
> Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
> ---
>
> Goes on top of fc/completion-tests (b8574ba7).
>
>  t/t9902-completion.sh |  436 +++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 436 insertions(+)

Perhaps it would make sense to create another file for these, specially since there's the plan of splitting the prompt stuff to a separate file.

Cheers.
-- 
Felipe Contreras
SZEDER Gábor· Apr 16, 2012, 09:49 UTC · re: Felipe Contreras · lore

Re: [PATCH] tests: add tests for the bash prompt functions in the completion script

Hi,
On Sun, Apr 15, 2012 at 11:56:36PM +0300, Felipe Contreras wrote:
Show 16 quoted lines
> On Sun, Apr 15, 2012 at 11:31 PM, SZEDER Gábor <szeder@ira.uka.de> wrote:
> > The tests cover the discovery of the .git directory in the __gitdir()
> > function in different scenarios, and the prompt itself, i.e. branch
> > name, detached heads, operations (rebase, merge, cherry-pick, bisect),
> > and status indicators (dirty, stash, untracked files; but not the
> > upstream status).
> >
> > Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
> > ---
> >
> > Goes on top of fc/completion-tests (b8574ba7).
> >
> >  t/t9902-completion.sh |  436 +++++++++++++++++++++++++++++++++++++++++++++++++
> >  1 file changed, 436 insertions(+)
> 
> Perhaps it would make sense to create another file for these,

I thought about that briefly but decided against it to avoid duplicating the bash-specific checks at the beginning, but I don't care that much either way.

> specially since there's the plan of splitting the prompt stuff to a
> separate file.

Oh, I was not aware of such a plan. What would be the benefits of such a move, why is it necessary? Where would you split it up? In particular, what would happen with __gitdir(), which is used in many completion functions and in __git_ps1() as well?

Best, Gábor

Felipe Contreras· Apr 16, 2012, 10:04 UTC · re: SZEDER Gábor · lore

Re: [PATCH] tests: add tests for the bash prompt functions in the completion script

2012/4/16 SZEDER Gábor <szeder@ira.uka.de>:
Show 31 quoted lines
> Hi,
>
> On Sun, Apr 15, 2012 at 11:56:36PM +0300, Felipe Contreras wrote:
>> On Sun, Apr 15, 2012 at 11:31 PM, SZEDER Gábor <szeder@ira.uka.de> wrote:
>> > The tests cover the discovery of the .git directory in the __gitdir()
>> > function in different scenarios, and the prompt itself, i.e. branch
>> > name, detached heads, operations (rebase, merge, cherry-pick, bisect),
>> > and status indicators (dirty, stash, untracked files; but not the
>> > upstream status).
>> >
>> > Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
>> > ---
>> >
>> > Goes on top of fc/completion-tests (b8574ba7).
>> >
>> >  t/t9902-completion.sh |  436 +++++++++++++++++++++++++++++++++++++++++++++++++
>> >  1 file changed, 436 insertions(+)
>>
>> Perhaps it would make sense to create another file for these,
>
> I thought about that briefly but decided against it to avoid
> duplicating the bash-specific checks at the beginning, but I don't
> care that much either way.
>
>> specially since there's the plan of splitting the prompt stuff to a
>> separate file.
>
> Oh, I was not aware of such a plan.  What would be the benefits of
> such a move, why is it necessary?  Where would you split it up?  In
> particular, what would happen with __gitdir(), which is used in many
> completion functions and in __git_ps1() as well?
I guess __gitdir() would be duplicated, I don't think that has been discussed.
http://thread.gmane.org/gmane.comp.version-control.git/194188

Basically splitting helps for systems that use bash-completion, and also helps for zsh users who don't want the bash completion, but still want __git_ps1(). I split the file myself for this reason.

Cheers.
-- 
Felipe Contreras
SZEDER Gábor· Apr 16, 2012, 10:20 UTC · re: Felipe Contreras · lore

Re: [PATCH] tests: add tests for the bash prompt functions in the completion script

On Mon, Apr 16, 2012 at 01:04:41PM +0300, Felipe Contreras wrote:
Show 15 quoted lines
> >> specially since there's the plan of splitting the prompt stuff to a
> >> separate file.
> >
> > Oh, I was not aware of such a plan.  What would be the benefits of
> > such a move, why is it necessary?  Where would you split it up?  In
> > particular, what would happen with __gitdir(), which is used in many
> > completion functions and in __git_ps1() as well?
> 
> I guess __gitdir() would be duplicated, I don't think that has been discussed.
> 
> http://thread.gmane.org/gmane.comp.version-control.git/194188
> 
> Basically splitting helps for systems that use bash-completion, and
> also helps for zsh users who don't want the bash completion, but still
> want __git_ps1(). I split the file myself for this reason.

Thanks for the pointer, those are valid reasons. I'll wait a day or two for feedback, then resubmit as a different test script.

Gábor
Jonathan Nieder· Apr 16, 2012, 16:01 UTC · re: SZEDER Gábor · lore

Re: [PATCH] tests: add tests for the bash prompt functions in the completion script

SZEDER Gábor wrote:
> I thought about that briefly but decided against it to avoid
> duplicating the bash-specific checks at the beginning, but I don't
> care that much either way.

Maybe a t/lib-completion.sh would take care of that. FWIW a clean split in the test script so it doesn't grow into something like the current fast-import tests sounds like a good idea to me. :)

SZEDER Gábor· Apr 16, 2012, 22:09 UTC · re: Jonathan Nieder · lore

[PATCH v2 1/2] tests: move code to run completion tests under bash into a helper library

Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
---
On Mon, Apr 16, 2012 at 11:01:24AM -0500, Jonathan Nieder wrote:
Show 7 quoted lines
> SZEDER Gábor wrote:
> 
> > I thought about that briefly but decided against it to avoid
> > duplicating the bash-specific checks at the beginning, but I don't
> > care that much either way.
> 
> Maybe a t/lib-completion.sh would take care of that.

Here you go. I put only the conditionals to run the test script under Bash and the sourcing of test-lib.sh into t/lib-completion.sh, because that's the common part that is required by the prompt tests, too. So in the end I'm not sure that t/lib-completion.sh is the right name for this file.

This patch could probably be squashed into the first commit of fc/completion-tests, i.e. 5c293a6b (tests: add initial bash completion tests, 2012-04-12).

 t/lib-completion.sh   |   17 +++++++++++++++++
 t/t9902-completion.sh |   14 +-------------
 2 files changed, 18 insertions(+), 13 deletions(-)
 create mode 100644 t/lib-completion.sh
Show changes to 2 files +18 −13

t/lib-completion.sh, t/t9902-completion.sh

diff --git a/t/lib-completion.sh b/t/lib-completion.sh
new file mode 100644
index 00000000..3d85feb2
--- /dev/null
+++ b/t/lib-completion.sh
@@ -0,0 +1,17 @@
+#!/bin/sh
+#
+# Ensures that tests of the completion script are run under Bash.
+
+if test -n "$BASH" && test -z "$POSIXLY_CORRECT"; then
+	# we are in full-on bash mode
+	true
+elif type bash >/dev/null 2>&1; then
+	# execute in full-on bash mode
+	unset POSIXLY_CORRECT
+	exec bash "$0" "$@"
+else
+	echo '1..0 #SKIP skipping bash completion tests; bash not available'
+	exit 0
+fi
+
+. ./test-lib.sh
diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index a6eaa15c..90b2f22e 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -3,21 +3,9 @@
 # Copyright (c) 2012 Felipe Contreras
 #
 
-if test -n "$BASH" && test -z "$POSIXLY_CORRECT"; then
-	# we are in full-on bash mode
-	true
-elif type bash >/dev/null 2>&1; then
-	# execute in full-on bash mode
-	unset POSIXLY_CORRECT
-	exec bash "$0" "$@"
-else
-	echo '1..0 #SKIP skipping bash completion tests; bash not available'
-	exit 0
-fi
-
 test_description='test bash completion'
 
-. ./test-lib.sh
+. ./lib-completion.sh
 
 complete ()
 {
-- 
1.7.10.216.gb52c0
SZEDER Gábor· Apr 16, 2012, 22:09 UTC · re: SZEDER Gábor · lore

[PATCH v2 2/2] tests: add tests for the bash prompt functions in the completion script

The tests cover the discovery of the .git directory in the __gitdir() function in different scenarios, and the prompt itself, i.e. branch name, detached heads, operations (rebase, merge, cherry-pick, bisect), and status indicators (dirty, stash, untracked files; but not the upstream status).

Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
---
Tests are the same as in the prvious round, but in a different test file.
 t/t9903-bash-prompt.sh |  448 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 448 insertions(+)
 create mode 100644 t/t9903-bash-prompt.sh
Show changes to t/t9903-bash-prompt.sh +448 −0
diff --git a/t/t9903-bash-prompt.sh b/t/t9903-bash-prompt.sh
new file mode 100644
index 00000000..41c183e6
--- /dev/null
+++ b/t/t9903-bash-prompt.sh
@@ -0,0 +1,448 @@
+#!/bin/sh
+#
+# Copyright (c) 2012 SZEDER Gábor
+#
+
+test_description='test git-specific bash prompt functions'
+
+. ./lib-completion.sh
+
+. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash"
+
+actual="$TRASH_DIRECTORY/actual"
+
+test_expect_success 'setup for prompt tests' '
+	mkdir -p subdir/subsubdir &&
+	git init otherrepo &&
+	echo 1 > file &&
+	git add file &&
+	test_tick &&
+	git commit -m initial &&
+	git tag -a -m msg1 t1 &&
+	git checkout -b b1 &&
+	echo 2 > file &&
+	git commit -m "second b1" file &&
+	echo 3 > file &&
+	git commit -m "third b1" file &&
+	git tag -a -m msg2 t2 &&
+	git checkout -b b2 master &&
+	echo 0 > file &&
+	git commit -m "second b2" file &&
+	git checkout master
+'
+
+test_expect_success 'gitdir - from command line (through $__git_dir)' '
+	echo "$TRASH_DIRECTORY/otherrepo/.git" > expected &&
+	(
+		__git_dir="$TRASH_DIRECTORY/otherrepo/.git" &&
+		__gitdir > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'gitdir - repo as argument' '
+	echo "otherrepo/.git" > expected &&
+	__gitdir "otherrepo" > "$actual" &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'gitdir - remote as argument' '
+	echo "remote" > expected &&
+	__gitdir "remote" > "$actual" &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'gitdir - .git directory in cwd' '
+	echo ".git" > expected &&
+	__gitdir > "$actual" &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'gitdir - .git directory in parent' '
+	echo "$TRASH_DIRECTORY/.git" > expected &&
+	(
+		cd subdir &&
+		__gitdir > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'gitdir - cwd is a .git directory' '
+	echo "." > expected &&
+	(
+		cd .git &&
+		__gitdir > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'gitdir - parent is a .git directory' '
+	echo "$TRASH_DIRECTORY/.git" > expected &&
+	(
+		cd .git/refs &&
+		__gitdir > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_failure 'gitdir - $GIT_DIR set while .git directory in cwd' '
+	echo "$TRASH_DIRECTORY/otherrepo/.git" > expected &&
+	(
+		GIT_DIR="$TRASH_DIRECTORY/otherrepo/.git" &&
+		export GIT_DIR &&
+		__gitdir > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'gitdir - $GIT_DIR set while .git directory in parent' '
+	echo "$TRASH_DIRECTORY/otherrepo/.git" > expected &&
+	(
+		GIT_DIR="$TRASH_DIRECTORY/otherrepo/.git" &&
+		export GIT_DIR &&
+		cd subdir &&
+		__gitdir > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'gitdir - gitfile in cwd' '
+	echo "$TRASH_DIRECTORY/otherrepo/.git" > expected &&
+	echo "gitdir: $TRASH_DIRECTORY/otherrepo/.git" > subdir/.git &&
+	test_when_finished "rm -f subdir/.git" &&
+	(
+		cd subdir &&
+		__gitdir > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'gitdir - gitfile in parent' '
+	echo "$TRASH_DIRECTORY/otherrepo/.git" > expected &&
+	echo "gitdir: $TRASH_DIRECTORY/otherrepo/.git" > subdir/.git &&
+	test_when_finished "rm -f subdir/.git" &&
+	(
+		cd subdir/subsubdir &&
+		__gitdir > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success SYMLINKS 'gitdir - resulting path avoids symlinks' '
+	echo "$TRASH_DIRECTORY/otherrepo/.git" >expected &&
+	mkdir otherrepo/dir &&
+	test_when_finished "rm -rf otherrepo/dir" &&
+	ln -s otherrepo/dir link &&
+	test_when_finished "rm -f link" &&
+	(
+		cd link &&
+		__gitdir > "$actual"
+	) &&
+	test_cmp expected actual
+'
+
+test_expect_success 'gitdir - not a git repository' '
+	(
+		cd subdir/subsubdir &&
+		GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY" &&
+		export GIT_CEILING_DIRECTORIES &&
+		test_must_fail __gitdir
+	)
+'
+
+test_expect_success 'prompt - branch name' '
+	printf " (master)" > expected &&
+	__git_ps1 > "$actual" &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - detached head' '
+	printf " ((%s...))" $(git log -1 --format="%h" b1^) > expected &&
+	git checkout b1^ &&
+	test_when_finished "git checkout master" &&
+	__git_ps1 > "$actual" &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - describe detached head - contains' '
+	printf " ((t2~1))" > expected &&
+	git checkout b1^ &&
+	test_when_finished "git checkout master" &&
+	(
+		GIT_PS1_DESCRIBE_STYLE=contains &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - describe detached head - branch' '
+	printf " ((b1~1))" > expected &&
+	git checkout b1^ &&
+	test_when_finished "git checkout master" &&
+	(
+		GIT_PS1_DESCRIBE_STYLE=branch &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - describe detached head - describe' '
+	printf " ((t1-1-g%s))" $(git log -1 --format="%h" b1^) > expected &&
+	git checkout b1^ &&
+	test_when_finished "git checkout master" &&
+	(
+		GIT_PS1_DESCRIBE_STYLE=describe &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - describe detached head - default' '
+	printf " ((t2))" > expected &&
+	git checkout --detach b1 &&
+	test_when_finished "git checkout master" &&
+	__git_ps1 > "$actual" &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - inside .git directory' '
+	printf " (GIT_DIR!)" > expected &&
+	(
+		cd .git &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - deep inside .git directory' '
+	printf " (GIT_DIR!)" > expected &&
+	(
+		cd .git/refs &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - inside bare repository' '
+	printf " (BARE:master)" > expected &&
+	git init --bare bare.git &&
+	test_when_finished "rm -rf bare.git" &&
+	(
+		cd bare.git &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - interactive rebase' '
+	printf " (b1|REBASE-i)" > expected
+	echo "#!$SHELL_PATH" >fake_editor.sh &&
+	cat >>fake_editor.sh <<\EOF &&
+echo "edit $(git log -1 --format="%h")" > "$1"
+EOF
+	test_when_finished "rm -f fake_editor.sh" &&
+	chmod a+x fake_editor.sh &&
+	test_set_editor "$TRASH_DIRECTORY/fake_editor.sh" &&
+	git checkout b1 &&
+	test_when_finished "git checkout master" &&
+	git rebase -i HEAD^ &&
+	test_when_finished "git rebase --abort"
+	__git_ps1 > "$actual" &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - rebase merge' '
+	printf " (b2|REBASE-m)" > expected &&
+	git checkout b2 &&
+	test_when_finished "git checkout master" &&
+	test_must_fail git rebase --merge b1 b2 &&
+	test_when_finished "git rebase --abort" &&
+	__git_ps1 > "$actual" &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - rebase' '
+	printf " ((t2)|REBASE)" > expected &&
+	git checkout b2 &&
+	test_when_finished "git checkout master" &&
+	test_must_fail git rebase b1 b2 &&
+	test_when_finished "git rebase --abort" &&
+	__git_ps1 > "$actual" &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - merge' '
+	printf " (b1|MERGING)" > expected &&
+	git checkout b1 &&
+	test_when_finished "git checkout master" &&
+	test_must_fail git merge b2 &&
+	test_when_finished "git reset --hard" &&
+	__git_ps1 > "$actual" &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - cherry-pick' '
+	printf " (master|CHERRY-PICKING)" > expected &&
+	test_must_fail git cherry-pick b1 &&
+	test_when_finished "git reset --hard" &&
+	__git_ps1 > "$actual" &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - bisect' '
+	printf " (master|BISECTING)" > expected &&
+	git bisect start &&
+	test_when_finished "git bisect reset" &&
+	__git_ps1 > "$actual" &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - dirty status indicator - clean' '
+	printf " (master)" > expected &&
+	(
+		GIT_PS1_SHOWDIRTYSTATE=y &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - dirty status indicator - dirty worktree' '
+	printf " (master *)" > expected &&
+	echo "dirty" > file &&
+	test_when_finished "git reset --hard" &&
+	(
+		GIT_PS1_SHOWDIRTYSTATE=y &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - dirty status indicator - dirty index' '
+	printf " (master +)" > expected &&
+	echo "dirty" > file &&
+	test_when_finished "git reset --hard" &&
+	git add -u &&
+	(
+		GIT_PS1_SHOWDIRTYSTATE=y &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - dirty status indicator - dirty index and worktree' '
+	printf " (master *+)" > expected &&
+	echo "dirty index" > file &&
+	test_when_finished "git reset --hard" &&
+	git add -u &&
+	echo "dirty worktree" > file &&
+	(
+		GIT_PS1_SHOWDIRTYSTATE=y &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - dirty status indicator - before root commit' '
+	printf " (master #)" > expected &&
+	(
+		GIT_PS1_SHOWDIRTYSTATE=y &&
+		cd otherrepo &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - dirty status indicator - disabled by config' '
+	printf " (master)" > expected &&
+	echo "dirty" > file &&
+	test_when_finished "git reset --hard" &&
+	test_config bash.showDirtyState false &&
+	(
+		GIT_PS1_SHOWDIRTYSTATE=y &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - dirty status indicator - not shown inside .git directory' '
+	printf " (GIT_DIR!)" > expected &&
+	echo "dirty" > file &&
+	test_when_finished "git reset --hard" &&
+	(
+		GIT_PS1_SHOWDIRTYSTATE=y &&
+		cd .git &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - stash status indicator - no stash' '
+	printf " (master)" > expected &&
+	(
+		GIT_PS1_SHOWSTASHSTATE=y &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - stash status indicator - stash' '
+	printf " (master $)" > expected &&
+	echo 2 >file &&
+	git stash &&
+	test_when_finished "git stash drop" &&
+	(
+		GIT_PS1_SHOWSTASHSTATE=y &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - stash status indicator - not shown inside .git directory' '
+	printf " (GIT_DIR!)" > expected &&
+	echo 2 >file &&
+	git stash &&
+	test_when_finished "git stash drop" &&
+	(
+		GIT_PS1_SHOWSTASHSTATE=y &&
+		cd .git &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - untracked files status indicator - no untracked files' '
+	printf " (master)" > expected &&
+	(
+		GIT_PS1_SHOWUNTRACKEDFILES=y &&
+		cd otherrepo &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - untracked files status indicator - untracked files' '
+	printf " (master %%)" > expected &&
+	(
+		GIT_PS1_SHOWUNTRACKEDFILES=y &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - untracked files status indicator - not shown inside .git directory' '
+	printf " (GIT_DIR!)" > expected &&
+	(
+		GIT_PS1_SHOWUNTRACKEDFILES=y &&
+		cd .git &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - format string starting with dash' '
+	printf -- "-master" > expected &&
+	__git_ps1 "-%s" > "$actual" &&
+	test_cmp expected "$actual"
+'
+
+test_done
-- 
1.7.10.216.gb52c0
SZEDER Gábor· Apr 17, 2012, 00:33 UTC · re: Jonathan Nieder · lore

Re: [PATCH v2 2/2] tests: add tests for the bash prompt functions in the completion script

On Mon, Apr 16, 2012 at 06:50:26PM -0500, Jonathan Nieder wrote:
Show 5 quoted lines
> SZEDER Gábor wrote:
> 
> >  create mode 100644 t/t9903-bash-prompt.sh
> 
> Should be 100755, no?
Yes, of course.  Forgot to re-format-patch after amend.
Jonathan Nieder· Apr 16, 2012, 23:49 UTC · re: SZEDER Gábor · lore

Re: [PATCH v2 1/2] tests: move code to run completion tests under bash into a helper library

SZEDER Gábor wrote:
Show 22 quoted lines
> --- a/t/t9902-completion.sh
> +++ b/t/t9902-completion.sh
> @@ -3,21 +3,9 @@
>  # Copyright (c) 2012 Felipe Contreras
>  #
>  
> -if test -n "$BASH" && test -z "$POSIXLY_CORRECT"; then
> -	# we are in full-on bash mode
> -	true
> -elif type bash >/dev/null 2>&1; then
> -	# execute in full-on bash mode
> -	unset POSIXLY_CORRECT
> -	exec bash "$0" "$@"
> -else
> -	echo '1..0 #SKIP skipping bash completion tests; bash not available'
> -	exit 0
> -fi
> -
>  test_description='test bash completion'
>  
> -. ./test-lib.sh
> +. ./lib-completion.sh

If I understand correctly, this sources lib-completion immediately instead of following the usual pattern of

	. ./test-lib.sh
	. "$TEST_DIRECTORY/lib-completion.sh"

to get the self-execing over with ASAP. "$0" always refers to the original shell script and not the file being sourced so this should be safe. Looks good.

SZEDER Gábor· Apr 17, 2012, 00:42 UTC · re: Jonathan Nieder · lore

Re: [PATCH v2 1/2] tests: move code to run completion tests under bash into a helper library

On Mon, Apr 16, 2012 at 06:49:24PM -0500, Jonathan Nieder wrote:
Show 30 quoted lines
> SZEDER Gábor wrote:
> 
> > --- a/t/t9902-completion.sh
> > +++ b/t/t9902-completion.sh
> > @@ -3,21 +3,9 @@
> >  # Copyright (c) 2012 Felipe Contreras
> >  #
> >  
> > -if test -n "$BASH" && test -z "$POSIXLY_CORRECT"; then
> > -	# we are in full-on bash mode
> > -	true
> > -elif type bash >/dev/null 2>&1; then
> > -	# execute in full-on bash mode
> > -	unset POSIXLY_CORRECT
> > -	exec bash "$0" "$@"
> > -else
> > -	echo '1..0 #SKIP skipping bash completion tests; bash not available'
> > -	exit 0
> > -fi
> > -
> >  test_description='test bash completion'
> >  
> > -. ./test-lib.sh
> > +. ./lib-completion.sh
> 
> If I understand correctly, this sources lib-completion immediately
> instead of following the usual pattern of
> 
> 	. ./test-lib.sh
> 	. "$TEST_DIRECTORY/lib-completion.sh"

There are a couple of other test libs that source test-lib.sh themselves, similarly to the new t/lib-completion.sh.

> to get the self-execing over with ASAP.
Exactly, like the original t/t9902-completion.sh.
> "$0" always refers to the
> original shell script and not the file being sourced so this should be
> safe.  Looks good.
Yeah, I tested the tests ;) with dash as /bin/sh; works for me.
Johannes Sixt· Apr 17, 2012, 07:20 UTC · re: SZEDER Gábor · lore

Re: [PATCH v2 1/2] tests: move code to run completion tests under bash into a helper library

Am 4/17/2012 0:09, schrieb SZEDER Gábor:
Show 21 quoted lines
> diff --git a/t/lib-completion.sh b/t/lib-completion.sh
> new file mode 100644
> index 00000000..3d85feb2
> --- /dev/null
> +++ b/t/lib-completion.sh
> @@ -0,0 +1,17 @@
> +#!/bin/sh
> +#
> +# Ensures that tests of the completion script are run under Bash.
> +
> +if test -n "$BASH" && test -z "$POSIXLY_CORRECT"; then
> +	# we are in full-on bash mode
> +	true
> +elif type bash >/dev/null 2>&1; then
> +	# execute in full-on bash mode
> +	unset POSIXLY_CORRECT
> +	exec bash "$0" "$@"
> +else
> +	echo '1..0 #SKIP skipping bash completion tests; bash not available'
> +	exit 0
> +fi

I wonder what this 'echo...;exit' is about (I do so since I saw it the first time when Felipe submitted his patch). Shouldn't it be more like:

	skip_all='skipping bash completion tests; bash not available'
	test_done
-- Hannes
Felipe Contreras· Apr 17, 2012, 07:37 UTC · re: Johannes Sixt · lore

Re: [PATCH v2 1/2] tests: move code to run completion tests under bash into a helper library

2012/4/17 Johannes Sixt <j.sixt@viscovery.net>:
Show 28 quoted lines
> Am 4/17/2012 0:09, schrieb SZEDER Gábor:
>> diff --git a/t/lib-completion.sh b/t/lib-completion.sh
>> new file mode 100644
>> index 00000000..3d85feb2
>> --- /dev/null
>> +++ b/t/lib-completion.sh
>> @@ -0,0 +1,17 @@
>> +#!/bin/sh
>> +#
>> +# Ensures that tests of the completion script are run under Bash.
>> +
>> +if test -n "$BASH" && test -z "$POSIXLY_CORRECT"; then
>> +     # we are in full-on bash mode
>> +     true
>> +elif type bash >/dev/null 2>&1; then
>> +     # execute in full-on bash mode
>> +     unset POSIXLY_CORRECT
>> +     exec bash "$0" "$@"
>> +else
>> +     echo '1..0 #SKIP skipping bash completion tests; bash not available'
>> +     exit 0
>> +fi
>
> I wonder what this 'echo...;exit' is about (I do so since I saw it the
> first time when Felipe submitted his patch). Shouldn't it be more like:
>
>        skip_all='skipping bash completion tests; bash not available'
>        test_done

It should, if the test library was loaded at that point, but it isn't. BTW, I didn't write that code.

In any case, what the user sees is exactly the same, except without color.
Cheers.
-- 
Felipe Contreras

← back to recent threads