Blob


1 #!/bin/sh
2 #
3 # Copyright (c) 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 #
5 # Permission to use, copy, modify, and distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
8 #
9 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 export GIT_AUTHOR_NAME="Flan Hacker"
18 export GIT_AUTHOR_EMAIL="flan_hacker@openbsd.org"
19 export GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME"
20 export GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL"
21 export GOT_AUTHOR="$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>"
22 export GOT_AUTHOR_8="flan_hac"
23 export GOT_AUTHOR_11="flan_hacker"
24 export GOT_LOG_DEFAULT_LIMIT=0
25 export GOT_TEST_ROOT="/tmp"
27 export MALLOC_OPTIONS=S
29 git_init()
30 {
31 git init -q "$1"
32 }
34 maybe_pack_repo()
35 {
36 local repo="$1"
37 if [ -n "$GOT_TEST_PACK" ]; then
38 (cd $repo && git repack -a -q)
39 fi
40 }
42 git_commit()
43 {
44 local repo="$1"
45 shift
46 (cd $repo && git commit --author="$GOT_AUTHOR" -q -a "$@")
47 maybe_pack_repo $repo
48 }
50 git_rm()
51 {
52 local repo="$1"
53 shift
54 (cd $repo && git rm -q "$@")
55 }
57 git_show_head()
58 {
59 local repo="$1"
60 (cd $repo && git show --no-patch --pretty='format:%H')
61 }
63 git_show_branch_head()
64 {
65 local repo="$1"
66 local branch="$2"
67 (cd $repo && git show --no-patch --pretty='format:%H' $branch)
68 }
71 git_show_author_time()
72 {
73 local repo="$1"
74 local object="$2"
75 (cd $repo && git show --no-patch --pretty='format:%at' $object)
76 }
78 git_show_tagger_time()
79 {
80 local repo="$1"
81 local tag="$2"
82 (cd $repo && git cat-file tag $tag | grep ^tagger | \
83 sed -e "s/^tagger $GOT_AUTHOR//" | cut -d' ' -f2)
84 }
86 git_show_parent_commit()
87 {
88 local repo="$1"
89 local commit="$2"
90 (cd $repo && git show --no-patch --pretty='format:%P' $commit)
91 }
93 git_show_tree()
94 {
95 local repo="$1"
96 (cd $repo && git show --no-patch --pretty='format:%T')
97 }
99 trim_obj_id()
101 local trimcount=$1
102 local id=$2
104 local pat=""
105 while [ "$trimcount" -gt 0 ]; do
106 pat="[0-9a-f]$pat"
107 trimcount=$((trimcount - 1))
108 done
110 echo ${id%$pat}
113 git_commit_tree()
115 local repo="$1"
116 local msg="$2"
117 local tree="$3"
118 (cd $repo && git commit-tree -m "$msg" "$tree")
119 maybe_pack_repo $repo
122 git_fsck()
124 local testroot="$1"
125 local repo="$2"
127 (cd $repo && git fsck --strict \
128 > $testroot/fsck.stdout 2> $testroot/fsck.stderr)
129 ret="$?"
130 if [ "$ret" != "0" ]; then
131 echo -n "git fsck: "
132 cat $testroot/fsck.stderr
133 echo "git fsck failed; leaving test data in $testroot"
134 return 1
135 fi
137 return 0
140 make_test_tree()
142 repo="$1"
144 echo alpha > $repo/alpha
145 echo beta > $repo/beta
146 mkdir $repo/gamma
147 echo delta > $repo/gamma/delta
148 mkdir $repo/epsilon
149 echo zeta > $repo/epsilon/zeta
152 make_single_file_repo()
154 repo="$1"
155 file="$2"
157 mkdir $repo
158 git_init $repo
159 echo "this is file $file" > $repo/$file
160 (cd $repo && git add .)
161 git_commit $repo -m "intialize $repo with file $file"
164 get_loose_object_path()
166 local repo="$1"
167 local id="$2"
168 local id0=`trim_obj_id 38 $id`
169 local idrest=`echo ${id#[0-9a-f][0-9a-f]}`
170 echo "$repo/.git/objects/$id0/$idrest"
173 get_blob_id()
175 repo="$1"
176 tree_path="$2"
177 filename="$3"
179 got tree -r $repo -i $tree_path | grep "[0-9a-f] ${filename}$" | \
180 cut -d' ' -f 1
183 test_init()
185 local testname="$1"
186 local no_tree="$2"
187 if [ -z "$testname" ]; then
188 echo "No test name provided" >&2
189 return 1
190 fi
191 local testroot=`mktemp -d "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXX"`
192 mkdir $testroot/repo
193 git_init $testroot/repo
194 if [ -z "$no_tree" ]; then
195 make_test_tree $testroot/repo
196 (cd $repo && git add .)
197 git_commit $testroot/repo -m "adding the test tree"
198 fi
199 touch $testroot/repo/.git/git-daemon-export-ok
200 echo "$testroot"
203 test_cleanup()
205 local testroot="$1"
207 git_fsck $testroot $testroot/repo
208 ret="$?"
209 if [ "$ret" != "0" ]; then
210 return $ret
211 fi
213 rm -rf "$testroot"
216 test_parseargs()
218 while getopts qr: flag; do
219 case $flag in
220 q) export GOT_TEST_QUIET=1
221 ;;
222 r) export GOT_TEST_ROOT=$OPTARG
223 ;;
224 ?) echo "Supported options:"
225 echo " -q: quiet mode"
226 echo " -r PATH: use PATH as test data root directory"
227 exit 2
228 ;;
229 esac
230 done
231 } >&2
233 run_test()
235 testfunc="$1"
236 if [ -z "$GOT_TEST_QUIET" ]; then
237 echo -n "$testfunc "
238 fi
239 $testfunc
242 test_done()
244 local testroot="$1"
245 local result="$2"
246 if [ "$result" = "0" ]; then
247 test_cleanup "$testroot" || return 1
248 if [ -z "$GOT_TEST_QUIET" ]; then
249 echo "ok"
250 fi
251 elif echo "$result" | grep -q "^xfail"; then
252 # expected test failure; test reproduces an unfixed bug
253 echo "$result"
254 test_cleanup "$testroot" || return 1
255 else
256 echo "test failed; leaving test data in $testroot"
257 fi