Diff options
| author | Bryce Kwon <bryce@brycekwon.com> | |
|---|---|---|
| committer | Bryce Kwon <bryce@brycekwon.com> | |
| commit | ||
| parent | ||
| tree | ||
| download | ||
Check the stats columns, periods and path
Each period check needed a single heading of the right shape, so a
wrong window or column count passed, and the weekly default had no
check. A request that straddles midnight is made again rather than
failing, since cgit reads the clock for itself and a new day can open a
new period. The absence checks gain a presence check on the same page.
| -rwxr-xr-x | tests/t0112-stats.sh | 168 |
1 file changed, 138 insertions, 30 deletions
diff --git a/tests/t0112-stats.sh b/tests/t0112-stats.sh index 9bd8b7c..4cf87ae 100755 --- a/tests/t0112-stats.sh +++ b/tests/t0112-stats.sh @@ -12,11 +12,37 @@ test_description='Check the statistics page' # install does. test_expect_success 'stats are off by default' ' cgit_url "foo/stats" >tmp && - grep "Status: 404" tmp && - cgit_url "foo" >tmp && + grep "^Status: 404" tmp && + grep "<div class=.error.>Stats are disabled</div>" tmp && + cgit_ok_url "foo" >tmp && + grep ">log</a>" tmp && ! grep ">stats</a>" tmp ' +# A word that names no period leaves the page off rather than picking one. +test_expect_success 'a max-stats naming no period leaves stats off' ' + cgitrc_repo badrc foo max-stats=decade && + cgit_rc badrc "url=foo/stats/" >tmp && + grep "^Status: 404" tmp && + grep "<div class=.error.>Stats are disabled</div>" tmp +' + +# Asks for a stats page into tmp, and fails when the day turned during the +# request, since cgit reads the clock for itself and a new day can open a new +# period. The day read is left in now as year, month, day, ISO week and its +# year, for the caller to work out the labels from. +stats_today() { + now=$(date -u "+%Y %m %d W%V %G") && + cgit_ok "$@" >tmp && + test "$(date -u "+%Y %m %d W%V %G")" = "$now" +} + +# The column labels of the stats table in tmp, one to a line. +period_labels() { + sed -n "s/^<tr><th scope=.col.>Author<\/th>\(.*\)<th scope=.col.>Total<\/th><\/tr>$/\1/p" tmp | + sed -e "s/<th scope=.col.>//g" -e "s/<\/th>/;/g" | tr ";" "\n" | sed "/^$/d" +} + # Makes the given number of empty commits by the named author. author_commits() { i=0 && @@ -58,7 +84,7 @@ test_expect_success 'set up a stats config' ' ' test_expect_success 'stats tab appears when enabled' ' - cgit_rc statsrc "url=stat/" >tmp && + cgit_ok statsrc "url=stat/" >tmp && grep ">stats</a>" tmp ' @@ -93,23 +119,31 @@ test_expect_success 'the authors past the row count share one row' ' ' # The table scrolls sideways on a narrow window with nothing in it a keyboard -# can reach, so the table itself takes focus. +# can reach, so the table itself takes focus, and the focus ring links have. test_expect_success 'the authors table can take keyboard focus' ' - grep "<table class=.stats. tabindex=.0. aria-label=.Commits per author.>" tmp + cgit_ok statsrc "url=stat/stats/" >tmp && + grep "<table class=.stats. tabindex=.0. aria-label=.Commits per author.>" tmp && + sed -n "/^div#cgit a:focus-visible,$/,/^}/p" \ + "$TEST_OUTPUT_DIRECTORY/../assets/cgit.css" >rule && + grep "^div#cgit table.stats:focus-visible {$" rule && + grep "^ outline: solid 2px var(--link);$" rule ' test_expect_success 'stat options sit above the content' ' + cgit_ok statsrc "url=stat/stats/" >tmp && tr -d "\n" <tmp | sed "s/<h2>Commits per author.*//" | grep "cgit-panel" ' -test_expect_success 'stats below a path stay a normal page' ' - cgit_rc statsrc "url=stat/stats/file-1" >tmp && - grep "Status: 200" tmp && - grep "<h2>Commits per author" tmp +# Only Ada touched ada.txt, and in a commit inside the window. +test_expect_success 'stats below a path count that path alone' ' + cgit_ok statsrc "url=stat/stats/ada.txt" >tmp && + grep "<h2>Commits per author per week (path .ada.txt.)</h2>" tmp && + grep "<td class=.left.>Ada One</td>" tmp && + ! grep ">Gil Seven<" tmp ' test_expect_success 'max-stats=year offers all four periods' ' - cgit_rc statsrc "url=stat/stats/" >tmp && + cgit_ok statsrc "url=stat/stats/" >tmp && grep "<option value=.w." tmp && grep "<option value=.m." tmp && grep "<option value=.q." tmp && @@ -118,26 +152,95 @@ test_expect_success 'max-stats=year offers all four periods' ' test_expect_success 'max-stats bounds the selectable periods' ' cgit_rc statmaxrc "url=stat/stats/&period=y" >tmp && - grep "Statistics type disabled" tmp && - cgit_rc statmaxrc "url=stat/stats/" >tmp && - ! grep "<option value=.q." tmp + grep "^Status: 400" tmp && + grep "<div class=.error.>Statistics type disabled: year</div>" tmp && + cgit_ok statmaxrc "url=stat/stats/" >tmp && + grep "<option value=.w." tmp && + grep "<option value=.m." tmp && + ! grep "<option value=.q." tmp && + ! grep "<option value=.y." tmp +' + +test_expect_success 'max-stats=week leaves no period to choose' ' + cgitrc_repo weekrc stat max-stats=week "repo.path=$PWD/repos/pulse/.git" && + cgit_ok weekrc "url=stat/stats/" >tmp && + grep "<select id=.stats-authors. name=.ofs.>" tmp && + ! grep "stats-period" tmp +' + +test_expect_success 'a period that is not one is refused' ' + cgit_rc statsrc "url=stat/stats/&period=x" >tmp && + grep "^Status: 404" tmp && + grep "<div class=.error.>Unknown statistics type: x</div>" tmp ' # Each period buckets commits and labels its columns its own way, so every one -# gets a page of its own here. -test_expect_success 'every period renders its own columns' ' - cgit_rc statsrc "url=stat/stats/&period=m" >tmp && - grep -E "<th[^>]*>[A-Z][a-z][a-z] [0-9]{4}</th>" tmp && - cgit_rc statsrc "url=stat/stats/&period=q" >tmp && - grep -E "<th[^>]*>Q[1-4] [0-9]{4}</th>" tmp && - cgit_rc statsrc "url=stat/stats/&period=y" >tmp && - grep -E "<th[^>]*>[0-9]{4}</th>" tmp -' - -# The yearly window opens on the first of January three years back. cgit reads -# the real clock, so the two commits sit either side of midnight on that day. -test_expect_success 'the window opens at midnight on its first day' ' - first=$(($(date -u +%Y) - 3)) && +# gets a page of its own here, with the current period last. The weeks before +# this one take date arithmetic the shell lacks, so only their shape is +# checked. +test_expect_success 'the weekly columns end with this week' ' + { + stats_today statsrc "url=stat/stats/&period=w" || + stats_today statsrc "url=stat/stats/&period=w" + } && + period_labels >actual && + test_line_count = 4 actual && + test $(grep -c "^W[0-9][0-9] [0-9][0-9][0-9][0-9]$" actual) -eq 4 && + test "$(sed -n 4p actual)" = "${now#* * * }" +' + +test_expect_success 'the monthly columns are the last four months' ' + { + stats_today statsrc "url=stat/stats/&period=m" || + stats_today statsrc "url=stat/stats/&period=m" + } && + set -- $now && + month=$(($1 * 12 + ${2#0} - 1)) && + for back in 3 2 1 0 + do + n=$((month - back)) && + printf "%s %d\n" $(echo Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec | + cut -d " " -f $((n % 12 + 1))) $((n / 12)) || return 1 + done >expect && + period_labels >actual && + test_cmp expect actual +' + +test_expect_success 'the quarterly columns are the last four quarters' ' + { + stats_today statsrc "url=stat/stats/&period=q" || + stats_today statsrc "url=stat/stats/&period=q" + } && + set -- $now && + quarter=$(($1 * 4 + (${2#0} - 1) / 3)) && + for back in 3 2 1 0 + do + n=$((quarter - back)) && + echo "Q$((n % 4 + 1)) $((n / 4))" || return 1 + done >expect && + period_labels >actual && + test_cmp expect actual +' + +test_expect_success 'the yearly columns are the last four years' ' + { + stats_today statsrc "url=stat/stats/&period=y" || + stats_today statsrc "url=stat/stats/&period=y" + } && + year=${now%% *} && + test_write_lines $((year - 3)) $((year - 2)) $((year - 1)) $year >expect && + period_labels >actual && + test_cmp expect actual +' + +# Builds a repository with a commit either side of midnight on the first of +# January three years back, where the yearly window opens, and asks for its +# stats into tmp. cgit reads the clock for itself, so this fails when the +# year turned during the request, and the caller asks again. +edge_stats() { + year=$(date -u +%Y) && + first=$((year - 3)) && + rm -rf repos/edge && test_create_repo repos/edge && ( cd repos/edge && @@ -151,7 +254,12 @@ test_expect_success 'the window opens at midnight on its first day' ' git commit --allow-empty -m first ) && cgitrc_repo edgerc edge max-stats=year && - cgit_rc edgerc "url=edge/stats/&period=y" >tmp && + cgit_ok edgerc "url=edge/stats/&period=y" >tmp && + test "$(date -u +%Y)" = "$year" +} + +test_expect_success 'the window opens at midnight on its first day' ' + { edge_stats || edge_stats; } && grep ">Early Bird<" tmp && ! grep ">Too Early<" tmp ' @@ -161,8 +269,8 @@ test_expect_success 'the window opens at midnight on its first day' ' test_expect_success 'a path with pathspec magic is refused' ' cgit_rc statsrc "url=stat/stats/%3A%28icase%29FILE-1" >tmp && grep "^Status: 400" tmp && - cgit_rc statsrc "url=stat/stats/file-1" >tmp && - grep "^Status: 200" tmp + cgit_ok statsrc "url=stat/stats/file-1" >tmp && + grep "<h2>Commits per author per week (path .file-1.)</h2>" tmp ' test_done |
