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 date()
30 {
31 DATECMD="date"
32 [ "$PLATFORM" != "linux" ] && DATECMD="gdate"
34 command "$DATECMD" "$@"
35 }
37 ln()
38 {
39 LNCMD="ln"
40 [ "$PLATFORM" != "linux" ] && LNCMD="gln"
42 command "$LNCMD" "$@"
43 }
45 sed()
46 {
47 SEDCMD="sed"
48 [ "$PLATFORM" = "linux" ] && {
49 set -- "$@"
50 [ $# -ge 4 ] && {
51 shift 2
53 command "$SEDCMD" -i "$@"
54 return
55 }
56 }
57 command "$SEDCMD" "$@"
58 }
61 git_init()
62 {
63 git init -q "$1"
65 # Switch the default branch to match our test expectations if needed.
66 # Only need to change HEAD since 'git init' did not create any refs.
67 # Relying on implementation details of 'git init' is no problem for us.
68 # We want to be alerted when Git changes fundamental assumptions such
69 # as what an empty repository looks like and where the default branch
70 # is set. In such cases Got's own tooling might well need to change
71 # its behaviour, too, and our tests should fail.
72 # TODO: Update all tests to assume 'main' instead of 'master' and
73 # switch to main here, to match Got's own default.
74 echo "ref: refs/heads/master" > "$1/.git/HEAD"
75 }
77 maybe_pack_repo()
78 {
79 local repo="$1"
80 if [ -n "$GOT_TEST_PACK" ]; then
81 (cd $repo && git repack -a -q)
82 fi
83 }
85 git_commit()
86 {
87 local repo="$1"
88 shift
89 (cd $repo && git commit --author="$GOT_AUTHOR" -q -a "$@")
90 maybe_pack_repo $repo
91 }
93 git_rm()
94 {
95 local repo="$1"
96 shift
97 (cd $repo && git rm -q "$@")
98 }
100 git_show_head()
102 local repo="$1"
103 (cd $repo && git show --no-patch --pretty='format:%H')
106 git_show_branch_head()
108 local repo="$1"
109 local branch="$2"
110 (cd $repo && git show --no-patch --pretty='format:%H' $branch)
114 git_show_author_time()
116 local repo="$1"
117 local object="$2"
118 (cd $repo && git show --no-patch --pretty='format:%at' $object)
121 git_show_tagger_time()
123 local repo="$1"
124 local tag="$2"
125 (cd $repo && git cat-file tag $tag | grep ^tagger | \
126 sed -e "s/^tagger $GOT_AUTHOR//" | cut -d' ' -f2)
129 git_show_parent_commit()
131 local repo="$1"
132 local commit="$2"
133 (cd $repo && git show --no-patch --pretty='format:%P' $commit)
136 git_show_tree()
138 local repo="$1"
139 (cd $repo && git show --no-patch --pretty='format:%T')
142 trim_obj_id()
144 local trimcount=$1
145 local id=$2
147 local pat=""
148 while [ "$trimcount" -gt 0 ]; do
149 pat="[0-9a-f]$pat"
150 trimcount=$((trimcount - 1))
151 done
153 echo ${id%$pat}
156 git_commit_tree()
158 local repo="$1"
159 local msg="$2"
160 local tree="$3"
161 (cd $repo && git commit-tree -m "$msg" "$tree")
162 maybe_pack_repo $repo
165 git_fsck()
167 local testroot="$1"
168 local repo="$2"
170 (cd $repo && git fsck --strict \
171 > $testroot/fsck.stdout 2> $testroot/fsck.stderr)
172 ret="$?"
173 if [ "$ret" != "0" ]; then
174 echo -n "git fsck: "
175 cat $testroot/fsck.stderr
176 echo "git fsck failed; leaving test data in $testroot"
177 return 1
178 fi
180 return 0
183 make_test_tree()
185 repo="$1"
187 echo alpha > $repo/alpha
188 echo beta > $repo/beta
189 mkdir $repo/gamma
190 echo delta > $repo/gamma/delta
191 mkdir $repo/epsilon
192 echo zeta > $repo/epsilon/zeta
195 make_single_file_repo()
197 repo="$1"
198 file="$2"
200 mkdir $repo
201 git_init $repo
202 echo "this is file $file" > $repo/$file
203 (cd $repo && git add .)
204 git_commit $repo -m "intialize $repo with file $file"
207 get_loose_object_path()
209 local repo="$1"
210 local id="$2"
211 local id0=`trim_obj_id 38 $id`
212 local idrest=`echo ${id#[0-9a-f][0-9a-f]}`
213 echo "$repo/.git/objects/$id0/$idrest"
216 get_blob_id()
218 repo="$1"
219 tree_path="$2"
220 filename="$3"
222 got tree -r $repo -i $tree_path | grep "[0-9a-f] ${filename}$" | \
223 cut -d' ' -f 1
226 test_init()
228 local testname="$1"
229 local no_tree="$2"
230 if [ -z "$testname" ]; then
231 echo "No test name provided" >&2
232 return 1
233 fi
234 local testroot=`mktemp -d "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXX"`
235 mkdir $testroot/repo
236 git_init $testroot/repo
237 if [ -z "$no_tree" ]; then
238 make_test_tree $testroot/repo
239 (cd $repo && git add .)
240 git_commit $testroot/repo -m "adding the test tree"
241 fi
242 touch $testroot/repo/.git/git-daemon-export-ok
243 echo "$testroot"
246 test_cleanup()
248 local testroot="$1"
250 git_fsck $testroot $testroot/repo
251 ret="$?"
252 if [ "$ret" != "0" ]; then
253 return $ret
254 fi
256 rm -rf "$testroot"
259 test_parseargs()
261 while getopts qr: flag; do
262 case $flag in
263 q) export GOT_TEST_QUIET=1
264 ;;
265 r) export GOT_TEST_ROOT=$OPTARG
266 ;;
267 ?) echo "Supported options:"
268 echo " -q: quiet mode"
269 echo " -r PATH: use PATH as test data root directory"
270 exit 2
271 ;;
272 esac
273 done
274 } >&2
276 run_test()
278 testfunc="$1"
279 if [ -z "$GOT_TEST_QUIET" ]; then
280 echo -n "$testfunc "
281 fi
282 $testfunc
285 test_done()
287 local testroot="$1"
288 local result="$2"
289 if [ "$result" = "0" ]; then
290 test_cleanup "$testroot" || return 1
291 if [ -z "$GOT_TEST_QUIET" ]; then
292 echo "ok"
293 fi
294 elif echo "$result" | grep -q "^xfail"; then
295 # expected test failure; test reproduces an unfixed bug
296 echo "$result"
297 test_cleanup "$testroot" || return 1
298 else
299 echo "test failed; leaving test data in $testroot"
300 fi