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 # Check to see if PLATFORM has a value. PLATFORM is populated when running
32 # via `./configure && make` but this isn't guaranteed if an individual test is
33 # run directly, such as `regress/cmdline/tag.sh`. In such cases, PLATFORM
34 # will be empty, but we still want to use it. Since we test for non-linux
35 # values, only set PLATFORM if we're running on Linu so that the correct
36 # commands are used.
37 [ -z "$PLATFORM" -a "$(uname)" = "Linux" ] && PLATFORM="linux"
39 date()
40 {
41 DATECMD="date"
42 [ "$PLATFORM" != "linux" ] && {
43 command -v "gdate" >/dev/null 2>&1 && {
44 DATECMD="gdate"
45 } || {
46 echo "Couldn't find gdate is GNU coreutils installed?"
47 }
48 }
49 command "$DATECMD" "$@"
50 }
52 ln()
53 {
54 LNCMD="ln"
55 [ "$PLATFORM" != "linux" ] && {
56 command -v "gln" >/dev/null 2>&1 && {
57 LNCMD="gln"
58 } || {
59 echo "Couldn't find gln is GNU coreutils installed?"
60 }
61 }
62 command "$LNCMD" "$@"
63 }
65 sed()
66 {
67 SEDCMD="sed"
69 # On non-linux systems, the sed command can happily accept "-i ''" as
70 # a valid command to not save backup files for in-place edits.
71 # However, on linux, "-i ''" would be treated as "-i" with a blank
72 # argument, and hence, no file to edit in-place, which is an error.
73 #
74 # Therefore, scan the argument list and remove "-i ''", replacing it
75 # with just "-i".
77 [ "$PLATFORM" = "linux" ] && {
78 for w in "$@"
79 do
80 [ "$w" = "-i" ] && {
81 seen=1
82 continue
83 }
85 [ "$seen" = "1" -a -z "$w" ] && {
86 # Move past -i and ''
87 shift 2
89 command "$SEDCMD" -i "$@"
90 return
91 }
92 done
93 }
94 command "$SEDCMD" "$@"
95 }
98 git_init()
99 {
100 git init -q "$1"
102 # Switch the default branch to match our test expectations if needed.
103 # Only need to change HEAD since 'git init' did not create any refs.
104 # Relying on implementation details of 'git init' is no problem for us.
105 # We want to be alerted when Git changes fundamental assumptions such
106 # as what an empty repository looks like and where the default branch
107 # is set. In such cases Got's own tooling might well need to change
108 # its behaviour, too, and our tests should fail.
109 # TODO: Update all tests to assume 'main' instead of 'master' and
110 # switch to main here, to match Got's own default.
111 echo "ref: refs/heads/master" > "$1/.git/HEAD"
114 maybe_pack_repo()
116 local repo="$1"
117 if [ -n "$GOT_TEST_PACK" ]; then
118 (cd $repo && git repack -a -q)
119 fi
122 git_commit()
124 local repo="$1"
125 shift
126 (cd $repo && git commit --author="$GOT_AUTHOR" -q -a "$@")
127 maybe_pack_repo $repo
130 git_rm()
132 local repo="$1"
133 shift
134 (cd $repo && git rm -q "$@")
137 git_show_head()
139 local repo="$1"
140 (cd $repo && git show --no-patch --pretty='format:%H')
143 git_show_branch_head()
145 local repo="$1"
146 local branch="$2"
147 (cd $repo && git show --no-patch --pretty='format:%H' $branch)
151 git_show_author_time()
153 local repo="$1"
154 local object="$2"
155 (cd $repo && git show --no-patch --pretty='format:%at' $object)
158 git_show_tagger_time()
160 local repo="$1"
161 local tag="$2"
162 (cd $repo && git cat-file tag $tag | grep ^tagger | \
163 sed -e "s/^tagger $GOT_AUTHOR//" | cut -d' ' -f2)
166 git_show_parent_commit()
168 local repo="$1"
169 local commit="$2"
170 (cd $repo && git show --no-patch --pretty='format:%P' $commit)
173 git_show_tree()
175 local repo="$1"
176 (cd $repo && git show --no-patch --pretty='format:%T')
179 trim_obj_id()
181 local trimcount=$1
182 local id=$2
184 local pat=""
185 while [ "$trimcount" -gt 0 ]; do
186 pat="[0-9a-f]$pat"
187 trimcount=$((trimcount - 1))
188 done
190 echo ${id%$pat}
193 git_commit_tree()
195 local repo="$1"
196 local msg="$2"
197 local tree="$3"
198 (cd $repo && git commit-tree -m "$msg" "$tree")
201 git_fsck()
203 local testroot="$1"
204 local repo="$2"
206 (cd $repo && git fsck --strict \
207 > $testroot/fsck.stdout 2> $testroot/fsck.stderr)
208 ret=$?
209 if [ $ret -ne 0 ]; then
210 echo -n "git fsck: "
211 cat $testroot/fsck.stderr
212 echo "git fsck failed; leaving test data in $testroot"
213 return 1
214 fi
216 return 0
219 make_test_tree()
221 repo="$1"
223 echo alpha > $repo/alpha
224 echo beta > $repo/beta
225 mkdir $repo/gamma
226 echo delta > $repo/gamma/delta
227 mkdir $repo/epsilon
228 echo zeta > $repo/epsilon/zeta
231 make_single_file_repo()
233 repo="$1"
234 file="$2"
236 mkdir $repo
237 git_init $repo
238 echo "this is file $file" > $repo/$file
239 (cd $repo && git add .)
240 git_commit $repo -m "intialize $repo with file $file"
243 get_loose_object_path()
245 local repo="$1"
246 local id="$2"
247 local id0=`trim_obj_id 38 $id`
248 local idrest=`echo ${id#[0-9a-f][0-9a-f]}`
249 echo "$repo/.git/objects/$id0/$idrest"
252 get_blob_id()
254 repo="$1"
255 tree_path="$2"
256 filename="$3"
258 got tree -r $repo -i $tree_path | grep "[0-9a-f] ${filename}$" | \
259 cut -d' ' -f 1
262 test_init()
264 local testname="$1"
265 local no_tree="$2"
266 if [ -z "$testname" ]; then
267 echo "No test name provided" >&2
268 return 1
269 fi
270 local testroot=`mktemp -d "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXX"`
271 mkdir $testroot/repo
272 git_init $testroot/repo
273 if [ -z "$no_tree" ]; then
274 make_test_tree $testroot/repo
275 (cd $repo && git add .)
276 git_commit $testroot/repo -m "adding the test tree"
277 fi
278 touch $testroot/repo/.git/git-daemon-export-ok
279 echo "$testroot"
282 test_cleanup()
284 local testroot="$1"
286 git_fsck $testroot $testroot/repo
287 ret=$?
288 if [ $ret -ne 0 ]; then
289 return $ret
290 fi
292 rm -rf "$testroot"
295 test_parseargs()
297 while getopts qr: flag; do
298 case $flag in
299 q) export GOT_TEST_QUIET=1
300 ;;
301 r) export GOT_TEST_ROOT=${OPTARG%/}
302 ;;
303 ?) echo "Supported options:"
304 echo " -q: quiet mode"
305 echo " -r PATH: use PATH as test data root directory"
306 exit 2
307 ;;
308 esac
309 done
310 } >&2
312 run_test()
314 testfunc="$1"
315 if [ -z "$GOT_TEST_QUIET" ]; then
316 echo -n "$testfunc "
317 fi
318 $testfunc
321 test_done()
323 local testroot="$1"
324 local result="$2"
325 if [ "$result" = "0" ]; then
326 test_cleanup "$testroot" || return 1
327 if [ -z "$GOT_TEST_QUIET" ]; then
328 echo "ok"
329 fi
330 elif echo "$result" | grep -q "^xfail"; then
331 # expected test failure; test reproduces an unfixed bug
332 echo "$result"
333 test_cleanup "$testroot" || return 1
334 else
335 echo "test failed; leaving test data in $testroot"
336 fi