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 export GIT_AUTHOR_NAME="Flan Hacker"
18 export GIT_AUTHOR_EMAIL="flan_hacker@openbsd.org"
19 export GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME"
20 export GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL"
21 export GOT_AUTHOR="$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>"
22 export GOT_AUTHOR_8="flan_hac"
23 export GOT_AUTHOR_11="flan_hacker"
24 export GOT_LOG_DEFAULT_LIMIT=0
25 export GOT_TEST_ROOT="/tmp"
27 export MALLOC_OPTIONS=S
29 git_init()
30 {
31 git init -q "$1"
33 # Switch the default branch to match our test expectations if needed.
34 # Only need to change HEAD since 'git init' did not create any refs.
35 # Relying on implementation details of 'git init' is no problem for us.
36 # We want to be alerted when Git changes fundamental assumptions such
37 # as what an empty repository looks like and where the default branch
38 # is set. In such cases Got's own tooling might well need to change
39 # its behaviour, too, and our tests should fail.
40 # TODO: Update all tests to assume 'main' instead of 'master' and
41 # switch to main here, to match Got's own default.
42 echo "ref: refs/heads/master" > "$1/.git/HEAD"
43 }
45 maybe_pack_repo()
46 {
47 local repo="$1"
48 if [ -n "$GOT_TEST_PACK" ]; then
49 (cd $repo && gotadmin pack -a > /dev/null)
50 (cd $repo && gotadmin cleanup -a -q)
51 fi
52 }
54 git_commit()
55 {
56 local repo="$1"
57 shift
58 (cd $repo && git commit --author="$GOT_AUTHOR" -q -a "$@")
59 maybe_pack_repo $repo
60 }
62 git_rm()
63 {
64 local repo="$1"
65 shift
66 (cd $repo && git rm -q "$@")
67 }
69 git_show_head()
70 {
71 local repo="$1"
72 (cd $repo && git show --no-patch --pretty='format:%H')
73 }
75 git_show_branch_head()
76 {
77 local repo="$1"
78 local branch="$2"
79 (cd $repo && git show --no-patch --pretty='format:%H' $branch)
80 }
83 git_show_author_time()
84 {
85 local repo="$1"
86 local object="$2"
87 (cd $repo && git show --no-patch --pretty='format:%at' $object)
88 }
90 git_show_tagger_time()
91 {
92 local repo="$1"
93 local tag="$2"
94 (cd $repo && git cat-file tag $tag | grep ^tagger | \
95 sed -e "s/^tagger $GOT_AUTHOR//" | cut -d' ' -f2)
96 }
98 git_show_parent_commit()
99 {
100 local repo="$1"
101 local commit="$2"
102 (cd $repo && git show --no-patch --pretty='format:%P' $commit)
105 git_show_tree()
107 local repo="$1"
108 (cd $repo && git show --no-patch --pretty='format:%T')
111 trim_obj_id()
113 local trimcount=$1
114 local id=$2
116 local pat=""
117 while [ "$trimcount" -gt 0 ]; do
118 pat="[0-9a-f]$pat"
119 trimcount=$((trimcount - 1))
120 done
122 echo ${id%$pat}
125 git_commit_tree()
127 local repo="$1"
128 local msg="$2"
129 local tree="$3"
130 (cd $repo && git commit-tree -m "$msg" "$tree")
133 git_fsck()
135 local testroot="$1"
136 local repo="$2"
138 (cd $repo && git fsck --strict \
139 > $testroot/fsck.stdout 2> $testroot/fsck.stderr)
140 ret=$?
141 if [ $ret -ne 0 ]; then
142 echo -n "git fsck: "
143 cat $testroot/fsck.stderr
144 echo "git fsck failed; leaving test data in $testroot"
145 return 1
146 fi
148 return 0
151 make_test_tree()
153 repo="$1"
155 echo alpha > $repo/alpha
156 echo beta > $repo/beta
157 mkdir $repo/gamma
158 echo delta > $repo/gamma/delta
159 mkdir $repo/epsilon
160 echo zeta > $repo/epsilon/zeta
163 make_single_file_repo()
165 repo="$1"
166 file="$2"
168 mkdir $repo
169 git_init $repo
170 echo "this is file $file" > $repo/$file
171 (cd $repo && git add .)
172 git_commit $repo -m "intialize $repo with file $file"
175 get_loose_object_path()
177 local repo="$1"
178 local id="$2"
179 local id0=`trim_obj_id 38 $id`
180 local idrest=`echo ${id#[0-9a-f][0-9a-f]}`
181 echo "$repo/.git/objects/$id0/$idrest"
184 get_blob_id()
186 repo="$1"
187 tree_path="$2"
188 filename="$3"
190 got tree -r $repo -i $tree_path | grep "[0-9a-f] ${filename}$" | \
191 cut -d' ' -f 1
194 test_init()
196 local testname="$1"
197 local no_tree="$2"
198 if [ -z "$testname" ]; then
199 echo "No test name provided" >&2
200 return 1
201 fi
202 local testroot=`mktemp -d "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXX"`
203 mkdir $testroot/repo
204 git_init $testroot/repo
205 if [ -z "$no_tree" ]; then
206 make_test_tree $testroot/repo
207 (cd $repo && git add .)
208 git_commit $testroot/repo -m "adding the test tree"
209 fi
210 touch $testroot/repo/.git/git-daemon-export-ok
211 echo "$testroot"
214 test_cleanup()
216 local testroot="$1"
218 git_fsck $testroot $testroot/repo
219 ret=$?
220 if [ $ret -ne 0 ]; then
221 return $ret
222 fi
224 rm -rf "$testroot"
227 test_parseargs()
229 while getopts qr: flag; do
230 case $flag in
231 q) export GOT_TEST_QUIET=1
232 ;;
233 r) export GOT_TEST_ROOT=$OPTARG
234 ;;
235 ?) echo "Supported options:"
236 echo " -q: quiet mode"
237 echo " -r PATH: use PATH as test data root directory"
238 exit 2
239 ;;
240 esac
241 done
242 } >&2
244 run_test()
246 testfunc="$1"
247 if [ -z "$GOT_TEST_QUIET" ]; then
248 echo -n "$testfunc "
249 fi
250 $testfunc
253 test_done()
255 local testroot="$1"
256 local result="$2"
257 if [ "$result" = "0" ]; then
258 test_cleanup "$testroot" || return 1
259 if [ -z "$GOT_TEST_QUIET" ]; then
260 echo "ok"
261 fi
262 elif echo "$result" | grep -q "^xfail"; then
263 # expected test failure; test reproduces an unfixed bug
264 echo "$result"
265 test_cleanup "$testroot" || return 1
266 else
267 echo "test failed; leaving test data in $testroot"
268 fi