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
28 export MALLOC_OPTIONS=S
30 # Check to see if PLATFORM has a value. PLATFORM is populated when running
31 # via `./configure && make` but this isn't guaranteed if an individual test is
32 # run directly, such as `regress/cmdline/tag.sh`. In such cases, PLATFORM
33 # will be empty, but we still want to use it. Since we test for non-linux
34 # values, only set PLATFORM if we're running on Linu so that the correct
35 # commands are used.
36 [ -z "$PLATFORM" -a "$(uname)" = "Linux" ] && PLATFORM="linux"
38 date()
39 {
40 DATECMD="date"
41 [ "$PLATFORM" != "linux" ] && {
42 command -v "gdate" >/dev/null 2>&1 && {
43 DATECMD="gdate"
44 } || {
45 echo "Couldn't find gdate is GNU coreutils installed?"
46 }
47 }
48 command "$DATECMD" "$@"
49 }
51 ln()
52 {
53 LNCMD="ln"
54 [ "$PLATFORM" != "linux" ] && {
55 command -v "gln" >/dev/null 2>&1 && {
56 LNCMD="gln"
57 } || {
58 echo "Couldn't find gln is GNU coreutils installed?"
59 }
60 }
61 command "$LNCMD" "$@"
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 (cd $repo && git repack -a -q)
118 fi
121 git_commit()
123 local repo="$1"
124 shift
125 (cd $repo && git commit --author="$GOT_AUTHOR" -q -a "$@")
126 maybe_pack_repo $repo
129 git_rm()
131 local repo="$1"
132 shift
133 (cd $repo && git rm -q "$@")
136 git_show_head()
138 local repo="$1"
139 (cd $repo && git show --no-patch --pretty='format:%H')
142 git_show_branch_head()
144 local repo="$1"
145 local branch="$2"
146 (cd $repo && git show --no-patch --pretty='format:%H' $branch)
150 git_show_author_time()
152 local repo="$1"
153 local object="$2"
154 (cd $repo && git show --no-patch --pretty='format:%at' $object)
157 git_show_tagger_time()
159 local repo="$1"
160 local tag="$2"
161 (cd $repo && git cat-file tag $tag | grep ^tagger | \
162 sed -e "s/^tagger $GOT_AUTHOR//" | cut -d' ' -f2)
165 git_show_parent_commit()
167 local repo="$1"
168 local commit="$2"
169 (cd $repo && git show --no-patch --pretty='format:%P' $commit)
172 git_show_tree()
174 local repo="$1"
175 (cd $repo && git show --no-patch --pretty='format:%T')
178 trim_obj_id()
180 local trimcount=$1
181 local id=$2
183 local pat=""
184 while [ "$trimcount" -gt 0 ]; do
185 pat="[0-9a-f]$pat"
186 trimcount=$((trimcount - 1))
187 done
189 echo ${id%$pat}
192 git_commit_tree()
194 local repo="$1"
195 local msg="$2"
196 local tree="$3"
197 (cd $repo && git commit-tree -m "$msg" "$tree")
200 git_fsck()
202 local testroot="$1"
203 local repo="$2"
205 (cd $repo && git fsck --strict \
206 > $testroot/fsck.stdout 2> $testroot/fsck.stderr)
207 ret=$?
208 if [ $ret -ne 0 ]; then
209 echo -n "git fsck: "
210 cat $testroot/fsck.stderr
211 echo "git fsck failed; leaving test data in $testroot"
212 return 1
213 fi
215 return 0
218 make_test_tree()
220 repo="$1"
222 echo alpha > $repo/alpha
223 echo beta > $repo/beta
224 mkdir $repo/gamma
225 echo delta > $repo/gamma/delta
226 mkdir $repo/epsilon
227 echo zeta > $repo/epsilon/zeta
230 make_single_file_repo()
232 repo="$1"
233 file="$2"
235 mkdir $repo
236 git_init $repo
237 echo "this is file $file" > $repo/$file
238 (cd $repo && git add .)
239 git_commit $repo -m "intialize $repo with file $file"
242 get_loose_object_path()
244 local repo="$1"
245 local id="$2"
246 local id0=`trim_obj_id 38 $id`
247 local idrest=`echo ${id#[0-9a-f][0-9a-f]}`
248 echo "$repo/.git/objects/$id0/$idrest"
251 get_blob_id()
253 repo="$1"
254 tree_path="$2"
255 filename="$3"
257 got tree -r $repo -i $tree_path | grep "[0-9a-f] ${filename}$" | \
258 cut -d' ' -f 1
261 test_init()
263 local testname="$1"
264 local no_tree="$2"
265 if [ -z "$testname" ]; then
266 echo "No test name provided" >&2
267 return 1
268 fi
269 local testroot=`mktemp -d "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXX"`
270 mkdir $testroot/repo
271 git_init $testroot/repo
272 if [ -z "$no_tree" ]; then
273 make_test_tree $testroot/repo
274 (cd $repo && git add .)
275 git_commit $testroot/repo -m "adding the test tree"
276 fi
277 touch $testroot/repo/.git/git-daemon-export-ok
278 echo "$testroot"
281 test_cleanup()
283 local testroot="$1"
285 git_fsck $testroot $testroot/repo
286 ret=$?
287 if [ $ret -ne 0 ]; then
288 return $ret
289 fi
291 rm -rf "$testroot"
294 test_parseargs()
296 while getopts qr: flag; do
297 case $flag in
298 q) export GOT_TEST_QUIET=1
299 ;;
300 r) export GOT_TEST_ROOT=$OPTARG
301 ;;
302 ?) echo "Supported options:"
303 echo " -q: quiet mode"
304 echo " -r PATH: use PATH as test data root directory"
305 exit 2
306 ;;
307 esac
308 done
309 } >&2
311 run_test()
313 testfunc="$1"
314 if [ -z "$GOT_TEST_QUIET" ]; then
315 echo -n "$testfunc "
316 fi
317 $testfunc
320 test_done()
322 local testroot="$1"
323 local result="$2"
324 if [ "$result" = "0" ]; then
325 test_cleanup "$testroot" || return 1
326 if [ -z "$GOT_TEST_QUIET" ]; then
327 echo "ok"
328 fi
329 elif echo "$result" | grep -q "^xfail"; then
330 # expected test failure; test reproduces an unfixed bug
331 echo "$result"
332 test_cleanup "$testroot" || return 1
333 else
334 echo "test failed; leaving test data in $testroot"
335 fi