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 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=8080
32 export LC_ALL=C
34 export MALLOC_OPTIONS=S
36 if [ "$(date -u -r 86400 +%F 2>/dev/null)" != 1970-01-02 ]; then
37 DATECMD=
38 for p in date gdate; do
39 if [ "$($p -u -d @86400 +%F 2>/dev/null)" = 1970-01-02 ]; then
40 DATECMD=$p
41 break
42 fi
43 done
44 if [ -z "$DATECMD" ]; then
45 echo "Couldn't find gdate, is GNU coreutils installed?" >&2
46 exit 1
47 fi
49 date()
50 {
51 local flag r u
52 while getopts r:u flag; do
53 case $flag in
54 r) r=$OPTARG ;;
55 u) u=-u ;;
56 ?) exit 1 ;;
57 esac
58 done
59 shift $((OPTIND - 1))
60 command "$DATECMD" $u ${r+-d"@$r"} "$@"
61 }
62 fi
64 git_init()
65 {
66 git init -q "$1"
68 # Switch the default branch to match our test expectations if needed.
69 # Only need to change HEAD since 'git init' did not create any refs.
70 # Relying on implementation details of 'git init' is no problem for us.
71 # We want to be alerted when Git changes fundamental assumptions such
72 # as what an empty repository looks like and where the default branch
73 # is set. In such cases Got's own tooling might well need to change
74 # its behaviour, too, and our tests should fail.
75 # TODO: Update all tests to assume 'main' instead of 'master' and
76 # switch to main here, to match Got's own default.
77 echo "ref: refs/heads/master" > "$1/.git/HEAD"
78 }
80 maybe_pack_repo()
81 {
82 local repo="$1"
83 if [ -n "$GOT_TEST_PACK" ]; then
84 arg=""
85 if [ "$GOT_TEST_PACK" = "ref-delta" ]; then
86 arg="-D"
87 fi
89 (cd $repo && gotadmin pack -a $arg > /dev/null)
90 (cd $repo && gotadmin cleanup -a -q)
91 fi
92 }
94 git_commit()
95 {
96 local repo="$1"
97 shift
98 git -C $repo commit --author="$GOT_AUTHOR" -q -a "$@"
99 maybe_pack_repo $repo
102 git_rm()
104 local repo="$1"
105 shift
106 git -C $repo rm -q "$@"
109 git_rmdir()
111 local repo="$1"
112 shift
113 git -C $repo rm -q -r "$@"
116 git_show_head()
118 local repo="$1"
119 git -C $repo show --no-patch --pretty='format:%H'
122 git_show_branch_head()
124 local repo="$1"
125 local branch="$2"
126 git -C $repo show --no-patch --pretty='format:%H' $branch
130 git_show_author_time()
132 local repo="$1"
133 local object="$2"
134 git -C $repo show --no-patch --pretty='format:%at' $object
137 git_show_tagger_time()
139 local repo="$1"
140 local tag="$2"
141 git -C $repo cat-file tag $tag | grep ^tagger | \
142 sed -e "s/^tagger $GOT_AUTHOR//" | cut -d' ' -f2
145 git_show_parent_commit()
147 local repo="$1"
148 local commit="$2"
149 git -C $repo show --no-patch --pretty='format:%P' $commit
152 git_show_tree()
154 local repo="$1"
155 git -C $repo show --no-patch --pretty='format:%T'
158 trim_obj_id()
160 local trimcount=$1
161 local id=$2
163 local pat=""
164 while [ "$trimcount" -gt 0 ]; do
165 pat="[0-9a-f]$pat"
166 trimcount=$((trimcount - 1))
167 done
169 echo ${id%$pat}
172 pop_idx()
174 shift "$1"
175 printf '%s' "${1:-index-out-of-bounds}"
178 git_commit_tree()
180 local repo="$1"
181 local msg="$2"
182 local tree="$3"
183 git -C $repo commit-tree -m "$msg" "$tree"
186 git_fsck()
188 local testroot="$1"
189 local repo="$2"
191 git -C $repo fsck --strict \
192 > $testroot/fsck.stdout 2> $testroot/fsck.stderr
193 ret=$?
194 if [ $ret -ne 0 ]; then
195 echo -n "git fsck: "
196 cat $testroot/fsck.stderr
197 echo "git fsck failed; leaving test data in $testroot"
198 return 1
199 fi
201 return 0
204 make_test_tree()
206 repo="$1"
208 echo alpha > $repo/alpha
209 echo beta > $repo/beta
210 mkdir $repo/gamma
211 echo delta > $repo/gamma/delta
212 mkdir $repo/epsilon
213 echo zeta > $repo/epsilon/zeta
216 make_single_file_repo()
218 repo="$1"
219 file="$2"
221 mkdir $repo
222 git_init $repo
223 echo "this is file $file" > $repo/$file
224 git -C $repo add .
225 git_commit $repo -m "intialize $repo with file $file"
228 get_loose_object_path()
230 local repo="$1"
231 local id="$2"
232 local id0=`trim_obj_id 38 $id`
233 local idrest=`echo ${id#[0-9a-f][0-9a-f]}`
234 echo "$repo/.git/objects/$id0/$idrest"
237 get_blob_id()
239 repo="$1"
240 tree_path="$2"
241 filename="$3"
243 got tree -r $repo -i $tree_path | grep "[0-9a-f] ${filename}$" | \
244 cut -d' ' -f 1
247 test_init()
249 local testname="$1"
250 local no_tree="$2"
251 if [ -z "$testname" ]; then
252 echo "No test name provided" >&2
253 return 1
254 fi
255 local testroot=`mktemp -d \
256 "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXXXX"`
257 mkdir $testroot/repo
258 git_init $testroot/repo
259 if [ -z "$no_tree" ]; then
260 make_test_tree $testroot/repo
261 git -C $repo add .
262 git_commit $testroot/repo -m "adding the test tree"
263 fi
264 touch $testroot/repo/.git/git-daemon-export-ok
265 echo "$testroot"
268 test_cleanup()
270 local testroot="$1"
272 git_fsck $testroot $testroot/repo
273 ret=$?
274 if [ $ret -ne 0 ]; then
275 return $ret
276 fi
278 rm -rf "$testroot"
281 test_parseargs()
283 while getopts qr: flag; do
284 case $flag in
285 q) export GOT_TEST_QUIET=1
286 ;;
287 r) export GOT_TEST_ROOT=${OPTARG%/}
288 ;;
289 ?) echo "Supported options:"
290 echo " -q: quiet mode"
291 echo " -r PATH: use PATH as test data root directory"
292 exit 2
293 ;;
294 esac
295 done
296 shift $(($OPTIND - 1))
297 regress_run_only="$@"
298 } >&2
300 run_test()
302 testfunc="$1"
304 if [ -n "$regress_run_only" ]; then
305 case "$regress_run_only" in
306 *$testfunc*) ;;
307 *) return ;;
308 esac
309 fi
311 if [ -z "$GOT_TEST_QUIET" ]; then
312 echo -n "$testfunc "
313 fi
314 $testfunc
317 test_done()
319 local testroot="$1"
320 local result="$2"
321 if [ "$result" = "0" ]; then
322 test_cleanup "$testroot" || return 1
323 if [ -z "$GOT_TEST_QUIET" ]; then
324 echo "ok"
325 fi
326 elif echo "$result" | grep -q "^xfail"; then
327 # expected test failure; test reproduces an unfixed bug
328 echo "$result"
329 test_cleanup "$testroot" || return 1
330 else
331 echo "test failed; leaving test data in $testroot"
332 fi