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 date()
30 {
31 DATECMD="date"
32 [ "$PLATFORM" != "linux" ] && DATECMD="gdate"
34 command "$DATECMD" "$@"
35 }
37 ln()
38 {
39 LNCMD="ln"
40 [ "$PLATFORM" != "linux" ] && LNCMD="gln"
42 command "$LNCMD" "$@"
43 }
45 sed()
46 {
47 SEDCMD="sed"
49 # On non-linux systems, the sed command can happily accept "-i ''" as
50 # a valid command to not save backup files for in-place edits.
51 # However, on linux, "-i ''" would be treated as "-i" with a blank
52 # argument, and hence, no file to edit in-place, which is an error.
53 #
54 # Therefore, scan the argument list and remove "-i ''", replacing it
55 # with just "-i".
56 [ "$PLATFORM" = "linux" ] && {
57 set -- "$@"
58 i=1
59 while [ "$i" -le "$#" ]; do
60 m=$((i + 1))
61 [ "${!i}" = "-i" ] && [ -z "${!m}" ] && {
62 shift 2
63 command "$SEDCMD" -i "$@"
64 return
65 }
66 i=$((i + 1))
67 done
68 }
69 command "$SEDCMD" "$@"
70 }
73 git_init()
74 {
75 git init -q "$1"
77 # Switch the default branch to match our test expectations if needed.
78 # Only need to change HEAD since 'git init' did not create any refs.
79 # Relying on implementation details of 'git init' is no problem for us.
80 # We want to be alerted when Git changes fundamental assumptions such
81 # as what an empty repository looks like and where the default branch
82 # is set. In such cases Got's own tooling might well need to change
83 # its behaviour, too, and our tests should fail.
84 # TODO: Update all tests to assume 'main' instead of 'master' and
85 # switch to main here, to match Got's own default.
86 echo "ref: refs/heads/master" > "$1/.git/HEAD"
87 }
89 maybe_pack_repo()
90 {
91 local repo="$1"
92 if [ -n "$GOT_TEST_PACK" ]; then
93 (cd $repo && git repack -a -q)
94 fi
95 }
97 git_commit()
98 {
99 local repo="$1"
100 shift
101 (cd $repo && git commit --author="$GOT_AUTHOR" -q -a "$@")
102 maybe_pack_repo $repo
105 git_rm()
107 local repo="$1"
108 shift
109 (cd $repo && git rm -q "$@")
112 git_show_head()
114 local repo="$1"
115 (cd $repo && git show --no-patch --pretty='format:%H')
118 git_show_branch_head()
120 local repo="$1"
121 local branch="$2"
122 (cd $repo && git show --no-patch --pretty='format:%H' $branch)
126 git_show_author_time()
128 local repo="$1"
129 local object="$2"
130 (cd $repo && git show --no-patch --pretty='format:%at' $object)
133 git_show_tagger_time()
135 local repo="$1"
136 local tag="$2"
137 (cd $repo && git cat-file tag $tag | grep ^tagger | \
138 sed -e "s/^tagger $GOT_AUTHOR//" | cut -d' ' -f2)
141 git_show_parent_commit()
143 local repo="$1"
144 local commit="$2"
145 (cd $repo && git show --no-patch --pretty='format:%P' $commit)
148 git_show_tree()
150 local repo="$1"
151 (cd $repo && git show --no-patch --pretty='format:%T')
154 trim_obj_id()
156 local trimcount=$1
157 local id=$2
159 local pat=""
160 while [ "$trimcount" -gt 0 ]; do
161 pat="[0-9a-f]$pat"
162 trimcount=$((trimcount - 1))
163 done
165 echo ${id%$pat}
168 git_commit_tree()
170 local repo="$1"
171 local msg="$2"
172 local tree="$3"
173 (cd $repo && git commit-tree -m "$msg" "$tree")
176 git_fsck()
178 local testroot="$1"
179 local repo="$2"
181 (cd $repo && git fsck --strict \
182 > $testroot/fsck.stdout 2> $testroot/fsck.stderr)
183 ret=$?
184 if [ $ret -ne 0 ]; then
185 echo -n "git fsck: "
186 cat $testroot/fsck.stderr
187 echo "git fsck failed; leaving test data in $testroot"
188 return 1
189 fi
191 return 0
194 make_test_tree()
196 repo="$1"
198 echo alpha > $repo/alpha
199 echo beta > $repo/beta
200 mkdir $repo/gamma
201 echo delta > $repo/gamma/delta
202 mkdir $repo/epsilon
203 echo zeta > $repo/epsilon/zeta
206 make_single_file_repo()
208 repo="$1"
209 file="$2"
211 mkdir $repo
212 git_init $repo
213 echo "this is file $file" > $repo/$file
214 (cd $repo && git add .)
215 git_commit $repo -m "intialize $repo with file $file"
218 get_loose_object_path()
220 local repo="$1"
221 local id="$2"
222 local id0=`trim_obj_id 38 $id`
223 local idrest=`echo ${id#[0-9a-f][0-9a-f]}`
224 echo "$repo/.git/objects/$id0/$idrest"
227 get_blob_id()
229 repo="$1"
230 tree_path="$2"
231 filename="$3"
233 got tree -r $repo -i $tree_path | grep "[0-9a-f] ${filename}$" | \
234 cut -d' ' -f 1
237 test_init()
239 local testname="$1"
240 local no_tree="$2"
241 if [ -z "$testname" ]; then
242 echo "No test name provided" >&2
243 return 1
244 fi
245 local testroot=`mktemp -d "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXX"`
246 mkdir $testroot/repo
247 git_init $testroot/repo
248 if [ -z "$no_tree" ]; then
249 make_test_tree $testroot/repo
250 (cd $repo && git add .)
251 git_commit $testroot/repo -m "adding the test tree"
252 fi
253 touch $testroot/repo/.git/git-daemon-export-ok
254 echo "$testroot"
257 test_cleanup()
259 local testroot="$1"
261 git_fsck $testroot $testroot/repo
262 ret=$?
263 if [ $ret -ne 0 ]; then
264 return $ret
265 fi
267 rm -rf "$testroot"
270 test_parseargs()
272 while getopts qr: flag; do
273 case $flag in
274 q) export GOT_TEST_QUIET=1
275 ;;
276 r) export GOT_TEST_ROOT=$OPTARG
277 ;;
278 ?) echo "Supported options:"
279 echo " -q: quiet mode"
280 echo " -r PATH: use PATH as test data root directory"
281 exit 2
282 ;;
283 esac
284 done
285 } >&2
287 run_test()
289 testfunc="$1"
290 if [ -z "$GOT_TEST_QUIET" ]; then
291 echo -n "$testfunc "
292 fi
293 $testfunc
296 test_done()
298 local testroot="$1"
299 local result="$2"
300 if [ "$result" = "0" ]; then
301 test_cleanup "$testroot" || return 1
302 if [ -z "$GOT_TEST_QUIET" ]; then
303 echo "ok"
304 fi
305 elif echo "$result" | grep -q "^xfail"; then
306 # expected test failure; test reproduces an unfixed bug
307 echo "$result"
308 test_cleanup "$testroot" || return 1
309 else
310 echo "test failed; leaving test data in $testroot"
311 fi