Add tests for git cat-file
Signed-off-by: Adam Roben <aroben@apple.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Adam Roben committed
Apr 23, 2008 at 15:17 UTC
b335d3f121536733fff3b71d4270c74a9d6e9e91
1 file changed
+121
t/t1006-cat-file.sh
new
+121
@@ -0,0 +1,121 @@
1
+#!/bin/sh
2
+
3
+test_description='git cat-file'
4
+
5
+. ./test-lib.sh
6
+
7
+echo_without_newline () {
8
+ printf '%s' "$*"
9
+}
10
+
11
+strlen () {
12
+ echo_without_newline "$1" | wc -c | sed -e 's/^ *//'
13
+}
14
+
15
+maybe_remove_timestamp () {
16
+ if test -z "$2"; then
17
+ echo_without_newline "$1"
18
+ else
19
+ echo_without_newline "$(printf '%s\n' "$1" | sed -e 's/ [0-9][0-9]* [-+][0-9][0-9][0-9][0-9]$//')"
20
+ fi
21
+}
22
+
23
+run_tests () {
24
+ type=$1
25
+ sha1=$2
26
+ size=$3
27
+ content=$4
28
+ pretty_content=$5
29
+ no_ts=$6
30
+
31
+ test_expect_success "$type exists" '
32
+ git cat-file -e $sha1
33
+ '
34
+
35
+ test_expect_success "Type of $type is correct" '
36
+ test $type = "$(git cat-file -t $sha1)"
37
+ '
38
+
39
+ test_expect_success "Size of $type is correct" '
40
+ test $size = "$(git cat-file -s $sha1)"
41
+ '
42
+
43
+ test -z "$content" ||
44
+ test_expect_success "Content of $type is correct" '
45
+ expect="$(maybe_remove_timestamp "$content" $no_ts)"
46
+ actual="$(maybe_remove_timestamp "$(git cat-file $type $sha1)" $no_ts)"
47
+
48
+ if test "z$expect" = "z$actual"
49
+ then
50
+ : happy
51
+ else
52
+ echo "Oops: expected $expect"
53
+ echo "but got $actual"
54
+ false
55
+ fi
56
+ '
57
+
58
+ test_expect_success "Pretty content of $type is correct" '
59
+ expect="$(maybe_remove_timestamp "$pretty_content" $no_ts)"
60
+ actual="$(maybe_remove_timestamp "$(git cat-file -p $sha1)" $no_ts)"
61
+ if test "z$expect" = "z$actual"
62
+ then
63
+ : happy
64
+ else
65
+ echo "Oops: expected $expect"
66
+ echo "but got $actual"
67
+ false
68
+ fi
69
+ '
70
+}
71
+
72
+hello_content="Hello World"
73
+hello_size=$(strlen "$hello_content")
74
+hello_sha1=$(echo_without_newline "$hello_content" | git hash-object --stdin)
75
+
76
+test_expect_success "setup" '
77
+ echo_without_newline "$hello_content" > hello &&
78
+ git update-index --add hello
79
+'
80
+
81
+run_tests 'blob' $hello_sha1 $hello_size "$hello_content" "$hello_content"
82
+
83
+tree_sha1=$(git write-tree)
84
+tree_size=33
85
+tree_pretty_content="100644 blob $hello_sha1 hello"
86
+
87
+run_tests 'tree' $tree_sha1 $tree_size "" "$tree_pretty_content"
88
+
89
+commit_message="Intial commit"
90
+commit_sha1=$(echo_without_newline "$commit_message" | git commit-tree $tree_sha1)
91
+commit_size=176
92
+commit_content="tree $tree_sha1
93
+author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> 0000000000 +0000
94
+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 0000000000 +0000
95
+
96
+$commit_message"
97
+
98
+run_tests 'commit' $commit_sha1 $commit_size "$commit_content" "$commit_content" 1
99
+
100
+tag_header_without_timestamp="object $hello_sha1
101
+type blob
102
+tag hellotag
103
+tagger $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
104
+tag_description="This is a tag"
105
+tag_content="$tag_header_without_timestamp 0000000000 +0000
106
+
107
+$tag_description"
108
+tag_pretty_content="$tag_header_without_timestamp Thu Jan 1 00:00:00 1970 +0000
109
+
110
+$tag_description"
111
+
112
+tag_sha1=$(echo_without_newline "$tag_content" | git mktag)
113
+tag_size=$(strlen "$tag_content")
114
+
115
+run_tests 'tag' $tag_sha1 $tag_size "$tag_content" "$tag_pretty_content" 1
116
+
117
+test_expect_success \
118
+ "Reach a blob from a tag pointing to it" \
119
+ "test '$hello_content' = \"\$(git cat-file blob $tag_sha1)\""
120
+
121
+test_done