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