@@ -183,7 +183,7 @@ test_git_directory_is_unchanged () {
183183 )
184184}
185185
186- test_git_directory_exists () {
186+ test_git_directory_exists () {
187187 test -e " .git/modules/$1 " &&
188188 if test -f sub1/.git
189189 then
@@ -303,13 +303,17 @@ test_submodule_content () {
303303# update" is run. And even then that command doesn't delete the work tree of
304304# a removed submodule.
305305#
306+ # The first argument of the callback function will be the name of the submodule.
307+ #
306308# Removing a submodule containing a .git directory must fail even when forced
307- # to protect the history!
309+ # to protect the history! If we are testing this case, the second argument of
310+ # the callback function will be 'test_must_fail', else it will be the empty
311+ # string.
308312#
309313
310- # Internal function; use test_submodule_switch() or
311- # test_submodule_forced_switch() instead.
312- test_submodule_switch_common () {
314+ # Internal function; use test_submodule_switch_func(), test_submodule_switch(),
315+ # or test_submodule_forced_switch() instead.
316+ test_submodule_switch_common () {
313317 command=" $1 "
314318 # ######################## Appearing submodule #########################
315319 # Switching to a commit letting a submodule appear creates empty dir ...
@@ -443,7 +447,7 @@ test_submodule_switch_common() {
443447 (
444448 cd submodule_update &&
445449 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
446- test_must_fail $command replace_sub1_with_directory &&
450+ $command replace_sub1_with_directory test_must_fail &&
447451 test_superproject_content origin/add_sub1 &&
448452 test_submodule_content sub1 origin/add_sub1
449453 )
@@ -456,7 +460,7 @@ test_submodule_switch_common() {
456460 cd submodule_update &&
457461 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
458462 replace_gitfile_with_git_dir sub1 &&
459- test_must_fail $command replace_sub1_with_directory &&
463+ $command replace_sub1_with_directory test_must_fail &&
460464 test_superproject_content origin/add_sub1 &&
461465 test_git_directory_is_unchanged sub1 &&
462466 test_submodule_content sub1 origin/add_sub1
@@ -470,7 +474,7 @@ test_submodule_switch_common() {
470474 (
471475 cd submodule_update &&
472476 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
473- test_must_fail $command replace_sub1_with_file &&
477+ $command replace_sub1_with_file test_must_fail &&
474478 test_superproject_content origin/add_sub1 &&
475479 test_submodule_content sub1 origin/add_sub1
476480 )
@@ -484,7 +488,7 @@ test_submodule_switch_common() {
484488 cd submodule_update &&
485489 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
486490 replace_gitfile_with_git_dir sub1 &&
487- test_must_fail $command replace_sub1_with_file &&
491+ $command replace_sub1_with_file test_must_fail &&
488492 test_superproject_content origin/add_sub1 &&
489493 test_git_directory_is_unchanged sub1 &&
490494 test_submodule_content sub1 origin/add_sub1
@@ -559,15 +563,28 @@ test_submodule_switch_common() {
559563# conditions, set the appropriate KNOWN_FAILURE_* variable used in the tests
560564# below to 1.
561565#
562- # Use as follows:
566+ # The first argument of the callback function will be the name of the submodule.
567+ #
568+ # Removing a submodule containing a .git directory must fail even when forced
569+ # to protect the history! If we are testing this case, the second argument of
570+ # the callback function will be 'test_must_fail', else it will be the empty
571+ # string.
572+ #
573+ # The following example uses `git some-command` as an example command to be
574+ # tested. It updates the worktree and index to match a target, but not any
575+ # submodule directories.
563576#
564577# my_func () {
565- # target=$1
566- # # Do something here that updates the worktree and index to match target,
567- # # but not any submodule directories.
578+ # ...prepare for `git some-command` to be run...
579+ # $2 git some-command "$1" &&
580+ # if test -n "$2"
581+ # then
582+ # return
583+ # fi &&
584+ # ...check the state after git some-command is run...
568585# }
569- # test_submodule_switch "my_func"
570- test_submodule_switch () {
586+ # test_submodule_switch_func "my_func"
587+ test_submodule_switch_func () {
571588 command=" $1 "
572589 test_submodule_switch_common " $command "
573590
@@ -580,17 +597,33 @@ test_submodule_switch () {
580597 cd submodule_update &&
581598 git branch -t add_sub1 origin/add_sub1 &&
582599 >sub1 &&
583- test_must_fail $command add_sub1 &&
600+ $command add_sub1 test_must_fail &&
584601 test_superproject_content origin/no_submodule &&
585602 test_must_be_empty sub1
586603 )
587604 '
588605}
589606
607+ # Ensures that the that the arg either contains "test_must_fail" or is empty.
608+ may_only_be_test_must_fail () {
609+ test -z " $1 " || test " $1 " = test_must_fail || die
610+ }
611+
612+ git_test_func () {
613+ may_only_be_test_must_fail " $2 " &&
614+ $2 git $gitcmd " $1 "
615+ }
616+
617+ test_submodule_switch () {
618+ gitcmd=" $1 "
619+ test_submodule_switch_func " git_test_func"
620+ }
621+
590622# Same as test_submodule_switch(), except that throwing away local changes in
591623# the superproject is allowed.
592624test_submodule_forced_switch () {
593- command=" $1 "
625+ gitcmd=" $1 "
626+ command=" git_test_func"
594627 KNOWN_FAILURE_FORCED_SWITCH_TESTS=1
595628 test_submodule_switch_common " $command "
596629
@@ -631,8 +664,8 @@ test_submodule_forced_switch () {
631664
632665# Internal function; use test_submodule_switch_recursing_with_args() or
633666# test_submodule_forced_switch_recursing_with_args() instead.
634- test_submodule_recursing_with_args_common () {
635- command=" $1 "
667+ test_submodule_recursing_with_args_common () {
668+ command=" $1 --recurse-submodules "
636669
637670 # ######################## Appearing submodule #########################
638671 # Switching to a commit letting a submodule appear checks it out ...
@@ -840,7 +873,7 @@ test_submodule_recursing_with_args_common() {
840873# test_submodule_switch_recursing_with_args "$GIT_COMMAND"
841874test_submodule_switch_recursing_with_args () {
842875 cmd_args=" $1 "
843- command=" git $cmd_args --recurse-submodules "
876+ command=" git $cmd_args "
844877 test_submodule_recursing_with_args_common " $command "
845878
846879 RESULTDS=success
@@ -957,7 +990,7 @@ test_submodule_switch_recursing_with_args () {
957990# away local changes in the superproject is allowed.
958991test_submodule_forced_switch_recursing_with_args () {
959992 cmd_args=" $1 "
960- command=" git $cmd_args --recurse-submodules "
993+ command=" git $cmd_args "
961994 test_submodule_recursing_with_args_common " $command "
962995
963996 RESULT=success
0 commit comments