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 -u -r 86400 +%F 2>/dev/null)" = 1970-01-02 ] || 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 }
52 local flag r u
53 while getopts r:u flag; do
54 case $flag in
55 r) r=$OPTARG ;;
56 u) u=-u ;;
57 ?) exit 1 ;;
58 esac
59 done
60 shift $((OPTIND - 1))
61 command "$DATECMD" $u ${r+-d"@$r"} "$@"
62 }
64 sed()
65 {
66 SEDCMD="sed"
68 # On non-linux systems, the sed command can happily accept "-i ''" as
69 # a valid command to not save backup files for in-place edits.
70 # However, on linux, "-i ''" would be treated as "-i" with a blank
71 # argument, and hence, no file to edit in-place, which is an error.
72 #
73 # Therefore, scan the argument list and remove "-i ''", replacing it
74 # with just "-i".
76 [ "$PLATFORM" = "linux" ] && {
77 for w in "$@"
78 do
79 [ "$w" = "-i" ] && {
80 seen=1
81 continue
82 }
84 [ "$seen" = "1" -a -z "$w" ] && {
85 # Move past -i and ''
86 shift 2
88 command "$SEDCMD" -i "$@"
89 return
90 }
91 done
92 }
93 command "$SEDCMD" "$@"
94 }
97 git_init()
98 {
99 git init -q "$1"
101 # Switch the default branch to match our test expectations if needed.
102 # Only need to change HEAD since 'git init' did not create any refs.
103 # Relying on implementation details of 'git init' is no problem for us.
104 # We want to be alerted when Git changes fundamental assumptions such
105 # as what an empty repository looks like and where the default branch
106 # is set. In such cases Got's own tooling might well need to change
107 # its behaviour, too, and our tests should fail.
108 # TODO: Update all tests to assume 'main' instead of 'master' and
109 # switch to main here, to match Got's own default.
110 echo "ref: refs/heads/master" > "$1/.git/HEAD"
113 maybe_pack_repo()
115 local repo="$1"
116 if [ -n "$GOT_TEST_PACK" ]; then
117 arg=""
118 if [ "$GOT_TEST_PACK" = "ref-delta" ]; then
119 arg="-D"
120 fi
122 (cd $repo && gotadmin pack -a $arg > /dev/null)
123 (cd $repo && gotadmin cleanup -a -q)
124 fi
127 git_commit()
129 local repo="$1"
130 shift
131 (cd $repo && git commit --author="$GOT_AUTHOR" -q -a "$@")
132 maybe_pack_repo $repo
135 git_rm()
137 local repo="$1"
138 shift
139 (cd $repo && git rm -q "$@")
142 git_show_head()
144 local repo="$1"
145 (cd $repo && git show --no-patch --pretty='format:%H')
148 git_show_branch_head()
150 local repo="$1"
151 local branch="$2"
152 (cd $repo && git show --no-patch --pretty='format:%H' $branch)
156 git_show_author_time()
158 local repo="$1"
159 local object="$2"
160 (cd $repo && git show --no-patch --pretty='format:%at' $object)
163 git_show_tagger_time()
165 local repo="$1"
166 local tag="$2"
167 (cd $repo && git cat-file tag $tag | grep ^tagger | \
168 sed -e "s/^tagger $GOT_AUTHOR//" | cut -d' ' -f2)
171 git_show_parent_commit()
173 local repo="$1"
174 local commit="$2"
175 (cd $repo && git show --no-patch --pretty='format:%P' $commit)
178 git_show_tree()
180 local repo="$1"
181 (cd $repo && git show --no-patch --pretty='format:%T')
184 trim_obj_id()
186 local trimcount=$1
187 local id=$2
189 local pat=""
190 while [ "$trimcount" -gt 0 ]; do
191 pat="[0-9a-f]$pat"
192 trimcount=$((trimcount - 1))
193 done
195 echo ${id%$pat}
198 git_commit_tree()
200 local repo="$1"
201 local msg="$2"
202 local tree="$3"
203 (cd $repo && git commit-tree -m "$msg" "$tree")
206 git_fsck()
208 local testroot="$1"
209 local repo="$2"
211 (cd $repo && git fsck --strict \
212 > $testroot/fsck.stdout 2> $testroot/fsck.stderr)
213 ret=$?
214 if [ $ret -ne 0 ]; then
215 echo -n "git fsck: "
216 cat $testroot/fsck.stderr
217 echo "git fsck failed; leaving test data in $testroot"
218 return 1
219 fi
221 return 0
224 make_test_tree()
226 repo="$1"
228 echo alpha > $repo/alpha
229 echo beta > $repo/beta
230 mkdir $repo/gamma
231 echo delta > $repo/gamma/delta
232 mkdir $repo/epsilon
233 echo zeta > $repo/epsilon/zeta
236 make_single_file_repo()
238 repo="$1"
239 file="$2"
241 mkdir $repo
242 git_init $repo
243 echo "this is file $file" > $repo/$file
244 (cd $repo && git add .)
245 git_commit $repo -m "intialize $repo with file $file"
248 get_loose_object_path()
250 local repo="$1"
251 local id="$2"
252 local id0=`trim_obj_id 38 $id`
253 local idrest=`echo ${id#[0-9a-f][0-9a-f]}`
254 echo "$repo/.git/objects/$id0/$idrest"
257 get_blob_id()
259 repo="$1"
260 tree_path="$2"
261 filename="$3"
263 got tree -r $repo -i $tree_path | grep "[0-9a-f] ${filename}$" | \
264 cut -d' ' -f 1
267 test_init()
269 local testname="$1"
270 local no_tree="$2"
271 if [ -z "$testname" ]; then
272 echo "No test name provided" >&2
273 return 1
274 fi
275 local testroot=`mktemp -d "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXX"`
276 mkdir $testroot/repo
277 git_init $testroot/repo
278 if [ -z "$no_tree" ]; then
279 make_test_tree $testroot/repo
280 (cd $repo && git add .)
281 git_commit $testroot/repo -m "adding the test tree"
282 fi
283 touch $testroot/repo/.git/git-daemon-export-ok
284 echo "$testroot"
287 test_cleanup()
289 local testroot="$1"
291 git_fsck $testroot $testroot/repo
292 ret=$?
293 if [ $ret -ne 0 ]; then
294 return $ret
295 fi
297 rm -rf "$testroot"
300 test_parseargs()
302 while getopts qr: flag; do
303 case $flag in
304 q) export GOT_TEST_QUIET=1
305 ;;
306 r) export GOT_TEST_ROOT=${OPTARG%/}
307 ;;
308 ?) echo "Supported options:"
309 echo " -q: quiet mode"
310 echo " -r PATH: use PATH as test data root directory"
311 exit 2
312 ;;
313 esac
314 done
315 shift $(($OPTIND - 1))
316 regress_run_only="$@"
317 } >&2
319 run_test()
321 testfunc="$1"
323 if [ -n "$regress_run_only" ]; then
324 case "$regress_run_only" in
325 *$testfunc*) ;;
326 *) return ;;
327 esac
328 fi
330 if [ -z "$GOT_TEST_QUIET" ]; then
331 echo -n "$testfunc "
332 fi
333 $testfunc
336 test_done()
338 local testroot="$1"
339 local result="$2"
340 if [ "$result" = "0" ]; then
341 test_cleanup "$testroot" || return 1
342 if [ -z "$GOT_TEST_QUIET" ]; then
343 echo "ok"
344 fi
345 elif echo "$result" | grep -q "^xfail"; then
346 # expected test failure; test reproduces an unfixed bug
347 echo "$result"
348 test_cleanup "$testroot" || return 1
349 else
350 echo "test failed; leaving test data in $testroot"
351 fi