diff mbox series

[1/3] t2018: cleanup in current test

Message ID c0c7171e3d523e5d4a0ac01810378447a38854da.1556226502.git.liu.denton@gmail.com (mailing list archive)
State New, archived
Headers show
Series checkout: allow -b/-B to work on a merge base | expand

Commit Message

Denton Liu April 25, 2019, 9:10 p.m. UTC
Before, in t2018, if do_checkout failed to create `branch2`, the next
test-case would run `git branch -D branch2` but then fail because it was
expecting `branch2` to exist, even though it doesn't. As a result, an
early failure could cause a cascading failure of tests.

Make test-case responsible for cleaning up their own branches so that
future tests can start with a sane environment.

Signed-off-by: Denton Liu <liu.denton@gmail.com>
---
 t/t2018-checkout-branch.sh | 39 +++++++++++++-------------------------
 1 file changed, 13 insertions(+), 26 deletions(-)

Comments

Eric Sunshine April 25, 2019, 10:34 p.m. UTC | #1
On Thu, Apr 25, 2019 at 5:10 PM Denton Liu <liu.denton@gmail.com> wrote:
> Before, in t2018, if do_checkout failed to create `branch2`, the next
> test-case would run `git branch -D branch2` but then fail because it was
> expecting `branch2` to exist, even though it doesn't. As a result, an
> early failure could cause a cascading failure of tests.
>
> Make test-case responsible for cleaning up their own branches so that
> future tests can start with a sane environment.
>
> Signed-off-by: Denton Liu <liu.denton@gmail.com>
> ---
> diff --git a/t/t2018-checkout-branch.sh b/t/t2018-checkout-branch.sh
> @@ -60,38 +60,36 @@ test_expect_success 'setup' '
>  test_expect_success 'checkout -b to a new branch, set to HEAD' '
> +       test_when_finished test_might_fail git branch -D branch2 &&
> +       test_when_finished git checkout branch1 &&

I'm aware that when-finished actions fire in reverse order but the
inherent subtlety of ordering of these two invocations still caught me
off-guard for a moment since they are reverse the order in which one
logically thinks about the actions which need to be performed. I
wonder if it would be easier to digest if written like this:

    test_when_finished '
        git checkout branch1 &&
        test_might_fail git branch -D branch2
    ' &&

(Probably not worth a re-roll.)
Denton Liu April 26, 2019, 12:40 a.m. UTC | #2
On Thu, Apr 25, 2019 at 06:34:07PM -0400, Eric Sunshine wrote:
> On Thu, Apr 25, 2019 at 5:10 PM Denton Liu <liu.denton@gmail.com> wrote:
> > Before, in t2018, if do_checkout failed to create `branch2`, the next
> > test-case would run `git branch -D branch2` but then fail because it was
> > expecting `branch2` to exist, even though it doesn't. As a result, an
> > early failure could cause a cascading failure of tests.
> >
> > Make test-case responsible for cleaning up their own branches so that
> > future tests can start with a sane environment.
> >
> > Signed-off-by: Denton Liu <liu.denton@gmail.com>
> > ---
> > diff --git a/t/t2018-checkout-branch.sh b/t/t2018-checkout-branch.sh
> > @@ -60,38 +60,36 @@ test_expect_success 'setup' '
> >  test_expect_success 'checkout -b to a new branch, set to HEAD' '
> > +       test_when_finished test_might_fail git branch -D branch2 &&
> > +       test_when_finished git checkout branch1 &&
> 
> I'm aware that when-finished actions fire in reverse order but the
> inherent subtlety of ordering of these two invocations still caught me
> off-guard for a moment since they are reverse the order in which one
> logically thinks about the actions which need to be performed. I
> wonder if it would be easier to digest if written like this:
> 
>     test_when_finished '
>         git checkout branch1 &&
>         test_might_fail git branch -D branch2
>     ' &&
> 
> (Probably not worth a re-roll.)

This sounds like a good idea. If Junio doesn't get around to it, I'll
send a v1.5 of this patch later. Thanks for the suggestion!
Junio C Hamano April 26, 2019, 2:50 a.m. UTC | #3
Eric Sunshine <sunshine@sunshineco.com> writes:

>>  test_expect_success 'checkout -b to a new branch, set to HEAD' '
>> +       test_when_finished test_might_fail git branch -D branch2 &&
>> +       test_when_finished git checkout branch1 &&
>
> I'm aware that when-finished actions fire in reverse order but the
> inherent subtlety of ordering of these two invocations still caught me
> off-guard for a moment since they are reverse the order in which one
> logically thinks about the actions which need to be performed. I
> wonder if it would be easier to digest if written like this:
>
>     test_when_finished '
>         git checkout branch1 &&
>         test_might_fail git branch -D branch2
>     ' &&

Perhaps.  Be careful in choosing the quotes between sq and dq,
though.
Eric Sunshine April 26, 2019, 6:58 a.m. UTC | #4
On Thu, Apr 25, 2019 at 10:50 PM Junio C Hamano <gitster@pobox.com> wrote:
> Eric Sunshine <sunshine@sunshineco.com> writes:
> >     test_when_finished '
> >         git checkout branch1 &&
> >         test_might_fail git branch -D branch2
> >     ' &&
>
> Perhaps.  Be careful in choosing the quotes between sq and dq,
> though.

Yep, thanks for catching my mistake. Definitely want double-quote, not
single-quote.
diff mbox series

Patch

diff --git a/t/t2018-checkout-branch.sh b/t/t2018-checkout-branch.sh
index c5014ad9a6..fdb7fd282d 100755
--- a/t/t2018-checkout-branch.sh
+++ b/t/t2018-checkout-branch.sh
@@ -60,38 +60,36 @@  test_expect_success 'setup' '
 '
 
 test_expect_success 'checkout -b to a new branch, set to HEAD' '
+	test_when_finished test_might_fail git branch -D branch2 &&
+	test_when_finished git checkout branch1 &&
 	do_checkout branch2
 '
 
 test_expect_success 'checkout -b to a new branch, set to an explicit ref' '
-	git checkout branch1 &&
-	git branch -D branch2 &&
-
+	test_when_finished test_might_fail git branch -D branch2 &&
+	test_when_finished git checkout branch1 &&
 	do_checkout branch2 $HEAD1
 '
 
 test_expect_success 'checkout -b to a new branch with unmergeable changes fails' '
-	git checkout branch1 &&
-
-	# clean up from previous test
-	git branch -D branch2 &&
-
 	setup_dirty_unmergeable &&
 	test_must_fail do_checkout branch2 $HEAD1 &&
 	test_dirty_unmergeable
 '
 
 test_expect_success 'checkout -f -b to a new branch with unmergeable changes discards changes' '
+	test_when_finished test_might_fail git branch -D branch2 &&
+	test_when_finished git checkout branch1 &&
+
 	# still dirty and on branch1
 	do_checkout branch2 $HEAD1 "-f -b" &&
 	test_must_fail test_dirty_unmergeable
 '
 
 test_expect_success 'checkout -b to a new branch preserves mergeable changes' '
-	git checkout branch1 &&
-
-	# clean up from previous test
-	git branch -D branch2 &&
+	test_when_finished test_might_fail git branch -D branch2 &&
+	test_when_finished git checkout branch1 &&
+	test_when_finished git reset --hard &&
 
 	setup_dirty_mergeable &&
 	do_checkout branch2 $HEAD1 &&
@@ -99,27 +97,18 @@  test_expect_success 'checkout -b to a new branch preserves mergeable changes' '
 '
 
 test_expect_success 'checkout -f -b to a new branch with mergeable changes discards changes' '
-	# clean up from previous test
-	git reset --hard &&
-
-	git checkout branch1 &&
-
-	# clean up from previous test
-	git branch -D branch2 &&
-
+	test_when_finished git reset --hard HEAD &&
 	setup_dirty_mergeable &&
 	do_checkout branch2 $HEAD1 "-f -b" &&
 	test_must_fail test_dirty_mergeable
 '
 
 test_expect_success 'checkout -b to an existing branch fails' '
-	git reset --hard HEAD &&
-
+	test_when_finished git reset --hard HEAD &&
 	test_must_fail do_checkout branch2 $HEAD2
 '
 
 test_expect_success 'checkout -b to @{-1} fails with the right branch name' '
-	git reset --hard HEAD &&
 	git checkout branch1 &&
 	git checkout branch2 &&
 	echo  >expect "fatal: A branch named '\''branch1'\'' already exists." &&
@@ -160,6 +149,7 @@  test_expect_success 'checkout -f -B to an existing branch with unmergeable chang
 '
 
 test_expect_success 'checkout -B to an existing branch preserves mergeable changes' '
+	test_when_finished git reset --hard &&
 	git checkout branch1 &&
 
 	setup_dirty_mergeable &&
@@ -168,9 +158,6 @@  test_expect_success 'checkout -B to an existing branch preserves mergeable chang
 '
 
 test_expect_success 'checkout -f -B to an existing branch with mergeable changes discards changes' '
-	# clean up from previous test
-	git reset --hard &&
-
 	git checkout branch1 &&
 
 	setup_dirty_mergeable &&