portable: regress: improve date wrapper
[got-portable.git] / regress / cmdline / common.sh
blob745539b43da693829d69925c94761edf2731751d
1 #!/bin/sh
3 # Copyright (c) 2019, 2020 Stefan Sperling <stsp@openbsd.org>
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.
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 regress_run_only=""
19 export GIT_AUTHOR_NAME="Flan Hacker"
20 export GIT_AUTHOR_EMAIL="flan_hacker@openbsd.org"
21 export GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME"
22 export GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL"
23 export GOT_AUTHOR="$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>"
24 export GOT_AUTHOR_8="flan_hac"
25 export GOT_AUTHOR_11="flan_hacker"
26 export GOT_LOG_DEFAULT_LIMIT=0
27 export GOT_TEST_ROOT="/tmp"
28 export GOT_IGNORE_GITCONFIG=1
29 export GOT_VERSION_STR=`got --version | cut -d ' ' -f2`
31 export LC_ALL=C
33 export MALLOC_OPTIONS=S
35 # Check to see if PLATFORM has a value. PLATFORM is populated when running
36 # via `./configure && make` but this isn't guaranteed if an individual test is
37 # run directly, such as `regress/cmdline/tag.sh`. In such cases, PLATFORM
38 # will be empty, but we still want to use it. Since we test for non-linux
39 # values, only set PLATFORM if we're running on Linu so that the correct
40 # commands are used.
41 [ -z "$PLATFORM" -a "$(uname)" = "Linux" ] && PLATFORM="linux"
43 if [ "$(date -u -r 86400 +%F 2>/dev/null)" != 1970-01-02 ]; then
44 DATECMD=
45 for p in date gdate; do
46 if [ "$($p -u -d @86400 +%F 2>/dev/null)" = 1970-01-02 ]; then
47 DATECMD=$p
48 break
50 done
51 if [ -z "$DATECMD" ]; then
52 echo "Couldn't find gdate, is GNU coreutils installed?" >&2
53 exit 1
56 date()
58 local flag r u
59 while getopts r:u flag; do
60 case $flag in
61 r) r=$OPTARG ;;
62 u) u=-u ;;
63 ?) exit 1 ;;
64 esac
65 done
66 shift $((OPTIND - 1))
67 command "$DATECMD" $u ${r+-d"@$r"} "$@"
71 sed()
73 SEDCMD="sed"
75 # On non-linux systems, the sed command can happily accept "-i ''" as
76 # a valid command to not save backup files for in-place edits.
77 # However, on linux, "-i ''" would be treated as "-i" with a blank
78 # argument, and hence, no file to edit in-place, which is an error.
80 # Therefore, scan the argument list and remove "-i ''", replacing it
81 # with just "-i".
83 [ "$PLATFORM" = "linux" ] && {
84 for w in "$@"
86 [ "$w" = "-i" ] && {
87 seen=1
88 continue
91 [ "$seen" = "1" -a -z "$w" ] && {
92 # Move past -i and ''
93 shift 2
95 command "$SEDCMD" -i "$@"
96 return
98 done
100 command "$SEDCMD" "$@"
104 git_init()
106 git init -q "$1"
108 # Switch the default branch to match our test expectations if needed.
109 # Only need to change HEAD since 'git init' did not create any refs.
110 # Relying on implementation details of 'git init' is no problem for us.
111 # We want to be alerted when Git changes fundamental assumptions such
112 # as what an empty repository looks like and where the default branch
113 # is set. In such cases Got's own tooling might well need to change
114 # its behaviour, too, and our tests should fail.
115 # TODO: Update all tests to assume 'main' instead of 'master' and
116 # switch to main here, to match Got's own default.
117 echo "ref: refs/heads/master" > "$1/.git/HEAD"
120 maybe_pack_repo()
122 local repo="$1"
123 if [ -n "$GOT_TEST_PACK" ]; then
124 arg=""
125 if [ "$GOT_TEST_PACK" = "ref-delta" ]; then
126 arg="-D"
129 (cd $repo && gotadmin pack -a $arg > /dev/null)
130 (cd $repo && gotadmin cleanup -a -q)
134 git_commit()
136 local repo="$1"
137 shift
138 (cd $repo && git commit --author="$GOT_AUTHOR" -q -a "$@")
139 maybe_pack_repo $repo
142 git_rm()
144 local repo="$1"
145 shift
146 (cd $repo && git rm -q "$@")
149 git_show_head()
151 local repo="$1"
152 (cd $repo && git show --no-patch --pretty='format:%H')
155 git_show_branch_head()
157 local repo="$1"
158 local branch="$2"
159 (cd $repo && git show --no-patch --pretty='format:%H' $branch)
163 git_show_author_time()
165 local repo="$1"
166 local object="$2"
167 (cd $repo && git show --no-patch --pretty='format:%at' $object)
170 git_show_tagger_time()
172 local repo="$1"
173 local tag="$2"
174 (cd $repo && git cat-file tag $tag | grep ^tagger | \
175 sed -e "s/^tagger $GOT_AUTHOR//" | cut -d' ' -f2)
178 git_show_parent_commit()
180 local repo="$1"
181 local commit="$2"
182 (cd $repo && git show --no-patch --pretty='format:%P' $commit)
185 git_show_tree()
187 local repo="$1"
188 (cd $repo && git show --no-patch --pretty='format:%T')
191 trim_obj_id()
193 local trimcount=$1
194 local id=$2
196 local pat=""
197 while [ "$trimcount" -gt 0 ]; do
198 pat="[0-9a-f]$pat"
199 trimcount=$((trimcount - 1))
200 done
202 echo ${id%$pat}
205 git_commit_tree()
207 local repo="$1"
208 local msg="$2"
209 local tree="$3"
210 (cd $repo && git commit-tree -m "$msg" "$tree")
213 git_fsck()
215 local testroot="$1"
216 local repo="$2"
218 (cd $repo && git fsck --strict \
219 > $testroot/fsck.stdout 2> $testroot/fsck.stderr)
220 ret=$?
221 if [ $ret -ne 0 ]; then
222 echo -n "git fsck: "
223 cat $testroot/fsck.stderr
224 echo "git fsck failed; leaving test data in $testroot"
225 return 1
228 return 0
231 make_test_tree()
233 repo="$1"
235 echo alpha > $repo/alpha
236 echo beta > $repo/beta
237 mkdir $repo/gamma
238 echo delta > $repo/gamma/delta
239 mkdir $repo/epsilon
240 echo zeta > $repo/epsilon/zeta
243 make_single_file_repo()
245 repo="$1"
246 file="$2"
248 mkdir $repo
249 git_init $repo
250 echo "this is file $file" > $repo/$file
251 (cd $repo && git add .)
252 git_commit $repo -m "intialize $repo with file $file"
255 get_loose_object_path()
257 local repo="$1"
258 local id="$2"
259 local id0=`trim_obj_id 38 $id`
260 local idrest=`echo ${id#[0-9a-f][0-9a-f]}`
261 echo "$repo/.git/objects/$id0/$idrest"
264 get_blob_id()
266 repo="$1"
267 tree_path="$2"
268 filename="$3"
270 got tree -r $repo -i $tree_path | grep "[0-9a-f] ${filename}$" | \
271 cut -d' ' -f 1
274 test_init()
276 local testname="$1"
277 local no_tree="$2"
278 if [ -z "$testname" ]; then
279 echo "No test name provided" >&2
280 return 1
282 local testroot=`mktemp -d "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXX"`
283 mkdir $testroot/repo
284 git_init $testroot/repo
285 if [ -z "$no_tree" ]; then
286 make_test_tree $testroot/repo
287 (cd $repo && git add .)
288 git_commit $testroot/repo -m "adding the test tree"
290 touch $testroot/repo/.git/git-daemon-export-ok
291 echo "$testroot"
294 test_cleanup()
296 local testroot="$1"
298 git_fsck $testroot $testroot/repo
299 ret=$?
300 if [ $ret -ne 0 ]; then
301 return $ret
304 rm -rf "$testroot"
307 test_parseargs()
309 while getopts qr: flag; do
310 case $flag in
311 q) export GOT_TEST_QUIET=1
313 r) export GOT_TEST_ROOT=${OPTARG%/}
315 ?) echo "Supported options:"
316 echo " -q: quiet mode"
317 echo " -r PATH: use PATH as test data root directory"
318 exit 2
320 esac
321 done
322 shift $(($OPTIND - 1))
323 regress_run_only="$@"
324 } >&2
326 run_test()
328 testfunc="$1"
330 if [ -n "$regress_run_only" ]; then
331 case "$regress_run_only" in
332 *$testfunc*) ;;
333 *) return ;;
334 esac
337 if [ -z "$GOT_TEST_QUIET" ]; then
338 echo -n "$testfunc "
340 $testfunc
343 test_done()
345 local testroot="$1"
346 local result="$2"
347 if [ "$result" = "0" ]; then
348 test_cleanup "$testroot" || return 1
349 if [ -z "$GOT_TEST_QUIET" ]; then
350 echo "ok"
352 elif echo "$result" | grep -q "^xfail"; then
353 # expected test failure; test reproduces an unfixed bug
354 echo "$result"
355 test_cleanup "$testroot" || return 1
356 else
357 echo "test failed; leaving test data in $testroot"