#!/bin/sh
# Checks how cgit shows a commit date, which is either an age relative to now
# or a calendar date in the format the config names, and in which timezone.
# The config is rewritten between cases so that each one names only the
# settings it is about and leaves the rest at their defaults.
test_description='Check the date display options'
. ./setup.sh
# The repo holds the commit mkrepo dates in 2005 at +0200, an old one at +0000
# and a fresh one, so that the log shows both the relative rendering and the
# calendar fallback that takes over past the two week cutoff. Each tag is
# dated in the upper half of an age bucket, where the bucket below would take
# it if its bound slipped, and one is dated a day from now. The minutes stop
# 20 seconds short of 90, which a slow run still rounds to.
test_expect_success 'set up a dated repo' '
mkrepo repos/dated 1 &&
(
cd repos/dated &&
GIT_AUTHOR_DATE="@1112911993 +0000" \
GIT_COMMITTER_DATE="@1112911993 +0000" \
git commit --allow-empty -m old &&
unset GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
git commit --allow-empty -m fresh &&
now=$(git log -1 --format=%ct) &&
for age in mins:5380 hours:108000 days:864000 weeks:3628800 \
months:47304000 years:94608000 future:-86400
do
GIT_COMMITTER_DATE="@$((now - ${age#*:})) +0000" \
git tag -a -m ${age%%:*} ${age%%:*} || return 1
done
)
'
daterc() {
cgitrc_repo daterc dated "$@"
}
test_expect_success 'relative dates are on by default' '
daterc &&
cgit_ok daterc "url=dated/refs/" >tmp &&
grep "class=.age-" tmp
'
test_expect_success 'each age bucket counts in its own unit' '
daterc &&
cgit_ok daterc "url=dated/refs/" >tmp &&
grep ">mins.*" tmp &&
grep ">hours.*" tmp &&
grep ">days.*" tmp &&
grep ">weeks.*" tmp &&
grep ">months.*" tmp &&
grep ">years.*" tmp
'
test_expect_success 'a date past now reads as no age at all' '
daterc &&
cgit_ok daterc "url=dated/refs/" >tmp &&
grep ">future.*" tmp
'
# The age is what shows, while the element keeps the date itself, strict in
# the attribute a machine reads and plain in the one a reader hovers over.
test_expect_success 'an age keeps the date it stands for' '
iso=$(git -C repos/dated for-each-ref --format="%(taggerdate:iso8601)" refs/tags/years) &&
strict=$(git -C repos/dated for-each-ref --format="%(taggerdate:iso-strict)" refs/tags/years) &&
test -n "$iso" &&
daterc &&
cgit_ok daterc "url=dated/refs/" >tmp &&
grep "" tmp
'
test_expect_success 'enable-relative-dates=0 shows a calendar date' '
daterc "enable-relative-dates=0" &&
cgit_ok daterc "url=dated/refs/" >tmp &&
! grep "class=.age-" tmp &&
grep "[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]<" tmp
'
test_expect_success 'the index and log columns follow the same flag' '
daterc "enable-relative-dates=0" &&
cgit_ok daterc "" >tmp &&
grep "[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]<" tmp &&
! grep "class=.age-" tmp &&
cgit_ok daterc "url=dated/log/" >tmp &&
grep ">2005-04-07<" tmp &&
! grep "class=.age-" tmp
'
test_expect_success 'date-format selects the calendar format' '
daterc "enable-relative-dates=0" "date-format=iso8601" &&
cgit_ok daterc "url=dated/refs/" >tmp &&
grep ">[0-9-]* [0-9][0-9]:[0-9][0-9]:[0-9][0-9] +0000<" tmp
'
test_expect_success 'date-format accepts a strftime format, on the index too' '
daterc "enable-relative-dates=0" "date-format=format:%Y/%m/%d" &&
cgit_ok daterc "url=dated/refs/" >tmp &&
grep ">[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]<" tmp &&
cgit_ok daterc "" >tmp &&
grep ">[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]<" tmp
'
test_expect_success 'an unknown date-format falls back to short' '
daterc "enable-relative-dates=0" "date-format=nonsense" &&
cgit_ok daterc "url=dated/refs/" >tmp &&
grep "[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]<" tmp &&
daterc "enable-relative-dates=0" "date-format=format:" &&
cgit_ok daterc "url=dated/refs/" >tmp &&
grep "[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]<" tmp
'
test_expect_success 'a date-format git would refuse falls back to short' '
daterc "enable-relative-dates=0" "date-format=local-local" &&
cgit_ok daterc "url=dated/refs/" >tmp &&
grep "[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]<" tmp
'
# The tests run under TZ=UTC, and the first commit was made at +0200, so the
# local time differs from the commit's own by two hours.
test_expect_success 'date-format=local is the default format in local time' '
daterc "enable-relative-dates=0" "date-format=local" &&
cgit_ok daterc "url=dated/log/" >tmp &&
grep ">Fri Apr 1 11:14:15 2005<" tmp
'
# A POSIX timezone string needs no zone files, so EST5 works anywhere.
test_expect_success 'a -local date-format shows the server timezone' '
daterc "enable-relative-dates=0" "date-format=iso8601-local" &&
(
TZ=EST5 &&
export TZ &&
cgit_ok daterc "url=dated/log/"
) >tmp &&
grep ">2005-04-07 17:13:13 -0500<" tmp
'
test_expect_success 'local-time shows commit times in the server timezone' '
sha=$(git -C repos/dated rev-parse HEAD~1) &&
daterc "local-time=1" &&
(
TZ=EST5 &&
export TZ &&
cgit_ok daterc "url=dated/commit/&id=$sha"
) >tmp &&
grep ">2005-04-07 17:13:13 -0500<" tmp &&
daterc &&
(
TZ=EST5 &&
export TZ &&
cgit_ok daterc "url=dated/commit/&id=$sha"
) >tmp &&
grep ">2005-04-07 22:13:13 +0000<" tmp
'
test_expect_success 'date-format still formats the log fallback' '
daterc "date-format=rfc2822" &&
cgit_ok daterc "url=dated/log/" >tmp &&
grep "class=.age-" tmp &&
grep ">[A-Z][a-z][a-z], [0-9]* Apr 2005 " tmp
'
test_expect_success 'date-format leaves the commit page on ISO 8601' '
daterc "date-format=rfc2822" &&
sha=$(git -C repos/dated rev-parse HEAD~1) &&
cgit_ok daterc "url=dated/commit/&id=$sha" >tmp &&
grep "2005-04-07 [0-9:]* +0000" tmp &&
! grep "Apr 2005" tmp
'
# Only a browser shows the wrap itself, so this checks the rule against it.
test_expect_success 'a date in a listing stays on one line' '
sed -n "/^div#cgit time.age-mins,$/,/^}/p" \
"$TEST_OUTPUT_DIRECTORY/../assets/cgit.css" >rule &&
grep "^div#cgit table.list time {$" rule &&
grep "^ white-space: nowrap;$" rule
'
# A pushed date or zone outside what git's date code accepts used to end the
# summary, refs and log pages inside git. Each is now shown as the epoch or
# as UTC, the way git's own log shows them.
test_expect_success 'odd dates and zones in pushed objects render' '
test_create_repo repos/odd &&
(
cd repos/odd &&
test_commit base &&
c=$(git rev-parse HEAD) &&
t=$(printf "object %s\\ntype commit\\ntag early\\ntagger E 1 -0100\\n\\nearly\\n" $c |
git hash-object -t tag -w --stdin) &&
git update-ref refs/tags/early $t &&
tree=$(git rev-parse HEAD^{tree}) &&
far=$(printf "tree %s\\nauthor A 9223372036854775808 +0000\\ncommitter A 9223372036854775808 +0000\\n\\nfar\\n" $tree |
git hash-object -t commit -w --literally --stdin) &&
git update-ref refs/heads/far $far &&
zone=$(printf "tree %s\\nauthor A 1700000000 -2147483648\\ncommitter A 1700000000 +99999999999\\n\\nzone\\n" $tree |
git hash-object -t commit -w --literally --stdin) &&
git update-ref refs/heads/zone $zone
) &&
cgitrc_repo oddrc odd &&
for q in "url=odd/" "url=odd/refs/" "url=odd/tag/&h=early" \
"url=odd/log/&h=far" "url=odd/commit/&h=far" "url=odd/commit/&h=zone" \
"url=odd/atom/&h=far"
do
cgit_rc oddrc "$q" >tmp 2>err &&
grep "^Status: 200" tmp &&
! grep "Unable to complete" tmp &&
! grep "Timestamp" err || return 1
done &&
cgit_rc oddrc "url=odd/commit/&h=far" >tmp &&
grep "1970-01-01" tmp
'
test_done