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 (cd $repo && git commit --author="$GOT_AUTHOR" -q -a "$@")
98 maybe_pack_repo $repo
99 }
101 git_rm()
103 local repo="$1"
104 shift
105 (cd $repo && git rm -q "$@")
108 git_rmdir()
110 local repo="$1"
111 shift
112 (cd $repo && git rm -q -r "$@")
115 git_show_head()
117 local repo="$1"
118 (cd $repo && git show --no-patch --pretty='format:%H')
121 git_show_branch_head()
123 local repo="$1"
124 local branch="$2"
125 (cd $repo && git show --no-patch --pretty='format:%H' $branch)
129 git_show_author_time()
131 local repo="$1"
132 local object="$2"
133 (cd $repo && git show --no-patch --pretty='format:%at' $object)
136 git_show_tagger_time()
138 local repo="$1"
139 local tag="$2"
140 (cd $repo && git 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 (cd $repo && git show --no-patch --pretty='format:%P' $commit)
151 git_show_tree()
153 local repo="$1"
154 (cd $repo && git 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 git_commit_tree()
173 local repo="$1"
174 local msg="$2"
175 local tree="$3"
176 (cd $repo && git commit-tree -m "$msg" "$tree")
179 git_fsck()
181 local testroot="$1"
182 local repo="$2"
184 (cd $repo && git fsck --strict \
185 > $testroot/fsck.stdout 2> $testroot/fsck.stderr)
186 ret=$?
187 if [ $ret -ne 0 ]; then
188 echo -n "git fsck: "
189 cat $testroot/fsck.stderr
190 echo "git fsck failed; leaving test data in $testroot"
191 return 1
192 fi
194 return 0
197 make_test_tree()
199 repo="$1"
201 echo alpha > $repo/alpha
202 echo beta > $repo/beta
203 mkdir $repo/gamma
204 echo delta > $repo/gamma/delta
205 mkdir $repo/epsilon
206 echo zeta > $repo/epsilon/zeta
209 make_single_file_repo()
211 repo="$1"
212 file="$2"
214 mkdir $repo
215 git_init $repo
216 echo "this is file $file" > $repo/$file
217 (cd $repo && git add .)
218 git_commit $repo -m "intialize $repo with file $file"
221 get_loose_object_path()
223 local repo="$1"
224 local id="$2"
225 local id0=`trim_obj_id 38 $id`
226 local idrest=`echo ${id#[0-9a-f][0-9a-f]}`
227 echo "$repo/.git/objects/$id0/$idrest"
230 get_blob_id()
232 repo="$1"
233 tree_path="$2"
234 filename="$3"
236 got tree -r $repo -i $tree_path | grep "[0-9a-f] ${filename}$" | \
237 cut -d' ' -f 1
240 test_init()
242 local testname="$1"
243 local no_tree="$2"
244 if [ -z "$testname" ]; then
245 echo "No test name provided" >&2
246 return 1
247 fi
248 local testroot=`mktemp -d \
249 "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXXXX"`
250 mkdir $testroot/repo
251 git_init $testroot/repo
252 if [ -z "$no_tree" ]; then
253 make_test_tree $testroot/repo
254 (cd $repo && git add .)
255 git_commit $testroot/repo -m "adding the test tree"
256 fi
257 touch $testroot/repo/.git/git-daemon-export-ok
258 echo "$testroot"
261 test_cleanup()
263 local testroot="$1"
265 git_fsck $testroot $testroot/repo
266 ret=$?
267 if [ $ret -ne 0 ]; then
268 return $ret
269 fi
271 rm -rf "$testroot"
274 test_parseargs()
276 while getopts qr: flag; do
277 case $flag in
278 q) export GOT_TEST_QUIET=1
279 ;;
280 r) export GOT_TEST_ROOT=${OPTARG%/}
281 ;;
282 ?) echo "Supported options:"
283 echo " -q: quiet mode"
284 echo " -r PATH: use PATH as test data root directory"
285 exit 2
286 ;;
287 esac
288 done
289 shift $(($OPTIND - 1))
290 regress_run_only="$@"
291 } >&2
293 run_test()
295 testfunc="$1"
297 if [ -n "$regress_run_only" ]; then
298 case "$regress_run_only" in
299 *$testfunc*) ;;
300 *) return ;;
301 esac
302 fi
304 if [ -z "$GOT_TEST_QUIET" ]; then
305 echo -n "$testfunc "
306 fi
307 $testfunc
310 test_done()
312 local testroot="$1"
313 local result="$2"
314 if [ "$result" = "0" ]; then
315 test_cleanup "$testroot" || return 1
316 if [ -z "$GOT_TEST_QUIET" ]; then
317 echo "ok"
318 fi
319 elif echo "$result" | grep -q "^xfail"; then
320 # expected test failure; test reproduces an unfixed bug
321 echo "$result"
322 test_cleanup "$testroot" || return 1
323 else
324 echo "test failed; leaving test data in $testroot"
325 fi