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"
26 export GOT_IGNORE_GITCONFIG=1
28 export MALLOC_OPTIONS=S
30 git_init()
31 {
32 git init -q "$1"
34 # Switch the default branch to match our test expectations if needed.
35 # Only need to change HEAD since 'git init' did not create any refs.
36 # Relying on implementation details of 'git init' is no problem for us.
37 # We want to be alerted when Git changes fundamental assumptions such
38 # as what an empty repository looks like and where the default branch
39 # is set. In such cases Got's own tooling might well need to change
40 # its behaviour, too, and our tests should fail.
41 # TODO: Update all tests to assume 'main' instead of 'master' and
42 # switch to main here, to match Got's own default.
43 echo "ref: refs/heads/master" > "$1/.git/HEAD"
44 }
46 maybe_pack_repo()
47 {
48 local repo="$1"
49 if [ -n "$GOT_TEST_PACK" ]; then
50 (cd $repo && gotadmin pack -a > /dev/null)
51 (cd $repo && gotadmin cleanup -a -q)
52 fi
53 }
55 git_commit()
56 {
57 local repo="$1"
58 shift
59 (cd $repo && git commit --author="$GOT_AUTHOR" -q -a "$@")
60 maybe_pack_repo $repo
61 }
63 git_rm()
64 {
65 local repo="$1"
66 shift
67 (cd $repo && git rm -q "$@")
68 }
70 git_show_head()
71 {
72 local repo="$1"
73 (cd $repo && git show --no-patch --pretty='format:%H')
74 }
76 git_show_branch_head()
77 {
78 local repo="$1"
79 local branch="$2"
80 (cd $repo && git show --no-patch --pretty='format:%H' $branch)
81 }
84 git_show_author_time()
85 {
86 local repo="$1"
87 local object="$2"
88 (cd $repo && git show --no-patch --pretty='format:%at' $object)
89 }
91 git_show_tagger_time()
92 {
93 local repo="$1"
94 local tag="$2"
95 (cd $repo && git cat-file tag $tag | grep ^tagger | \
96 sed -e "s/^tagger $GOT_AUTHOR//" | cut -d' ' -f2)
97 }
99 git_show_parent_commit()
101 local repo="$1"
102 local commit="$2"
103 (cd $repo && git show --no-patch --pretty='format:%P' $commit)
106 git_show_tree()
108 local repo="$1"
109 (cd $repo && git show --no-patch --pretty='format:%T')
112 trim_obj_id()
114 local trimcount=$1
115 local id=$2
117 local pat=""
118 while [ "$trimcount" -gt 0 ]; do
119 pat="[0-9a-f]$pat"
120 trimcount=$((trimcount - 1))
121 done
123 echo ${id%$pat}
126 git_commit_tree()
128 local repo="$1"
129 local msg="$2"
130 local tree="$3"
131 (cd $repo && git commit-tree -m "$msg" "$tree")
134 git_fsck()
136 local testroot="$1"
137 local repo="$2"
139 (cd $repo && git fsck --strict \
140 > $testroot/fsck.stdout 2> $testroot/fsck.stderr)
141 ret=$?
142 if [ $ret -ne 0 ]; then
143 echo -n "git fsck: "
144 cat $testroot/fsck.stderr
145 echo "git fsck failed; leaving test data in $testroot"
146 return 1
147 fi
149 return 0
152 make_test_tree()
154 repo="$1"
156 echo alpha > $repo/alpha
157 echo beta > $repo/beta
158 mkdir $repo/gamma
159 echo delta > $repo/gamma/delta
160 mkdir $repo/epsilon
161 echo zeta > $repo/epsilon/zeta
164 make_single_file_repo()
166 repo="$1"
167 file="$2"
169 mkdir $repo
170 git_init $repo
171 echo "this is file $file" > $repo/$file
172 (cd $repo && git add .)
173 git_commit $repo -m "intialize $repo with file $file"
176 get_loose_object_path()
178 local repo="$1"
179 local id="$2"
180 local id0=`trim_obj_id 38 $id`
181 local idrest=`echo ${id#[0-9a-f][0-9a-f]}`
182 echo "$repo/.git/objects/$id0/$idrest"
185 get_blob_id()
187 repo="$1"
188 tree_path="$2"
189 filename="$3"
191 got tree -r $repo -i $tree_path | grep "[0-9a-f] ${filename}$" | \
192 cut -d' ' -f 1
195 test_init()
197 local testname="$1"
198 local no_tree="$2"
199 if [ -z "$testname" ]; then
200 echo "No test name provided" >&2
201 return 1
202 fi
203 local testroot=`mktemp -d "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXX"`
204 mkdir $testroot/repo
205 git_init $testroot/repo
206 if [ -z "$no_tree" ]; then
207 make_test_tree $testroot/repo
208 (cd $repo && git add .)
209 git_commit $testroot/repo -m "adding the test tree"
210 fi
211 touch $testroot/repo/.git/git-daemon-export-ok
212 echo "$testroot"
215 test_cleanup()
217 local testroot="$1"
219 git_fsck $testroot $testroot/repo
220 ret=$?
221 if [ $ret -ne 0 ]; then
222 return $ret
223 fi
225 rm -rf "$testroot"
228 test_parseargs()
230 while getopts qr: flag; do
231 case $flag in
232 q) export GOT_TEST_QUIET=1
233 ;;
234 r) export GOT_TEST_ROOT=$OPTARG
235 ;;
236 ?) echo "Supported options:"
237 echo " -q: quiet mode"
238 echo " -r PATH: use PATH as test data root directory"
239 exit 2
240 ;;
241 esac
242 done
243 } >&2
245 run_test()
247 testfunc="$1"
248 if [ -z "$GOT_TEST_QUIET" ]; then
249 echo -n "$testfunc "
250 fi
251 $testfunc
254 test_done()
256 local testroot="$1"
257 local result="$2"
258 if [ "$result" = "0" ]; then
259 test_cleanup "$testroot" || return 1
260 if [ -z "$GOT_TEST_QUIET" ]; then
261 echo "ok"
262 fi
263 elif echo "$result" | grep -q "^xfail"; then
264 # expected test failure; test reproduces an unfixed bug
265 echo "$result"
266 test_cleanup "$testroot" || return 1
267 else
268 echo "test failed; leaving test data in $testroot"
269 fi