contrib/subtree: Clean and refactor test code

Mostly prepare for the later tests refactoring. This moves some common code to helper functions and generally cleans things up to be more presentable. Signed-off-by: Techlive Zheng <techlivezheng@gmail.com> Signed-off-by: David A. Greene <greened@obbligato.org> Signed-off-by: Jeff King <peff@peff.net>

Techlive Zheng committed Nov 12, 2015 at 20:32 UTC b0638aa2f8f7fa4e9c5d8569ee30b64fc21fba32
2 files changed +79 -55
contrib/subtree/t/Makefile
+24 -7
@@ -13,11 +13,23 @@ TAR ?= $(TAR)
13 RM ?= rm -f
14 PROVE ?= prove
15 DEFAULT_TEST_TARGET ?= test
16 +TEST_LINT ?= test-lint
17 +
18 +ifdef TEST_OUTPUT_DIRECTORY
19 +TEST_RESULTS_DIRECTORY = $(TEST_OUTPUT_DIRECTORY)/test-results
20 +else
21 +TEST_RESULTS_DIRECTORY = ../../../t/test-results
22 +endif
23
24 # Shell quote;
25 SHELL_PATH_SQ = $(subst ','\'',$(SHELL_PATH))
26 +PERL_PATH_SQ = $(subst ','\'',$(PERL_PATH))
27 +TEST_RESULTS_DIRECTORY_SQ = $(subst ','\'',$(TEST_RESULTS_DIRECTORY))
28
20 -T = $(wildcard t[0-9][0-9][0-9][0-9]-*.sh)
29 +T = $(sort $(wildcard t[0-9][0-9][0-9][0-9]-*.sh))
30 +TSVN = $(sort $(wildcard t91[0-9][0-9]-*.sh))
31 +TGITWEB = $(sort $(wildcard t95[0-9][0-9]-*.sh))
32 +THELPERS = $(sort $(filter-out $(T),$(wildcard *.sh)))
33
34 all: $(DEFAULT_TEST_TARGET)
35
@@ -26,20 +38,22 @@ test: pre-clean $(TEST_LINT)
38
39 prove: pre-clean $(TEST_LINT)
40 @echo "*** prove ***"; GIT_CONFIG=.git/config $(PROVE) --exec '$(SHELL_PATH_SQ)' $(GIT_PROVE_OPTS) $(T) :: $(GIT_TEST_OPTS)
29 - $(MAKE) clean
41 + $(MAKE) clean-except-prove-cache
42
43 $(T):
44 @echo "*** $@ ***"; GIT_CONFIG=.git/config '$(SHELL_PATH_SQ)' $@ $(GIT_TEST_OPTS)
45
46 pre-clean:
35 - $(RM) -r test-results
47 + $(RM) -r '$(TEST_RESULTS_DIRECTORY_SQ)'
48
37 -clean:
38 - $(RM) -r 'trash directory'.* test-results
49 +clean-except-prove-cache:
50 + $(RM) -r 'trash directory'.* '$(TEST_RESULTS_DIRECTORY_SQ)'
51 $(RM) -r valgrind/bin
52 +
53 +clean: clean-except-prove-cache
54 $(RM) .prove
55
42 -test-lint: test-lint-duplicates test-lint-executable
56 +test-lint: test-lint-duplicates test-lint-executable test-lint-shell-syntax
57
58 test-lint-duplicates:
59 @dups=`echo $(T) | tr ' ' '\n' | sed 's/-.*//' | sort | uniq -d` && \
@@ -51,12 +65,15 @@ test-lint-executable:
65 test -z "$$bad" || { \
66 echo >&2 "non-executable tests:" $$bad; exit 1; }
67
68 +test-lint-shell-syntax:
69 + @'$(PERL_PATH_SQ)' ../../../t/check-non-portable-shell.pl $(T) $(THELPERS)
70 +
71 aggregate-results-and-cleanup: $(T)
72 $(MAKE) aggregate-results
73 $(MAKE) clean
74
75 aggregate-results:
59 - for f in ../../../t/test-results/t*-*.counts; do \
76 + for f in '$(TEST_RESULTS_DIRECTORY_SQ)'/t*-*.counts; do \
77 echo "$$f"; \
78 done | '$(SHELL_PATH_SQ)' ../../../t/aggregate-results.sh
79
contrib/subtree/t/t7900-subtree.sh
+55 -48
@@ -5,7 +5,7 @@
5 #
6 test_description='Basic porcelain support for subtrees
7
8 -This test verifies the basic operation of the merge, pull, add
8 +This test verifies the basic operation of the add, pull, merge
9 and split subcommands of git subtree.
10 '
11
@@ -20,7 +20,6 @@ create()
20 git add "$1"
21 }
22
23 -
23 check_equal()
24 {
25 test_debug 'echo'
@@ -38,6 +37,30 @@ undo()
37 git reset --hard HEAD~
38 }
39
40 +# Make sure no patch changes more than one file.
41 +# The original set of commits changed only one file each.
42 +# A multi-file change would imply that we pruned commits
43 +# too aggressively.
44 +join_commits()
45 +{
46 + commit=
47 + all=
48 + while read x y; do
49 + if [ -z "$x" ]; then
50 + continue
51 + elif [ "$x" = "commit:" ]; then
52 + if [ -n "$commit" ]; then
53 + echo "$commit $all"
54 + all=
55 + fi
56 + commit="$y"
57 + else
58 + all="$all $y"
59 + fi
60 + done
61 + echo "$commit $all"
62 +}
63 +
64 last_commit_message()
65 {
66 git log --pretty=format:%s -1
@@ -123,9 +146,11 @@ test_expect_success 'add subproj to mainline' '
146 check_equal ''"$(last_commit_message)"'' "Add '"'sub dir/'"' from commit '"'"'''"$(git rev-parse sub1)"'''"'"'"
147 '
148
126 -# this shouldn't actually do anything, since FETCH_HEAD is already a parent
127 -test_expect_success 'merge fetched subproj' '
128 - git merge -m "merge -s -ours" -s ours FETCH_HEAD
149 +test_expect_success 'merge the added subproj again, should do nothing' '
150 + # this shouldn not actually do anything, since FETCH_HEAD
151 + # is already a parent
152 + result=$(git merge -s ours -m "merge -s -ours" FETCH_HEAD) &&
153 + check_equal "${result}" "Already up-to-date."
154 '
155
156 test_expect_success 'add main-sub5' '
@@ -167,7 +192,7 @@ test_expect_success 'merge new subproj history into subdir' '
192 undo
193 '
194
170 -test_expect_success 'Check that prefix argument is required for split' '
195 +test_expect_success 'split requires option --prefix' '
196 echo "You must provide the --prefix option." > expected &&
197 test_must_fail git subtree split > actual 2>&1 &&
198 test_debug "printf '"'"'expected: '"'"'" &&
@@ -178,15 +203,15 @@ test_expect_success 'Check that prefix argument is required for split' '
203 rm -f expected actual
204 '
205
181 -test_expect_success 'Check that the <prefix> exists for a split' '
182 - echo "'"'"'non-existent-directory'"'"'" does not exist\; use "'"'"'git subtree add'"'"'" > expected &&
206 +test_expect_success 'split requires path given by option --prefix must exist' '
207 + echo "'\''non-existent-directory'\'' does not exist; use '\''git subtree add'\''" > expected &&
208 test_must_fail git subtree split --prefix=non-existent-directory > actual 2>&1 &&
209 test_debug "printf '"'"'expected: '"'"'" &&
210 test_debug "cat expected" &&
211 test_debug "printf '"'"'actual: '"'"'" &&
212 test_debug "cat actual" &&
188 - test_cmp expected actual
189 -# rm -f expected actual
213 + test_cmp expected actual &&
214 + rm -f expected actual
215 '
216
217 test_expect_success 'check if --message works for split+rejoin' '
@@ -279,18 +304,22 @@ test_expect_success 'merge split into subproj' '
304
305 chkm="main4
306 main6"
307 +
308 chkms="main-sub10
309 main-sub5
310 main-sub7
311 main-sub8"
312 +
313 chkms_sub=$(cat <<TXT | sed 's,^,sub dir/,'
314 $chkms
315 TXT
316 )
317 +
318 chks="sub1
319 sub2
320 sub3
321 sub9"
322 +
323 chks_sub=$(cat <<TXT | sed 's,^,sub dir/,'
324 $chks
325 TXT
@@ -301,6 +330,7 @@ test_expect_success 'make sure exactly the right set of files ends up in the sub
330 check_equal "$subfiles" "$chkms
331 $chks"
332 '
333 +
334 test_expect_success 'make sure the subproj history *only* contains commits that affect the subdir' '
335 allchanges=''"$(git log --name-only --pretty=format:'"''"' | sort | sed "/^$/d")"'' &&
336 check_equal "$allchanges" "$chkms
@@ -324,26 +354,27 @@ $chks_sub"
354 '
355
356 test_expect_success 'make sure each filename changed exactly once in the entire history' '
327 - # main-sub?? and /subdir/main-sub?? both change, because those are the
328 - # changes that were split into their own history. And subdir/sub?? never
357 + # main-sub?? and sub dir/main-sub?? both change, because those are the
358 + # changes that were split into their own history. And sub dir/sub?? never
359 # change, since they were *only* changed in the subtree branch.
360 allchanges=''"$(git log --name-only --pretty=format:'"''"' | sort | sed "/^$/d")"'' &&
331 - check_equal "$allchanges" ''"$(cat <<TXT | sort
361 + expected=''"$(cat <<TXT | sort
362 $chkms
363 $chkm
364 $chks
365 $chkms_sub
366 TXT
337 -)"''
367 +)"'' &&
368 + check_equal "$allchanges" "$expected"
369 '
370
371 test_expect_success 'make sure the --rejoin commits never make it into subproj' '
341 - check_equal ''"$(git log --pretty=format:'"'%s'"' HEAD^2 | grep -i split)"'' ""
372 + check_equal "$(git log --pretty=format:"%s" HEAD^2 | grep -i split)" ""
373 '
374
375 test_expect_success 'make sure no "git subtree" tagged commits make it into subproj' '
376 # They are meaningless to subproj since one side of the merge refers to the mainline
346 - check_equal ''"$(git log --pretty=format:'"'%s%n%b'"' HEAD^2 | grep "git-subtree.*:")"'' ""
377 + check_equal "$(git log --pretty=format:"%s%n%b" HEAD^2 | grep "git-subtree.*:")" ""
378 '
379
380 # prepare second pair of repositories
@@ -408,13 +439,13 @@ test_expect_success 'split for main-sub4 without --onto' '
439 git subtree split --prefix "sub dir" --branch mainsub4
440 '
441
411 -# at this point, the new commit parent should be sub3 if it is not,
442 +# At this point, the new commit parent should be sub3. If it is not,
443 # something went wrong (the "newparent" of "master~" commit should
444 # have been sub3, but it was not, because its cache was not set to
414 -# itself)
445 +# itself).
446
447 test_expect_success 'check that the commit parent is sub3' '
417 - check_equal ''"$(git log --pretty=format:%P -1 mainsub4)"'' ''"$(git rev-parse sub3)"''
448 + check_equal "$(git log --pretty=format:%P -1 mainsub4)" "$(git rev-parse sub3)"
449 '
450
451 test_expect_success 'add main-sub5' '
@@ -431,36 +462,12 @@ test_expect_success 'split for main-sub5 without --onto' '
462 check_equal ''"$(git log --pretty=format:%P -1 mainsub5)"'' ""
463 '
464
434 -# make sure no patch changes more than one file. The original set of commits
435 -# changed only one file each. A multi-file change would imply that we pruned
436 -# commits too aggressively.
437 -joincommits()
438 -{
439 - commit=
440 - all=
441 - while read x y; do
442 - #echo "{$x}" >&2
443 - if [ -z "$x" ]; then
444 - continue
445 - elif [ "$x" = "commit:" ]; then
446 - if [ -n "$commit" ]; then
447 - echo "$commit $all"
448 - all=
449 - fi
450 - commit="$y"
451 - else
452 - all="$all $y"
453 - fi
454 - done
455 - echo "$commit $all"
456 -}
457 -
465 test_expect_success 'verify one file change per commit' '
466 x= &&
460 - list=''"$(git log --pretty=format:'"'commit: %H'"' | joincommits)"'' &&
467 + list=''"$(git log --pretty=format:'"'commit: %H'"' | join_commits)"'' &&
468 # test_debug "echo HERE" &&
469 # test_debug "echo ''"$list"''" &&
463 - (git log --pretty=format:'"'commit: %H'"' | joincommits |
470 + git log --pretty=format:'"'commit: %H'"' | join_commits |
471 ( while read commit a b; do
472 test_debug "echo Verifying commit "''"$commit"''
473 test_debug "echo a: "''"$a"''
@@ -468,15 +475,15 @@ test_expect_success 'verify one file change per commit' '
475 check_equal "$b" ""
476 x=1
477 done
471 - check_equal "$x" 1
472 - ))
478 + check_equal "$x" "1"
479 + )
480 '
481
482 # test push
483
484 cd ../..
485
479 -mkdir test-push
486 +mkdir -p test-push
487
488 cd test-push
489