match individual test names provided on command line more restrictively
[got-portable.git] / regress / cmdline / common.sh
blob846927bed95156d34df3b653c51fcc395c8cd38a
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 export MALLOC_OPTIONS=S
37 if [ "$(date -u -r 86400 +%F 2>/dev/null)" != 1970-01-02 ]; then
38 DATECMD=
39 for p in date gdate; do
40 if [ "$($p -u -d @86400 +%F 2>/dev/null)" = 1970-01-02 ]; then
41 DATECMD=$p
42 break
44 done
45 if [ -z "$DATECMD" ]; then
46 echo "Couldn't find gdate, is GNU coreutils installed?" >&2
47 exit 1
50 date()
52 local flag r u
53 while getopts r:u flag; do
54 case $flag in
55 r) r=$OPTARG ;;
56 u) u=-u ;;
57 ?) exit 1 ;;
58 esac
59 done
60 shift $((OPTIND - 1))
61 command "$DATECMD" $u ${r+-d"@$r"} "$@"
65 git_init()
67 git init -q --object-format=${GOT_TEST_ALGO} "$1"
69 # Switch the default branch to match our test expectations if needed.
70 # Only need to change HEAD since 'git init' did not create any refs.
71 # Relying on implementation details of 'git init' is no problem for us.
72 # We want to be alerted when Git changes fundamental assumptions such
73 # as what an empty repository looks like and where the default branch
74 # is set. In such cases Got's own tooling might well need to change
75 # its behaviour, too, and our tests should fail.
76 # TODO: Update all tests to assume 'main' instead of 'master' and
77 # switch to main here, to match Got's own default.
78 echo "ref: refs/heads/master" > "$1/.git/HEAD"
81 maybe_pack_repo()
83 local repo="$1"
84 if [ -n "$GOT_TEST_PACK" ]; then
85 arg=""
86 if [ "$GOT_TEST_PACK" = "ref-delta" ]; then
87 arg="-D"
90 gotadmin pack -r "$repo" -a $arg > /dev/null
91 gotadmin cleanup -r "$repo" -a -q
95 git_commit()
97 local repo="$1"
98 shift
99 git -C $repo commit --author="$GOT_AUTHOR" -q -a "$@"
100 maybe_pack_repo $repo
103 git_rm()
105 local repo="$1"
106 shift
107 git -C $repo rm -q "$@"
110 git_rmdir()
112 local repo="$1"
113 shift
114 git -C $repo rm -q -r "$@"
117 git_show_head()
119 local repo="$1"
120 git -C $repo show --no-patch --pretty='format:%H'
123 git_show_branch_head()
125 local repo="$1"
126 local branch="$2"
127 git -C $repo show --no-patch --pretty='format:%H' $branch
131 git_show_author_time()
133 local repo="$1"
134 local object="$2"
135 git -C $repo show --no-patch --pretty='format:%at' $object
138 git_show_tagger_time()
140 local repo="$1"
141 local tag="$2"
142 git -C $repo cat-file tag $tag | grep ^tagger | \
143 sed -e "s/^tagger $GOT_AUTHOR//" | cut -d' ' -f2
146 git_show_parent_commit()
148 local repo="$1"
149 local commit="$2"
150 git -C $repo show --no-patch --pretty='format:%P' $commit
153 git_show_tree()
155 local repo="$1"
156 git -C $repo show --no-patch --pretty='format:%T'
159 trim_obj_id()
161 expr "$2" : "\([0-9a-f]\{1,$1\}\)"
164 pop_idx()
166 shift "$1"
167 printf '%s' "${1:-index-out-of-bounds}"
170 git_commit_tree()
172 local repo="$1"
173 local msg="$2"
174 local tree="$3"
175 git -C $repo commit-tree -m "$msg" "$tree"
178 git_fsck()
180 local testroot="$1"
181 local repo="$2"
183 git -C $repo fsck --strict \
184 > $testroot/fsck.stdout 2> $testroot/fsck.stderr
185 ret=$?
186 if [ $ret -ne 0 ]; then
187 echo -n "git fsck: "
188 cat $testroot/fsck.stderr
189 echo "git fsck failed; leaving test data in $testroot"
190 return 1
193 return 0
196 make_test_tree()
198 repo="$1"
200 echo alpha > $repo/alpha
201 echo beta > $repo/beta
202 mkdir $repo/gamma
203 echo delta > $repo/gamma/delta
204 mkdir $repo/epsilon
205 echo zeta > $repo/epsilon/zeta
208 make_single_file_repo()
210 repo="$1"
211 file="$2"
213 mkdir $repo
214 git_init $repo
215 echo "this is file $file" > $repo/$file
216 git -C $repo add .
217 git_commit $repo -m "initialize $repo with file $file"
220 get_loose_object_path()
222 local repo="$1"
223 local id="$2"
224 local id0=`trim_obj_id 2 $id`
225 local idrest=`echo ${id#[0-9a-f][0-9a-f]}`
226 echo "$repo/.git/objects/$id0/$idrest"
229 get_blob_id()
231 repo="$1"
232 tree_path="$2"
233 filename="$3"
235 got tree -r $repo -i $tree_path | grep "[0-9a-f] ${filename}$" | \
236 cut -d' ' -f 1
239 test_init()
241 local testname="$1"
242 local no_tree="$2"
243 if [ -z "$testname" ]; then
244 echo "No test name provided" >&2
245 return 1
247 local testroot=`mktemp -d \
248 "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXXXX"`
249 mkdir $testroot/repo
250 git_init $testroot/repo
251 if [ -z "$no_tree" ]; then
252 make_test_tree $testroot/repo
253 git -C $repo add .
254 git_commit $testroot/repo -m "adding the test tree"
256 touch $testroot/repo/.git/git-daemon-export-ok
257 echo "$testroot"
260 test_cleanup()
262 local testroot="$1"
264 git_fsck $testroot $testroot/repo
265 ret=$?
266 if [ $ret -ne 0 ]; then
267 return $ret
270 rm -rf "$testroot"
273 test_parseargs()
275 while getopts qr: flag; do
276 case $flag in
277 q) export GOT_TEST_QUIET=1
279 r) export GOT_TEST_ROOT=${OPTARG%/}
281 ?) echo "Supported options:"
282 echo " -q: quiet mode"
283 echo " -r PATH: use PATH as test data root directory"
284 exit 2
286 esac
287 done
288 shift $(($OPTIND - 1))
289 regress_run_only="$@"
290 } >&2
292 run_test()
294 testfunc="$1"
295 limits="$2"
297 if [ -n "$regress_run_only" ]; then
298 case "$regress_run_only" in
299 *$testfunc) ;;
300 *) return ;;
301 esac
304 if [ "${GOT_TEST_ALGO}" = sha256 -a "$limits" = no-sha256 ]; then
305 return
308 if [ -z "$GOT_TEST_QUIET" ]; then
309 echo -n "$testfunc "
311 $testfunc
314 test_done()
316 local testroot="$1"
317 local result="$2"
318 if [ "$result" = "0" ]; then
319 test_cleanup "$testroot" || return 1
320 if [ -z "$GOT_TEST_QUIET" ]; then
321 echo "ok"
323 elif echo "$result" | grep -q "^xfail"; then
324 # expected test failure; test reproduces an unfixed bug
325 echo "$result"
326 test_cleanup "$testroot" || return 1
327 else
328 echo "test failed; leaving test data in $testroot"