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 # Check to see if PLATFORM has a value. PLATFORM is populated when running
34 # via `./configure && make` but this isn't guaranteed if an individual test is
35 # run directly, such as `regress/cmdline/tag.sh`. In such cases, PLATFORM
36 # will be empty, but we still want to use it. Since we test for non-linux
37 # values, only set PLATFORM if we're running on Linu so that the correct
38 # commands are used.
39 [ -z "$PLATFORM" -a "$(uname)" = "Linux" ] && PLATFORM="linux"
41 date()
42 {
43 DATECMD="date"
44 [ "$PLATFORM" != "linux" ] && {
45 command -v "gdate" >/dev/null 2>&1 && {
46 DATECMD="gdate"
47 } || {
48 echo "Couldn't find gdate is GNU coreutils installed?"
49 }
50 }
51 command "$DATECMD" "$@"
52 }
54 ln()
55 {
56 LNCMD="ln"
57 [ "$PLATFORM" != "linux" ] && {
58 command -v "gln" >/dev/null 2>&1 && {
59 LNCMD="gln"
60 } || {
61 echo "Couldn't find gln is GNU coreutils installed?"
62 }
63 }
64 command "$LNCMD" "$@"
65 }
67 sed()
68 {
69 SEDCMD="sed"
71 # On non-linux systems, the sed command can happily accept "-i ''" as
72 # a valid command to not save backup files for in-place edits.
73 # However, on linux, "-i ''" would be treated as "-i" with a blank
74 # argument, and hence, no file to edit in-place, which is an error.
75 #
76 # Therefore, scan the argument list and remove "-i ''", replacing it
77 # with just "-i".
79 [ "$PLATFORM" = "linux" ] && {
80 for w in "$@"
81 do
82 [ "$w" = "-i" ] && {
83 seen=1
84 continue
85 }
87 [ "$seen" = "1" -a -z "$w" ] && {
88 # Move past -i and ''
89 shift 2
91 command "$SEDCMD" -i "$@"
92 return
93 }
94 done
95 }
96 command "$SEDCMD" "$@"
97 }
100 git_init()
102 git init -q "$1"
104 # Switch the default branch to match our test expectations if needed.
105 # Only need to change HEAD since 'git init' did not create any refs.
106 # Relying on implementation details of 'git init' is no problem for us.
107 # We want to be alerted when Git changes fundamental assumptions such
108 # as what an empty repository looks like and where the default branch
109 # is set. In such cases Got's own tooling might well need to change
110 # its behaviour, too, and our tests should fail.
111 # TODO: Update all tests to assume 'main' instead of 'master' and
112 # switch to main here, to match Got's own default.
113 echo "ref: refs/heads/master" > "$1/.git/HEAD"
116 maybe_pack_repo()
118 local repo="$1"
119 if [ -n "$GOT_TEST_PACK" ]; then
120 arg=""
121 if [ "$GOT_TEST_PACK" = "ref-delta" ]; then
122 arg="-D"
123 fi
125 (cd $repo && gotadmin pack -a $arg > /dev/null)
126 (cd $repo && gotadmin cleanup -a -q)
127 fi
130 git_commit()
132 local repo="$1"
133 shift
134 (cd $repo && git commit --author="$GOT_AUTHOR" -q -a "$@")
135 maybe_pack_repo $repo
138 git_rm()
140 local repo="$1"
141 shift
142 (cd $repo && git rm -q "$@")
145 git_show_head()
147 local repo="$1"
148 (cd $repo && git show --no-patch --pretty='format:%H')
151 git_show_branch_head()
153 local repo="$1"
154 local branch="$2"
155 (cd $repo && git show --no-patch --pretty='format:%H' $branch)
159 git_show_author_time()
161 local repo="$1"
162 local object="$2"
163 (cd $repo && git show --no-patch --pretty='format:%at' $object)
166 git_show_tagger_time()
168 local repo="$1"
169 local tag="$2"
170 (cd $repo && git cat-file tag $tag | grep ^tagger | \
171 sed -e "s/^tagger $GOT_AUTHOR//" | cut -d' ' -f2)
174 git_show_parent_commit()
176 local repo="$1"
177 local commit="$2"
178 (cd $repo && git show --no-patch --pretty='format:%P' $commit)
181 git_show_tree()
183 local repo="$1"
184 (cd $repo && git show --no-patch --pretty='format:%T')
187 trim_obj_id()
189 local trimcount=$1
190 local id=$2
192 local pat=""
193 while [ "$trimcount" -gt 0 ]; do
194 pat="[0-9a-f]$pat"
195 trimcount=$((trimcount - 1))
196 done
198 echo ${id%$pat}
201 git_commit_tree()
203 local repo="$1"
204 local msg="$2"
205 local tree="$3"
206 (cd $repo && git commit-tree -m "$msg" "$tree")
209 git_fsck()
211 local testroot="$1"
212 local repo="$2"
214 (cd $repo && git fsck --strict \
215 > $testroot/fsck.stdout 2> $testroot/fsck.stderr)
216 ret=$?
217 if [ $ret -ne 0 ]; then
218 echo -n "git fsck: "
219 cat $testroot/fsck.stderr
220 echo "git fsck failed; leaving test data in $testroot"
221 return 1
222 fi
224 return 0
227 make_test_tree()
229 repo="$1"
231 echo alpha > $repo/alpha
232 echo beta > $repo/beta
233 mkdir $repo/gamma
234 echo delta > $repo/gamma/delta
235 mkdir $repo/epsilon
236 echo zeta > $repo/epsilon/zeta
239 make_single_file_repo()
241 repo="$1"
242 file="$2"
244 mkdir $repo
245 git_init $repo
246 echo "this is file $file" > $repo/$file
247 (cd $repo && git add .)
248 git_commit $repo -m "intialize $repo with file $file"
251 get_loose_object_path()
253 local repo="$1"
254 local id="$2"
255 local id0=`trim_obj_id 38 $id`
256 local idrest=`echo ${id#[0-9a-f][0-9a-f]}`
257 echo "$repo/.git/objects/$id0/$idrest"
260 get_blob_id()
262 repo="$1"
263 tree_path="$2"
264 filename="$3"
266 got tree -r $repo -i $tree_path | grep "[0-9a-f] ${filename}$" | \
267 cut -d' ' -f 1
270 test_init()
272 local testname="$1"
273 local no_tree="$2"
274 if [ -z "$testname" ]; then
275 echo "No test name provided" >&2
276 return 1
277 fi
278 local testroot=`mktemp -d "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXX"`
279 mkdir $testroot/repo
280 git_init $testroot/repo
281 if [ -z "$no_tree" ]; then
282 make_test_tree $testroot/repo
283 (cd $repo && git add .)
284 git_commit $testroot/repo -m "adding the test tree"
285 fi
286 touch $testroot/repo/.git/git-daemon-export-ok
287 echo "$testroot"
290 test_cleanup()
292 local testroot="$1"
294 git_fsck $testroot $testroot/repo
295 ret=$?
296 if [ $ret -ne 0 ]; then
297 return $ret
298 fi
300 rm -rf "$testroot"
303 test_parseargs()
305 while getopts qr: flag; do
306 case $flag in
307 q) export GOT_TEST_QUIET=1
308 ;;
309 r) export GOT_TEST_ROOT=${OPTARG%/}
310 ;;
311 ?) echo "Supported options:"
312 echo " -q: quiet mode"
313 echo " -r PATH: use PATH as test data root directory"
314 exit 2
315 ;;
316 esac
317 done
318 shift $(($OPTIND - 1))
319 regress_run_only="$@"
320 } >&2
322 run_test()
324 testfunc="$1"
326 if [ -n "$regress_run_only" ]; then
327 case "$regress_run_only" in
328 *$testfunc*) ;;
329 *) return ;;
330 esac
331 fi
333 if [ -z "$GOT_TEST_QUIET" ]; then
334 echo -n "$testfunc "
335 fi
336 $testfunc
339 test_done()
341 local testroot="$1"
342 local result="$2"
343 if [ "$result" = "0" ]; then
344 test_cleanup "$testroot" || return 1
345 if [ -z "$GOT_TEST_QUIET" ]; then
346 echo "ok"
347 fi
348 elif echo "$result" | grep -q "^xfail"; then
349 # expected test failure; test reproduces an unfixed bug
350 echo "$result"
351 test_cleanup "$testroot" || return 1
352 else
353 echo "test failed; leaving test data in $testroot"
354 fi