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 git_init()
36 {
37 git init -q "$1"
39 # Switch the default branch to match our test expectations if needed.
40 # Only need to change HEAD since 'git init' did not create any refs.
41 # Relying on implementation details of 'git init' is no problem for us.
42 # We want to be alerted when Git changes fundamental assumptions such
43 # as what an empty repository looks like and where the default branch
44 # is set. In such cases Got's own tooling might well need to change
45 # its behaviour, too, and our tests should fail.
46 # TODO: Update all tests to assume 'main' instead of 'master' and
47 # switch to main here, to match Got's own default.
48 echo "ref: refs/heads/master" > "$1/.git/HEAD"
49 }
51 maybe_pack_repo()
52 {
53 local repo="$1"
54 if [ -n "$GOT_TEST_PACK" ]; then
55 arg=""
56 if [ "$GOT_TEST_PACK" = "ref-delta" ]; then
57 arg="-D"
58 fi
60 (cd $repo && gotadmin pack -a $arg > /dev/null)
61 (cd $repo && gotadmin cleanup -a -q)
62 fi
63 }
65 git_commit()
66 {
67 local repo="$1"
68 shift
69 git -C $repo commit --author="$GOT_AUTHOR" -q -a "$@"
70 maybe_pack_repo $repo
71 }
73 git_rm()
74 {
75 local repo="$1"
76 shift
77 git -C $repo rm -q "$@"
78 }
80 git_rmdir()
81 {
82 local repo="$1"
83 shift
84 git -C $repo rm -q -r "$@"
85 }
87 git_show_head()
88 {
89 local repo="$1"
90 git -C $repo show --no-patch --pretty='format:%H'
91 }
93 git_show_branch_head()
94 {
95 local repo="$1"
96 local branch="$2"
97 git -C $repo show --no-patch --pretty='format:%H' $branch
98 }
101 git_show_author_time()
103 local repo="$1"
104 local object="$2"
105 git -C $repo show --no-patch --pretty='format:%at' $object
108 git_show_tagger_time()
110 local repo="$1"
111 local tag="$2"
112 git -C $repo cat-file tag $tag | grep ^tagger | \
113 sed -e "s/^tagger $GOT_AUTHOR//" | cut -d' ' -f2
116 git_show_parent_commit()
118 local repo="$1"
119 local commit="$2"
120 git -C $repo show --no-patch --pretty='format:%P' $commit
123 git_show_tree()
125 local repo="$1"
126 git -C $repo show --no-patch --pretty='format:%T'
129 trim_obj_id()
131 local trimcount=$1
132 local id=$2
134 local pat=""
135 while [ "$trimcount" -gt 0 ]; do
136 pat="[0-9a-f]$pat"
137 trimcount=$((trimcount - 1))
138 done
140 echo ${id%$pat}
143 pop_idx()
145 shift "$1"
146 printf '%s' "${1:-index-out-of-bounds}"
149 git_commit_tree()
151 local repo="$1"
152 local msg="$2"
153 local tree="$3"
154 git -C $repo commit-tree -m "$msg" "$tree"
157 git_fsck()
159 local testroot="$1"
160 local repo="$2"
162 git -C $repo fsck --strict \
163 > $testroot/fsck.stdout 2> $testroot/fsck.stderr
164 ret=$?
165 if [ $ret -ne 0 ]; then
166 echo -n "git fsck: "
167 cat $testroot/fsck.stderr
168 echo "git fsck failed; leaving test data in $testroot"
169 return 1
170 fi
172 return 0
175 make_test_tree()
177 repo="$1"
179 echo alpha > $repo/alpha
180 echo beta > $repo/beta
181 mkdir $repo/gamma
182 echo delta > $repo/gamma/delta
183 mkdir $repo/epsilon
184 echo zeta > $repo/epsilon/zeta
187 make_single_file_repo()
189 repo="$1"
190 file="$2"
192 mkdir $repo
193 git_init $repo
194 echo "this is file $file" > $repo/$file
195 git -C $repo add .
196 git_commit $repo -m "intialize $repo with file $file"
199 get_loose_object_path()
201 local repo="$1"
202 local id="$2"
203 local id0=`trim_obj_id 38 $id`
204 local idrest=`echo ${id#[0-9a-f][0-9a-f]}`
205 echo "$repo/.git/objects/$id0/$idrest"
208 get_blob_id()
210 repo="$1"
211 tree_path="$2"
212 filename="$3"
214 got tree -r $repo -i $tree_path | grep "[0-9a-f] ${filename}$" | \
215 cut -d' ' -f 1
218 test_init()
220 local testname="$1"
221 local no_tree="$2"
222 if [ -z "$testname" ]; then
223 echo "No test name provided" >&2
224 return 1
225 fi
226 local testroot=`mktemp -d \
227 "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXXXX"`
228 mkdir $testroot/repo
229 git_init $testroot/repo
230 if [ -z "$no_tree" ]; then
231 make_test_tree $testroot/repo
232 git -C $repo add .
233 git_commit $testroot/repo -m "adding the test tree"
234 fi
235 touch $testroot/repo/.git/git-daemon-export-ok
236 echo "$testroot"
239 test_cleanup()
241 local testroot="$1"
243 git_fsck $testroot $testroot/repo
244 ret=$?
245 if [ $ret -ne 0 ]; then
246 return $ret
247 fi
249 rm -rf "$testroot"
252 test_parseargs()
254 while getopts qr: flag; do
255 case $flag in
256 q) export GOT_TEST_QUIET=1
257 ;;
258 r) export GOT_TEST_ROOT=${OPTARG%/}
259 ;;
260 ?) echo "Supported options:"
261 echo " -q: quiet mode"
262 echo " -r PATH: use PATH as test data root directory"
263 exit 2
264 ;;
265 esac
266 done
267 shift $(($OPTIND - 1))
268 regress_run_only="$@"
269 } >&2
271 run_test()
273 testfunc="$1"
275 if [ -n "$regress_run_only" ]; then
276 case "$regress_run_only" in
277 *$testfunc*) ;;
278 *) return ;;
279 esac
280 fi
282 if [ -z "$GOT_TEST_QUIET" ]; then
283 echo -n "$testfunc "
284 fi
285 $testfunc
288 test_done()
290 local testroot="$1"
291 local result="$2"
292 if [ "$result" = "0" ]; then
293 test_cleanup "$testroot" || return 1
294 if [ -z "$GOT_TEST_QUIET" ]; then
295 echo "ok"
296 fi
297 elif echo "$result" | grep -q "^xfail"; then
298 # expected test failure; test reproduces an unfixed bug
299 echo "$result"
300 test_cleanup "$testroot" || return 1
301 else
302 echo "test failed; leaving test data in $testroot"
303 fi