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 # Check to see if PLATFORM has a value. PLATFORM is populated when running
30 # via `./configure && make` but this isn't guaranteed if an individual test is
31 # run directly, such as `regress/cmdline/tag.sh`. In such cases, PLATFORM
32 # will be empty, but we still want to use it. Since we test for non-linux
33 # values, only set PLATFORM if we're running on Linu so that the correct
34 # commands are used.
35 [ -z "$PLATFORM" ] && [ "$OSTYPE" = "linux-gnu" ] && PLATFORM="linux"
37 date()
38 {
39 DATECMD="date"
40 [ "$PLATFORM" != "linux" ] && {
41 command -v "gdate" >/dev/null 2>&1 && {
42 DATECMD="gdate"
43 } || {
44 echo "Couldn't find `gdate` is GNU coreutils installed?"
45 }
46 }
47 command "$DATECMD" "$@"
48 }
50 ln()
51 {
52 LNCMD="ln"
53 [ "$PLATFORM" != "linux" ] && {
54 command -v "gln" >/dev/null 2>&1 && {
55 LNCMD="gln"
56 } || {
57 echo "Couldn't find `gln` is GNU coreutils installed?"
58 }
59 }
60 command "$LNCMD" "$@"
61 }
63 sed()
64 {
65 SEDCMD="sed"
67 # On non-linux systems, the sed command can happily accept "-i ''" as
68 # a valid command to not save backup files for in-place edits.
69 # However, on linux, "-i ''" would be treated as "-i" with a blank
70 # argument, and hence, no file to edit in-place, which is an error.
71 #
72 # Therefore, scan the argument list and remove "-i ''", replacing it
73 # with just "-i".
74 [ "$PLATFORM" = "linux" ] && {
75 set -- "$@"
76 i=1
77 while [ "$i" -le "$#" ]; do
78 m=$((i + 1))
79 [ "${!i}" = "-i" ] && [ -z "${!m}" ] && {
80 shift 2
81 command "$SEDCMD" -i "$@"
82 return
83 }
84 i=$((i + 1))
85 done
86 }
87 command "$SEDCMD" "$@"
88 }
91 git_init()
92 {
93 git init -q "$1"
95 # Switch the default branch to match our test expectations if needed.
96 # Only need to change HEAD since 'git init' did not create any refs.
97 # Relying on implementation details of 'git init' is no problem for us.
98 # We want to be alerted when Git changes fundamental assumptions such
99 # as what an empty repository looks like and where the default branch
100 # is set. In such cases Got's own tooling might well need to change
101 # its behaviour, too, and our tests should fail.
102 # TODO: Update all tests to assume 'main' instead of 'master' and
103 # switch to main here, to match Got's own default.
104 echo "ref: refs/heads/master" > "$1/.git/HEAD"
107 maybe_pack_repo()
109 local repo="$1"
110 if [ -n "$GOT_TEST_PACK" ]; then
111 (cd $repo && git repack -a -q)
112 fi
115 git_commit()
117 local repo="$1"
118 shift
119 (cd $repo && git commit --author="$GOT_AUTHOR" -q -a "$@")
120 maybe_pack_repo $repo
123 git_rm()
125 local repo="$1"
126 shift
127 (cd $repo && git rm -q "$@")
130 git_show_head()
132 local repo="$1"
133 (cd $repo && git show --no-patch --pretty='format:%H')
136 git_show_branch_head()
138 local repo="$1"
139 local branch="$2"
140 (cd $repo && git show --no-patch --pretty='format:%H' $branch)
144 git_show_author_time()
146 local repo="$1"
147 local object="$2"
148 (cd $repo && git show --no-patch --pretty='format:%at' $object)
151 git_show_tagger_time()
153 local repo="$1"
154 local tag="$2"
155 (cd $repo && git cat-file tag $tag | grep ^tagger | \
156 sed -e "s/^tagger $GOT_AUTHOR//" | cut -d' ' -f2)
159 git_show_parent_commit()
161 local repo="$1"
162 local commit="$2"
163 (cd $repo && git show --no-patch --pretty='format:%P' $commit)
166 git_show_tree()
168 local repo="$1"
169 (cd $repo && git show --no-patch --pretty='format:%T')
172 trim_obj_id()
174 local trimcount=$1
175 local id=$2
177 local pat=""
178 while [ "$trimcount" -gt 0 ]; do
179 pat="[0-9a-f]$pat"
180 trimcount=$((trimcount - 1))
181 done
183 echo ${id%$pat}
186 git_commit_tree()
188 local repo="$1"
189 local msg="$2"
190 local tree="$3"
191 (cd $repo && git commit-tree -m "$msg" "$tree")
194 git_fsck()
196 local testroot="$1"
197 local repo="$2"
199 (cd $repo && git fsck --strict \
200 > $testroot/fsck.stdout 2> $testroot/fsck.stderr)
201 ret=$?
202 if [ $ret -ne 0 ]; then
203 echo -n "git fsck: "
204 cat $testroot/fsck.stderr
205 echo "git fsck failed; leaving test data in $testroot"
206 return 1
207 fi
209 return 0
212 make_test_tree()
214 repo="$1"
216 echo alpha > $repo/alpha
217 echo beta > $repo/beta
218 mkdir $repo/gamma
219 echo delta > $repo/gamma/delta
220 mkdir $repo/epsilon
221 echo zeta > $repo/epsilon/zeta
224 make_single_file_repo()
226 repo="$1"
227 file="$2"
229 mkdir $repo
230 git_init $repo
231 echo "this is file $file" > $repo/$file
232 (cd $repo && git add .)
233 git_commit $repo -m "intialize $repo with file $file"
236 get_loose_object_path()
238 local repo="$1"
239 local id="$2"
240 local id0=`trim_obj_id 38 $id`
241 local idrest=`echo ${id#[0-9a-f][0-9a-f]}`
242 echo "$repo/.git/objects/$id0/$idrest"
245 get_blob_id()
247 repo="$1"
248 tree_path="$2"
249 filename="$3"
251 got tree -r $repo -i $tree_path | grep "[0-9a-f] ${filename}$" | \
252 cut -d' ' -f 1
255 test_init()
257 local testname="$1"
258 local no_tree="$2"
259 if [ -z "$testname" ]; then
260 echo "No test name provided" >&2
261 return 1
262 fi
263 local testroot=`mktemp -d "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXX"`
264 mkdir $testroot/repo
265 git_init $testroot/repo
266 if [ -z "$no_tree" ]; then
267 make_test_tree $testroot/repo
268 (cd $repo && git add .)
269 git_commit $testroot/repo -m "adding the test tree"
270 fi
271 touch $testroot/repo/.git/git-daemon-export-ok
272 echo "$testroot"
275 test_cleanup()
277 local testroot="$1"
279 git_fsck $testroot $testroot/repo
280 ret=$?
281 if [ $ret -ne 0 ]; then
282 return $ret
283 fi
285 rm -rf "$testroot"
288 test_parseargs()
290 while getopts qr: flag; do
291 case $flag in
292 q) export GOT_TEST_QUIET=1
293 ;;
294 r) export GOT_TEST_ROOT=$OPTARG
295 ;;
296 ?) echo "Supported options:"
297 echo " -q: quiet mode"
298 echo " -r PATH: use PATH as test data root directory"
299 exit 2
300 ;;
301 esac
302 done
303 } >&2
305 run_test()
307 testfunc="$1"
308 if [ -z "$GOT_TEST_QUIET" ]; then
309 echo -n "$testfunc "
310 fi
311 $testfunc
314 test_done()
316 local testroot="$1"
317 local result="$2"
318 if [ "$result" = "0" ]; then
319 test_cleanup "$testroot" || return 1
320 if [ -z "$GOT_TEST_QUIET" ]; then
321 echo "ok"
322 fi
323 elif echo "$result" | grep -q "^xfail"; then
324 # expected test failure; test reproduces an unfixed bug
325 echo "$result"
326 test_cleanup "$testroot" || return 1
327 else
328 echo "test failed; leaving test data in $testroot"
329 fi