Tests: clean up submodule recursive helpers
This continues the work in commit d3b5a49 ("Tests: clean up and document submodule helpers", 2017-11-08). Factor out the commonalities from test_submodule_switch_recursing_with_args() and test_submodule_forced_switch_recursing_with_args() in lib-submodule-update.sh, and document their usage. Some tests differ slightly in their test assertions; I have used the superset of those assertions in that case. Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jonathan Tan committed
Nov 21, 2017 at 14:12 UTC
a05004471630ecc97dcb84cde2a1616ac9a3c477
1 file changed
+125
-218
t/lib-submodule-update.sh
+125
-218
@@ -554,6 +554,10 @@ test_submodule_switch_common() {
554
# - if succeeds, once "git submodule update" is invoked, the contents of
555
# submodule directories are updated
556
#
557
+# If the command under test is known to not work with submodules in certain
558
+# conditions, set the appropriate KNOWN_FAILURE_* variable used in the tests
559
+# below to 1.
560
+#
561
# Use as follows:
562
#
563
# my_func () {
@@ -622,19 +626,11 @@ test_submodule_forced_switch () {
626
# - Removing a submodule with a git directory absorbs the submodules
627
# git directory first into the superproject.
628
625
-test_submodule_switch_recursing_with_args () {
626
- cmd_args="$1"
627
- command="git $cmd_args --recurse-submodules"
628
- RESULTDS=success
629
- if test "$KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS" = 1
630
- then
631
- RESULTDS=failure
632
- fi
633
- RESULTOI=success
634
- if test "$KNOWN_FAILURE_SUBMODULE_OVERWRITE_IGNORED_UNTRACKED" = 1
635
- then
636
- RESULTOI=failure
637
- fi
629
+# Internal function; use test_submodule_switch_recursing_with_args() or
630
+# test_submodule_forced_switch_recursing_with_args() instead.
631
+test_submodule_recursing_with_args_common() {
632
+ command="$1"
633
+
634
######################### Appearing submodule #########################
635
# Switching to a commit letting a submodule appear checks it out ...
636
test_expect_success "$command: added submodule is checked out" '
@@ -648,7 +644,7 @@ test_submodule_switch_recursing_with_args () {
644
test_submodule_content sub1 origin/add_sub1
645
)
646
'
651
- # ... ignoring an empty existing directory ...
647
+ # ... ignoring an empty existing directory.
648
test_expect_success "$command: added submodule is checked out in empty dir" '
649
prolog &&
650
reset_work_tree_to_interested no_submodule &&
@@ -661,34 +657,6 @@ test_submodule_switch_recursing_with_args () {
657
test_submodule_content sub1 origin/add_sub1
658
)
659
'
664
- # ... unless there is an untracked file in its place.
665
- test_expect_success "$command: added submodule doesn't remove untracked file with same name" '
666
- prolog &&
667
- reset_work_tree_to_interested no_submodule &&
668
- (
669
- cd submodule_update &&
670
- git branch -t add_sub1 origin/add_sub1 &&
671
- : >sub1 &&
672
- test_must_fail $command add_sub1 &&
673
- test_superproject_content origin/no_submodule &&
674
- test_must_be_empty sub1
675
- )
676
- '
677
- # ... but an ignored file is fine.
678
- test_expect_$RESULTOI "$command: added submodule removes an untracked ignored file" '
679
- test_when_finished "rm submodule_update/.git/info/exclude" &&
680
- prolog &&
681
- reset_work_tree_to_interested no_submodule &&
682
- (
683
- cd submodule_update &&
684
- git branch -t add_sub1 origin/add_sub1 &&
685
- : >sub1 &&
686
- echo sub1 >.git/info/exclude
687
- $command add_sub1 &&
688
- test_superproject_content origin/add_sub1 &&
689
- test_submodule_content sub1 origin/add_sub1
690
- )
691
- '
660
# Replacing a tracked file with a submodule produces a checked out submodule
661
test_expect_success "$command: replace tracked file with submodule checks out submodule" '
662
prolog &&
@@ -742,33 +710,6 @@ test_submodule_switch_recursing_with_args () {
710
test_git_directory_exists sub1
711
)
712
'
745
- # Replacing a submodule with files in a directory must succeeds
746
- # when the submodule is clean
747
- test_expect_$RESULTDS "$command: replace submodule with a directory" '
748
- prolog &&
749
- reset_work_tree_to_interested add_sub1 &&
750
- (
751
- cd submodule_update &&
752
- git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
753
- $command replace_sub1_with_directory &&
754
- test_superproject_content origin/replace_sub1_with_directory &&
755
- test_submodule_content sub1 origin/replace_sub1_with_directory
756
- )
757
- '
758
- # ... absorbing a .git directory.
759
- test_expect_$RESULTDS "$command: replace submodule containing a .git directory with a directory must absorb the git dir" '
760
- prolog &&
761
- reset_work_tree_to_interested add_sub1 &&
762
- (
763
- cd submodule_update &&
764
- git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
765
- replace_gitfile_with_git_dir sub1 &&
766
- rm -rf .git/modules &&
767
- $command replace_sub1_with_directory &&
768
- test_superproject_content origin/replace_sub1_with_directory &&
769
- test_git_directory_exists sub1
770
- )
771
- '
713
714
# Replacing it with a file ...
715
test_expect_success "$command: replace submodule with a file" '
@@ -782,7 +723,11 @@ test_submodule_switch_recursing_with_args () {
723
test -f sub1
724
)
725
'
785
-
726
+ RESULTDS=success
727
+ if test "$KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS" = 1
728
+ then
729
+ RESULTDS=failure
730
+ fi
731
# ... must check its local work tree for untracked files
732
test_expect_$RESULTDS "$command: replace submodule with a file must fail with untracked files" '
733
prolog &&
@@ -794,21 +739,7 @@ test_submodule_switch_recursing_with_args () {
739
test_must_fail $command replace_sub1_with_file &&
740
test_superproject_content origin/add_sub1 &&
741
test_submodule_content sub1 origin/add_sub1
797
- )
798
- '
799
-
800
- # ... and ignored files are ignored
801
- test_expect_success "$command: replace submodule with a file works ignores ignored files in submodule" '
802
- test_when_finished "rm submodule_update/.git/modules/sub1/info/exclude" &&
803
- prolog &&
804
- reset_work_tree_to_interested add_sub1 &&
805
- (
806
- cd submodule_update &&
807
- git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
808
- : >sub1/ignored &&
809
- $command replace_sub1_with_file &&
810
- test_superproject_content origin/replace_sub1_with_file &&
811
- test -f sub1
742
+ test -f sub1/untracked_file
743
)
744
'
745
@@ -825,19 +756,6 @@ test_submodule_switch_recursing_with_args () {
756
test_submodule_content sub1 origin/modify_sub1
757
)
758
'
828
-
829
- test_expect_success "git -c submodule.recurse=true $cmd_args: modified submodule updates submodule work tree" '
830
- prolog &&
831
- reset_work_tree_to_interested add_sub1 &&
832
- (
833
- cd submodule_update &&
834
- git branch -t modify_sub1 origin/modify_sub1 &&
835
- git -c submodule.recurse=true $cmd_args modify_sub1 &&
836
- test_superproject_content origin/modify_sub1 &&
837
- test_submodule_content sub1 origin/modify_sub1
838
- )
839
- '
840
-
759
# Updating a submodule to an invalid sha1 doesn't update the
760
# superproject nor the submodule's work tree.
761
test_expect_success "$command: updating to a missing submodule commit fails" '
@@ -851,126 +769,166 @@ test_submodule_switch_recursing_with_args () {
769
test_submodule_content sub1 origin/add_sub1
770
)
771
'
854
-
855
- # recursing deeper than one level doesn't work yet.
856
- test_expect_success "$command: modified submodule updates submodule recursively" '
857
- prolog &&
858
- reset_work_tree_to_interested add_nested_sub &&
859
- (
860
- cd submodule_update &&
861
- git branch -t modify_sub1_recursively origin/modify_sub1_recursively &&
862
- $command modify_sub1_recursively &&
863
- test_superproject_content origin/modify_sub1_recursively &&
864
- test_submodule_content sub1 origin/modify_sub1_recursively &&
865
- test_submodule_content -C sub1 sub2 origin/modify_sub1_recursively
866
- )
867
- '
772
}
773
870
-# Test that submodule contents are updated when switching between commits
871
-# that change a submodule, but throwing away local changes in
872
-# the superproject as well as the submodule is allowed.
873
-test_submodule_forced_switch_recursing_with_args () {
774
+# Declares and invokes several tests that, in various situations, checks that
775
+# the provided Git command, when invoked with --recurse-submodules:
776
+# - succeeds in updating the worktree and index of a superproject to a target
777
+# commit, or fails atomically (depending on the test situation)
778
+# - if succeeds, the contents of submodule directories are updated
779
+#
780
+# Specify the Git command so that "git $GIT_COMMAND --recurse-submodules"
781
+# works.
782
+#
783
+# If the command under test is known to not work with submodules in certain
784
+# conditions, set the appropriate KNOWN_FAILURE_* variable used in the tests
785
+# below to 1.
786
+#
787
+# Use as follows:
788
+#
789
+# test_submodule_switch_recursing_with_args "$GIT_COMMAND"
790
+test_submodule_switch_recursing_with_args () {
791
cmd_args="$1"
792
command="git $cmd_args --recurse-submodules"
876
- RESULT=success
793
+ test_submodule_recursing_with_args_common "$command"
794
+
795
+ RESULTDS=success
796
if test "$KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS" = 1
797
then
879
- RESULT=failure
798
+ RESULTDS=failure
799
fi
881
- ######################### Appearing submodule #########################
882
- # Switching to a commit letting a submodule appear creates empty dir ...
883
- test_expect_success "$command: added submodule is checked out" '
800
+ RESULTOI=success
801
+ if test "$KNOWN_FAILURE_SUBMODULE_OVERWRITE_IGNORED_UNTRACKED" = 1
802
+ then
803
+ RESULTOI=failure
804
+ fi
805
+ # Switching to a commit letting a submodule appear cannot override an
806
+ # untracked file.
807
+ test_expect_success "$command: added submodule doesn't remove untracked file with same name" '
808
prolog &&
809
reset_work_tree_to_interested no_submodule &&
810
(
811
cd submodule_update &&
812
git branch -t add_sub1 origin/add_sub1 &&
889
- $command add_sub1 &&
890
- test_superproject_content origin/add_sub1 &&
891
- test_submodule_content sub1 origin/add_sub1
813
+ : >sub1 &&
814
+ test_must_fail $command add_sub1 &&
815
+ test_superproject_content origin/no_submodule &&
816
+ test_must_be_empty sub1
817
)
818
'
894
- # ... and doesn't care if it already exists ...
895
- test_expect_success "$command: added submodule ignores empty directory" '
819
+ # ... but an ignored file is fine.
820
+ test_expect_$RESULTOI "$command: added submodule removes an untracked ignored file" '
821
+ test_when_finished "rm submodule_update/.git/info/exclude" &&
822
prolog &&
823
reset_work_tree_to_interested no_submodule &&
824
(
825
cd submodule_update &&
826
git branch -t add_sub1 origin/add_sub1 &&
901
- mkdir sub1 &&
827
+ : >sub1 &&
828
+ echo sub1 >.git/info/exclude
829
$command add_sub1 &&
830
test_superproject_content origin/add_sub1 &&
831
test_submodule_content sub1 origin/add_sub1
832
)
833
'
907
- # ... not caring about an untracked file either
908
- test_expect_success "$command: added submodule does remove untracked unignored file with same name when forced" '
834
+
835
+ # Replacing a submodule with files in a directory must succeeds
836
+ # when the submodule is clean
837
+ test_expect_$RESULTDS "$command: replace submodule with a directory" '
838
prolog &&
910
- reset_work_tree_to_interested no_submodule &&
839
+ reset_work_tree_to_interested add_sub1 &&
840
(
841
cd submodule_update &&
913
- git branch -t add_sub1 origin/add_sub1 &&
914
- >sub1 &&
915
- $command add_sub1 &&
916
- test_superproject_content origin/add_sub1 &&
917
- test_submodule_content sub1 origin/add_sub1
842
+ git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
843
+ $command replace_sub1_with_directory &&
844
+ test_superproject_content origin/replace_sub1_with_directory &&
845
+ test_submodule_content sub1 origin/replace_sub1_with_directory
846
)
847
'
920
- # Replacing a tracked file with a submodule checks out the submodule
921
- test_expect_success "$command: replace tracked file with submodule populates the submodule" '
848
+ # ... absorbing a .git directory.
849
+ test_expect_$RESULTDS "$command: replace submodule containing a .git directory with a directory must absorb the git dir" '
850
prolog &&
923
- reset_work_tree_to_interested replace_sub1_with_file &&
851
+ reset_work_tree_to_interested add_sub1 &&
852
(
853
cd submodule_update &&
926
- git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
927
- $command replace_file_with_sub1 &&
928
- test_superproject_content origin/replace_file_with_sub1 &&
929
- test_submodule_content sub1 origin/replace_file_with_sub1
854
+ git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
855
+ replace_gitfile_with_git_dir sub1 &&
856
+ rm -rf .git/modules &&
857
+ $command replace_sub1_with_directory &&
858
+ test_superproject_content origin/replace_sub1_with_directory &&
859
+ test_git_directory_exists sub1
860
)
861
'
932
- # ... as does removing a directory with tracked files with a
933
- # submodule.
934
- test_expect_success "$command: replace directory with submodule" '
862
+
863
+ # ... and ignored files are ignored
864
+ test_expect_success "$command: replace submodule with a file works ignores ignored files in submodule" '
865
+ test_when_finished "rm submodule_update/.git/modules/sub1/info/exclude" &&
866
prolog &&
936
- reset_work_tree_to_interested replace_sub1_with_directory &&
867
+ reset_work_tree_to_interested add_sub1 &&
868
(
869
cd submodule_update &&
939
- git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
940
- $command replace_directory_with_sub1 &&
941
- test_superproject_content origin/replace_directory_with_sub1 &&
942
- test_submodule_content sub1 origin/replace_directory_with_sub1
870
+ git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
871
+ : >sub1/ignored &&
872
+ $command replace_sub1_with_file &&
873
+ test_superproject_content origin/replace_sub1_with_file &&
874
+ test -f sub1
875
)
876
'
877
946
- ######################## Disappearing submodule #######################
947
- # Removing a submodule doesn't remove its work tree ...
948
- test_expect_success "$command: removed submodule leaves submodule directory and its contents in place" '
878
+ test_expect_success "git -c submodule.recurse=true $cmd_args: modified submodule updates submodule work tree" '
879
prolog &&
880
reset_work_tree_to_interested add_sub1 &&
881
(
882
cd submodule_update &&
953
- git branch -t remove_sub1 origin/remove_sub1 &&
954
- $command remove_sub1 &&
955
- test_superproject_content origin/remove_sub1 &&
956
- ! test -e sub1
883
+ git branch -t modify_sub1 origin/modify_sub1 &&
884
+ git -c submodule.recurse=true $cmd_args modify_sub1 &&
885
+ test_superproject_content origin/modify_sub1 &&
886
+ test_submodule_content sub1 origin/modify_sub1
887
)
888
'
959
- # ... especially when it contains a .git directory.
960
- test_expect_success "$command: removed submodule leaves submodule containing a .git directory alone" '
889
+
890
+ # recursing deeper than one level doesn't work yet.
891
+ test_expect_success "$command: modified submodule updates submodule recursively" '
892
prolog &&
962
- reset_work_tree_to_interested add_sub1 &&
893
+ reset_work_tree_to_interested add_nested_sub &&
894
(
895
cd submodule_update &&
965
- git branch -t remove_sub1 origin/remove_sub1 &&
966
- replace_gitfile_with_git_dir sub1 &&
967
- rm -rf .git/modules/sub1 &&
968
- $command remove_sub1 &&
969
- test_superproject_content origin/remove_sub1 &&
970
- test_git_directory_exists sub1 &&
971
- ! test -e sub1
896
+ git branch -t modify_sub1_recursively origin/modify_sub1_recursively &&
897
+ $command modify_sub1_recursively &&
898
+ test_superproject_content origin/modify_sub1_recursively &&
899
+ test_submodule_content sub1 origin/modify_sub1_recursively &&
900
+ test_submodule_content -C sub1 sub2 origin/modify_sub1_recursively
901
+ )
902
+ '
903
+}
904
+
905
+# Same as test_submodule_switch_recursing_with_args(), except that throwing
906
+# away local changes in the superproject is allowed.
907
+test_submodule_forced_switch_recursing_with_args () {
908
+ cmd_args="$1"
909
+ command="git $cmd_args --recurse-submodules"
910
+ test_submodule_recursing_with_args_common "$command"
911
+
912
+ RESULT=success
913
+ if test "$KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS" = 1
914
+ then
915
+ RESULT=failure
916
+ fi
917
+ # Switching to a commit letting a submodule appear does not care about
918
+ # an untracked file.
919
+ test_expect_success "$command: added submodule does remove untracked unignored file with same name when forced" '
920
+ prolog &&
921
+ reset_work_tree_to_interested no_submodule &&
922
+ (
923
+ cd submodule_update &&
924
+ git branch -t add_sub1 origin/add_sub1 &&
925
+ >sub1 &&
926
+ $command add_sub1 &&
927
+ test_superproject_content origin/add_sub1 &&
928
+ test_submodule_content sub1 origin/add_sub1
929
)
930
'
931
+
932
# Replacing a submodule with files in a directory ...
933
test_expect_success "$command: replace submodule with a directory" '
934
prolog &&
@@ -997,17 +955,6 @@ test_submodule_forced_switch_recursing_with_args () {
955
test_git_directory_exists sub1
956
)
957
'
1000
- # Replacing it with a file
1001
- test_expect_success "$command: replace submodule with a file" '
1002
- prolog &&
1003
- reset_work_tree_to_interested add_sub1 &&
1004
- (
1005
- cd submodule_update &&
1006
- git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
1007
- $command replace_sub1_with_file &&
1008
- test_superproject_content origin/replace_sub1_with_file
1009
- )
1010
- '
958
959
# ... even if the submodule contains ignored files
960
test_expect_success "$command: replace submodule with a file ignoring ignored files" '
@@ -1022,46 +969,6 @@ test_submodule_forced_switch_recursing_with_args () {
969
)
970
'
971
1025
- # ... but stops for untracked files that would be lost
1026
- test_expect_$RESULT "$command: replace submodule with a file stops for untracked files" '
1027
- prolog &&
1028
- reset_work_tree_to_interested add_sub1 &&
1029
- (
1030
- cd submodule_update &&
1031
- git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
1032
- : >sub1/untracked_file &&
1033
- test_must_fail $command replace_sub1_with_file &&
1034
- test_superproject_content origin/add_sub1 &&
1035
- test -f sub1/untracked_file
1036
- )
1037
- '
1038
-
1039
- ########################## Modified submodule #########################
1040
- # Updating a submodule sha1 updates the submodule's work tree
1041
- test_expect_success "$command: modified submodule updates submodule work tree" '
1042
- prolog &&
1043
- reset_work_tree_to_interested add_sub1 &&
1044
- (
1045
- cd submodule_update &&
1046
- git branch -t modify_sub1 origin/modify_sub1 &&
1047
- $command modify_sub1 &&
1048
- test_superproject_content origin/modify_sub1 &&
1049
- test_submodule_content sub1 origin/modify_sub1
1050
- )
1051
- '
1052
- # Updating a submodule to an invalid sha1 doesn't update the
1053
- # submodule's work tree, subsequent update will fail
1054
- test_expect_success "$command: modified submodule does not update submodule work tree to invalid commit" '
1055
- prolog &&
1056
- reset_work_tree_to_interested add_sub1 &&
1057
- (
1058
- cd submodule_update &&
1059
- git branch -t invalid_sub1 origin/invalid_sub1 &&
1060
- test_must_fail $command invalid_sub1 &&
1061
- test_superproject_content origin/add_sub1 &&
1062
- test_submodule_content sub1 origin/add_sub1
1063
- )
1064
- '
972
# Updating a submodule from an invalid sha1 updates
973
test_expect_success "$command: modified submodule does update submodule work tree from invalid commit" '
974
prolog &&