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 LC_ALL=C
33 export MALLOC_OPTIONS=S
35 # Check to see if PLATFORM has a value. PLATFORM is populated when running
36 # via `./configure && make` but this isn't guaranteed if an individual test is
37 # run directly, such as `regress/cmdline/tag.sh`. In such cases, PLATFORM
38 # will be empty, but we still want to use it. Since we test for non-linux
39 # values, only set PLATFORM if we're running on Linu so that the correct
40 # commands are used.
41 [ -z "$PLATFORM" -a "$(uname)" = "Linux" ] && PLATFORM="linux"
43 [ "$(date -u -r 86400 +%F 2>/dev/null)" = 1970-01-02 ] || date()
44 {
45 DATECMD="date"
46 [ "$PLATFORM" != "linux" ] && {
47 command -v "gdate" >/dev/null 2>&1 && {
48 DATECMD="gdate"
49 } || {
50 echo "Couldn't find gdate is GNU coreutils installed?"
51 }
52 }
54 local flag r u
55 while getopts r:u flag; do
56 case $flag in
57 r) r=$OPTARG ;;
58 u) u=-u ;;
59 ?) exit 1 ;;
60 esac
61 done
62 shift $((OPTIND - 1))
63 command "$DATECMD" $u ${r+-d"@$r"} "$@"
64 }
66 sed()
67 {
68 SEDCMD="sed"
70 # On non-linux systems, the sed command can happily accept "-i ''" as
71 # a valid command to not save backup files for in-place edits.
72 # However, on linux, "-i ''" would be treated as "-i" with a blank
73 # argument, and hence, no file to edit in-place, which is an error.
74 #
75 # Therefore, scan the argument list and remove "-i ''", replacing it
76 # with just "-i".
78 [ "$PLATFORM" = "linux" ] && {
79 for w in "$@"
80 do
81 [ "$w" = "-i" ] && {
82 seen=1
83 continue
84 }
86 [ "$seen" = "1" -a -z "$w" ] && {
87 # Move past -i and ''
88 shift 2
90 command "$SEDCMD" -i "$@"
91 return
92 }
93 done
94 }
95 command "$SEDCMD" "$@"
96 }
99 git_init()
101 git init -q "$1"
103 # Switch the default branch to match our test expectations if needed.
104 # Only need to change HEAD since 'git init' did not create any refs.
105 # Relying on implementation details of 'git init' is no problem for us.
106 # We want to be alerted when Git changes fundamental assumptions such
107 # as what an empty repository looks like and where the default branch
108 # is set. In such cases Got's own tooling might well need to change
109 # its behaviour, too, and our tests should fail.
110 # TODO: Update all tests to assume 'main' instead of 'master' and
111 # switch to main here, to match Got's own default.
112 echo "ref: refs/heads/master" > "$1/.git/HEAD"
115 maybe_pack_repo()
117 local repo="$1"
118 if [ -n "$GOT_TEST_PACK" ]; then
119 arg=""
120 if [ "$GOT_TEST_PACK" = "ref-delta" ]; then
121 arg="-D"
122 fi
124 (cd $repo && gotadmin pack -a $arg > /dev/null)
125 (cd $repo && gotadmin cleanup -a -q)
126 fi
129 git_commit()
131 local repo="$1"
132 shift
133 (cd $repo && git commit --author="$GOT_AUTHOR" -q -a "$@")
134 maybe_pack_repo $repo
137 git_rm()
139 local repo="$1"
140 shift
141 (cd $repo && git rm -q "$@")
144 git_show_head()
146 local repo="$1"
147 (cd $repo && git show --no-patch --pretty='format:%H')
150 git_show_branch_head()
152 local repo="$1"
153 local branch="$2"
154 (cd $repo && git show --no-patch --pretty='format:%H' $branch)
158 git_show_author_time()
160 local repo="$1"
161 local object="$2"
162 (cd $repo && git show --no-patch --pretty='format:%at' $object)
165 git_show_tagger_time()
167 local repo="$1"
168 local tag="$2"
169 (cd $repo && git cat-file tag $tag | grep ^tagger | \
170 sed -e "s/^tagger $GOT_AUTHOR//" | cut -d' ' -f2)
173 git_show_parent_commit()
175 local repo="$1"
176 local commit="$2"
177 (cd $repo && git show --no-patch --pretty='format:%P' $commit)
180 git_show_tree()
182 local repo="$1"
183 (cd $repo && git show --no-patch --pretty='format:%T')
186 trim_obj_id()
188 local trimcount=$1
189 local id=$2
191 local pat=""
192 while [ "$trimcount" -gt 0 ]; do
193 pat="[0-9a-f]$pat"
194 trimcount=$((trimcount - 1))
195 done
197 echo ${id%$pat}
200 git_commit_tree()
202 local repo="$1"
203 local msg="$2"
204 local tree="$3"
205 (cd $repo && git commit-tree -m "$msg" "$tree")
208 git_fsck()
210 local testroot="$1"
211 local repo="$2"
213 (cd $repo && git fsck --strict \
214 > $testroot/fsck.stdout 2> $testroot/fsck.stderr)
215 ret=$?
216 if [ $ret -ne 0 ]; then
217 echo -n "git fsck: "
218 cat $testroot/fsck.stderr
219 echo "git fsck failed; leaving test data in $testroot"
220 return 1
221 fi
223 return 0
226 make_test_tree()
228 repo="$1"
230 echo alpha > $repo/alpha
231 echo beta > $repo/beta
232 mkdir $repo/gamma
233 echo delta > $repo/gamma/delta
234 mkdir $repo/epsilon
235 echo zeta > $repo/epsilon/zeta
238 make_single_file_repo()
240 repo="$1"
241 file="$2"
243 mkdir $repo
244 git_init $repo
245 echo "this is file $file" > $repo/$file
246 (cd $repo && git add .)
247 git_commit $repo -m "intialize $repo with file $file"
250 get_loose_object_path()
252 local repo="$1"
253 local id="$2"
254 local id0=`trim_obj_id 38 $id`
255 local idrest=`echo ${id#[0-9a-f][0-9a-f]}`
256 echo "$repo/.git/objects/$id0/$idrest"
259 get_blob_id()
261 repo="$1"
262 tree_path="$2"
263 filename="$3"
265 got tree -r $repo -i $tree_path | grep "[0-9a-f] ${filename}$" | \
266 cut -d' ' -f 1
269 test_init()
271 local testname="$1"
272 local no_tree="$2"
273 if [ -z "$testname" ]; then
274 echo "No test name provided" >&2
275 return 1
276 fi
277 local testroot=`mktemp -d "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXX"`
278 mkdir $testroot/repo
279 git_init $testroot/repo
280 if [ -z "$no_tree" ]; then
281 make_test_tree $testroot/repo
282 (cd $repo && git add .)
283 git_commit $testroot/repo -m "adding the test tree"
284 fi
285 touch $testroot/repo/.git/git-daemon-export-ok
286 echo "$testroot"
289 test_cleanup()
291 local testroot="$1"
293 git_fsck $testroot $testroot/repo
294 ret=$?
295 if [ $ret -ne 0 ]; then
296 return $ret
297 fi
299 rm -rf "$testroot"
302 test_parseargs()
304 while getopts qr: flag; do
305 case $flag in
306 q) export GOT_TEST_QUIET=1
307 ;;
308 r) export GOT_TEST_ROOT=${OPTARG%/}
309 ;;
310 ?) echo "Supported options:"
311 echo " -q: quiet mode"
312 echo " -r PATH: use PATH as test data root directory"
313 exit 2
314 ;;
315 esac
316 done
317 shift $(($OPTIND - 1))
318 regress_run_only="$@"
319 } >&2
321 run_test()
323 testfunc="$1"
325 if [ -n "$regress_run_only" ]; then
326 case "$regress_run_only" in
327 *$testfunc*) ;;
328 *) return ;;
329 esac
330 fi
332 if [ -z "$GOT_TEST_QUIET" ]; then
333 echo -n "$testfunc "
334 fi
335 $testfunc
338 test_done()
340 local testroot="$1"
341 local result="$2"
342 if [ "$result" = "0" ]; then
343 test_cleanup "$testroot" || return 1
344 if [ -z "$GOT_TEST_QUIET" ]; then
345 echo "ok"
346 fi
347 elif echo "$result" | grep -q "^xfail"; then
348 # expected test failure; test reproduces an unfixed bug
349 echo "$result"
350 test_cleanup "$testroot" || return 1
351 else
352 echo "test failed; leaving test data in $testroot"
353 fi