#!/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. 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() { { echo "virtual-root=/" echo "cache-size=0" printf '%s\n' "$@" echo "repo.url=dated" echo "repo.path=$PWD/repos/dated/.git" } >daterc } test_expect_success 'relative dates are on by default' ' daterc && cgit_rc 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_rc 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_rc daterc "" >tmp && ! grep "class=.age-" tmp && cgit_rc daterc "url=dated/log/" >tmp && ! grep "class=.age-" tmp ' test_expect_success 'date-format selects the calendar format' ' daterc "enable-relative-dates=0" "date-format=iso8601" && cgit_rc 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' ' daterc "enable-relative-dates=0" "date-format=format:%Y/%m/%d" && cgit_rc 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 'an unknown date-format falls back to short' ' daterc "enable-relative-dates=0" "date-format=nonsense" && cgit_rc 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_rc 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_rc daterc "url=dated/refs/" >tmp && grep "^Status: 200" tmp && grep "[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]<" tmp ' test_expect_success 'date-format=local is the default format in local time' ' daterc "enable-relative-dates=0" "date-format=local" && cgit_rc daterc "url=dated/refs/" >tmp && grep ">[A-Z][a-z][a-z] [A-Z][a-z][a-z] [0-9]* [0-9:]* [0-9]*<" tmp ' test_expect_success 'date-format still formats the log fallback' ' daterc "date-format=rfc2822" && cgit_rc 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_rc daterc "url=dated/commit/&id=$sha" >tmp && grep "2005-04-07 [0-9:]* +0000" tmp && ! grep "Apr 2005" tmp ' test_done