Merge branch 'rr/t4041-cleanup'
[git.git] / t / t0000-basic.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2005 Junio C Hamano
4 #
5
6 test_description='Test the very basics part #1.
7
8 The rest of the test suite does not check the basic operation of git
9 plumbing commands to work very carefully.  Their job is to concentrate
10 on tricky features that caused bugs in the past to detect regression.
11
12 This test runs very basic features, like registering things in cache,
13 writing tree, etc.
14
15 Note that this test *deliberately* hard-codes many expected object
16 IDs.  When object ID computation changes, like in the previous case of
17 swapping compression and hashing order, the person who is making the
18 modification *should* take notice and update the test vectors here.
19 '
20
21 . ./test-lib.sh
22
23 ################################################################
24 # git init has been done in an empty repository.
25 # make sure it is empty.
26
27 test_expect_success '.git/objects should be empty after git init in an empty repo' '
28         find .git/objects -type f -print >should-be-empty &&
29         test_line_count = 0 should-be-empty
30 '
31
32 # also it should have 2 subdirectories; no fan-out anymore, pack, and info.
33 # 3 is counting "objects" itself
34 test_expect_success '.git/objects should have 3 subdirectories' '
35         find .git/objects -type d -print >full-of-directories &&
36         test_line_count = 3 full-of-directories
37 '
38
39 ################################################################
40 # Test harness
41 test_expect_success 'success is reported like this' '
42         :
43 '
44 test_expect_failure 'pretend we have a known breakage' '
45         false
46 '
47
48 test_expect_success 'pretend we have fixed a known breakage (run in sub test-lib)' "
49         mkdir passing-todo &&
50         (cd passing-todo &&
51         cat >passing-todo.sh <<-EOF &&
52         #!$SHELL_PATH
53
54         test_description='A passing TODO test
55
56         This is run in a sub test-lib so that we do not get incorrect
57         passing metrics
58         '
59
60         # Point to the t/test-lib.sh, which isn't in ../ as usual
61         TEST_DIRECTORY=\"$TEST_DIRECTORY\"
62         . \"\$TEST_DIRECTORY\"/test-lib.sh
63
64         test_expect_failure 'pretend we have fixed a known breakage' '
65                 :
66         '
67
68         test_done
69         EOF
70         chmod +x passing-todo.sh &&
71         ./passing-todo.sh >out 2>err &&
72         ! test -s err &&
73         sed -e 's/^> //' >expect <<-\\EOF &&
74         > ok 1 - pretend we have fixed a known breakage # TODO known breakage
75         > # fixed 1 known breakage(s)
76         > # passed all 1 test(s)
77         > 1..1
78         EOF
79         test_cmp expect out)
80 "
81 test_set_prereq HAVEIT
82 haveit=no
83 test_expect_success HAVEIT 'test runs if prerequisite is satisfied' '
84         test_have_prereq HAVEIT &&
85         haveit=yes
86 '
87 donthaveit=yes
88 test_expect_success DONTHAVEIT 'unmet prerequisite causes test to be skipped' '
89         donthaveit=no
90 '
91 if test $haveit$donthaveit != yesyes
92 then
93         say "bug in test framework: prerequisite tags do not work reliably"
94         exit 1
95 fi
96
97 test_set_prereq HAVETHIS
98 haveit=no
99 test_expect_success HAVETHIS,HAVEIT 'test runs if prerequisites are satisfied' '
100         test_have_prereq HAVEIT &&
101         test_have_prereq HAVETHIS &&
102         haveit=yes
103 '
104 donthaveit=yes
105 test_expect_success HAVEIT,DONTHAVEIT 'unmet prerequisites causes test to be skipped' '
106         donthaveit=no
107 '
108 donthaveiteither=yes
109 test_expect_success DONTHAVEIT,HAVEIT 'unmet prerequisites causes test to be skipped' '
110         donthaveiteither=no
111 '
112 if test $haveit$donthaveit$donthaveiteither != yesyesyes
113 then
114         say "bug in test framework: multiple prerequisite tags do not work reliably"
115         exit 1
116 fi
117
118 test_lazy_prereq LAZY_TRUE true
119 havetrue=no
120 test_expect_success LAZY_TRUE 'test runs if lazy prereq is satisfied' '
121         havetrue=yes
122 '
123 donthavetrue=yes
124 test_expect_success !LAZY_TRUE 'missing lazy prereqs skip tests' '
125         donthavetrue=no
126 '
127
128 if test "$havetrue$donthavetrue" != yesyes
129 then
130         say 'bug in test framework: lazy prerequisites do not work'
131         exit 1
132 fi
133
134 test_lazy_prereq LAZY_FALSE false
135 nothavefalse=no
136 test_expect_success !LAZY_FALSE 'negative lazy prereqs checked' '
137         nothavefalse=yes
138 '
139 havefalse=yes
140 test_expect_success LAZY_FALSE 'missing negative lazy prereqs will skip' '
141         havefalse=no
142 '
143
144 if test "$nothavefalse$havefalse" != yesyes
145 then
146         say 'bug in test framework: negative lazy prerequisites do not work'
147         exit 1
148 fi
149
150 clean=no
151 test_expect_success 'tests clean up after themselves' '
152         test_when_finished clean=yes
153 '
154
155 if test $clean != yes
156 then
157         say "bug in test framework: basic cleanup command does not work reliably"
158         exit 1
159 fi
160
161 test_expect_success 'tests clean up even on failures' "
162         mkdir failing-cleanup &&
163         (
164         cd failing-cleanup &&
165
166         cat >failing-cleanup.sh <<-EOF &&
167         #!$SHELL_PATH
168
169         test_description='Failing tests with cleanup commands'
170
171         # Point to the t/test-lib.sh, which isn't in ../ as usual
172         TEST_DIRECTORY=\"$TEST_DIRECTORY\"
173         . \"\$TEST_DIRECTORY\"/test-lib.sh
174
175         test_expect_success 'tests clean up even after a failure' '
176                 touch clean-after-failure &&
177                 test_when_finished rm clean-after-failure &&
178                 (exit 1)
179         '
180         test_expect_success 'failure to clean up causes the test to fail' '
181                 test_when_finished \"(exit 2)\"
182         '
183         test_done
184
185         EOF
186
187         chmod +x failing-cleanup.sh &&
188         test_must_fail ./failing-cleanup.sh >out 2>err &&
189         ! test -s err &&
190         ! test -f \"trash directory.failing-cleanup/clean-after-failure\" &&
191         sed -e 's/Z$//' -e 's/^> //' >expect <<-\\EOF &&
192         > not ok - 1 tests clean up even after a failure
193         > #     Z
194         > #     touch clean-after-failure &&
195         > #     test_when_finished rm clean-after-failure &&
196         > #     (exit 1)
197         > #     Z
198         > not ok - 2 failure to clean up causes the test to fail
199         > #     Z
200         > #     test_when_finished \"(exit 2)\"
201         > #     Z
202         > # failed 2 among 2 test(s)
203         > 1..2
204         EOF
205         test_cmp expect out
206         )
207 "
208
209 ################################################################
210 # Basics of the basics
211
212 # updating a new file without --add should fail.
213 test_expect_success 'git update-index without --add should fail adding' '
214         test_must_fail git update-index should-be-empty
215 '
216
217 # and with --add it should succeed, even if it is empty (it used to fail).
218 test_expect_success 'git update-index with --add should succeed' '
219         git update-index --add should-be-empty
220 '
221
222 test_expect_success 'writing tree out with git write-tree' '
223         tree=$(git write-tree)
224 '
225
226 # we know the shape and contents of the tree and know the object ID for it.
227 test_expect_success 'validate object ID of a known tree' '
228         test "$tree" = 7bb943559a305bdd6bdee2cef6e5df2413c3d30a
229     '
230
231 # Removing paths.
232 test_expect_success 'git update-index without --remove should fail removing' '
233         rm -f should-be-empty full-of-directories &&
234         test_must_fail git update-index should-be-empty
235 '
236
237 test_expect_success 'git update-index with --remove should be able to remove' '
238         git update-index --remove should-be-empty
239 '
240
241 # Empty tree can be written with recent write-tree.
242 test_expect_success 'git write-tree should be able to write an empty tree' '
243         tree=$(git write-tree)
244 '
245
246 test_expect_success 'validate object ID of a known tree' '
247         test "$tree" = 4b825dc642cb6eb9a060e54bf8d69288fbee4904
248 '
249
250 # Various types of objects
251
252 # Some filesystems do not support symblic links; on such systems
253 # some expected values are different
254 if test_have_prereq SYMLINKS
255 then
256         expectfilter=cat
257         expectedtree=087704a96baf1c2d1c869a8b084481e121c88b5b
258         expectedptree1=21ae8269cacbe57ae09138dcc3a2887f904d02b3
259         expectedptree2=3c5e5399f3a333eddecce7a9b9465b63f65f51e2
260 else
261         expectfilter='grep -v sym'
262         expectedtree=8e18edf7d7edcf4371a3ac6ae5f07c2641db7c46
263         expectedptree1=cfb8591b2f65de8b8cc1020cd7d9e67e7793b325
264         expectedptree2=ce580448f0148b985a513b693fdf7d802cacb44f
265 fi
266
267
268 test_expect_success 'adding various types of objects with git update-index --add' '
269         mkdir path2 path3 path3/subp3 &&
270         paths="path0 path2/file2 path3/file3 path3/subp3/file3" &&
271         (
272                 for p in $paths
273                 do
274                         echo "hello $p" >$p || exit 1
275                         if test_have_prereq SYMLINKS
276                         then
277                                 ln -s "hello $p" ${p}sym || exit 1
278                         fi
279                 done
280         ) &&
281         find path* ! -type d -print | xargs git update-index --add
282 '
283
284 # Show them and see that matches what we expect.
285 test_expect_success 'showing stage with git ls-files --stage' '
286         git ls-files --stage >current
287 '
288
289 test_expect_success 'validate git ls-files output for a known tree' '
290         $expectfilter >expected <<-\EOF &&
291         100644 f87290f8eb2cbbea7857214459a0739927eab154 0       path0
292         120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0       path0sym
293         100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0       path2/file2
294         120000 d8ce161addc5173867a3c3c730924388daedbc38 0       path2/file2sym
295         100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0       path3/file3
296         120000 8599103969b43aff7e430efea79ca4636466794f 0       path3/file3sym
297         100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0       path3/subp3/file3
298         120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0       path3/subp3/file3sym
299         EOF
300         test_cmp expected current
301 '
302
303 test_expect_success 'writing tree out with git write-tree' '
304         tree=$(git write-tree)
305 '
306
307 test_expect_success 'validate object ID for a known tree' '
308         test "$tree" = "$expectedtree"
309 '
310
311 test_expect_success 'showing tree with git ls-tree' '
312     git ls-tree $tree >current
313 '
314
315 test_expect_success SYMLINKS 'git ls-tree output for a known tree' '
316         cat >expected <<-\EOF &&
317         100644 blob f87290f8eb2cbbea7857214459a0739927eab154    path0
318         120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01    path0sym
319         040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe    path2
320         040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3    path3
321         EOF
322         test_cmp expected current
323 '
324
325 # This changed in ls-tree pathspec change -- recursive does
326 # not show tree nodes anymore.
327 test_expect_success 'showing tree with git ls-tree -r' '
328         git ls-tree -r $tree >current
329 '
330
331 test_expect_success 'git ls-tree -r output for a known tree' '
332         $expectfilter >expected <<-\EOF &&
333         100644 blob f87290f8eb2cbbea7857214459a0739927eab154    path0
334         120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01    path0sym
335         100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7    path2/file2
336         120000 blob d8ce161addc5173867a3c3c730924388daedbc38    path2/file2sym
337         100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376    path3/file3
338         120000 blob 8599103969b43aff7e430efea79ca4636466794f    path3/file3sym
339         100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f    path3/subp3/file3
340         120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c    path3/subp3/file3sym
341         EOF
342         test_cmp expected current
343 '
344
345 # But with -r -t we can have both.
346 test_expect_success 'showing tree with git ls-tree -r -t' '
347         git ls-tree -r -t $tree >current
348 '
349
350 test_expect_success SYMLINKS 'git ls-tree -r output for a known tree' '
351         cat >expected <<-\EOF &&
352         100644 blob f87290f8eb2cbbea7857214459a0739927eab154    path0
353         120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01    path0sym
354         040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe    path2
355         100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7    path2/file2
356         120000 blob d8ce161addc5173867a3c3c730924388daedbc38    path2/file2sym
357         040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3    path3
358         100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376    path3/file3
359         120000 blob 8599103969b43aff7e430efea79ca4636466794f    path3/file3sym
360         040000 tree 3c5e5399f3a333eddecce7a9b9465b63f65f51e2    path3/subp3
361         100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f    path3/subp3/file3
362         120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c    path3/subp3/file3sym
363         EOF
364         test_cmp expected current
365 '
366
367 test_expect_success 'writing partial tree out with git write-tree --prefix' '
368         ptree=$(git write-tree --prefix=path3)
369 '
370
371 test_expect_success 'validate object ID for a known tree' '
372         test "$ptree" = "$expectedptree1"
373 '
374
375 test_expect_success 'writing partial tree out with git write-tree --prefix' '
376         ptree=$(git write-tree --prefix=path3/subp3)
377 '
378
379 test_expect_success 'validate object ID for a known tree' '
380         test "$ptree" = "$expectedptree2"
381 '
382
383 test_expect_success 'put invalid objects into the index' '
384         rm -f .git/index &&
385         cat >badobjects <<-\EOF &&
386         100644 blob 1000000000000000000000000000000000000000    dir/file1
387         100644 blob 2000000000000000000000000000000000000000    dir/file2
388         100644 blob 3000000000000000000000000000000000000000    dir/file3
389         100644 blob 4000000000000000000000000000000000000000    dir/file4
390         100644 blob 5000000000000000000000000000000000000000    dir/file5
391         EOF
392         git update-index --index-info <badobjects
393 '
394
395 test_expect_success 'writing this tree without --missing-ok' '
396         test_must_fail git write-tree
397 '
398
399 test_expect_success 'writing this tree with --missing-ok' '
400         git write-tree --missing-ok
401 '
402
403
404 ################################################################
405 test_expect_success 'git read-tree followed by write-tree should be idempotent' '
406         rm -f .git/index
407         git read-tree $tree &&
408         test -f .git/index &&
409         newtree=$(git write-tree) &&
410         test "$newtree" = "$tree"
411 '
412
413 test_expect_success 'validate git diff-files output for a know cache/work tree state' '
414         $expectfilter >expected <<\EOF &&
415 :100644 100644 f87290f8eb2cbbea7857214459a0739927eab154 0000000000000000000000000000000000000000 M      path0
416 :120000 120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0000000000000000000000000000000000000000 M      path0sym
417 :100644 100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0000000000000000000000000000000000000000 M      path2/file2
418 :120000 120000 d8ce161addc5173867a3c3c730924388daedbc38 0000000000000000000000000000000000000000 M      path2/file2sym
419 :100644 100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0000000000000000000000000000000000000000 M      path3/file3
420 :120000 120000 8599103969b43aff7e430efea79ca4636466794f 0000000000000000000000000000000000000000 M      path3/file3sym
421 :100644 100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0000000000000000000000000000000000000000 M      path3/subp3/file3
422 :120000 120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0000000000000000000000000000000000000000 M      path3/subp3/file3sym
423 EOF
424         git diff-files >current &&
425         test_cmp current expected
426 '
427
428 test_expect_success 'git update-index --refresh should succeed' '
429         git update-index --refresh
430 '
431
432 test_expect_success 'no diff after checkout and git update-index --refresh' '
433         git diff-files >current &&
434         cmp -s current /dev/null
435 '
436
437 ################################################################
438 P=$expectedtree
439
440 test_expect_success 'git commit-tree records the correct tree in a commit' '
441         commit0=$(echo NO | git commit-tree $P) &&
442         tree=$(git show --pretty=raw $commit0 |
443                  sed -n -e "s/^tree //p" -e "/^author /q") &&
444         test "z$tree" = "z$P"
445 '
446
447 test_expect_success 'git commit-tree records the correct parent in a commit' '
448         commit1=$(echo NO | git commit-tree $P -p $commit0) &&
449         parent=$(git show --pretty=raw $commit1 |
450                 sed -n -e "s/^parent //p" -e "/^author /q") &&
451         test "z$commit0" = "z$parent"
452 '
453
454 test_expect_success 'git commit-tree omits duplicated parent in a commit' '
455         commit2=$(echo NO | git commit-tree $P -p $commit0 -p $commit0) &&
456              parent=$(git show --pretty=raw $commit2 |
457                 sed -n -e "s/^parent //p" -e "/^author /q" |
458                 sort -u) &&
459         test "z$commit0" = "z$parent" &&
460         numparent=$(git show --pretty=raw $commit2 |
461                 sed -n -e "s/^parent //p" -e "/^author /q" |
462                 wc -l) &&
463         test $numparent = 1
464 '
465
466 test_expect_success 'update-index D/F conflict' '
467         mv path0 tmp &&
468         mv path2 path0 &&
469         mv tmp path2 &&
470         git update-index --add --replace path2 path0/file2 &&
471         numpath0=$(git ls-files path0 | wc -l) &&
472         test $numpath0 = 1
473 '
474
475 test_expect_success 'very long name in the index handled sanely' '
476
477         a=a && # 1
478         a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 16
479         a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 256
480         a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 4096
481         a=${a}q &&
482
483         >path4 &&
484         git update-index --add path4 &&
485         (
486                 git ls-files -s path4 |
487                 sed -e "s/      .*/     /" |
488                 tr -d "\012"
489                 echo "$a"
490         ) | git update-index --index-info &&
491         len=$(git ls-files "a*" | wc -c) &&
492         test $len = 4098
493 '
494
495 test_done