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 git_init()
37 {
38 git init -q "$1"
40 # Switch the default branch to match our test expectations if needed.
41 # Only need to change HEAD since 'git init' did not create any refs.
42 # Relying on implementation details of 'git init' is no problem for us.
43 # We want to be alerted when Git changes fundamental assumptions such
44 # as what an empty repository looks like and where the default branch
45 # is set. In such cases Got's own tooling might well need to change
46 # its behaviour, too, and our tests should fail.
47 # TODO: Update all tests to assume 'main' instead of 'master' and
48 # switch to main here, to match Got's own default.
49 echo "ref: refs/heads/master" > "$1/.git/HEAD"
50 }
52 maybe_pack_repo()
53 {
54 local repo="$1"
55 if [ -n "$GOT_TEST_PACK" ]; then
56 arg=""
57 if [ "$GOT_TEST_PACK" = "ref-delta" ]; then
58 arg="-D"
59 fi
61 (cd $repo && gotadmin pack -a $arg > /dev/null)
62 (cd $repo && gotadmin cleanup -a -q)
63 fi
64 }
66 git_commit()
67 {
68 local repo="$1"
69 shift
70 git -C $repo commit --author="$GOT_AUTHOR" -q -a "$@"
71 maybe_pack_repo $repo
72 }
74 git_rm()
75 {
76 local repo="$1"
77 shift
78 git -C $repo rm -q "$@"
79 }
81 git_rmdir()
82 {
83 local repo="$1"
84 shift
85 git -C $repo rm -q -r "$@"
86 }
88 git_show_head()
89 {
90 local repo="$1"
91 git -C $repo show --no-patch --pretty='format:%H'
92 }
94 git_show_branch_head()
95 {
96 local repo="$1"
97 local branch="$2"
98 git -C $repo show --no-patch --pretty='format:%H' $branch
99 }
102 git_show_author_time()
104 local repo="$1"
105 local object="$2"
106 git -C $repo show --no-patch --pretty='format:%at' $object
109 git_show_tagger_time()
111 local repo="$1"
112 local tag="$2"
113 git -C $repo cat-file tag $tag | grep ^tagger | \
114 sed -e "s/^tagger $GOT_AUTHOR//" | cut -d' ' -f2
117 git_show_parent_commit()
119 local repo="$1"
120 local commit="$2"
121 git -C $repo show --no-patch --pretty='format:%P' $commit
124 git_show_tree()
126 local repo="$1"
127 git -C $repo show --no-patch --pretty='format:%T'
130 trim_obj_id()
132 local trimcount=$1
133 local id=$2
135 local pat=""
136 while [ "$trimcount" -gt 0 ]; do
137 pat="[0-9a-f]$pat"
138 trimcount=$((trimcount - 1))
139 done
141 echo ${id%$pat}
144 pop_idx()
146 shift "$1"
147 printf '%s' "${1:-index-out-of-bounds}"
150 git_commit_tree()
152 local repo="$1"
153 local msg="$2"
154 local tree="$3"
155 git -C $repo commit-tree -m "$msg" "$tree"
158 git_fsck()
160 local testroot="$1"
161 local repo="$2"
163 git -C $repo fsck --strict \
164 > $testroot/fsck.stdout 2> $testroot/fsck.stderr
165 ret=$?
166 if [ $ret -ne 0 ]; then
167 echo -n "git fsck: "
168 cat $testroot/fsck.stderr
169 echo "git fsck failed; leaving test data in $testroot"
170 return 1
171 fi
173 return 0
176 make_test_tree()
178 repo="$1"
180 echo alpha > $repo/alpha
181 echo beta > $repo/beta
182 mkdir $repo/gamma
183 echo delta > $repo/gamma/delta
184 mkdir $repo/epsilon
185 echo zeta > $repo/epsilon/zeta
188 make_single_file_repo()
190 repo="$1"
191 file="$2"
193 mkdir $repo
194 git_init $repo
195 echo "this is file $file" > $repo/$file
196 git -C $repo add .
197 git_commit $repo -m "intialize $repo with file $file"
200 get_loose_object_path()
202 local repo="$1"
203 local id="$2"
204 local id0=`trim_obj_id 38 $id`
205 local idrest=`echo ${id#[0-9a-f][0-9a-f]}`
206 echo "$repo/.git/objects/$id0/$idrest"
209 get_blob_id()
211 repo="$1"
212 tree_path="$2"
213 filename="$3"
215 got tree -r $repo -i $tree_path | grep "[0-9a-f] ${filename}$" | \
216 cut -d' ' -f 1
219 test_init()
221 local testname="$1"
222 local no_tree="$2"
223 if [ -z "$testname" ]; then
224 echo "No test name provided" >&2
225 return 1
226 fi
227 local testroot=`mktemp -d \
228 "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXXXX"`
229 mkdir $testroot/repo
230 git_init $testroot/repo
231 if [ -z "$no_tree" ]; then
232 make_test_tree $testroot/repo
233 git -C $repo add .
234 git_commit $testroot/repo -m "adding the test tree"
235 fi
236 touch $testroot/repo/.git/git-daemon-export-ok
237 echo "$testroot"
240 test_cleanup()
242 local testroot="$1"
244 git_fsck $testroot $testroot/repo
245 ret=$?
246 if [ $ret -ne 0 ]; then
247 return $ret
248 fi
250 rm -rf "$testroot"
253 test_parseargs()
255 while getopts qr: flag; do
256 case $flag in
257 q) export GOT_TEST_QUIET=1
258 ;;
259 r) export GOT_TEST_ROOT=${OPTARG%/}
260 ;;
261 ?) echo "Supported options:"
262 echo " -q: quiet mode"
263 echo " -r PATH: use PATH as test data root directory"
264 exit 2
265 ;;
266 esac
267 done
268 shift $(($OPTIND - 1))
269 regress_run_only="$@"
270 } >&2
272 run_test()
274 testfunc="$1"
276 if [ -n "$regress_run_only" ]; then
277 case "$regress_run_only" in
278 *$testfunc*) ;;
279 *) return ;;
280 esac
281 fi
283 if [ -z "$GOT_TEST_QUIET" ]; then
284 echo -n "$testfunc "
285 fi
286 $testfunc
289 test_done()
291 local testroot="$1"
292 local result="$2"
293 if [ "$result" = "0" ]; then
294 test_cleanup "$testroot" || return 1
295 if [ -z "$GOT_TEST_QUIET" ]; then
296 echo "ok"
297 fi
298 elif echo "$result" | grep -q "^xfail"; then
299 # expected test failure; test reproduces an unfixed bug
300 echo "$result"
301 test_cleanup "$testroot" || return 1
302 else
303 echo "test failed; leaving test data in $testroot"
304 fi