1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
test_description='Check the statistics page'
. ./setup.sh
test_expect_success 'stats are off by default' '
cgit_url "foo/stats" >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
'
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
'
stats_today() {
now=$(date -u "+%Y %m %d W%V %G") &&
cgit_ok "$@" >tmp &&
test "$(date -u "+%Y %m %d W%V %G")" = "$now"
}
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"
}
author_commits() {
i=0 &&
while test $i -lt $2
do
GIT_AUTHOR_NAME="$1" git commit --allow-empty -m "$1 $i" &&
i=$((i + 1)) || return 1
done
}
test_expect_success 'set up a stats config' '
mkrepo repos/pulse 2 &&
(
cd repos/pulse &&
unset GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
git checkout -b side &&
author_commits "Gil Seven" 1 &&
git checkout - &&
echo ada >ada.txt &&
git add ada.txt &&
GIT_AUTHOR_NAME="Ada One" git commit -m "add ada.txt" &&
author_commits "Ben Two" 2 &&
author_commits "Cy Three" 3 &&
author_commits "Di Four" 4 &&
author_commits "Ed Five" 5 &&
author_commits "Flo Six" 6 &&
author_commits "Gil Seven" 6 &&
GIT_AUTHOR_NAME="Mo Merger" git merge --no-ff -m merge side &&
git commit-graph write --reachable
) &&
cgitrc_repo statsrc stat max-stats=year "repo.path=$PWD/repos/pulse/.git" &&
cgitrc_repo statmaxrc stat max-stats=month "repo.path=$PWD/repos/pulse/.git"
'
test_expect_success 'stats tab appears when enabled' '
cgit_ok statsrc "url=stat/" >tmp &&
grep ">stats</a>" tmp
'
test_expect_success 'stats page renders the authors table' '
cgit_ok statsrc "url=stat/stats/" >tmp &&
grep "<h2>Commits per author per week</h2>" tmp
'
test_expect_success 'each author is counted and the busiest comes first' '
cgit_ok statsrc "url=stat/stats/" >tmp &&
sed -n "s/^<tr><td class=.left.>\([^<]*\)<\/td>.*<td class=.sum.>\([0-9]*\)<\/td><\/tr>$/\1 \2/p" tmp >actual &&
test_write_lines "Gil Seven 7" "Flo Six 6" "Ed Five 5" "Di Four 4" \
"Cy Three 3" "Ben Two 2" "Ada One 1" >expect &&
test_cmp expect actual &&
grep "^<tr><td class=.left.>Ada One</td>\(<td>[0-9]*</td>\)\{4\}<td class=.sum.>1</td></tr>$" tmp &&
grep "^<tr><td class=.total.>Total</td>\(<td class=.sum.>[0-9]*</td>\)\{4\}<td class=.sum.>28</td></tr>$" tmp
'
test_expect_success 'a merge counts for nobody' '
cgit_ok statsrc "url=stat/stats/" >tmp &&
grep "<td class=.left.>Gil Seven</td>.*<td class=.sum.>7</td>" tmp &&
! grep ">Mo Merger<" tmp
'
test_expect_success 'the authors past the row count share one row' '
cgit_ok statsrc "url=stat/stats/&ofs=1" >tmp &&
sed -n "s/^<tr><td class=.left.>\([^<]*\)<\/td>.*<td class=.sum.>\([0-9]*\)<\/td><\/tr>$/\1 \2/p" tmp >actual &&
test_write_lines "Gil Seven 7" "Others (6) 21" >expect &&
test_cmp expect actual
'
test_expect_success 'the authors table can take keyboard focus' '
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 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_ok statsrc "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 bounds the selectable periods' '
cgit_rc statmaxrc "url=stat/stats/&period=y" >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
'
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
'
edge_stats() {
year=$(date -u +%Y) &&
first=$((year - 3)) &&
rm -rf repos/edge &&
test_create_repo repos/edge &&
(
cd repos/edge &&
GIT_AUTHOR_NAME="Too Early" \
GIT_AUTHOR_DATE="$((first - 1))-12-31 23:59:59 +0000" \
GIT_COMMITTER_DATE="$((first - 1))-12-31 23:59:59 +0000" \
git commit --allow-empty -m before &&
GIT_AUTHOR_NAME="Early Bird" \
GIT_AUTHOR_DATE="$first-01-01 00:00:00 +0000" \
GIT_COMMITTER_DATE="$first-01-01 00:00:00 +0000" \
git commit --allow-empty -m first
) &&
cgitrc_repo edgerc edge max-stats=year &&
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
'
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_ok statsrc "url=stat/stats/file-1" >tmp &&
grep "<h2>Commits per author per week (path .file-1.)</h2>" tmp
'
test_done
|