make got-read-object clear its imsgbuf before exit in an error case
[got-portable.git] / regress / cmdline / common.sh
blobe606991123823035db24ea363dd8e9e0250df81b
1 #!/bin/sh
3 # Copyright (c) 2019, 2020 Stefan Sperling <stsp@openbsd.org>
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.
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 regress_run_only=""
19 export GIT_AUTHOR_NAME="Flan Hacker"
20 export GIT_AUTHOR_EMAIL="flan_hacker@openbsd.org"
21 export GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME"
22 export GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL"
23 export GOT_AUTHOR="$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>"
24 export GOT_AUTHOR_8="flan_hac"
25 export GOT_AUTHOR_11="flan_hacker"
26 export GOT_LOG_DEFAULT_LIMIT=0
27 export GOT_TEST_ROOT="/tmp"
28 export GOT_IGNORE_GITCONFIG=1
29 export GOT_VERSION_STR=`got --version | cut -d ' ' -f2`
30 export GOT_TEST_HTTP_PORT=${GOT_TEST_HTTP_PORT:-8080}
31 export GOT_TEST_ALGO="${GOT_TEST_ALGO:-sha1}"
33 export LC_ALL=C
35 if [ -n "$MEM_CHECK" ] && [ "$MEM_CHECK" -gt 0 ] && [ "$MEM_CHECK" -lt 5 ]; then
36 export MALLOC_OPTIONS="$MEM_CHECK"S
37 else
38 export MALLOC_OPTIONS=S
41 if [ "$(date -u -r 86400 +%F 2>/dev/null)" != 1970-01-02 ]; then
42 DATECMD=
43 for p in date gdate; do
44 if [ "$($p -u -d @86400 +%F 2>/dev/null)" = 1970-01-02 ]; then
45 DATECMD=$p
46 break
48 done
49 if [ -z "$DATECMD" ]; then
50 echo "Couldn't find gdate, is GNU coreutils installed?" >&2
51 exit 1
54 date()
56 local flag r u
57 while getopts r:u flag; do
58 case $flag in
59 r) r=$OPTARG ;;
60 u) u=-u ;;
61 ?) exit 1 ;;
62 esac
63 done
64 shift $((OPTIND - 1))
65 command "$DATECMD" $u ${r+-d"@$r"} "$@"
69 git_init()
71 git init -q --object-format=${GOT_TEST_ALGO} "$1"
73 # Switch the default branch to match our test expectations if needed.
74 # Only need to change HEAD since 'git init' did not create any refs.
75 # Relying on implementation details of 'git init' is no problem for us.
76 # We want to be alerted when Git changes fundamental assumptions such
77 # as what an empty repository looks like and where the default branch
78 # is set. In such cases Got's own tooling might well need to change
79 # its behaviour, too, and our tests should fail.
80 # TODO: Update all tests to assume 'main' instead of 'master' and
81 # switch to main here, to match Got's own default.
82 echo "ref: refs/heads/master" > "$1/.git/HEAD"
85 maybe_pack_repo()
87 local repo="$1"
88 if [ -n "$GOT_TEST_PACK" ]; then
89 arg=""
90 if [ "$GOT_TEST_PACK" = "ref-delta" ]; then
91 arg="-D"
94 gotadmin pack -r "$repo" -a $arg > /dev/null
95 gotadmin cleanup -r "$repo" -a -q
99 git_commit()
101 local repo="$1"
102 shift
103 git -C $repo commit --author="$GOT_AUTHOR" -q -a "$@"
104 maybe_pack_repo $repo
107 git_rm()
109 local repo="$1"
110 shift
111 git -C $repo rm -q "$@"
114 git_rmdir()
116 local repo="$1"
117 shift
118 git -C $repo rm -q -r "$@"
121 git_show_head()
123 local repo="$1"
124 git -C $repo show --no-patch --pretty='format:%H'
127 git_show_branch_head()
129 local repo="$1"
130 local branch="$2"
131 git -C $repo show --no-patch --pretty='format:%H' $branch
135 git_show_author_time()
137 local repo="$1"
138 local object="$2"
139 git -C $repo show --no-patch --pretty='format:%at' $object
142 git_show_tagger_time()
144 local repo="$1"
145 local tag="$2"
146 git -C $repo cat-file tag $tag | grep ^tagger | \
147 sed -e "s/^tagger $GOT_AUTHOR//" | cut -d' ' -f2
150 git_show_parent_commit()
152 local repo="$1"
153 local commit="$2"
154 git -C $repo show --no-patch --pretty='format:%P' $commit
157 git_show_tree()
159 local repo="$1"
160 git -C $repo show --no-patch --pretty='format:%T'
163 trim_obj_id()
165 expr "$2" : "\([0-9a-f]\{1,$1\}\)"
168 pop_idx()
170 shift "$1"
171 printf '%s' "${1:-index-out-of-bounds}"
174 git_commit_tree()
176 local repo="$1"
177 local msg="$2"
178 local tree="$3"
179 git -C $repo commit-tree -m "$msg" "$tree"
182 git_fsck()
184 local testroot="$1"
185 local repo="$2"
187 git -C $repo fsck --strict \
188 > $testroot/fsck.stdout 2> $testroot/fsck.stderr
189 ret=$?
190 if [ $ret -ne 0 ]; then
191 echo -n "git fsck: "
192 cat $testroot/fsck.stderr
193 echo "git fsck failed; leaving test data in $testroot"
194 return 1
197 return 0
200 make_test_tree()
202 repo="$1"
204 echo alpha > $repo/alpha
205 echo beta > $repo/beta
206 mkdir $repo/gamma
207 echo delta > $repo/gamma/delta
208 mkdir $repo/epsilon
209 echo zeta > $repo/epsilon/zeta
212 make_single_file_repo()
214 repo="$1"
215 file="$2"
217 mkdir $repo
218 git_init $repo
219 echo "this is file $file" > $repo/$file
220 git -C $repo add .
221 git_commit $repo -m "initialize $repo with file $file"
224 get_loose_object_path()
226 local repo="$1"
227 local id="$2"
228 local id0=`trim_obj_id 2 $id`
229 local idrest=`echo ${id#[0-9a-f][0-9a-f]}`
230 echo "$repo/.git/objects/$id0/$idrest"
233 get_blob_id()
235 repo="$1"
236 tree_path="$2"
237 filename="$3"
239 got tree -r $repo -i $tree_path | grep "[0-9a-f] ${filename}$" | \
240 cut -d' ' -f 1
243 test_init()
245 local testname="$1"
246 local no_tree="$2"
247 if [ -z "$testname" ]; then
248 echo "No test name provided" >&2
249 return 1
251 local testroot=`mktemp -d \
252 "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXXXX"`
253 mkdir $testroot/repo
254 git_init $testroot/repo
255 if [ -z "$no_tree" ]; then
256 make_test_tree $testroot/repo
257 git -C $repo add .
258 git_commit $testroot/repo -m "adding the test tree"
260 touch $testroot/repo/.git/git-daemon-export-ok
261 echo "$testroot"
264 test_cleanup()
266 local testroot="$1"
268 git_fsck $testroot $testroot/repo
269 ret=$?
270 if [ $ret -ne 0 ]; then
271 return $ret
274 rm -rf "$testroot"
277 test_parseargs()
279 while getopts qr: flag; do
280 case $flag in
281 q) export GOT_TEST_QUIET=1
283 r) export GOT_TEST_ROOT=${OPTARG%/}
285 ?) echo "Supported options:"
286 echo " -q: quiet mode"
287 echo " -r PATH: use PATH as test data root directory"
288 exit 2
290 esac
291 done
292 shift $(($OPTIND - 1))
293 regress_run_only="$@"
294 } >&2
296 run_test()
298 testfunc="$1"
299 limits="$2"
301 if [ -n "$regress_run_only" ]; then
302 case "$regress_run_only" in
303 *$testfunc) ;;
304 *) return ;;
305 esac
308 if [ "${GOT_TEST_ALGO}" = sha256 -a "$limits" = no-sha256 ]; then
309 return
312 if [ -z "$GOT_TEST_QUIET" ]; then
313 echo -n "$testfunc "
315 $testfunc
318 test_done()
320 local testroot="$1"
321 local result="$2"
322 if [ "$result" = "0" ]; then
323 test_cleanup "$testroot" || return 1
324 if [ -z "$GOT_TEST_QUIET" ]; then
325 echo "ok"
327 elif echo "$result" | grep -q "^xfail"; then
328 # expected test failure; test reproduces an unfixed bug
329 echo "$result"
330 test_cleanup "$testroot" || return 1
331 else
332 echo "test failed; leaving test data in $testroot"
336 test_memleak_done()
338 local testroot="$1"
339 local result="$2"
341 kdump -u malloc -f $testroot/ktrace.out > $testroot/leak-report
342 if egrep -q "( got 0x|/bin/got-)" $testroot/leak-report; then
343 cat $testroot/leak-report
344 result=1
347 test_done "$testroot" "$result"
350 check_memleak()
352 local testroot="$1"
354 if [ -z "$1" ]; then
355 echo "Testroot not passed"
356 exit 1
359 if [ ! -d "$testroot" ]; then
360 echo "Directory does not exist"
361 exit 1
364 echo "ktrace -d -tu -i -f $testroot/ktrace.out"