blob: 3728412cfa11a9582c9af360c8096eb8de9d324b (plain) (blame)
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
#!/bin/sh

# Checks the per repository statistics page, which stays off until max-stats
# turns it on and then reports commits per author. A second config sets a
# finer max-stats so that what it takes out of the period selector is covered
# as well.

test_description='Check the statistics page'
. ./setup.sh

# The shared test config sets no max-stats, so this is what an untouched
# install does.
test_expect_success 'stats are off by default' '
	cgit_url "foo/stats" >tmp &&
	grep "Status: 404" tmp &&
	cgit_url "foo" >tmp &&
	! grep ">stats</a>" tmp
'

# The authors table counts only recent periods, so the fixture needs a commit
# dated now to sit beside the 2005 dates the shared repositories carry.
test_expect_success 'set up a stats config' '
	mkrepo repos/pulse 2 &&
	(
		cd repos/pulse &&
		GIT_AUTHOR_DATE="@$(date +%s) +0000" \
		GIT_COMMITTER_DATE="@$(date +%s) +0000" \
			git commit --allow-empty -m fresh &&
		git commit-graph write --reachable
	) &&
	{
		echo "virtual-root=/" &&
		echo "cache-size=0" &&
		echo "max-stats=year" &&
		echo "repo.url=stat" &&
		echo "repo.path=$PWD/repos/pulse/.git"
	} >statsrc &&
	{
		echo "virtual-root=/" &&
		echo "cache-size=0" &&
		echo "max-stats=month" &&
		echo "repo.url=stat" &&
		echo "repo.path=$PWD/repos/pulse/.git"
	} >statmaxrc
'

statq() { CGIT_CONFIG="$PWD/statsrc" QUERY_STRING="$1" cgit; }

test_expect_success 'stats tab appears when enabled' '
	statq "url=stat/" >tmp &&
	grep ">stats</a>" tmp
'

test_expect_success 'stats page renders the authors table' '
	statq "url=stat/stats/" >tmp &&
	grep "<h2>Commits per author" tmp
'

test_expect_success 'no language breakdown is rendered' '
	! grep -i "language" tmp
'

test_expect_success 'stat options sit above the content' '
	tr -d "\n" <tmp | sed "s/<h2>Commits per author.*//" | grep "cgit-panel"
'

test_expect_success 'stats below a path stay a normal page' '
	statq "url=stat/stats/file-1" >tmp &&
	grep "Status: 200" tmp &&
	grep "<h2>Commits per author" tmp
'

test_expect_success 'max-stats=year offers all four periods' '
	statq "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_CONFIG="$PWD/statmaxrc" QUERY_STRING="url=stat/stats/&period=y" cgit >tmp &&
	grep "Statistics type disabled" tmp &&
	CGIT_CONFIG="$PWD/statmaxrc" QUERY_STRING="url=stat/stats/" cgit >tmp &&
	! grep "<option value=.q." 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' '
	statq "url=stat/stats/&period=m" >tmp &&
	grep -E "<th[^>]*>[A-Z][a-z][a-z] [0-9]{4}</th>" tmp &&
	statq "url=stat/stats/&period=q" >tmp &&
	grep -E "<th[^>]*>Q[1-4] [0-9]{4}</th>" tmp &&
	statq "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)) &&
	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
	) &&
	{
		echo "virtual-root=/" &&
		echo "cache-size=0" &&
		echo "max-stats=year" &&
		echo "repo.url=edge" &&
		echo "repo.path=$PWD/repos/edge/.git"
	} >edgerc &&
	CGIT_CONFIG="$PWD/edgerc" QUERY_STRING="url=edge/stats/&period=y" cgit >tmp &&
	grep ">Early Bird<" tmp &&
	! grep ">Too Early<" tmp
'

# A path starting with a colon would reach git as pathspec magic, which the
# log, the feed and the patch page refuse as well.
test_expect_success 'a path with pathspec magic is refused' '
	statq "url=stat/stats/%3A%28icase%29FILE-1" >tmp &&
	grep "^Status: 400" tmp &&
	statq "url=stat/stats/file-1" >tmp &&
	grep "^Status: 200" tmp
'

test_done