1#!/bin/sh
2#
3# Copyright (c) 2005 Junio C Hamano
4#
5
6test_description='Test the very basics part #1.
7
8The rest of the test suite does not check the basic operation of git
9plumbing commands to work very carefully.  Their job is to concentrate
10on tricky features that caused bugs in the past to detect regression.
11
12This test runs very basic features, like registering things in cache,
13writing tree, etc.
14
15Note that this test *deliberately* hard-codes many expected object
16IDs.  When object ID computation changes, like in the previous case of
17swapping compression and hashing order, the person who is making the
18modification *should* take notice and update the test vectors here.
19'
20
21. ./test-lib.sh
22. "$TEST_DIRECTORY"/lib-subtest.sh
23
24try_local_xy () {
25	local x="local" y="alsolocal" &&
26	echo "$x $y"
27}
28
29# Check whether the shell supports the "local" keyword. "local" is not
30# POSIX-standard, but it is very widely supported by POSIX-compliant
31# shells, and we rely on it within Git's test framework.
32#
33# If your shell fails this test, the results of other tests may be
34# unreliable. You may wish to report the problem to the Git mailing
35# list <git@vger.kernel.org>, as it could cause us to reconsider
36# relying on "local".
37test_expect_success 'verify that the running shell supports "local"' '
38	x="notlocal" &&
39	y="alsonotlocal" &&
40	echo "local alsolocal" >expected1 &&
41	try_local_xy >actual1 &&
42	test_cmp expected1 actual1 &&
43	echo "notlocal alsonotlocal" >expected2 &&
44	echo "$x $y" >actual2 &&
45	test_cmp expected2 actual2
46'
47
48################################################################
49# git init has been done in an empty repository.
50# make sure it is empty.
51
52test_expect_success '.git/objects should be empty after git init in an empty repo' '
53	find .git/objects -type f -print >should-be-empty &&
54	test_line_count = 0 should-be-empty
55'
56
57# also it should have 2 subdirectories; no fan-out anymore, pack, and info.
58# 3 is counting "objects" itself
59test_expect_success '.git/objects should have 3 subdirectories' '
60	find .git/objects -type d -print >full-of-directories &&
61	test_line_count = 3 full-of-directories
62'
63
64################################################################
65# Test harness
66test_expect_success 'success is reported like this' '
67	:
68'
69
70test_expect_success 'subtest: 3 passing tests' '
71	write_and_run_sub_test_lib_test full-pass <<-\EOF &&
72	for i in 1 2 3
73	do
74		test_expect_success "passing test #$i" "true"
75	done
76	test_done
77	EOF
78	check_sub_test_lib_test full-pass <<-\EOF
79	> ok 1 - passing test #1
80	> ok 2 - passing test #2
81	> ok 3 - passing test #3
82	> # passed all 3 test(s)
83	> 1..3
84	EOF
85'
86
87test_expect_success 'subtest: 2/3 tests passing' '
88	write_and_run_sub_test_lib_test_err partial-pass <<-\EOF &&
89	test_expect_success "passing test #1" "true"
90	test_expect_success "failing test #2" "false"
91	test_expect_success "passing test #3" "true"
92	test_done
93	EOF
94	check_sub_test_lib_test partial-pass <<-\EOF
95	> ok 1 - passing test #1
96	> not ok 2 - failing test #2
97	#	false
98	> ok 3 - passing test #3
99	> # failed 1 among 3 test(s)
100	> 1..3
101	EOF
102'
103
104test_expect_success 'subtest: a failing TODO test' '
105	write_and_run_sub_test_lib_test failing-todo <<-\EOF &&
106	test_expect_success "passing test" "true"
107	test_expect_failure "pretend we have a known breakage" "false"
108	test_done
109	EOF
110	check_sub_test_lib_test failing-todo <<-\EOF
111	> ok 1 - passing test
112	> not ok 2 - pretend we have a known breakage # TODO known breakage
113	> # still have 1 known breakage(s)
114	> # passed all remaining 1 test(s)
115	> 1..2
116	EOF
117'
118
119test_expect_success 'subtest: a passing TODO test' '
120	write_and_run_sub_test_lib_test passing-todo <<-\EOF &&
121	test_expect_failure "pretend we have fixed a known breakage" "true"
122	test_done
123	EOF
124	check_sub_test_lib_test passing-todo <<-\EOF
125	> ok 1 - pretend we have fixed a known breakage # TODO known breakage vanished
126	> # 1 known breakage(s) vanished; please update test(s)
127	> 1..1
128	EOF
129'
130
131test_expect_success 'subtest: 2 TODO tests, one passin' '
132	write_and_run_sub_test_lib_test partially-passing-todos <<-\EOF &&
133	test_expect_failure "pretend we have a known breakage" "false"
134	test_expect_success "pretend we have a passing test" "true"
135	test_expect_failure "pretend we have fixed another known breakage" "true"
136	test_done
137	EOF
138	check_sub_test_lib_test partially-passing-todos <<-\EOF
139	> not ok 1 - pretend we have a known breakage # TODO known breakage
140	> ok 2 - pretend we have a passing test
141	> ok 3 - pretend we have fixed another known breakage # TODO known breakage vanished
142	> # 1 known breakage(s) vanished; please update test(s)
143	> # still have 1 known breakage(s)
144	> # passed all remaining 1 test(s)
145	> 1..3
146	EOF
147'
148
149test_expect_success 'subtest: mixed results: pass, failure and a TODO test' '
150	write_and_run_sub_test_lib_test_err mixed-results1 <<-\EOF &&
151	test_expect_success "passing test" "true"
152	test_expect_success "failing test" "false"
153	test_expect_failure "pretend we have a known breakage" "false"
154	test_done
155	EOF
156	check_sub_test_lib_test mixed-results1 <<-\EOF
157	> ok 1 - passing test
158	> not ok 2 - failing test
159	> #	false
160	> not ok 3 - pretend we have a known breakage # TODO known breakage
161	> # still have 1 known breakage(s)
162	> # failed 1 among remaining 2 test(s)
163	> 1..3
164	EOF
165'
166
167test_expect_success 'subtest: mixed results: a mixture of all possible results' '
168	write_and_run_sub_test_lib_test_err mixed-results2 <<-\EOF &&
169	test_expect_success "passing test" "true"
170	test_expect_success "passing test" "true"
171	test_expect_success "passing test" "true"
172	test_expect_success "passing test" "true"
173	test_expect_success "failing test" "false"
174	test_expect_success "failing test" "false"
175	test_expect_success "failing test" "false"
176	test_expect_failure "pretend we have a known breakage" "false"
177	test_expect_failure "pretend we have a known breakage" "false"
178	test_expect_failure "pretend we have fixed a known breakage" "true"
179	test_done
180	EOF
181	check_sub_test_lib_test mixed-results2 <<-\EOF
182	> ok 1 - passing test
183	> ok 2 - passing test
184	> ok 3 - passing test
185	> ok 4 - passing test
186	> not ok 5 - failing test
187	> #	false
188	> not ok 6 - failing test
189	> #	false
190	> not ok 7 - failing test
191	> #	false
192	> not ok 8 - pretend we have a known breakage # TODO known breakage
193	> not ok 9 - pretend we have a known breakage # TODO known breakage
194	> ok 10 - pretend we have fixed a known breakage # TODO known breakage vanished
195	> # 1 known breakage(s) vanished; please update test(s)
196	> # still have 2 known breakage(s)
197	> # failed 3 among remaining 7 test(s)
198	> 1..10
199	EOF
200'
201
202test_expect_success 'subtest: --verbose option' '
203	write_and_run_sub_test_lib_test_err t1234-verbose --verbose <<-\EOF &&
204	test_expect_success "passing test" true
205	test_expect_success "test with output" "echo foo"
206	test_expect_success "failing test" false
207	test_done
208	EOF
209	mv t1234-verbose/out t1234-verbose/out+ &&
210	grep -v "^Initialized empty" t1234-verbose/out+ >t1234-verbose/out &&
211	check_sub_test_lib_test t1234-verbose <<-\EOF
212	> expecting success of 1234.1 '\''passing test'\'': true
213	> ok 1 - passing test
214	> Z
215	> expecting success of 1234.2 '\''test with output'\'': echo foo
216	> foo
217	> ok 2 - test with output
218	> Z
219	> expecting success of 1234.3 '\''failing test'\'': false
220	> not ok 3 - failing test
221	> #	false
222	> Z
223	> # failed 1 among 3 test(s)
224	> 1..3
225	EOF
226'
227
228test_expect_success 'subtest: --verbose-only option' '
229	run_sub_test_lib_test_err \
230		t1234-verbose \
231		--verbose-only=2 &&
232	check_sub_test_lib_test t1234-verbose <<-\EOF
233	> ok 1 - passing test
234	> Z
235	> expecting success of 1234.2 '\''test with output'\'': echo foo
236	> foo
237	> ok 2 - test with output
238	> Z
239	> not ok 3 - failing test
240	> #	false
241	> # failed 1 among 3 test(s)
242	> 1..3
243	EOF
244'
245
246test_expect_success 'subtest: skip one with GIT_SKIP_TESTS' '
247	(
248		run_sub_test_lib_test full-pass \
249			--skip="full.2" &&
250		check_sub_test_lib_test full-pass <<-\EOF
251		> ok 1 - passing test #1
252		> ok 2 # skip passing test #2 (GIT_SKIP_TESTS)
253		> ok 3 - passing test #3
254		> # passed all 3 test(s)
255		> 1..3
256		EOF
257	)
258'
259
260test_expect_success 'subtest: skip several with GIT_SKIP_TESTS' '
261	(
262		write_and_run_sub_test_lib_test git-skip-tests-several \
263			--skip="git.2 git.5" <<-\EOF &&
264		for i in 1 2 3 4 5 6
265		do
266			test_expect_success "passing test #$i" "true"
267		done
268		test_done
269		EOF
270		check_sub_test_lib_test git-skip-tests-several <<-\EOF
271		> ok 1 - passing test #1
272		> ok 2 # skip passing test #2 (GIT_SKIP_TESTS)
273		> ok 3 - passing test #3
274		> ok 4 - passing test #4
275		> ok 5 # skip passing test #5 (GIT_SKIP_TESTS)
276		> ok 6 - passing test #6
277		> # passed all 6 test(s)
278		> 1..6
279		EOF
280	)
281'
282
283test_expect_success 'subtest: sh pattern skipping with GIT_SKIP_TESTS' '
284	(
285		run_sub_test_lib_test git-skip-tests-several \
286			--skip="git.[2-5]" &&
287		check_sub_test_lib_test git-skip-tests-several <<-\EOF
288		> ok 1 - passing test #1
289		> ok 2 # skip passing test #2 (GIT_SKIP_TESTS)
290		> ok 3 # skip passing test #3 (GIT_SKIP_TESTS)
291		> ok 4 # skip passing test #4 (GIT_SKIP_TESTS)
292		> ok 5 # skip passing test #5 (GIT_SKIP_TESTS)
293		> ok 6 - passing test #6
294		> # passed all 6 test(s)
295		> 1..6
296		EOF
297	)
298'
299
300test_expect_success 'subtest: skip entire test suite with GIT_SKIP_TESTS' '
301	(
302		GIT_SKIP_TESTS="git" && export GIT_SKIP_TESTS &&
303		run_sub_test_lib_test git-skip-tests-several \
304			--skip="git" &&
305		check_sub_test_lib_test git-skip-tests-several <<-\EOF
306		> 1..0 # SKIP skip all tests in git
307		EOF
308	)
309'
310
311test_expect_success 'subtest: GIT_SKIP_TESTS does not skip unmatched suite' '
312	(
313		GIT_SKIP_TESTS="notgit" && export GIT_SKIP_TESTS &&
314		run_sub_test_lib_test full-pass \
315			--skip="notfull" &&
316		check_sub_test_lib_test full-pass <<-\EOF
317		> ok 1 - passing test #1
318		> ok 2 - passing test #2
319		> ok 3 - passing test #3
320		> # passed all 3 test(s)
321		> 1..3
322		EOF
323	)
324'
325
326test_expect_success 'subtest: --run basic' '
327	run_sub_test_lib_test git-skip-tests-several --run="1,3,5" &&
328	check_sub_test_lib_test git-skip-tests-several <<-\EOF
329	> ok 1 - passing test #1
330	> ok 2 # skip passing test #2 (--run)
331	> ok 3 - passing test #3
332	> ok 4 # skip passing test #4 (--run)
333	> ok 5 - passing test #5
334	> ok 6 # skip passing test #6 (--run)
335	> # passed all 6 test(s)
336	> 1..6
337	EOF
338'
339
340test_expect_success 'subtest: --run with a range' '
341	run_sub_test_lib_test git-skip-tests-several \
342		--run="1-3" &&
343	check_sub_test_lib_test git-skip-tests-several <<-\EOF
344	> ok 1 - passing test #1
345	> ok 2 - passing test #2
346	> ok 3 - passing test #3
347	> ok 4 # skip passing test #4 (--run)
348	> ok 5 # skip passing test #5 (--run)
349	> ok 6 # skip passing test #6 (--run)
350	> # passed all 6 test(s)
351	> 1..6
352	EOF
353'
354
355test_expect_success 'subtest: --run with two ranges' '
356	run_sub_test_lib_test git-skip-tests-several \
357		--run="1-2,5-6" &&
358	check_sub_test_lib_test git-skip-tests-several <<-\EOF
359	> ok 1 - passing test #1
360	> ok 2 - passing test #2
361	> ok 3 # skip passing test #3 (--run)
362	> ok 4 # skip passing test #4 (--run)
363	> ok 5 - passing test #5
364	> ok 6 - passing test #6
365	> # passed all 6 test(s)
366	> 1..6
367	EOF
368'
369
370test_expect_success 'subtest: --run with a left open range' '
371	run_sub_test_lib_test git-skip-tests-several \
372		--run="-3" &&
373	check_sub_test_lib_test git-skip-tests-several <<-\EOF
374	> ok 1 - passing test #1
375	> ok 2 - passing test #2
376	> ok 3 - passing test #3
377	> ok 4 # skip passing test #4 (--run)
378	> ok 5 # skip passing test #5 (--run)
379	> ok 6 # skip passing test #6 (--run)
380	> # passed all 6 test(s)
381	> 1..6
382	EOF
383'
384
385test_expect_success 'subtest: --run with a right open range' '
386	run_sub_test_lib_test git-skip-tests-several \
387		--run="4-" &&
388	check_sub_test_lib_test git-skip-tests-several <<-\EOF
389	> ok 1 # skip passing test #1 (--run)
390	> ok 2 # skip passing test #2 (--run)
391	> ok 3 # skip passing test #3 (--run)
392	> ok 4 - passing test #4
393	> ok 5 - passing test #5
394	> ok 6 - passing test #6
395	> # passed all 6 test(s)
396	> 1..6
397	EOF
398'
399
400test_expect_success 'subtest: --run with basic negation' '
401	run_sub_test_lib_test git-skip-tests-several \
402		--run="!3" &&
403	check_sub_test_lib_test git-skip-tests-several <<-\EOF
404	> ok 1 - passing test #1
405	> ok 2 - passing test #2
406	> ok 3 # skip passing test #3 (--run)
407	> ok 4 - passing test #4
408	> ok 5 - passing test #5
409	> ok 6 - passing test #6
410	> # passed all 6 test(s)
411	> 1..6
412	EOF
413'
414
415test_expect_success 'subtest: --run with two negations' '
416	run_sub_test_lib_test git-skip-tests-several \
417		--run="!3,!6" &&
418	check_sub_test_lib_test git-skip-tests-several <<-\EOF
419	> ok 1 - passing test #1
420	> ok 2 - passing test #2
421	> ok 3 # skip passing test #3 (--run)
422	> ok 4 - passing test #4
423	> ok 5 - passing test #5
424	> ok 6 # skip passing test #6 (--run)
425	> # passed all 6 test(s)
426	> 1..6
427	EOF
428'
429
430test_expect_success 'subtest: --run a range and negation' '
431	run_sub_test_lib_test git-skip-tests-several \
432		--run="-4,!2" &&
433	check_sub_test_lib_test git-skip-tests-several <<-\EOF
434	> ok 1 - passing test #1
435	> ok 2 # skip passing test #2 (--run)
436	> ok 3 - passing test #3
437	> ok 4 - passing test #4
438	> ok 5 # skip passing test #5 (--run)
439	> ok 6 # skip passing test #6 (--run)
440	> # passed all 6 test(s)
441	> 1..6
442	EOF
443'
444
445test_expect_success 'subtest: --run range negation' '
446	run_sub_test_lib_test git-skip-tests-several \
447		--run="!1-3" &&
448	check_sub_test_lib_test git-skip-tests-several <<-\EOF
449	> ok 1 # skip passing test #1 (--run)
450	> ok 2 # skip passing test #2 (--run)
451	> ok 3 # skip passing test #3 (--run)
452	> ok 4 - passing test #4
453	> ok 5 - passing test #5
454	> ok 6 - passing test #6
455	> # passed all 6 test(s)
456	> 1..6
457	EOF
458'
459
460test_expect_success 'subtest: --run include, exclude and include' '
461	run_sub_test_lib_test git-skip-tests-several \
462		--run="1-5,!1-3,2" &&
463	check_sub_test_lib_test git-skip-tests-several <<-\EOF
464	> ok 1 # skip passing test #1 (--run)
465	> ok 2 - passing test #2
466	> ok 3 # skip passing test #3 (--run)
467	> ok 4 - passing test #4
468	> ok 5 - passing test #5
469	> ok 6 # skip passing test #6 (--run)
470	> # passed all 6 test(s)
471	> 1..6
472	EOF
473'
474
475test_expect_success 'subtest: --run include, exclude and include, comma separated' '
476	run_sub_test_lib_test git-skip-tests-several \
477		--run=1-5,!1-3,2 &&
478	check_sub_test_lib_test git-skip-tests-several <<-\EOF
479	> ok 1 # skip passing test #1 (--run)
480	> ok 2 - passing test #2
481	> ok 3 # skip passing test #3 (--run)
482	> ok 4 - passing test #4
483	> ok 5 - passing test #5
484	> ok 6 # skip passing test #6 (--run)
485	> # passed all 6 test(s)
486	> 1..6
487	EOF
488'
489
490test_expect_success 'subtest: --run exclude and include' '
491	run_sub_test_lib_test git-skip-tests-several \
492		--run="!3-,5" &&
493	check_sub_test_lib_test git-skip-tests-several <<-\EOF
494	> ok 1 - passing test #1
495	> ok 2 - passing test #2
496	> ok 3 # skip passing test #3 (--run)
497	> ok 4 # skip passing test #4 (--run)
498	> ok 5 - passing test #5
499	> ok 6 # skip passing test #6 (--run)
500	> # passed all 6 test(s)
501	> 1..6
502	EOF
503'
504
505test_expect_success 'subtest: --run empty selectors' '
506	run_sub_test_lib_test git-skip-tests-several \
507		--run="1,,3,,,5" &&
508	check_sub_test_lib_test git-skip-tests-several <<-\EOF
509	> ok 1 - passing test #1
510	> ok 2 # skip passing test #2 (--run)
511	> ok 3 - passing test #3
512	> ok 4 # skip passing test #4 (--run)
513	> ok 5 - passing test #5
514	> ok 6 # skip passing test #6 (--run)
515	> # passed all 6 test(s)
516	> 1..6
517	EOF
518'
519
520test_expect_success 'subtest: --run substring selector' '
521	write_and_run_sub_test_lib_test run-substring-selector \
522		--run="relevant" <<-\EOF &&
523	test_expect_success "relevant test" "true"
524	for i in 1 2 3 4 5 6
525	do
526		test_expect_success "other test #$i" "true"
527	done
528	test_done
529	EOF
530	check_sub_test_lib_test run-substring-selector <<-\EOF
531	> ok 1 - relevant test
532	> ok 2 # skip other test #1 (--run)
533	> ok 3 # skip other test #2 (--run)
534	> ok 4 # skip other test #3 (--run)
535	> ok 5 # skip other test #4 (--run)
536	> ok 6 # skip other test #5 (--run)
537	> ok 7 # skip other test #6 (--run)
538	> # passed all 7 test(s)
539	> 1..7
540	EOF
541'
542
543test_expect_success 'subtest: --run keyword selection' '
544	write_and_run_sub_test_lib_test_err run-inv-range-start \
545		--run="a-5" <<-\EOF &&
546	test_expect_success "passing test #1" "true"
547	test_done
548	EOF
549	check_sub_test_lib_test_err run-inv-range-start \
550		<<-\EOF_OUT 3<<-EOF_ERR
551	> FATAL: Unexpected exit with code 1
552	EOF_OUT
553	> error: --run: invalid non-numeric in range start: ${SQ}a-5${SQ}
554	EOF_ERR
555'
556
557test_expect_success 'subtest: --run invalid range end' '
558	run_sub_test_lib_test_err run-inv-range-start \
559		--run="1-z" &&
560	check_sub_test_lib_test_err run-inv-range-start \
561		<<-\EOF_OUT 3<<-EOF_ERR
562	> FATAL: Unexpected exit with code 1
563	EOF_OUT
564	> error: --run: invalid non-numeric in range end: ${SQ}1-z${SQ}
565	EOF_ERR
566'
567
568test_expect_success 'subtest: tests respect prerequisites' '
569	write_and_run_sub_test_lib_test prereqs <<-\EOF &&
570
571	test_set_prereq HAVEIT
572	test_expect_success HAVEIT "prereq is satisfied" "true"
573	test_expect_success "have_prereq works" "
574		test_have_prereq HAVEIT
575	"
576	test_expect_success DONTHAVEIT "prereq not satisfied" "false"
577
578	test_set_prereq HAVETHIS
579	test_expect_success HAVETHIS,HAVEIT "multiple prereqs" "true"
580	test_expect_success HAVEIT,DONTHAVEIT "mixed prereqs (yes,no)" "false"
581	test_expect_success DONTHAVEIT,HAVEIT "mixed prereqs (no,yes)" "false"
582
583	test_done
584	EOF
585
586	check_sub_test_lib_test prereqs <<-\EOF
587	ok 1 - prereq is satisfied
588	ok 2 - have_prereq works
589	ok 3 # skip prereq not satisfied (missing DONTHAVEIT)
590	ok 4 - multiple prereqs
591	ok 5 # skip mixed prereqs (yes,no) (missing DONTHAVEIT of HAVEIT,DONTHAVEIT)
592	ok 6 # skip mixed prereqs (no,yes) (missing DONTHAVEIT of DONTHAVEIT,HAVEIT)
593	# passed all 6 test(s)
594	1..6
595	EOF
596'
597
598test_expect_success 'subtest: tests respect lazy prerequisites' '
599	write_and_run_sub_test_lib_test lazy-prereqs <<-\EOF &&
600
601	test_lazy_prereq LAZY_TRUE true
602	test_expect_success LAZY_TRUE "lazy prereq is satisifed" "true"
603	test_expect_success !LAZY_TRUE "negative lazy prereq" "false"
604
605	test_lazy_prereq LAZY_FALSE false
606	test_expect_success LAZY_FALSE "lazy prereq not satisfied" "false"
607	test_expect_success !LAZY_FALSE "negative false prereq" "true"
608
609	test_done
610	EOF
611
612	check_sub_test_lib_test lazy-prereqs <<-\EOF
613	ok 1 - lazy prereq is satisifed
614	ok 2 # skip negative lazy prereq (missing !LAZY_TRUE)
615	ok 3 # skip lazy prereq not satisfied (missing LAZY_FALSE)
616	ok 4 - negative false prereq
617	# passed all 4 test(s)
618	1..4
619	EOF
620'
621
622test_expect_success 'subtest: nested lazy prerequisites' '
623	write_and_run_sub_test_lib_test nested-lazy <<-\EOF &&
624
625	test_lazy_prereq NESTED_INNER "
626		>inner &&
627		rm -f outer
628	"
629	test_lazy_prereq NESTED_PREREQ "
630		>outer &&
631		test_have_prereq NESTED_INNER &&
632		echo can create new file in cwd >file &&
633		test_path_is_file outer &&
634		test_path_is_missing inner
635	"
636	test_expect_success NESTED_PREREQ "evaluate nested prereq" "true"
637
638	test_done
639	EOF
640
641	check_sub_test_lib_test nested-lazy <<-\EOF
642	ok 1 - evaluate nested prereq
643	# passed all 1 test(s)
644	1..1
645	EOF
646'
647
648test_expect_success 'subtest: lazy prereqs do not turn off tracing' '
649	write_and_run_sub_test_lib_test lazy-prereq-and-tracing \
650		-v -x <<-\EOF &&
651	test_lazy_prereq LAZY true
652
653	test_expect_success lazy "test_have_prereq LAZY && echo trace"
654
655	test_done
656	EOF
657
658	grep "echo trace" lazy-prereq-and-tracing/err
659'
660
661test_expect_success 'subtest: tests clean up after themselves' '
662	write_and_run_sub_test_lib_test cleanup <<-\EOF &&
663	clean=no
664	test_expect_success "do cleanup" "
665		test_when_finished clean=yes
666	"
667	test_expect_success "cleanup happened" "
668		test $clean = yes
669	"
670	test_done
671	EOF
672
673	check_sub_test_lib_test cleanup <<-\EOF
674	ok 1 - do cleanup
675	ok 2 - cleanup happened
676	# passed all 2 test(s)
677	1..2
678	EOF
679'
680
681test_expect_success 'subtest: tests clean up even on failures' '
682	write_and_run_sub_test_lib_test_err \
683		failing-cleanup <<-\EOF &&
684	test_expect_success "tests clean up even after a failure" "
685		touch clean-after-failure &&
686		test_when_finished rm clean-after-failure &&
687		(exit 1)
688	"
689	test_expect_success "failure to clean up causes the test to fail" "
690		test_when_finished \"(exit 2)\"
691	"
692	test_done
693	EOF
694	check_sub_test_lib_test failing-cleanup <<-\EOF
695	> not ok 1 - tests clean up even after a failure
696	> #	Z
697	> #	touch clean-after-failure &&
698	> #	test_when_finished rm clean-after-failure &&
699	> #	(exit 1)
700	> #	Z
701	> not ok 2 - failure to clean up causes the test to fail
702	> #	Z
703	> #	test_when_finished "(exit 2)"
704	> #	Z
705	> # failed 2 among 2 test(s)
706	> 1..2
707	EOF
708'
709
710test_expect_success 'subtest: test_atexit is run' '
711	write_and_run_sub_test_lib_test_err \
712		atexit-cleanup -i <<-\EOF &&
713	test_expect_success "tests clean up even after a failure" "
714		> ../../clean-atexit &&
715		test_atexit rm ../../clean-atexit &&
716		> ../../also-clean-atexit &&
717		test_atexit rm ../../also-clean-atexit &&
718		> ../../dont-clean-atexit &&
719		(exit 1)
720	"
721	test_done
722	EOF
723	test_path_is_file dont-clean-atexit &&
724	test_path_is_missing clean-atexit &&
725	test_path_is_missing also-clean-atexit
726'
727
728test_expect_success 'test_oid provides sane info by default' '
729	test_oid zero >actual &&
730	grep "^00*\$" actual &&
731	rawsz="$(test_oid rawsz)" &&
732	hexsz="$(test_oid hexsz)" &&
733	test "$hexsz" -eq $(wc -c <actual) &&
734	test $(( $rawsz * 2)) -eq "$hexsz"
735'
736
737test_expect_success 'test_oid can look up data for SHA-1' '
738	test_when_finished "test_detect_hash" &&
739	test_set_hash sha1 &&
740	test_oid zero >actual &&
741	grep "^00*\$" actual &&
742	rawsz="$(test_oid rawsz)" &&
743	hexsz="$(test_oid hexsz)" &&
744	test $(wc -c <actual) -eq 40 &&
745	test "$rawsz" -eq 20 &&
746	test "$hexsz" -eq 40
747'
748
749test_expect_success 'test_oid can look up data for SHA-256' '
750	test_when_finished "test_detect_hash" &&
751	test_set_hash sha256 &&
752	test_oid zero >actual &&
753	grep "^00*\$" actual &&
754	rawsz="$(test_oid rawsz)" &&
755	hexsz="$(test_oid hexsz)" &&
756	test $(wc -c <actual) -eq 64 &&
757	test "$rawsz" -eq 32 &&
758	test "$hexsz" -eq 64
759'
760
761test_expect_success 'test_oid can look up data for a specified algorithm' '
762	rawsz="$(test_oid --hash=sha1 rawsz)" &&
763	hexsz="$(test_oid --hash=sha1 hexsz)" &&
764	test "$rawsz" -eq 20 &&
765	test "$hexsz" -eq 40 &&
766	rawsz="$(test_oid --hash=sha256 rawsz)" &&
767	hexsz="$(test_oid --hash=sha256 hexsz)" &&
768	test "$rawsz" -eq 32 &&
769	test "$hexsz" -eq 64
770'
771
772test_expect_success 'test_bool_env' '
773	(
774		sane_unset envvar &&
775
776		test_bool_env envvar true &&
777		! test_bool_env envvar false &&
778
779		envvar= &&
780		export envvar &&
781		! test_bool_env envvar true &&
782		! test_bool_env envvar false &&
783
784		envvar=true &&
785		test_bool_env envvar true &&
786		test_bool_env envvar false &&
787
788		envvar=false &&
789		! test_bool_env envvar true &&
790		! test_bool_env envvar false &&
791
792		envvar=invalid &&
793		# When encountering an invalid bool value, test_bool_env
794		# prints its error message to the original stderr of the
795		# test script, hence the redirection of fd 7, and aborts
796		# with "exit 1", hence the subshell.
797		! ( test_bool_env envvar true ) 7>err &&
798		grep "error: test_bool_env requires bool values" err &&
799
800		envvar=true &&
801		! ( test_bool_env envvar invalid ) 7>err &&
802		grep "error: test_bool_env requires bool values" err
803	)
804'
805
806################################################################
807# Basics of the basics
808
809test_oid_cache <<\EOF
810path0f sha1:f87290f8eb2cbbea7857214459a0739927eab154
811path0f sha256:638106af7c38be056f3212cbd7ac65bc1bac74f420ca5a436ff006a9d025d17d
812
813path0s sha1:15a98433ae33114b085f3eb3bb03b832b3180a01
814path0s sha256:3a24cc53cf68edddac490bbf94a418a52932130541361f685df685e41dd6c363
815
816path2f sha1:3feff949ed00a62d9f7af97c15cd8a30595e7ac7
817path2f sha256:2a7f36571c6fdbaf0e3f62751a0b25a3f4c54d2d1137b3f4af9cb794bb498e5f
818
819path2s sha1:d8ce161addc5173867a3c3c730924388daedbc38
820path2s sha256:18fd611b787c2e938ddcc248fabe4d66a150f9364763e9ec133dd01d5bb7c65a
821
822path2d sha1:58a09c23e2ca152193f2786e06986b7b6712bdbe
823path2d sha256:00e4b32b96e7e3d65d79112dcbea53238a22715f896933a62b811377e2650c17
824
825path3f sha1:0aa34cae68d0878578ad119c86ca2b5ed5b28376
826path3f sha256:09f58616b951bd571b8cb9dc76d372fbb09ab99db2393f5ab3189d26c45099ad
827
828path3s sha1:8599103969b43aff7e430efea79ca4636466794f
829path3s sha256:fce1aed087c053306f3f74c32c1a838c662bbc4551a7ac2420f5d6eb061374d0
830
831path3d sha1:21ae8269cacbe57ae09138dcc3a2887f904d02b3
832path3d sha256:9b60497be959cb830bf3f0dc82bcc9ad9e925a24e480837ade46b2295e47efe1
833
834subp3f sha1:00fb5908cb97c2564a9783c0c64087333b3b464f
835subp3f sha256:a1a9e16998c988453f18313d10375ee1d0ddefe757e710dcae0d66aa1e0c58b3
836
837subp3s sha1:6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c
838subp3s sha256:81759d9f5e93c6546ecfcadb560c1ff057314b09f93fe8ec06e2d8610d34ef10
839
840subp3d sha1:3c5e5399f3a333eddecce7a9b9465b63f65f51e2
841subp3d sha256:76b4ef482d4fa1c754390344cf3851c7f883b27cf9bc999c6547928c46aeafb7
842
843root sha1:087704a96baf1c2d1c869a8b084481e121c88b5b
844root sha256:9481b52abab1b2ffeedbf9de63ce422b929f179c1b98ff7bee5f8f1bc0710751
845
846simpletree sha1:7bb943559a305bdd6bdee2cef6e5df2413c3d30a
847simpletree sha256:1710c07a6c86f9a3c7376364df04c47ee39e5a5e221fcdd84b743bc9bb7e2bc5
848EOF
849
850# updating a new file without --add should fail.
851test_expect_success 'git update-index without --add should fail adding' '
852	test_must_fail git update-index should-be-empty
853'
854
855# and with --add it should succeed, even if it is empty (it used to fail).
856test_expect_success 'git update-index with --add should succeed' '
857	git update-index --add should-be-empty
858'
859
860test_expect_success 'writing tree out with git write-tree' '
861	tree=$(git write-tree)
862'
863
864# we know the shape and contents of the tree and know the object ID for it.
865test_expect_success 'validate object ID of a known tree' '
866	test "$tree" = "$(test_oid simpletree)"
867    '
868
869# Removing paths.
870test_expect_success 'git update-index without --remove should fail removing' '
871	rm -f should-be-empty full-of-directories &&
872	test_must_fail git update-index should-be-empty
873'
874
875test_expect_success 'git update-index with --remove should be able to remove' '
876	git update-index --remove should-be-empty
877'
878
879# Empty tree can be written with recent write-tree.
880test_expect_success 'git write-tree should be able to write an empty tree' '
881	tree=$(git write-tree)
882'
883
884test_expect_success 'validate object ID of a known tree' '
885	test "$tree" = $EMPTY_TREE
886'
887
888# Various types of objects
889
890test_expect_success 'adding various types of objects with git update-index --add' '
891	mkdir path2 path3 path3/subp3 &&
892	paths="path0 path2/file2 path3/file3 path3/subp3/file3" &&
893	(
894		for p in $paths
895		do
896			echo "hello $p" >$p || exit 1
897			test_ln_s_add "hello $p" ${p}sym || exit 1
898		done
899	) &&
900	find path* ! -type d -print | xargs git update-index --add
901'
902
903# Show them and see that matches what we expect.
904test_expect_success 'showing stage with git ls-files --stage' '
905	git ls-files --stage >current
906'
907
908test_expect_success 'validate git ls-files output for a known tree' '
909	cat >expected <<-EOF &&
910	100644 $(test_oid path0f) 0	path0
911	120000 $(test_oid path0s) 0	path0sym
912	100644 $(test_oid path2f) 0	path2/file2
913	120000 $(test_oid path2s) 0	path2/file2sym
914	100644 $(test_oid path3f) 0	path3/file3
915	120000 $(test_oid path3s) 0	path3/file3sym
916	100644 $(test_oid subp3f) 0	path3/subp3/file3
917	120000 $(test_oid subp3s) 0	path3/subp3/file3sym
918	EOF
919	test_cmp expected current
920'
921
922test_expect_success 'writing tree out with git write-tree' '
923	tree=$(git write-tree)
924'
925
926test_expect_success 'validate object ID for a known tree' '
927	test "$tree" = "$(test_oid root)"
928'
929
930test_expect_success 'showing tree with git ls-tree' '
931    git ls-tree $tree >current
932'
933
934test_expect_success 'git ls-tree output for a known tree' '
935	cat >expected <<-EOF &&
936	100644 blob $(test_oid path0f)	path0
937	120000 blob $(test_oid path0s)	path0sym
938	040000 tree $(test_oid path2d)	path2
939	040000 tree $(test_oid path3d)	path3
940	EOF
941	test_cmp expected current
942'
943
944# This changed in ls-tree pathspec change -- recursive does
945# not show tree nodes anymore.
946test_expect_success 'showing tree with git ls-tree -r' '
947	git ls-tree -r $tree >current
948'
949
950test_expect_success 'git ls-tree -r output for a known tree' '
951	cat >expected <<-EOF &&
952	100644 blob $(test_oid path0f)	path0
953	120000 blob $(test_oid path0s)	path0sym
954	100644 blob $(test_oid path2f)	path2/file2
955	120000 blob $(test_oid path2s)	path2/file2sym
956	100644 blob $(test_oid path3f)	path3/file3
957	120000 blob $(test_oid path3s)	path3/file3sym
958	100644 blob $(test_oid subp3f)	path3/subp3/file3
959	120000 blob $(test_oid subp3s)	path3/subp3/file3sym
960	EOF
961	test_cmp expected current
962'
963
964# But with -r -t we can have both.
965test_expect_success 'showing tree with git ls-tree -r -t' '
966	git ls-tree -r -t $tree >current
967'
968
969test_expect_success 'git ls-tree -r output for a known tree' '
970	cat >expected <<-EOF &&
971	100644 blob $(test_oid path0f)	path0
972	120000 blob $(test_oid path0s)	path0sym
973	040000 tree $(test_oid path2d)	path2
974	100644 blob $(test_oid path2f)	path2/file2
975	120000 blob $(test_oid path2s)	path2/file2sym
976	040000 tree $(test_oid path3d)	path3
977	100644 blob $(test_oid path3f)	path3/file3
978	120000 blob $(test_oid path3s)	path3/file3sym
979	040000 tree $(test_oid subp3d)	path3/subp3
980	100644 blob $(test_oid subp3f)	path3/subp3/file3
981	120000 blob $(test_oid subp3s)	path3/subp3/file3sym
982	EOF
983	test_cmp expected current
984'
985
986test_expect_success 'writing partial tree out with git write-tree --prefix' '
987	ptree=$(git write-tree --prefix=path3)
988'
989
990test_expect_success 'validate object ID for a known tree' '
991	test "$ptree" = $(test_oid path3d)
992'
993
994test_expect_success 'writing partial tree out with git write-tree --prefix' '
995	ptree=$(git write-tree --prefix=path3/subp3)
996'
997
998test_expect_success 'validate object ID for a known tree' '
999	test "$ptree" = $(test_oid subp3d)
1000'
1001
1002test_expect_success 'put invalid objects into the index' '
1003	rm -f .git/index &&
1004	suffix=$(echo $ZERO_OID | sed -e "s/^.//") &&
1005	cat >badobjects <<-EOF &&
1006	100644 blob $(test_oid 001)	dir/file1
1007	100644 blob $(test_oid 002)	dir/file2
1008	100644 blob $(test_oid 003)	dir/file3
1009	100644 blob $(test_oid 004)	dir/file4
1010	100644 blob $(test_oid 005)	dir/file5
1011	EOF
1012	git update-index --index-info <badobjects
1013'
1014
1015test_expect_success 'writing this tree without --missing-ok' '
1016	test_must_fail git write-tree
1017'
1018
1019test_expect_success 'writing this tree with --missing-ok' '
1020	git write-tree --missing-ok
1021'
1022
1023
1024################################################################
1025test_expect_success 'git read-tree followed by write-tree should be idempotent' '
1026	rm -f .git/index &&
1027	git read-tree $tree &&
1028	test_path_is_file .git/index &&
1029	newtree=$(git write-tree) &&
1030	test "$newtree" = "$tree"
1031'
1032
1033test_expect_success 'validate git diff-files output for a know cache/work tree state' '
1034	cat >expected <<EOF &&
1035:100644 100644 $(test_oid path0f) $ZERO_OID M	path0
1036:120000 120000 $(test_oid path0s) $ZERO_OID M	path0sym
1037:100644 100644 $(test_oid path2f) $ZERO_OID M	path2/file2
1038:120000 120000 $(test_oid path2s) $ZERO_OID M	path2/file2sym
1039:100644 100644 $(test_oid path3f) $ZERO_OID M	path3/file3
1040:120000 120000 $(test_oid path3s) $ZERO_OID M	path3/file3sym
1041:100644 100644 $(test_oid subp3f) $ZERO_OID M	path3/subp3/file3
1042:120000 120000 $(test_oid subp3s) $ZERO_OID M	path3/subp3/file3sym
1043EOF
1044	git diff-files >current &&
1045	test_cmp expected current
1046'
1047
1048test_expect_success 'git update-index --refresh should succeed' '
1049	git update-index --refresh
1050'
1051
1052test_expect_success 'no diff after checkout and git update-index --refresh' '
1053	git diff-files >current &&
1054	cmp -s current /dev/null
1055'
1056
1057################################################################
1058P=$(test_oid root)
1059
1060test_expect_success 'git commit-tree records the correct tree in a commit' '
1061	commit0=$(echo NO | git commit-tree $P) &&
1062	git show --pretty=raw $commit0 >out &&
1063	tree=$(sed -n -e "s/^tree //p" -e "/^author /q" out) &&
1064	test "z$tree" = "z$P"
1065'
1066
1067test_expect_success 'git commit-tree records the correct parent in a commit' '
1068	commit1=$(echo NO | git commit-tree $P -p $commit0) &&
1069	git show --pretty=raw $commit1 >out &&
1070	parent=$(sed -n -e "s/^parent //p" -e "/^author /q" out) &&
1071	test "z$commit0" = "z$parent"
1072'
1073
1074test_expect_success 'git commit-tree omits duplicated parent in a commit' '
1075	commit2=$(echo NO | git commit-tree $P -p $commit0 -p $commit0) &&
1076	git show --pretty=raw $commit2 >out &&
1077	cat >match.sed <<-\EOF &&
1078	s/^parent //p
1079	/^author /q
1080	EOF
1081	parent=$(sed -n -f match.sed out | sort -u) &&
1082	test "z$commit0" = "z$parent" &&
1083	git show --pretty=raw $commit2 >out &&
1084	test_stdout_line_count = 1 sed -n -f match.sed out
1085'
1086
1087test_expect_success 'update-index D/F conflict' '
1088	mv path0 tmp &&
1089	mv path2 path0 &&
1090	mv tmp path2 &&
1091	git update-index --add --replace path2 path0/file2 &&
1092	numpath0=$(git ls-files path0 | wc -l) &&
1093	test $numpath0 = 1
1094'
1095
1096test_expect_success 'very long name in the index handled sanely' '
1097
1098	a=a && # 1
1099	a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 16
1100	a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 256
1101	a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 4096
1102	a=${a}q &&
1103
1104	>path4 &&
1105	git update-index --add path4 &&
1106	(
1107		git ls-files -s path4 |
1108		sed -e "s/	.*/	/" |
1109		tr -d "\012" &&
1110		echo "$a"
1111	) | git update-index --index-info &&
1112	len=$(git ls-files "a*" | wc -c) &&
1113	test $len = 4098
1114'
1115
1116test_expect_success 'test_must_fail on a failing git command' '
1117	test_must_fail git notacommand
1118'
1119
1120test_expect_success 'test_must_fail on a failing git command with env' '
1121	test_must_fail env var1=a var2=b git notacommand
1122'
1123
1124test_expect_success 'test_must_fail rejects a non-git command' '
1125	! test_must_fail grep ^$ notafile 2>err &&
1126	grep -F "test_must_fail: only '"'"'git'"'"' is allowed" err
1127'
1128
1129test_expect_success 'test_must_fail rejects a non-git command with env' '
1130	! test_must_fail env var1=a var2=b grep ^$ notafile 2>err &&
1131	grep -F "test_must_fail: only '"'"'git'"'"' is allowed" err
1132'
1133
1134test_done
1135