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`
31 export LC_ALL=C
33 export MALLOC_OPTIONS=S
35 if [ "$(date -u -r 86400 +%F 2>/dev/null)" != 1970-01-02 ]; then
36 DATECMD=
37 for p in date gdate; do
38 if [ "$($p -u -d @86400 +%F 2>/dev/null)" = 1970-01-02 ]; then
39 DATECMD=$p
40 break
41 fi
42 done
43 if [ -z "$DATECMD" ]; then
44 echo "Couldn't find gdate, is GNU coreutils installed?" >&2
45 exit 1
46 fi
48 date()
49 {
50 local flag r u
51 while getopts r:u flag; do
52 case $flag in
53 r) r=$OPTARG ;;
54 u) u=-u ;;
55 ?) exit 1 ;;
56 esac
57 done
58 shift $((OPTIND - 1))
59 command "$DATECMD" $u ${r+-d"@$r"} "$@"
60 }
61 fi
63 git_init()
64 {
65 git init -q "$1"
67 # Switch the default branch to match our test expectations if needed.
68 # Only need to change HEAD since 'git init' did not create any refs.
69 # Relying on implementation details of 'git init' is no problem for us.
70 # We want to be alerted when Git changes fundamental assumptions such
71 # as what an empty repository looks like and where the default branch
72 # is set. In such cases Got's own tooling might well need to change
73 # its behaviour, too, and our tests should fail.
74 # TODO: Update all tests to assume 'main' instead of 'master' and
75 # switch to main here, to match Got's own default.
76 echo "ref: refs/heads/master" > "$1/.git/HEAD"
77 }
79 maybe_pack_repo()
80 {
81 local repo="$1"
82 if [ -n "$GOT_TEST_PACK" ]; then
83 arg=""
84 if [ "$GOT_TEST_PACK" = "ref-delta" ]; then
85 arg="-D"
86 fi
88 (cd $repo && gotadmin pack -a $arg > /dev/null)
89 (cd $repo && gotadmin cleanup -a -q)
90 fi
91 }
93 git_commit()
94 {
95 local repo="$1"
96 shift
97 git -C $repo commit --author="$GOT_AUTHOR" -q -a "$@"
98 maybe_pack_repo $repo
99 }
101 git_rm()
103 local repo="$1"
104 shift
105 git -C $repo rm -q "$@"
108 git_rmdir()
110 local repo="$1"
111 shift
112 git -C $repo rm -q -r "$@"
115 git_show_head()
117 local repo="$1"
118 git -C $repo show --no-patch --pretty='format:%H'
121 git_show_branch_head()
123 local repo="$1"
124 local branch="$2"
125 git -C $repo show --no-patch --pretty='format:%H' $branch
129 git_show_author_time()
131 local repo="$1"
132 local object="$2"
133 git -C $repo show --no-patch --pretty='format:%at' $object
136 git_show_tagger_time()
138 local repo="$1"
139 local tag="$2"
140 git -C $repo cat-file tag $tag | grep ^tagger | \
141 sed -e "s/^tagger $GOT_AUTHOR//" | cut -d' ' -f2
144 git_show_parent_commit()
146 local repo="$1"
147 local commit="$2"
148 git -C $repo show --no-patch --pretty='format:%P' $commit
151 git_show_tree()
153 local repo="$1"
154 git -C $repo show --no-patch --pretty='format:%T'
157 trim_obj_id()
159 local trimcount=$1
160 local id=$2
162 local pat=""
163 while [ "$trimcount" -gt 0 ]; do
164 pat="[0-9a-f]$pat"
165 trimcount=$((trimcount - 1))
166 done
168 echo ${id%$pat}
171 pop_idx()
173 shift "$1"
174 printf '%s' "${1:-index-out-of-bounds}"
177 git_commit_tree()
179 local repo="$1"
180 local msg="$2"
181 local tree="$3"
182 git -C $repo commit-tree -m "$msg" "$tree"
185 git_fsck()
187 local testroot="$1"
188 local repo="$2"
190 git -C $repo fsck --strict \
191 > $testroot/fsck.stdout 2> $testroot/fsck.stderr
192 ret=$?
193 if [ $ret -ne 0 ]; then
194 echo -n "git fsck: "
195 cat $testroot/fsck.stderr
196 echo "git fsck failed; leaving test data in $testroot"
197 return 1
198 fi
200 return 0
203 make_test_tree()
205 repo="$1"
207 echo alpha > $repo/alpha
208 echo beta > $repo/beta
209 mkdir $repo/gamma
210 echo delta > $repo/gamma/delta
211 mkdir $repo/epsilon
212 echo zeta > $repo/epsilon/zeta
215 make_single_file_repo()
217 repo="$1"
218 file="$2"
220 mkdir $repo
221 git_init $repo
222 echo "this is file $file" > $repo/$file
223 git -C $repo add .
224 git_commit $repo -m "intialize $repo with file $file"
227 get_loose_object_path()
229 local repo="$1"
230 local id="$2"
231 local id0=`trim_obj_id 38 $id`
232 local idrest=`echo ${id#[0-9a-f][0-9a-f]}`
233 echo "$repo/.git/objects/$id0/$idrest"
236 get_blob_id()
238 repo="$1"
239 tree_path="$2"
240 filename="$3"
242 got tree -r $repo -i $tree_path | grep "[0-9a-f] ${filename}$" | \
243 cut -d' ' -f 1
246 test_init()
248 local testname="$1"
249 local no_tree="$2"
250 if [ -z "$testname" ]; then
251 echo "No test name provided" >&2
252 return 1
253 fi
254 local testroot=`mktemp -d \
255 "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXXXX"`
256 mkdir $testroot/repo
257 git_init $testroot/repo
258 if [ -z "$no_tree" ]; then
259 make_test_tree $testroot/repo
260 git -C $repo add .
261 git_commit $testroot/repo -m "adding the test tree"
262 fi
263 touch $testroot/repo/.git/git-daemon-export-ok
264 echo "$testroot"
267 test_cleanup()
269 local testroot="$1"
271 git_fsck $testroot $testroot/repo
272 ret=$?
273 if [ $ret -ne 0 ]; then
274 return $ret
275 fi
277 rm -rf "$testroot"
280 test_parseargs()
282 while getopts qr: flag; do
283 case $flag in
284 q) export GOT_TEST_QUIET=1
285 ;;
286 r) export GOT_TEST_ROOT=${OPTARG%/}
287 ;;
288 ?) echo "Supported options:"
289 echo " -q: quiet mode"
290 echo " -r PATH: use PATH as test data root directory"
291 exit 2
292 ;;
293 esac
294 done
295 shift $(($OPTIND - 1))
296 regress_run_only="$@"
297 } >&2
299 run_test()
301 testfunc="$1"
303 if [ -n "$regress_run_only" ]; then
304 case "$regress_run_only" in
305 *$testfunc*) ;;
306 *) return ;;
307 esac
308 fi
310 if [ -z "$GOT_TEST_QUIET" ]; then
311 echo -n "$testfunc "
312 fi
313 $testfunc
316 test_done()
318 local testroot="$1"
319 local result="$2"
320 if [ "$result" = "0" ]; then
321 test_cleanup "$testroot" || return 1
322 if [ -z "$GOT_TEST_QUIET" ]; then
323 echo "ok"
324 fi
325 elif echo "$result" | grep -q "^xfail"; then
326 # expected test failure; test reproduces an unfixed bug
327 echo "$result"
328 test_cleanup "$testroot" || return 1
329 else
330 echo "test failed; leaving test data in $testroot"
331 fi