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 MALLOC_OPTIONS=S
33 git_init()
34 {
35 git init -q "$1"
37 # Switch the default branch to match our test expectations if needed.
38 # Only need to change HEAD since 'git init' did not create any refs.
39 # Relying on implementation details of 'git init' is no problem for us.
40 # We want to be alerted when Git changes fundamental assumptions such
41 # as what an empty repository looks like and where the default branch
42 # is set. In such cases Got's own tooling might well need to change
43 # its behaviour, too, and our tests should fail.
44 # TODO: Update all tests to assume 'main' instead of 'master' and
45 # switch to main here, to match Got's own default.
46 echo "ref: refs/heads/master" > "$1/.git/HEAD"
47 }
49 maybe_pack_repo()
50 {
51 local repo="$1"
52 if [ -n "$GOT_TEST_PACK" ]; then
53 arg=""
54 if [ "$GOT_TEST_PACK" = "ref-delta" ]; then
55 arg="-D"
56 fi
58 (cd $repo && gotadmin pack -a $arg > /dev/null)
59 (cd $repo && gotadmin cleanup -a -q)
60 fi
61 }
63 git_commit()
64 {
65 local repo="$1"
66 shift
67 (cd $repo && git commit --author="$GOT_AUTHOR" -q -a "$@")
68 maybe_pack_repo $repo
69 }
71 git_rm()
72 {
73 local repo="$1"
74 shift
75 (cd $repo && git rm -q "$@")
76 }
78 git_show_head()
79 {
80 local repo="$1"
81 (cd $repo && git show --no-patch --pretty='format:%H')
82 }
84 git_show_branch_head()
85 {
86 local repo="$1"
87 local branch="$2"
88 (cd $repo && git show --no-patch --pretty='format:%H' $branch)
89 }
92 git_show_author_time()
93 {
94 local repo="$1"
95 local object="$2"
96 (cd $repo && git show --no-patch --pretty='format:%at' $object)
97 }
99 git_show_tagger_time()
101 local repo="$1"
102 local tag="$2"
103 (cd $repo && git cat-file tag $tag | grep ^tagger | \
104 sed -e "s/^tagger $GOT_AUTHOR//" | cut -d' ' -f2)
107 git_show_parent_commit()
109 local repo="$1"
110 local commit="$2"
111 (cd $repo && git show --no-patch --pretty='format:%P' $commit)
114 git_show_tree()
116 local repo="$1"
117 (cd $repo && git show --no-patch --pretty='format:%T')
120 trim_obj_id()
122 local trimcount=$1
123 local id=$2
125 local pat=""
126 while [ "$trimcount" -gt 0 ]; do
127 pat="[0-9a-f]$pat"
128 trimcount=$((trimcount - 1))
129 done
131 echo ${id%$pat}
134 git_commit_tree()
136 local repo="$1"
137 local msg="$2"
138 local tree="$3"
139 (cd $repo && git commit-tree -m "$msg" "$tree")
142 git_fsck()
144 local testroot="$1"
145 local repo="$2"
147 (cd $repo && git fsck --strict \
148 > $testroot/fsck.stdout 2> $testroot/fsck.stderr)
149 ret=$?
150 if [ $ret -ne 0 ]; then
151 echo -n "git fsck: "
152 cat $testroot/fsck.stderr
153 echo "git fsck failed; leaving test data in $testroot"
154 return 1
155 fi
157 return 0
160 make_test_tree()
162 repo="$1"
164 echo alpha > $repo/alpha
165 echo beta > $repo/beta
166 mkdir $repo/gamma
167 echo delta > $repo/gamma/delta
168 mkdir $repo/epsilon
169 echo zeta > $repo/epsilon/zeta
172 make_single_file_repo()
174 repo="$1"
175 file="$2"
177 mkdir $repo
178 git_init $repo
179 echo "this is file $file" > $repo/$file
180 (cd $repo && git add .)
181 git_commit $repo -m "intialize $repo with file $file"
184 get_loose_object_path()
186 local repo="$1"
187 local id="$2"
188 local id0=`trim_obj_id 38 $id`
189 local idrest=`echo ${id#[0-9a-f][0-9a-f]}`
190 echo "$repo/.git/objects/$id0/$idrest"
193 get_blob_id()
195 repo="$1"
196 tree_path="$2"
197 filename="$3"
199 got tree -r $repo -i $tree_path | grep "[0-9a-f] ${filename}$" | \
200 cut -d' ' -f 1
203 test_init()
205 local testname="$1"
206 local no_tree="$2"
207 if [ -z "$testname" ]; then
208 echo "No test name provided" >&2
209 return 1
210 fi
211 local testroot=`mktemp -d "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXX"`
212 mkdir $testroot/repo
213 git_init $testroot/repo
214 if [ -z "$no_tree" ]; then
215 make_test_tree $testroot/repo
216 (cd $repo && git add .)
217 git_commit $testroot/repo -m "adding the test tree"
218 fi
219 touch $testroot/repo/.git/git-daemon-export-ok
220 echo "$testroot"
223 test_cleanup()
225 local testroot="$1"
227 git_fsck $testroot $testroot/repo
228 ret=$?
229 if [ $ret -ne 0 ]; then
230 return $ret
231 fi
233 rm -rf "$testroot"
236 test_parseargs()
238 while getopts qr: flag; do
239 case $flag in
240 q) export GOT_TEST_QUIET=1
241 ;;
242 r) export GOT_TEST_ROOT=${OPTARG%/}
243 ;;
244 ?) echo "Supported options:"
245 echo " -q: quiet mode"
246 echo " -r PATH: use PATH as test data root directory"
247 exit 2
248 ;;
249 esac
250 done
251 shift $(($OPTIND - 1))
252 regress_run_only="$@"
253 } >&2
255 run_test()
257 testfunc="$1"
259 if [ -n "$regress_run_only" ]; then
260 case "$regress_run_only" in
261 *$testfunc*) ;;
262 *) return ;;
263 esac
264 fi
266 if [ -z "$GOT_TEST_QUIET" ]; then
267 echo -n "$testfunc "
268 fi
269 $testfunc
272 test_done()
274 local testroot="$1"
275 local result="$2"
276 if [ "$result" = "0" ]; then
277 test_cleanup "$testroot" || return 1
278 if [ -z "$GOT_TEST_QUIET" ]; then
279 echo "ok"
280 fi
281 elif echo "$result" | grep -q "^xfail"; then
282 # expected test failure; test reproduces an unfixed bug
283 echo "$result"
284 test_cleanup "$testroot" || return 1
285 else
286 echo "test failed; leaving test data in $testroot"
287 fi