t5004: resurrect original empty tar archive test
[alt-git.git] / t / t5004-archive-corner-cases.sh
blobf25f06b02209fbf72b9022a21d8ab9a90cb6628b
1 #!/bin/sh
3 test_description='test corner cases of git-archive'
4 . ./test-lib.sh
6 test_expect_success 'create commit with empty tree' '
7 git commit --allow-empty -m foo
10 # Make a dir and clean it up afterwards
11 make_dir() {
12 mkdir "$1" &&
13 test_when_finished "rm -rf '$1'"
16 # Check that the dir given in "$1" contains exactly the
17 # set of paths given as arguments.
18 check_dir() {
19 dir=$1; shift
21 echo "$dir" &&
22 for i in "$@"; do
23 echo "$dir/$i"
24 done
25 } | sort >expect &&
26 find "$dir" ! -name pax_global_header -print | sort >actual &&
27 test_cmp expect actual
30 # bsdtar/libarchive versions before 3.1.3 consider a tar file with a
31 # global pax header that is not followed by a file record as corrupt.
32 if "$TAR" tf "$TEST_DIRECTORY"/t5004/empty-with-pax-header.tar >/dev/null 2>&1
33 then
34 test_set_prereq HEADER_ONLY_TAR_OK
37 test_expect_success HEADER_ONLY_TAR_OK 'tar archive of commit with empty tree' '
38 git archive --format=tar HEAD >empty-with-pax-header.tar &&
39 make_dir extract &&
40 "$TAR" xf empty-with-pax-header.tar -C extract &&
41 check_dir extract
44 test_expect_success 'tar archive of empty tree is empty' '
45 git archive --format=tar HEAD: >empty.tar &&
46 perl -e "print \"\\0\" x 10240" >10knuls.tar &&
47 test_cmp 10knuls.tar empty.tar
50 test_expect_success 'tar archive of empty tree with prefix' '
51 git archive --format=tar --prefix=foo/ HEAD >prefix.tar &&
52 make_dir extract &&
53 "$TAR" xf prefix.tar -C extract &&
54 check_dir extract foo
57 test_expect_success UNZIP 'zip archive of empty tree is empty' '
58 # Detect the exit code produced when our particular flavor of unzip
59 # sees an empty archive. Infozip will generate a warning and exit with
60 # code 1. But in the name of sanity, we do not expect other unzip
61 # implementations to do the same thing (it would be perfectly
62 # reasonable to exit 0, for example).
64 # This makes our test less rigorous on some platforms (unzip may not
65 # handle the empty repo at all, making our later check of its exit code
66 # a no-op). But we cannot do anything reasonable except skip the test
67 # on such platforms anyway, and this is the moral equivalent.
68 "$GIT_UNZIP" "$TEST_DIRECTORY"/t5004/empty.zip
69 expect_code=$?
71 git archive --format=zip HEAD >empty.zip &&
72 make_dir extract &&
74 cd extract &&
75 test_expect_code $expect_code "$GIT_UNZIP" ../empty.zip
76 ) &&
77 check_dir extract
80 test_expect_success UNZIP 'zip archive of empty tree with prefix' '
81 # We do not have to play exit-code tricks here, because our
82 # result should not be empty; it has a directory in it.
83 git archive --format=zip --prefix=foo/ HEAD >prefix.zip &&
84 make_dir extract &&
86 cd extract &&
87 "$GIT_UNZIP" ../prefix.zip
88 ) &&
89 check_dir extract foo
92 test_expect_success 'archive complains about pathspec on empty tree' '
93 test_must_fail git archive --format=tar HEAD -- foo >/dev/null
96 test_expect_success 'create a commit with an empty subtree' '
97 empty_tree=$(git hash-object -t tree /dev/null) &&
98 root_tree=$(printf "040000 tree $empty_tree\tsub\n" | git mktree)
101 test_expect_success 'archive empty subtree with no pathspec' '
102 git archive --format=tar $root_tree >subtree-all.tar &&
103 make_dir extract &&
104 "$TAR" xf subtree-all.tar -C extract &&
105 check_dir extract sub
108 test_expect_success 'archive empty subtree by direct pathspec' '
109 git archive --format=tar $root_tree -- sub >subtree-path.tar &&
110 make_dir extract &&
111 "$TAR" xf subtree-path.tar -C extract &&
112 check_dir extract sub
115 test_done