Skip to content

Commit 7f330ea

Browse files
Denton-Lgitster
authored andcommitted
lib-submodule-update: use callbacks in test_submodule_switch_common()
When we run a test helper function in test_submodule_switch_common(), we sometimes specify a whole helper function as the $command. When we do this, in some test cases, we just mark the whole function with `test_must_fail`. However, it's possible that the helper function might fail earlier or later than expected due to an introduced bug. If this happens, then the test case will still report as passing but it should really be marked as failing since it didn't actually display the intended behaviour. Instead of invoking $command as one monolithic helper function, break it up into three parts: 1. $command which is always a git command. 2. $before which is a callback function that runs just prior to $command. 3. $after which is a callback function that runs just after $command. If the command requires a filename argument, specify it as `\$arg` since that variable will be set and the whole $command string will be eval'd. Unfortunately, there is no way to get rid of the eval as some of the commands are passed (such as the `git pull` tests) require that no additional arguments are passed so we must have some mechanism for the caller to specify whether or not it wants the filename argument. The $before and $after callback functions will be passed the filename as the first arg. These callback functions are optional and, if missing, will be replaced with `true`. Also, in the case where we have a `test_must_fail` test, $after will not be executed, similar to how the helper functions currently behave when the git command fails and exits the &&-chain. Finally, as an added bonus, `test_must_fail` will only run on $command which is guaranteed to be a git command. An alternate design was considered where the $OVERWRITING_FAIL is set from the test_submodule_switch_common() function and passed to the helper function. This approach was considered too difficult to understand due to the fact that using a signalling magic environment variable might be too indirect. Signed-off-by: Denton Liu <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent aa06180 commit 7f330ea

8 files changed

+108
-79
lines changed

t/lib-submodule-update.sh

Lines changed: 74 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,20 @@ test_submodule_content () {
310310
# Internal function; use test_submodule_switch_func(), test_submodule_switch(),
311311
# or test_submodule_forced_switch() instead.
312312
test_submodule_switch_common () {
313-
command="$1"
313+
command="$1" # should be a git command
314+
before="$2"
315+
after="$3"
316+
317+
if test -z "$before"
318+
then
319+
before=true
320+
fi
321+
322+
if test -z "$after"
323+
then
324+
after=true
325+
fi
326+
314327
######################### Appearing submodule #########################
315328
# Switching to a commit letting a submodule appear creates empty dir ...
316329
if test "$KNOWN_FAILURE_STASH_DOES_IGNORE_SUBMODULE_CHANGES" = 1
@@ -326,7 +339,10 @@ test_submodule_switch_common () {
326339
(
327340
cd submodule_update &&
328341
git branch -t add_sub1 origin/add_sub1 &&
329-
$command add_sub1 &&
342+
arg=add_sub1 &&
343+
$before "$arg" &&
344+
eval $command &&
345+
$after "$arg" &&
330346
test_superproject_content origin/add_sub1 &&
331347
test_dir_is_empty sub1 &&
332348
git submodule update --init --recursive &&
@@ -341,7 +357,10 @@ test_submodule_switch_common () {
341357
cd submodule_update &&
342358
mkdir sub1 &&
343359
git branch -t add_sub1 origin/add_sub1 &&
344-
$command add_sub1 &&
360+
arg=add_sub1 &&
361+
$before "$arg" &&
362+
eval $command &&
363+
$after "$arg" &&
345364
test_superproject_content origin/add_sub1 &&
346365
test_dir_is_empty sub1 &&
347366
git submodule update --init --recursive &&
@@ -356,7 +375,10 @@ test_submodule_switch_common () {
356375
(
357376
cd submodule_update &&
358377
git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
359-
$command replace_file_with_sub1 &&
378+
arg=replace_file_with_sub1 &&
379+
$before "$arg" &&
380+
eval $command &&
381+
$after "$arg" &&
360382
test_superproject_content origin/replace_file_with_sub1 &&
361383
test_dir_is_empty sub1 &&
362384
git submodule update --init --recursive &&
@@ -380,7 +402,10 @@ test_submodule_switch_common () {
380402
(
381403
cd submodule_update &&
382404
git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
383-
$command replace_directory_with_sub1 &&
405+
arg=replace_directory_with_sub1 &&
406+
$before "$arg" &&
407+
eval $command &&
408+
$after "$arg" &&
384409
test_superproject_content origin/replace_directory_with_sub1 &&
385410
test_dir_is_empty sub1 &&
386411
git submodule update --init --recursive &&
@@ -402,7 +427,10 @@ test_submodule_switch_common () {
402427
(
403428
cd submodule_update &&
404429
git branch -t remove_sub1 origin/remove_sub1 &&
405-
$command remove_sub1 &&
430+
arg=remove_sub1 &&
431+
$before "$arg" &&
432+
eval $command &&
433+
$after "$arg" &&
406434
test_superproject_content origin/remove_sub1 &&
407435
test_submodule_content sub1 origin/add_sub1
408436
)
@@ -415,7 +443,10 @@ test_submodule_switch_common () {
415443
cd submodule_update &&
416444
git branch -t remove_sub1 origin/remove_sub1 &&
417445
replace_gitfile_with_git_dir sub1 &&
418-
$command remove_sub1 &&
446+
arg=remove_sub1 &&
447+
$before "$arg" &&
448+
eval $command &&
449+
$after "$arg" &&
419450
test_superproject_content origin/remove_sub1 &&
420451
test_git_directory_is_unchanged sub1 &&
421452
test_submodule_content sub1 origin/add_sub1
@@ -443,7 +474,9 @@ test_submodule_switch_common () {
443474
(
444475
cd submodule_update &&
445476
git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
446-
test_must_fail $command replace_sub1_with_directory &&
477+
arg=replace_sub1_with_directory &&
478+
$before "$arg" &&
479+
eval test_must_fail $command &&
447480
test_superproject_content origin/add_sub1 &&
448481
test_submodule_content sub1 origin/add_sub1
449482
)
@@ -456,7 +489,9 @@ test_submodule_switch_common () {
456489
cd submodule_update &&
457490
git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
458491
replace_gitfile_with_git_dir sub1 &&
459-
test_must_fail $command replace_sub1_with_directory &&
492+
arg=replace_sub1_with_directory &&
493+
$before "$arg" &&
494+
eval test_must_fail $command &&
460495
test_superproject_content origin/add_sub1 &&
461496
test_git_directory_is_unchanged sub1 &&
462497
test_submodule_content sub1 origin/add_sub1
@@ -470,7 +505,9 @@ test_submodule_switch_common () {
470505
(
471506
cd submodule_update &&
472507
git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
473-
test_must_fail $command replace_sub1_with_file &&
508+
arg=replace_sub1_with_file &&
509+
$before "$arg" &&
510+
eval test_must_fail $command &&
474511
test_superproject_content origin/add_sub1 &&
475512
test_submodule_content sub1 origin/add_sub1
476513
)
@@ -484,7 +521,9 @@ test_submodule_switch_common () {
484521
cd submodule_update &&
485522
git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
486523
replace_gitfile_with_git_dir sub1 &&
487-
test_must_fail $command replace_sub1_with_file &&
524+
arg=replace_sub1_with_file &&
525+
$before "$arg" &&
526+
eval test_must_fail $command &&
488527
test_superproject_content origin/add_sub1 &&
489528
test_git_directory_is_unchanged sub1 &&
490529
test_submodule_content sub1 origin/add_sub1
@@ -508,7 +547,10 @@ test_submodule_switch_common () {
508547
(
509548
cd submodule_update &&
510549
git branch -t modify_sub1 origin/modify_sub1 &&
511-
$command modify_sub1 &&
550+
arg=modify_sub1 &&
551+
$before "$arg" &&
552+
eval $command &&
553+
$after "$arg" &&
512554
test_superproject_content origin/modify_sub1 &&
513555
test_submodule_content sub1 origin/add_sub1 &&
514556
git submodule update &&
@@ -523,7 +565,10 @@ test_submodule_switch_common () {
523565
(
524566
cd submodule_update &&
525567
git branch -t invalid_sub1 origin/invalid_sub1 &&
526-
$command invalid_sub1 &&
568+
arg=invalid_sub1 &&
569+
$before "$arg" &&
570+
eval $command &&
571+
$after "$arg" &&
527572
test_superproject_content origin/invalid_sub1 &&
528573
test_submodule_content sub1 origin/add_sub1 &&
529574
test_must_fail git submodule update &&
@@ -538,7 +583,10 @@ test_submodule_switch_common () {
538583
(
539584
cd submodule_update &&
540585
git branch -t valid_sub1 origin/valid_sub1 &&
541-
$command valid_sub1 &&
586+
arg=valid_sub1 &&
587+
$before "$arg" &&
588+
eval $command &&
589+
$after "$arg" &&
542590
test_superproject_content origin/valid_sub1 &&
543591
test_dir_is_empty sub1 &&
544592
git submodule update --init --recursive &&
@@ -568,8 +616,10 @@ test_submodule_switch_common () {
568616
# }
569617
# test_submodule_switch_func "my_func"
570618
test_submodule_switch_func () {
571-
command="$1"
572-
test_submodule_switch_common "$command"
619+
command="git $1"
620+
before="$2"
621+
after="$3"
622+
test_submodule_switch_common "$command" "$before" "$after"
573623

574624
# An empty directory does not prevent the creation of a submodule of
575625
# the same name, but a file does.
@@ -580,23 +630,25 @@ test_submodule_switch_func () {
580630
cd submodule_update &&
581631
git branch -t add_sub1 origin/add_sub1 &&
582632
>sub1 &&
583-
test_must_fail $command add_sub1 &&
633+
arg=add_sub1 &&
634+
$before "$arg" &&
635+
eval test_must_fail $command &&
584636
test_superproject_content origin/no_submodule &&
585637
test_must_be_empty sub1
586638
)
587639
'
588640
}
589641

590642
test_submodule_switch () {
591-
test_submodule_switch_func "git $1"
643+
test_submodule_switch_func "$1 \$arg"
592644
}
593645

594646
# Same as test_submodule_switch(), except that throwing away local changes in
595647
# the superproject is allowed.
596648
test_submodule_forced_switch () {
597-
command="$1"
649+
command="git $1 \$arg"
598650
KNOWN_FAILURE_FORCED_SWITCH_TESTS=1
599-
test_submodule_switch_common "git $command"
651+
test_submodule_switch_common "$command"
600652

601653
# When forced, a file in the superproject does not prevent creating a
602654
# submodule of the same name.
@@ -607,7 +659,8 @@ test_submodule_forced_switch () {
607659
cd submodule_update &&
608660
git branch -t add_sub1 origin/add_sub1 &&
609661
>sub1 &&
610-
$command add_sub1 &&
662+
arg=add_sub1 &&
663+
eval $command &&
611664
test_superproject_content origin/add_sub1 &&
612665
test_dir_is_empty sub1
613666
)

t/t3426-rebase-submodule.sh

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@ git_rebase () {
1616
git revert HEAD &&
1717
git status -su >actual &&
1818
ls -1pR * >>actual &&
19-
test_cmp expect actual &&
20-
git rebase "$1"
19+
test_cmp expect actual
2120
}
2221

23-
test_submodule_switch_func "git_rebase"
22+
test_submodule_switch_func "rebase \$arg" "git_rebase"
2423

2524
git_rebase_interactive () {
2625
git status -su >expect &&
@@ -34,11 +33,10 @@ git_rebase_interactive () {
3433
ls -1pR * >>actual &&
3534
test_cmp expect actual &&
3635
set_fake_editor &&
37-
echo "fake-editor.sh" >.git/info/exclude &&
38-
git rebase -i "$1"
36+
echo "fake-editor.sh" >.git/info/exclude
3937
}
4038

41-
test_submodule_switch_func "git_rebase_interactive"
39+
test_submodule_switch_func "rebase -i \$arg" "git_rebase_interactive"
4240

4341
test_expect_success 'rebase interactive ignores modified submodules' '
4442
test_when_finished "rm -rf super sub" &&

t/t3513-revert-submodule.sh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ test_description='revert can handle submodules'
1111
# first so we can restore the work tree test setup after doing the checkout
1212
# and revert. We test here that the restored work tree content is identical
1313
# to that at the beginning. The last revert is then tested by the framework.
14-
git_revert () {
14+
git_revert_before () {
1515
git status -su >expect &&
1616
ls -1pR * >>expect &&
17-
tar cf "$TRASH_DIRECTORY/tmp.tar" * &&
18-
git checkout "$1" &&
17+
tar cf "$TRASH_DIRECTORY/tmp.tar" *
18+
}
19+
20+
git_revert_after () {
1921
git revert HEAD &&
2022
rm -rf * &&
2123
tar xf "$TRASH_DIRECTORY/tmp.tar" &&
@@ -26,6 +28,6 @@ git_revert () {
2628
}
2729

2830
KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR=1
29-
test_submodule_switch_func "git_revert"
31+
test_submodule_switch_func "checkout \$arg" "git_revert_before" "git_revert_after"
3032

3133
test_done

t/t3906-stash-submodule.sh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ test_description='stash can handle submodules'
55
. ./test-lib.sh
66
. "$TEST_DIRECTORY"/lib-submodule-update.sh
77

8-
git_stash () {
8+
git_stash_before () {
99
git status -su >expect &&
10-
ls -1pR * >>expect &&
11-
git read-tree -u -m "$1" &&
10+
ls -1pR * >>expect
11+
}
12+
13+
git_stash_after () {
1214
git stash &&
1315
git status -su >actual &&
1416
ls -1pR * >>actual &&
@@ -19,7 +21,7 @@ git_stash () {
1921
KNOWN_FAILURE_STASH_DOES_IGNORE_SUBMODULE_CHANGES=1
2022
KNOWN_FAILURE_CHERRY_PICK_SEES_EMPTY_COMMIT=1
2123
KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR=1
22-
test_submodule_switch_func "git_stash"
24+
test_submodule_switch_func "read-tree -u -m \$arg" "git_stash_before" "git_stash_after"
2325

2426
setup_basic () {
2527
test_when_finished "rm -rf main sub" &&

t/t4137-apply-submodule.sh

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,12 @@ test_description='git apply handling submodules'
55
. ./test-lib.sh
66
. "$TEST_DIRECTORY"/lib-submodule-update.sh
77

8-
apply_index () {
9-
git diff --ignore-submodules=dirty "..$1" | git apply --index -
8+
create_diff () {
9+
git diff --ignore-submodules=dirty "..$1" >diff
1010
}
1111

12-
test_submodule_switch_func "apply_index"
12+
test_submodule_switch_func "apply --index diff" "create_diff"
1313

14-
apply_3way () {
15-
git diff --ignore-submodules=dirty "..$1" | git apply --3way -
16-
}
17-
18-
test_submodule_switch_func "apply_3way"
14+
test_submodule_switch_func "apply --3way diff" "create_diff"
1915

2016
test_done

t/t4255-am-submodule.sh

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,14 @@ test_description='git am handling submodules'
55
. ./test-lib.sh
66
. "$TEST_DIRECTORY"/lib-submodule-update.sh
77

8-
am () {
9-
git format-patch --stdout --ignore-submodules=dirty "..$1" | git am -
8+
create_patch () {
9+
git format-patch --stdout --ignore-submodules=dirty "..$1" >patch
1010
}
1111

12-
test_submodule_switch_func "am"
13-
14-
am_3way () {
15-
git format-patch --stdout --ignore-submodules=dirty "..$1" | git am --3way -
16-
}
12+
test_submodule_switch_func "am patch" "create_patch"
1713

1814
KNOWN_FAILURE_NOFF_MERGE_ATTEMPTS_TO_MERGE_REMOVED_SUBMODULE_FILES=1
19-
test_submodule_switch_func "am_3way"
15+
test_submodule_switch_func "am --3way patch" "create_patch"
2016

2117
test_expect_success 'setup diff.submodule' '
2218
test_commit one &&

t/t5572-pull-submodule.sh

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,16 @@ reset_branch_to_HEAD () {
1111
git branch --set-upstream-to="origin/$1" "$1"
1212
}
1313

14-
git_pull () {
15-
reset_branch_to_HEAD "$1" &&
16-
git pull
17-
}
18-
1914
# pulls without conflicts
20-
test_submodule_switch_func "git_pull"
21-
22-
git_pull_ff () {
23-
reset_branch_to_HEAD "$1" &&
24-
git pull --ff
25-
}
15+
test_submodule_switch_func "pull" "reset_branch_to_HEAD"
2616

27-
test_submodule_switch_func "git_pull_ff"
17+
test_submodule_switch_func "pull --ff" "reset_branch_to_HEAD"
2818

29-
git_pull_ff_only () {
30-
reset_branch_to_HEAD "$1" &&
31-
git pull --ff-only
32-
}
33-
34-
test_submodule_switch_func "git_pull_ff_only"
35-
36-
git_pull_noff () {
37-
reset_branch_to_HEAD "$1" &&
38-
git pull --no-ff
39-
}
19+
test_submodule_switch_func "pull --ff-only" "reset_branch_to_HEAD"
4020

4121
KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR=1
4222
KNOWN_FAILURE_NOFF_MERGE_ATTEMPTS_TO_MERGE_REMOVED_SUBMODULE_FILES=1
43-
test_submodule_switch_func "git_pull_noff"
23+
test_submodule_switch_func "pull --no-ff" "reset_branch_to_HEAD"
4424

4525
test_expect_success 'pull --recurse-submodule setup' '
4626
test_create_repo child &&

0 commit comments

Comments
 (0)