Skip to content

Commit b0638aa

Browse files
weynhamzpeff
authored andcommitted
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 <[email protected]> Signed-off-by: David A. Greene <[email protected]> Signed-off-by: Jeff King <[email protected]>
1 parent f34be46 commit b0638aa

File tree

2 files changed

+79
-55
lines changed

2 files changed

+79
-55
lines changed

contrib/subtree/t/Makefile

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,23 @@ TAR ?= $(TAR)
1313
RM ?= rm -f
1414
PROVE ?= prove
1515
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
1623

1724
# Shell quote;
1825
SHELL_PATH_SQ = $(subst ','\'',$(SHELL_PATH))
26+
PERL_PATH_SQ = $(subst ','\'',$(PERL_PATH))
27+
TEST_RESULTS_DIRECTORY_SQ = $(subst ','\'',$(TEST_RESULTS_DIRECTORY))
1928

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)))
2133

2234
all: $(DEFAULT_TEST_TARGET)
2335

@@ -26,20 +38,22 @@ test: pre-clean $(TEST_LINT)
2638

2739
prove: pre-clean $(TEST_LINT)
2840
@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
3042

3143
$(T):
3244
@echo "*** $@ ***"; GIT_CONFIG=.git/config '$(SHELL_PATH_SQ)' $@ $(GIT_TEST_OPTS)
3345

3446
pre-clean:
35-
$(RM) -r test-results
47+
$(RM) -r '$(TEST_RESULTS_DIRECTORY_SQ)'
3648

37-
clean:
38-
$(RM) -r 'trash directory'.* test-results
49+
clean-except-prove-cache:
50+
$(RM) -r 'trash directory'.* '$(TEST_RESULTS_DIRECTORY_SQ)'
3951
$(RM) -r valgrind/bin
52+
53+
clean: clean-except-prove-cache
4054
$(RM) .prove
4155

42-
test-lint: test-lint-duplicates test-lint-executable
56+
test-lint: test-lint-duplicates test-lint-executable test-lint-shell-syntax
4357

4458
test-lint-duplicates:
4559
@dups=`echo $(T) | tr ' ' '\n' | sed 's/-.*//' | sort | uniq -d` && \
@@ -51,12 +65,15 @@ test-lint-executable:
5165
test -z "$$bad" || { \
5266
echo >&2 "non-executable tests:" $$bad; exit 1; }
5367

68+
test-lint-shell-syntax:
69+
@'$(PERL_PATH_SQ)' ../../../t/check-non-portable-shell.pl $(T) $(THELPERS)
70+
5471
aggregate-results-and-cleanup: $(T)
5572
$(MAKE) aggregate-results
5673
$(MAKE) clean
5774

5875
aggregate-results:
59-
for f in ../../../t/test-results/t*-*.counts; do \
76+
for f in '$(TEST_RESULTS_DIRECTORY_SQ)'/t*-*.counts; do \
6077
echo "$$f"; \
6178
done | '$(SHELL_PATH_SQ)' ../../../t/aggregate-results.sh
6279

contrib/subtree/t/t7900-subtree.sh

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#
66
test_description='Basic porcelain support for subtrees
77
8-
This test verifies the basic operation of the merge, pull, add
8+
This test verifies the basic operation of the add, pull, merge
99
and split subcommands of git subtree.
1010
'
1111

@@ -20,7 +20,6 @@ create()
2020
git add "$1"
2121
}
2222

23-
2423
check_equal()
2524
{
2625
test_debug 'echo'
@@ -38,6 +37,30 @@ undo()
3837
git reset --hard HEAD~
3938
}
4039

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+
4164
last_commit_message()
4265
{
4366
git log --pretty=format:%s -1
@@ -123,9 +146,11 @@ test_expect_success 'add subproj to mainline' '
123146
check_equal ''"$(last_commit_message)"'' "Add '"'sub dir/'"' from commit '"'"'''"$(git rev-parse sub1)"'''"'"'"
124147
'
125148

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."
129154
'
130155

131156
test_expect_success 'add main-sub5' '
@@ -167,7 +192,7 @@ test_expect_success 'merge new subproj history into subdir' '
167192
undo
168193
'
169194

170-
test_expect_success 'Check that prefix argument is required for split' '
195+
test_expect_success 'split requires option --prefix' '
171196
echo "You must provide the --prefix option." > expected &&
172197
test_must_fail git subtree split > actual 2>&1 &&
173198
test_debug "printf '"'"'expected: '"'"'" &&
@@ -178,15 +203,15 @@ test_expect_success 'Check that prefix argument is required for split' '
178203
rm -f expected actual
179204
'
180205

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 &&
183208
test_must_fail git subtree split --prefix=non-existent-directory > actual 2>&1 &&
184209
test_debug "printf '"'"'expected: '"'"'" &&
185210
test_debug "cat expected" &&
186211
test_debug "printf '"'"'actual: '"'"'" &&
187212
test_debug "cat actual" &&
188-
test_cmp expected actual
189-
# rm -f expected actual
213+
test_cmp expected actual &&
214+
rm -f expected actual
190215
'
191216

192217
test_expect_success 'check if --message works for split+rejoin' '
@@ -279,18 +304,22 @@ test_expect_success 'merge split into subproj' '
279304

280305
chkm="main4
281306
main6"
307+
282308
chkms="main-sub10
283309
main-sub5
284310
main-sub7
285311
main-sub8"
312+
286313
chkms_sub=$(cat <<TXT | sed 's,^,sub dir/,'
287314
$chkms
288315
TXT
289316
)
317+
290318
chks="sub1
291319
sub2
292320
sub3
293321
sub9"
322+
294323
chks_sub=$(cat <<TXT | sed 's,^,sub dir/,'
295324
$chks
296325
TXT
@@ -301,6 +330,7 @@ test_expect_success 'make sure exactly the right set of files ends up in the sub
301330
check_equal "$subfiles" "$chkms
302331
$chks"
303332
'
333+
304334
test_expect_success 'make sure the subproj history *only* contains commits that affect the subdir' '
305335
allchanges=''"$(git log --name-only --pretty=format:'"''"' | sort | sed "/^$/d")"'' &&
306336
check_equal "$allchanges" "$chkms
@@ -324,26 +354,27 @@ $chks_sub"
324354
'
325355

326356
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
329359
# change, since they were *only* changed in the subtree branch.
330360
allchanges=''"$(git log --name-only --pretty=format:'"''"' | sort | sed "/^$/d")"'' &&
331-
check_equal "$allchanges" ''"$(cat <<TXT | sort
361+
expected=''"$(cat <<TXT | sort
332362
$chkms
333363
$chkm
334364
$chks
335365
$chkms_sub
336366
TXT
337-
)"''
367+
)"'' &&
368+
check_equal "$allchanges" "$expected"
338369
'
339370

340371
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)" ""
342373
'
343374

344375
test_expect_success 'make sure no "git subtree" tagged commits make it into subproj' '
345376
# 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.*:")" ""
347378
'
348379

349380
# prepare second pair of repositories
@@ -408,13 +439,13 @@ test_expect_success 'split for main-sub4 without --onto' '
408439
git subtree split --prefix "sub dir" --branch mainsub4
409440
'
410441

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,
412443
# something went wrong (the "newparent" of "master~" commit should
413444
# have been sub3, but it was not, because its cache was not set to
414-
# itself)
445+
# itself).
415446

416447
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)"
418449
'
419450

420451
test_expect_success 'add main-sub5' '
@@ -431,52 +462,28 @@ test_expect_success 'split for main-sub5 without --onto' '
431462
check_equal ''"$(git log --pretty=format:%P -1 mainsub5)"'' ""
432463
'
433464

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-
458465
test_expect_success 'verify one file change per commit' '
459466
x= &&
460-
list=''"$(git log --pretty=format:'"'commit: %H'"' | joincommits)"'' &&
467+
list=''"$(git log --pretty=format:'"'commit: %H'"' | join_commits)"'' &&
461468
# test_debug "echo HERE" &&
462469
# test_debug "echo ''"$list"''" &&
463-
(git log --pretty=format:'"'commit: %H'"' | joincommits |
470+
git log --pretty=format:'"'commit: %H'"' | join_commits |
464471
( while read commit a b; do
465472
test_debug "echo Verifying commit "''"$commit"''
466473
test_debug "echo a: "''"$a"''
467474
test_debug "echo b: "''"$b"''
468475
check_equal "$b" ""
469476
x=1
470477
done
471-
check_equal "$x" 1
472-
))
478+
check_equal "$x" "1"
479+
)
473480
'
474481

475482
# test push
476483

477484
cd ../..
478485

479-
mkdir test-push
486+
mkdir -p test-push
480487

481488
cd test-push
482489

0 commit comments

Comments
 (0)