Use Test Harness

Clean up the git-subtree tests to conform the git project conventions
and use the existing test harness.

Signed-off-by: David A. Greene <greened@obbligato.org>
This commit is contained in:
David A. Greene 2012-01-29 16:09:58 -06:00
parent 9e2a55a276
commit d3a04e06c7

View file

@ -1,6 +1,14 @@
#!/bin/bash #!/bin/sh
. shellopts.sh #
set -e # Copyright (c) 2012 Avery Pennaraum
#
test_description='Basic porcelain support for subtrees
This test verifies the basic operation of the merge, pull, add
and split subcommands of git subtree.
'
. ./test-lib.sh
create() create()
{ {
@ -8,42 +16,16 @@ create()
git add "$1" git add "$1"
} }
check()
{
echo
echo "check:" "$@"
if "$@"; then
echo ok
return 0
else
echo FAILED
exit 1
fi
}
check_not()
{
echo
echo "check: NOT " "$@"
if "$@"; then
echo FAILED
exit 1
else
echo ok
return 0
fi
}
check_equal() check_equal()
{ {
echo test_debug 'echo'
echo "check a:" "{$1}" test_debug "echo \"check a:\" \"{$1}\""
echo " b:" "{$2}" test_debug "echo \" b:\" \"{$2}\""
if [ "$1" = "$2" ]; then if [ "$1" = "$2" ]; then
return 0 return 0
else else
echo FAILED return 1
exit 1
fi fi
} }
@ -76,144 +58,257 @@ last_commit_message()
git log --pretty=format:%s -1 git log --pretty=format:%s -1
} }
rm -rf mainline subproj # 1
mkdir mainline subproj test_expect_success 'init subproj' '
test_create_repo subproj
'
# To the subproject!
cd subproj cd subproj
git init
create sub1 # 2
git commit -m 'sub1' test_expect_success 'add sub1' '
git branch sub1 create sub1 &&
git branch -m master subproj git commit -m "sub1" &&
check true git branch sub1 &&
git branch -m master subproj
'
create sub2 # 3
git commit -m 'sub2' test_expect_success 'add sub2' '
git branch sub2 create sub2 &&
git commit -m "sub2" &&
git branch sub2
'
create sub3 # 4
git commit -m 'sub3' test_expect_success 'add sub3' '
git branch sub3 create sub3 &&
git commit -m "sub3" &&
git branch sub3
'
cd ../mainline # Back to mainline
git init cd ..
create main4
git commit -m 'main4'
git branch -m master mainline
git branch subdir
git fetch ../subproj sub1 # 5
git branch sub1 FETCH_HEAD test_expect_success 'add main4' '
create main4 &&
git commit -m "main4" &&
git branch -m master mainline &&
git branch subdir
'
# check if --message works for add # 6
check_not git subtree merge --prefix=subdir sub1 test_expect_success 'fetch subproj history' '
check_not git subtree pull --prefix=subdir ../subproj sub1 git fetch ./subproj sub1 &&
git subtree add --prefix=subdir --message="Added subproject" sub1 git branch sub1 FETCH_HEAD
check_equal "$(last_commit_message)" "Added subproject" '
undo
# check if --message works as -m and --prefix as -P # 7
git subtree add -P subdir -m "Added subproject using git subtree" sub1 test_expect_success 'no subtree exists in main tree' '
check_equal "$(last_commit_message)" "Added subproject using git subtree" test_must_fail git subtree merge --prefix=subdir sub1
undo '
# check if --message works with squash too # 8
git subtree add -P subdir -m "Added subproject with squash" --squash sub1 test_expect_success 'no pull from non-existant subtree' '
check_equal "$(last_commit_message)" "Added subproject with squash" test_must_fail git subtree pull --prefix=subdir ./subproj sub1
undo '
git subtree add --prefix=subdir/ FETCH_HEAD # 9
check_equal "$(last_commit_message)" "Add 'subdir/' from commit '$(git rev-parse sub1)'" test_expect_success 'check if --message works for add' '
git subtree add --prefix=subdir --message="Added subproject" sub1 &&
check_equal ''"$(last_commit_message)"'' "Added subproject" &&
undo
'
# 10
test_expect_success 'check if --message works as -m and --prefix as -P' '
git subtree add -P subdir -m "Added subproject using git subtree" sub1 &&
check_equal ''"$(last_commit_message)"'' "Added subproject using git subtree" &&
undo
'
# 11
test_expect_success 'check if --message works with squash too' '
git subtree add -P subdir -m "Added subproject with squash" --squash sub1 &&
check_equal ''"$(last_commit_message)"'' "Added subproject with squash" &&
undo
'
# 12
test_expect_success 'add subproj to mainline' '
git subtree add --prefix=subdir/ FETCH_HEAD &&
check_equal ''"$(last_commit_message)"'' "Add '"'subdir/'"' from commit '"'"'''"$(git rev-parse sub1)"'''"'"'"
'
# 13
# this shouldn't actually do anything, since FETCH_HEAD is already a parent # this shouldn't actually do anything, since FETCH_HEAD is already a parent
git merge -m 'merge -s -ours' -s ours FETCH_HEAD test_expect_success 'merge fetched subproj' '
git merge -m "merge -s -ours" -s ours FETCH_HEAD
'
create subdir/main-sub5 # 14
git commit -m 'main-sub5' test_expect_success 'add main-sub5' '
create subdir/main-sub5 &&
git commit -m "main-sub5"
'
create main6 # 15
git commit -m 'main6 boring' test_expect_success 'add main6' '
create main6 &&
git commit -m "main6 boring"
'
create subdir/main-sub7 # 16
git commit -m 'main-sub7' test_expect_success 'add main-sub7' '
create subdir/main-sub7 &&
git commit -m "main-sub7"
'
git fetch ../subproj sub2 # 17
git branch sub2 FETCH_HEAD test_expect_success 'fetch new subproj history' '
git fetch ./subproj sub2 &&
git branch sub2 FETCH_HEAD
'
# check if --message works for merge # 18
git subtree merge --prefix=subdir -m "Merged changes from subproject" sub2 test_expect_success 'check if --message works for merge' '
check_equal "$(last_commit_message)" "Merged changes from subproject" git subtree merge --prefix=subdir -m "Merged changes from subproject" sub2 &&
undo check_equal ''"$(last_commit_message)"'' "Merged changes from subproject" &&
undo
'
# check if --message for merge works with squash too # 19
git subtree merge --prefix subdir -m "Merged changes from subproject using squash" --squash sub2 test_expect_success 'check if --message for merge works with squash too' '
check_equal "$(last_commit_message)" "Merged changes from subproject using squash" git subtree merge --prefix subdir -m "Merged changes from subproject using squash" --squash sub2 &&
undo check_equal ''"$(last_commit_message)"'' "Merged changes from subproject using squash" &&
undo
'
git subtree merge --prefix=subdir FETCH_HEAD # 20
git branch pre-split test_expect_success 'merge new subproj history into subdir' '
check_equal "$(last_commit_message)" "Merge commit '$(git rev-parse sub2)' into mainline" git subtree merge --prefix=subdir FETCH_HEAD &&
git branch pre-split &&
check_equal ''"$(last_commit_message)"'' "Merge commit '"'"'"$(git rev-parse sub2)"'"'"' into mainline"
'
# Check that prefix argument is required for split (exits with warning and exit status = 1) # 21
! result=$(git subtree split 2>&1) test_expect_success 'Check that prefix argument is required for split' '
check_equal "You must provide the --prefix option." "$result" echo "You must provide the --prefix option." > expected &&
test_must_fail git subtree split > actual 2>&1 &&
test_debug "echo -n expected: " &&
test_debug "cat expected" &&
test_debug "echo -n actual: " &&
test_debug "cat actual" &&
test_cmp expected actual &&
rm -f expected actual
'
# Check that the <prefix> exists for a split. # 22
! result=$(git subtree split --prefix=non-existent-directory 2>&1) test_expect_success 'Check that the <prefix> exists for a split' '
check_equal "'non-existent-directory' does not exist; use 'git subtree add'" \ echo "'"'"'non-existent-directory'"'"'" does not exist\; use "'"'"'git subtree add'"'"'" > expected &&
"$result" test_must_fail git subtree split --prefix=non-existent-directory > actual 2>&1 &&
test_debug "echo -n expected: " &&
test_debug "cat expected" &&
test_debug "echo -n actual: " &&
test_debug "cat actual" &&
test_cmp expected actual
# rm -f expected actual
'
# check if --message works for split+rejoin # 23
spl1=$(git subtree split --annotate='*' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin) test_expect_success 'check if --message works for split+rejoin' '
echo "spl1={$spl1}" spl1=''"$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
git branch spl1 "$spl1" git branch spl1 "$spl1" &&
check_equal "$(last_commit_message)" "Split & rejoin" check_equal ''"$(last_commit_message)"'' "Split & rejoin" &&
undo undo
'
# check split with --branch # 24
git subtree split --annotate='*' --prefix subdir --onto FETCH_HEAD --branch splitbr1 test_expect_success 'check split with --branch' '
check_equal "$(git rev-parse splitbr1)" "$spl1" spl1=$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin) &&
undo &&
git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --branch splitbr1 &&
check_equal ''"$(git rev-parse splitbr1)"'' "$spl1"
'
# check split with --branch for an existing branch # 25
git branch splitbr2 sub1 test_expect_success 'check split with --branch for an existing branch' '
git subtree split --annotate='*' --prefix subdir --onto FETCH_HEAD --branch splitbr2 spl1=''"$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
check_equal "$(git rev-parse splitbr2)" "$spl1" undo &&
git branch splitbr2 sub1 &&
git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --branch splitbr2 &&
check_equal ''"$(git rev-parse splitbr2)"'' "$spl1"
'
# check split with --branch for an incompatible branch # 26
result=$(git subtree split --prefix subdir --onto FETCH_HEAD --branch subdir || echo "caught error") test_expect_success 'check split with --branch for an incompatible branch' '
check_equal "$result" "caught error" test_must_fail git subtree split --prefix subdir --onto FETCH_HEAD --branch subdir
'
git subtree split --annotate='*' --prefix subdir --onto FETCH_HEAD --rejoin # 27
check_equal "$(last_commit_message)" "Split 'subdir/' into commit '$spl1'" test_expect_success 'check split+rejoin' '
spl1=''"$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
undo &&
git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --rejoin &&
check_equal ''"$(last_commit_message)"'' "Split '"'"'subdir/'"'"' into commit '"'"'"$spl1"'"'"'"
'
create subdir/main-sub8 # 28
git commit -m 'main-sub8' test_expect_success 'add main-sub8' '
create subdir/main-sub8 &&
git commit -m "main-sub8"
'
cd ../subproj # To the subproject!
git fetch ../mainline spl1 cd ./subproj
git branch spl1 FETCH_HEAD
git merge FETCH_HEAD
create sub9 # 29
git commit -m 'sub9' test_expect_success 'merge split into subproj' '
git fetch .. spl1 &&
git branch spl1 FETCH_HEAD &&
git merge FETCH_HEAD
'
cd ../mainline # 30
split2=$(git subtree split --annotate='*' --prefix subdir/ --rejoin) test_expect_success 'add sub9' '
git branch split2 "$split2" create sub9 &&
git commit -m "sub9"
'
create subdir/main-sub10 # Back to mainline
git commit -m 'main-sub10' cd ..
spl3=$(git subtree split --annotate='*' --prefix subdir --rejoin) # 31
git branch spl3 "$spl3" test_expect_success 'split for sub8' '
split2=''"$(git subtree split --annotate='"'*'"' --prefix subdir/ --rejoin)"''
git branch split2 "$split2"
'
cd ../subproj # 32
git fetch ../mainline spl3 test_expect_success 'add main-sub10' '
git branch spl3 FETCH_HEAD create subdir/main-sub10 &&
git merge FETCH_HEAD git commit -m "main-sub10"
git branch subproj-merge-spl3 '
# 33
test_expect_success 'split for sub10' '
spl3=''"$(git subtree split --annotate='"'*'"' --prefix subdir --rejoin)"'' &&
git branch spl3 "$spl3"
'
# To the subproject!
cd ./subproj
# 34
test_expect_success 'merge split into subproj' '
git fetch .. spl3 &&
git branch spl3 FETCH_HEAD &&
git merge FETCH_HEAD &&
git branch subproj-merge-spl3
'
chkm="main4 main6" chkm="main4 main6"
chkms="main-sub10 main-sub5 main-sub7 main-sub8" chkms="main-sub10 main-sub5 main-sub7 main-sub8"
@ -221,89 +316,150 @@ chkms_sub=$(echo $chkms | multiline | sed 's,^,subdir/,' | fixnl)
chks="sub1 sub2 sub3 sub9" chks="sub1 sub2 sub3 sub9"
chks_sub=$(echo $chks | multiline | sed 's,^,subdir/,' | fixnl) chks_sub=$(echo $chks | multiline | sed 's,^,subdir/,' | fixnl)
# make sure exactly the right set of files ends up in the subproj # 35
subfiles=$(git ls-files | fixnl) test_expect_success 'make sure exactly the right set of files ends up in the subproj' '
check_equal "$subfiles" "$chkms $chks" subfiles=''"$(git ls-files | fixnl)"'' &&
check_equal "$subfiles" "$chkms $chks"
'
# make sure the subproj history *only* contains commits that affect the subdir. # 36
allchanges=$(git log --name-only --pretty=format:'' | sort | fixnl) test_expect_success 'make sure the subproj history *only* contains commits that affect the subdir' '
check_equal "$allchanges" "$chkms $chks" allchanges=''"$(git log --name-only --pretty=format:'"''"' | sort | fixnl)"'' &&
check_equal "$allchanges" "$chkms $chks"
'
cd ../mainline # Back to mainline
git fetch ../subproj subproj-merge-spl3 cd ..
git branch subproj-merge-spl3 FETCH_HEAD
git subtree pull --prefix=subdir ../subproj subproj-merge-spl3
# make sure exactly the right set of files ends up in the mainline # 37
mainfiles=$(git ls-files | fixnl) test_expect_success 'pull from subproj' '
check_equal "$mainfiles" "$chkm $chkms_sub $chks_sub" git fetch ./subproj subproj-merge-spl3 &&
git branch subproj-merge-spl3 FETCH_HEAD &&
git subtree pull --prefix=subdir ./subproj subproj-merge-spl3
'
# make sure each filename changed exactly once in the entire history. # 38
# 'main-sub??' and '/subdir/main-sub??' both change, because those are the test_expect_success 'make sure exactly the right set of files ends up in the mainline' '
# changes that were split into their own history. And 'subdir/sub??' never mainfiles=''"$(git ls-files | fixnl)"'' &&
# change, since they were *only* changed in the subtree branch. check_equal "$mainfiles" "$chkm $chkms_sub $chks_sub"
allchanges=$(git log --name-only --pretty=format:'' | sort | fixnl) '
check_equal "$allchanges" "$(echo $chkms $chkm $chks $chkms_sub | multiline | sort | fixnl)"
# make sure the --rejoin commits never make it into subproj # 39
check_equal "$(git log --pretty=format:'%s' HEAD^2 | grep -i split)" "" test_expect_success 'make sure each filename changed exactly once in the entire history' '
# main-sub?? and /subdir/main-sub?? both change, because those are the
# changes that were split into their own history. And subdir/sub?? never
# change, since they were *only* changed in the subtree branch.
allchanges=''"$(git log --name-only --pretty=format:'"''"' | sort | fixnl)"'' &&
check_equal "$allchanges" ''"$(echo $chkms $chkm $chks $chkms_sub | multiline | sort | fixnl)"''
'
# make sure no 'git subtree' tagged commits make it into subproj. (They're # 40
# meaningless to subproj since one side of the merge refers to the mainline) test_expect_success 'make sure the --rejoin commits never make it into subproj' '
check_equal "$(git log --pretty=format:'%s%n%b' HEAD^2 | grep 'git-subtree.*:')" "" check_equal ''"$(git log --pretty=format:'"'%s'"' HEAD^2 | grep -i split)"'' ""
'
# 41
test_expect_success 'make sure no "git subtree" tagged commits make it into subproj' '
# They are meaningless to subproj since one side of the merge refers to the mainline
check_equal ''"$(git log --pretty=format:'"'%s%n%b'"' HEAD^2 | grep "git-subtree.*:")"'' ""
'
# check if split can find proper base without --onto
# prepare second pair of repositories # prepare second pair of repositories
mkdir test2 mkdir test2
cd test2 cd test2
mkdir main # 42
test_expect_success 'init main' '
test_create_repo main
'
cd main cd main
git init
create main1 # 43
git commit -m "main1" test_expect_success 'add main1' '
create main1 &&
git commit -m "main1"
'
cd .. cd ..
mkdir sub
# 44
test_expect_success 'init sub' '
test_create_repo sub
'
cd sub cd sub
git init
create sub2 # 45
git commit -m "sub2" test_expect_success 'add sub2' '
create sub2 &&
git commit -m "sub2"
'
cd ../main cd ../main
git fetch ../sub master
git branch sub2 FETCH_HEAD # check if split can find proper base without --onto
git subtree add --prefix subdir sub2
# 46
test_expect_success 'add sub as subdir in main' '
git fetch ../sub master &&
git branch sub2 FETCH_HEAD &&
git subtree add --prefix subdir sub2
'
cd ../sub cd ../sub
create sub3
git commit -m "sub3" # 47
test_expect_success 'add sub3' '
create sub3 &&
git commit -m "sub3"
'
cd ../main cd ../main
git fetch ../sub master
git branch sub3 FETCH_HEAD
git subtree merge --prefix subdir sub3
create subdir/main-sub4 # 48
git commit -m "main-sub4" test_expect_success 'merge from sub' '
git subtree split --prefix subdir --branch mainsub4 git fetch ../sub master &&
git branch sub3 FETCH_HEAD &&
git subtree merge --prefix subdir sub3
'
# at this point, the new commit's parent should be sub3 # 49
# if it's not, something went wrong (the "newparent" of "master~" commit should have been sub3, test_expect_success 'add main-sub4' '
# but it wasn't, because it's cache was not set to itself) create subdir/main-sub4 &&
check_equal "$(git log --pretty=format:%P -1 mainsub4)" "$(git rev-parse sub3)" git commit -m "main-sub4"
'
mkdir subdir2 # 50
create subdir2/main-sub5 test_expect_success 'split for main-sub4 without --onto' '
git commit -m "main-sub5" git subtree split --prefix subdir --branch mainsub4
git subtree split --prefix subdir2 --branch mainsub5 '
# also test that we still can split out an entirely new subtree # at this point, the new commit parent should be sub3 if it is not,
# if the parent of the first commit in the tree isn't empty, # something went wrong (the "newparent" of "master~" commit should
# then the new subtree has accidently been attached to something # have been sub3, but it was not, because its cache was not set to
check_equal "$(git log --pretty=format:%P -1 mainsub5)" "" # itself)
# 51
test_expect_success 'check that the commit parent is sub3' '
check_equal ''"$(git log --pretty=format:%P -1 mainsub4)"'' ''"$(git rev-parse sub3)"''
'
# 52
test_expect_success 'add main-sub5' '
mkdir subdir2 &&
create subdir2/main-sub5 &&
git commit -m "main-sub5"
'
# 53
test_expect_success 'split for main-sub5 without --onto' '
# also test that we still can split out an entirely new subtree
# if the parent of the first commit in the tree is not empty,
# then the new subtree has accidently been attached to something
git subtree split --prefix subdir2 --branch mainsub5 &&
check_equal ''"$(git log --pretty=format:%P -1 mainsub5)"'' ""
'
# make sure no patch changes more than one file. The original set of commits # make sure no patch changes more than one file. The original set of commits
# changed only one file each. A multi-file change would imply that we pruned # changed only one file each. A multi-file change would imply that we pruned
@ -313,7 +469,7 @@ joincommits()
commit= commit=
all= all=
while read x y; do while read x y; do
echo "{$x}" >&2 #echo "{$x}" >&2
if [ -z "$x" ]; then if [ -z "$x" ]; then
continue continue
elif [ "$x" = "commit:" ]; then elif [ "$x" = "commit:" ]; then
@ -328,15 +484,23 @@ joincommits()
done done
echo "$commit $all" echo "$commit $all"
} }
x=
git log --pretty=format:'commit: %H' | joincommits |
( while read commit a b; do
echo "Verifying commit $commit"
check_equal "$b" ""
x=1
done
check_equal "$x" 1
) || exit 1
echo # 54
echo 'ok' test_expect_success 'verify one file change per commit' '
x= &&
list=''"$(git log --pretty=format:'"'commit: %H'"' | joincommits)"'' &&
# test_debug "echo HERE" &&
# test_debug "echo ''"$list"''" &&
(git log --pretty=format:'"'commit: %H'"' | joincommits |
( while read commit a b; do
test_debug "echo Verifying commit "''"$commit"''
test_debug "echo a: "''"$a"''
test_debug "echo b: "''"$b"''
check_equal "$b" ""
x=1
done
check_equal "$x" 1
))
'
test_done