Skip to content

Commit d62717b

Browse files
committed
Merge branch 'dd/t5403-modernise'
Test clean-up. * dd/t5403-modernise: t5403: use test_cmp for post-checkout argument checks t5403: introduce check_post_checkout helper function
2 parents 4ae96a4 + 7a747f9 commit d62717b

1 file changed

Lines changed: 30 additions & 23 deletions

File tree

t/t5403-post-checkout-hook.sh

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,20 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
99

1010
. ./test-lib.sh
1111

12+
# Usage: check_post_checkout <file> <old-ref> <new-ref> <flag>
13+
#
14+
# Verify that the post-checkout hook arguments in <file> match the expected
15+
# values: <old-ref> for the previous HEAD, <new-ref> for the new HEAD, and
16+
# <flag> indicating whether this was a branch checkout (1) or file checkout (0).
17+
check_post_checkout () {
18+
test "$#" = 4 || BUG "check_post_checkout takes 4 args"
19+
echo "old=$2 new=$3 flag=$4" >expect &&
20+
test_cmp expect "$1"
21+
}
22+
1223
test_expect_success setup '
1324
test_hook --setup post-checkout <<-\EOF &&
14-
echo "$@" >.git/post-checkout.args
25+
echo "old=$1 new=$2 flag=$3" >.git/post-checkout.args
1526
EOF
1627
test_commit one &&
1728
test_commit two &&
@@ -23,29 +34,30 @@ test_expect_success setup '
2334
test_expect_success 'post-checkout receives the right arguments with HEAD unchanged ' '
2435
test_when_finished "rm -f .git/post-checkout.args" &&
2536
git checkout main &&
26-
read old new flag <.git/post-checkout.args &&
27-
test $old = $new && test $flag = 1
37+
check_post_checkout .git/post-checkout.args \
38+
"$(git rev-parse HEAD)" "$(git rev-parse HEAD)" 1
2839
'
2940

3041
test_expect_success 'post-checkout args are correct with git checkout -b ' '
3142
test_when_finished "rm -f .git/post-checkout.args" &&
3243
git checkout -b new1 &&
33-
read old new flag <.git/post-checkout.args &&
34-
test $old = $new && test $flag = 1
44+
check_post_checkout .git/post-checkout.args \
45+
"$(git rev-parse HEAD)" "$(git rev-parse HEAD)" 1
3546
'
3647

3748
test_expect_success 'post-checkout receives the right args with HEAD changed ' '
3849
test_when_finished "rm -f .git/post-checkout.args" &&
50+
old=$(git rev-parse HEAD) &&
3951
git checkout two &&
40-
read old new flag <.git/post-checkout.args &&
41-
test $old != $new && test $flag = 1
52+
check_post_checkout .git/post-checkout.args \
53+
"$old" "$(git rev-parse HEAD)" 1
4254
'
4355

4456
test_expect_success 'post-checkout receives the right args when not switching branches ' '
4557
test_when_finished "rm -f .git/post-checkout.args" &&
4658
git checkout main -- three.t &&
47-
read old new flag <.git/post-checkout.args &&
48-
test $old = $new && test $flag = 0
59+
check_post_checkout .git/post-checkout.args \
60+
"$(git rev-parse HEAD)" "$(git rev-parse HEAD)" 0
4961
'
5062

5163
test_rebase () {
@@ -55,21 +67,17 @@ test_rebase () {
5567
git checkout -B rebase-test main &&
5668
rm -f .git/post-checkout.args &&
5769
git rebase $args rebase-on-me &&
58-
read old new flag <.git/post-checkout.args &&
59-
test_cmp_rev main $old &&
60-
test_cmp_rev rebase-on-me $new &&
61-
test $flag = 1
70+
check_post_checkout .git/post-checkout.args \
71+
"$(git rev-parse main)" "$(git rev-parse rebase-on-me)" 1
6272
'
6373

6474
test_expect_success "post-checkout is triggered on rebase $args with fast-forward" '
6575
test_when_finished "rm -f .git/post-checkout.args" &&
6676
git checkout -B ff-rebase-test rebase-on-me^ &&
6777
rm -f .git/post-checkout.args &&
6878
git rebase $args rebase-on-me &&
69-
read old new flag <.git/post-checkout.args &&
70-
test_cmp_rev rebase-on-me^ $old &&
71-
test_cmp_rev rebase-on-me $new &&
72-
test $flag = 1
79+
check_post_checkout .git/post-checkout.args \
80+
"$(git rev-parse rebase-on-me^)" "$(git rev-parse rebase-on-me)" 1
7381
'
7482

7583
test_expect_success "rebase $args fast-forward branch checkout runs post-checkout hook" '
@@ -79,10 +87,8 @@ test_rebase () {
7987
git checkout two &&
8088
rm -f .git/post-checkout.args &&
8189
git rebase $args HEAD rebase-fast-forward &&
82-
read old new flag <.git/post-checkout.args &&
83-
test_cmp_rev two $old &&
84-
test_cmp_rev three $new &&
85-
test $flag = 1
90+
check_post_checkout .git/post-checkout.args \
91+
"$(git rev-parse two)" "$(git rev-parse three)" 1
8692
'
8793

8894
test_expect_success "rebase $args checkout does not remove untracked files" '
@@ -106,10 +112,11 @@ test_rebase --merge
106112
test_expect_success 'post-checkout hook is triggered by clone' '
107113
mkdir -p templates/hooks &&
108114
write_script templates/hooks/post-checkout <<-\EOF &&
109-
echo "$@" >"$GIT_DIR/post-checkout.args"
115+
echo "old=$1 new=$2 flag=$3" >"$GIT_DIR/post-checkout.args"
110116
EOF
111117
git clone --template=templates . clone3 &&
112-
test_path_is_file clone3/.git/post-checkout.args
118+
check_post_checkout clone3/.git/post-checkout.args \
119+
"$(test_oid zero)" "$(git -C clone3 rev-parse HEAD)" 1
113120
'
114121

115122
test_done

0 commit comments

Comments
 (0)