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

# The tree page. The checks against foo+bar cover a file name and a branch
# name that both contain a plus, which cgit percent-encodes in a path as
# well as in a query, since a bare plus in a served path reads back as a
# space through a query string.

test_description='Check content on tree page'
. ./setup.sh

test_expect_success 'generate bar/tree' 'cgit_url "bar/tree" >tmp'
test_expect_success 'find file-1' 'grep "file-1" tmp'
test_expect_success 'find file-50' 'grep "file-50" tmp'

test_expect_success 'generate bar/tree/file-50' 'cgit_url "bar/tree/file-50" >tmp'

test_expect_success 'find line 1' '
	grep "<a id=.n1. href=.#n1.>1</a>" tmp
'

test_expect_success 'no line 2' '
	! grep "<a id=.n2. href=.#n2.>2</a>" tmp
'

# The numbers and the code are two columns of text that a browser keeps in
# step only while both are set in one font. Only a browser shows the drift,
# so this checks the rule that prevents it.
test_expect_success 'the code is set in the font of its line numbers' '
	sed -n "/^div#cgit table.blob code {/,/^}/p" \
		"$TEST_OUTPUT_DIRECTORY/../assets/cgit.css" >rule &&
	grep "^  font: inherit;$" rule
'

# A browser starts a new line at more characters than git ends one at, and
# each would push the rest of the code a line below its numbers.
test_expect_success 'set up a file holding the characters a browser breaks at' '
	test_create_repo repos/breaks &&
	(
		cd repos/breaks &&
		printf "a\\rb\\vc\\fd\\302\\205e\\342\\200\\250f\\342\\200\\251g\\nh\\r\\ni\\r" >breaks.txt &&
		git add breaks.txt &&
		git commit -m breaks
	) &&
	{
		echo "virtual-root=/" &&
		echo "cache-size=0" &&
		echo "enable-blame=1" &&
		echo "repo.url=breaks" &&
		echo "repo.path=$PWD/repos/breaks/.git"
	} >breaksrc
'

test_expect_success 'each of them is shown as an escape in the tree' '
	CGIT_CONFIG="$PWD/breaksrc" QUERY_STRING="url=breaks/tree/breaks.txt" cgit >tmp &&
	grep "<code>a<span class=.escape.>\\\\r</span>b<span class=.escape.>\\\\v</span>c" tmp &&
	grep "c<span class=.escape.>\\\\f</span>d<span class=.escape.>\\\\u0085</span>e" tmp &&
	grep "e<span class=.escape.>\\\\u2028</span>f<span class=.escape.>\\\\u2029</span>g$" tmp
'

test_expect_success 'only a carriage return before a line feed is left as it is' '
	printf "h\\r\\n" >expect &&
	grep "^h" tmp >actual &&
	test_cmp expect actual &&
	grep "^i<span class=.escape.>\\\\r</span></code>" tmp
'

test_expect_success 'the numbers count only the lines git counts' '
	grep "<a id=.n3. href=.#n3.>3</a>" tmp &&
	! grep "<a id=.n4. href=.#n4.>4</a>" tmp
'

test_expect_success 'the blame page escapes them the same way' '
	CGIT_CONFIG="$PWD/breaksrc" QUERY_STRING="url=breaks/blame/breaks.txt" cgit >tmp &&
	grep "<code>a<span class=.escape.>\\\\r</span>b<span class=.escape.>\\\\v</span>c" tmp &&
	grep "e<span class=.escape.>\\\\u2028</span>f<span class=.escape.>\\\\u2029</span>g$" tmp &&
	grep "^i<span class=.escape.>\\\\r</span></code>" tmp
'

# The striped background is padded with spaces to the widest line, which is
# 31 columns once its six escapes are written out, though it is 18 bytes.
test_expect_success 'the blame stripes are as wide as the escaped line' '
	grep "^ \\{31\\}</pre></div>" tmp
'

test_expect_success 'generate foo+bar/tree' 'cgit_url "foo%2bbar/tree" >tmp'

test_expect_success 'verify a+b link' '
	grep "/foo%2Bbar/tree/a%2Bb" tmp
'

test_expect_success 'generate foo+bar/tree?h=1+2' 'cgit_url "foo%2bbar/tree&h=1%2b2" >tmp'

test_expect_success 'verify a+b?h=1+2 link' '
	grep "/foo%2Bbar/tree/a%2Bb?h=1%2B2" tmp
'

test_expect_success 'set up a repo with a nested submodule' '
	mkrepo repos/submodules 1 &&
	(
		cd repos/submodules &&
		sub=$(git rev-parse HEAD) &&
		git update-index --add --cacheinfo 160000,$sub,vendor/sub &&
		git commit -m gitlink
	) &&
	{
		echo "virtual-root=/" &&
		echo "cache-size=0" &&
		echo "enable-blame=1" &&
		echo "repo.url=submodules" &&
		echo "repo.path=$PWD/repos/submodules/.git"
	} >submodrc
'

test_expect_success 'generate submodules/tree/vendor' '
	CGIT_CONFIG="$PWD/submodrc" QUERY_STRING="url=submodules/tree/vendor" cgit >tmp
'

test_expect_success 'a submodule row shows the entry name alone' '
	grep "class=.ls-mod.>sub</span>" tmp &&
	! grep ">vendor/sub</span>" tmp
'

test_expect_success 'the submodule hash is titled with the full id' '
	sub=$(git -C repos/submodules rev-parse HEAD~1) &&
	grep "class=.ls-mod-hash. title=.$sub.>" tmp
'

test_expect_success 'a submodule row shows no size' '
	grep "ls-mod" tmp >row &&
	grep "class=.ls-size.>-</td>" row
'

test_expect_success 'a submodule row offers log but not blame or plain' '
	grep "ls-mod" tmp >row &&
	grep ">log</a>" row &&
	! grep ">blame</a>" row &&
	! grep ">plain</a>" row
'

test_expect_success 'a file row still offers blame' '
	CGIT_CONFIG="$PWD/submodrc" QUERY_STRING="url=submodules/tree/" cgit >tmp &&
	grep ">blame</a>" tmp
'

# A link target inside the tree becomes a link, while an absolute one names a
# file on whatever machine checks the tree out, not a path in the tree.
test_expect_success 'only a link target inside the tree is linked' '
	test_create_repo repos/links &&
	(
		cd repos/links &&
		echo hi >target.txt &&
		git add target.txt &&
		rel=$(printf target.txt | git hash-object -w --stdin) &&
		abs=$(printf /etc/passwd | git hash-object -w --stdin) &&
		git update-index --add --cacheinfo 120000,$rel,rel &&
		git update-index --add --cacheinfo 120000,$abs,abs &&
		git commit -m links
	) &&
	{
		echo "virtual-root=/" &&
		echo "cache-size=0" &&
		echo "repo.url=links" &&
		echo "repo.path=$PWD/repos/links/.git"
	} >linksrc &&
	CGIT_CONFIG="$PWD/linksrc" QUERY_STRING="url=links/tree/" cgit >tmp &&
	grep " -> <a class=.ls-blob. href=./links/tree/target.txt.>target.txt</a>" tmp &&
	grep " -> /etc/passwd</td>" tmp &&
	! grep "href=./links/tree/etc/passwd" tmp
'

# A row follows a chain of single directories for a set number of levels and
# stops on the last one it links, with no separator left hanging after it.
test_expect_success 'a long directory chain ends on a link' '
	test_create_repo repos/deep &&
	(
		cd repos/deep &&
		p=d1 &&
		for i in $(test_seq 2 17)
		do
			p=$p/d$i || return 1
		done &&
		mkdir -p $p &&
		echo deep >$p/file &&
		git add . &&
		git commit -m deep
	) &&
	{
		echo "virtual-root=/" &&
		echo "cache-size=0" &&
		echo "repo.url=deep" &&
		echo "repo.path=$PWD/repos/deep/.git"
	} >deeprc &&
	CGIT_CONFIG="$PWD/deeprc" QUERY_STRING="url=deep/tree/" cgit >tmp &&
	grep ">d15</a></td>" tmp &&
	! grep " / </td>" tmp
'

test_expect_success 'a binary file in the tree shows as a hex dump' '
	test_create_repo repos/bin &&
	(
		cd repos/bin &&
		printf "\\000\\001\\002bin" >blob.bin &&
		git add blob.bin &&
		git commit -m binary
	) &&
	{
		echo "virtual-root=/" &&
		echo "cache-size=0" &&
		echo "repo.url=bin" &&
		echo "repo.path=$PWD/repos/bin/.git"
	} >binrc &&
	CGIT_CONFIG="$PWD/binrc" QUERY_STRING="url=bin/tree/blob.bin" cgit >tmp &&
	grep "<table class=.bin-blob.>" tmp &&
	grep "<td class=.right.>0000</td><td class=.hex.> 00 01 02 62 69 6e" tmp
'

test_done