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" -a "$(uname)" = "Linux" ] && 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".
75 [ "$PLATFORM" = "linux" ] && {
76 for w in "$@"
77 do
78 [ "$w" = "-i" ] && {
79 seen=1
80 continue
81 }
83 [ "$seen" = "1" -a -z "$w" ] && {
84 # Move past -i and ''
85 shift 2
87 command "$SEDCMD" -i "$@"
88 return
89 }
90 done
91 }
92 command "$SEDCMD" "$@"
93 }
96 git_init()
97 {
98 git init -q "$1"
100 # Switch the default branch to match our test expectations if needed.
101 # Only need to change HEAD since 'git init' did not create any refs.
102 # Relying on implementation details of 'git init' is no problem for us.
103 # We want to be alerted when Git changes fundamental assumptions such
104 # as what an empty repository looks like and where the default branch
105 # is set. In such cases Got's own tooling might well need to change
106 # its behaviour, too, and our tests should fail.
107 # TODO: Update all tests to assume 'main' instead of 'master' and
108 # switch to main here, to match Got's own default.
109 echo "ref: refs/heads/master" > "$1/.git/HEAD"
112 maybe_pack_repo()
114 local repo="$1"
115 if [ -n "$GOT_TEST_PACK" ]; then
116 (cd $repo && git repack -a -q)
117 fi
120 git_commit()
122 local repo="$1"
123 shift
124 (cd $repo && git commit --author="$GOT_AUTHOR" -q -a "$@")
125 maybe_pack_repo $repo
128 git_rm()
130 local repo="$1"
131 shift
132 (cd $repo && git rm -q "$@")
135 git_show_head()
137 local repo="$1"
138 (cd $repo && git show --no-patch --pretty='format:%H')
141 git_show_branch_head()
143 local repo="$1"
144 local branch="$2"
145 (cd $repo && git show --no-patch --pretty='format:%H' $branch)
149 git_show_author_time()
151 local repo="$1"
152 local object="$2"
153 (cd $repo && git show --no-patch --pretty='format:%at' $object)
156 git_show_tagger_time()
158 local repo="$1"
159 local tag="$2"
160 (cd $repo && git cat-file tag $tag | grep ^tagger | \
161 sed -e "s/^tagger $GOT_AUTHOR//" | cut -d' ' -f2)
164 git_show_parent_commit()
166 local repo="$1"
167 local commit="$2"
168 (cd $repo && git show --no-patch --pretty='format:%P' $commit)
171 git_show_tree()
173 local repo="$1"
174 (cd $repo && git show --no-patch --pretty='format:%T')
177 trim_obj_id()
179 local trimcount=$1
180 local id=$2
182 local pat=""
183 while [ "$trimcount" -gt 0 ]; do
184 pat="[0-9a-f]$pat"
185 trimcount=$((trimcount - 1))
186 done
188 echo ${id%$pat}
191 git_commit_tree()
193 local repo="$1"
194 local msg="$2"
195 local tree="$3"
196 (cd $repo && git commit-tree -m "$msg" "$tree")
199 git_fsck()
201 local testroot="$1"
202 local repo="$2"
204 (cd $repo && git fsck --strict \
205 > $testroot/fsck.stdout 2> $testroot/fsck.stderr)
206 ret=$?
207 if [ $ret -ne 0 ]; then
208 echo -n "git fsck: "
209 cat $testroot/fsck.stderr
210 echo "git fsck failed; leaving test data in $testroot"
211 return 1
212 fi
214 return 0
217 make_test_tree()
219 repo="$1"
221 echo alpha > $repo/alpha
222 echo beta > $repo/beta
223 mkdir $repo/gamma
224 echo delta > $repo/gamma/delta
225 mkdir $repo/epsilon
226 echo zeta > $repo/epsilon/zeta
229 make_single_file_repo()
231 repo="$1"
232 file="$2"
234 mkdir $repo
235 git_init $repo
236 echo "this is file $file" > $repo/$file
237 (cd $repo && git add .)
238 git_commit $repo -m "intialize $repo with file $file"
241 get_loose_object_path()
243 local repo="$1"
244 local id="$2"
245 local id0=`trim_obj_id 38 $id`
246 local idrest=`echo ${id#[0-9a-f][0-9a-f]}`
247 echo "$repo/.git/objects/$id0/$idrest"
250 get_blob_id()
252 repo="$1"
253 tree_path="$2"
254 filename="$3"
256 got tree -r $repo -i $tree_path | grep "[0-9a-f] ${filename}$" | \
257 cut -d' ' -f 1
260 test_init()
262 local testname="$1"
263 local no_tree="$2"
264 if [ -z "$testname" ]; then
265 echo "No test name provided" >&2
266 return 1
267 fi
268 local testroot=`mktemp -d "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXX"`
269 mkdir $testroot/repo
270 git_init $testroot/repo
271 if [ -z "$no_tree" ]; then
272 make_test_tree $testroot/repo
273 (cd $repo && git add .)
274 git_commit $testroot/repo -m "adding the test tree"
275 fi
276 touch $testroot/repo/.git/git-daemon-export-ok
277 echo "$testroot"
280 test_cleanup()
282 local testroot="$1"
284 git_fsck $testroot $testroot/repo
285 ret=$?
286 if [ $ret -ne 0 ]; then
287 return $ret
288 fi
290 rm -rf "$testroot"
293 test_parseargs()
295 while getopts qr: flag; do
296 case $flag in
297 q) export GOT_TEST_QUIET=1
298 ;;
299 r) export GOT_TEST_ROOT=$OPTARG
300 ;;
301 ?) echo "Supported options:"
302 echo " -q: quiet mode"
303 echo " -r PATH: use PATH as test data root directory"
304 exit 2
305 ;;
306 esac
307 done
308 } >&2
310 run_test()
312 testfunc="$1"
313 if [ -z "$GOT_TEST_QUIET" ]; then
314 echo -n "$testfunc "
315 fi
316 $testfunc
319 test_done()
321 local testroot="$1"
322 local result="$2"
323 if [ "$result" = "0" ]; then
324 test_cleanup "$testroot" || return 1
325 if [ -z "$GOT_TEST_QUIET" ]; then
326 echo "ok"
327 fi
328 elif echo "$result" | grep -q "^xfail"; then
329 # expected test failure; test reproduces an unfixed bug
330 echo "$result"
331 test_cleanup "$testroot" || return 1
332 else
333 echo "test failed; leaving test data in $testroot"
334 fi