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
|
test_description='Check filtered content'
. ./setup.sh
for prefix in exec lua
do
filter_prereq=
test "$prefix" = exec || filter_prereq=CGIT_LUA
test_expect_success $filter_prereq "the $prefix source filter gets the file name and fills the code cell" '
cgit_ok_url "filter-$prefix/tree/a%2bb" >tmp &&
grep "<code>a+b HELLO$" tmp &&
tr -d "\n" <tmp >flat.out &&
grep "<code>a+b HELLO</code>" flat.out
'
test_expect_success $filter_prereq "the $prefix about filter gets the readme name and the tree url" '
cgit_ok_url "filter-$prefix/about/" >tmp &&
grep "<div id=.about.>a+b /filter-$prefix/tree/ HELLO$" tmp
'
test_expect_success $filter_prereq "the $prefix commit filter rewrites the subject" '
cgit_ok_url "filter-$prefix/commit/" >tmp &&
grep "<div class=.commit-subject.>ADD A+B" tmp
'
test_expect_success $filter_prereq "the $prefix email filter gets the author's address and name" '
cgit_ok_url "filter-$prefix/commit/" >tmp &&
grep "<author@example.com> commit A U THOR <AUTHOR@EXAMPLE.COM>" tmp
'
test_expect_success $filter_prereq "the $prefix email filter gets the committer's address and name" '
cgit_ok_url "filter-$prefix/commit/" >tmp &&
grep "<committer@example.com> commit C O MITTER <COMMITTER@EXAMPLE.COM>" tmp
'
done
test_expect_success 'the blame page goes through the source filter too' '
cgitrc_copy blamerc enable-blame=1 &&
cgit_rc blamerc "url=filter-exec/blame/a%2bb" >tmp &&
grep "<td class=.linenumbers.>" tmp &&
grep "<code>a+b HELLO$" tmp
'
test_expect_success 'the about filter gets the tree url at the readme branch' '
cgitrc_repo aboutargsrc filtered readme=:a+b "about-filter=exec:$FILTER_DIRECTORY/dump.sh" "root-readme=$PWD/root.txt" "repo.path=$PWD/repos/filter/.git" &&
cgit_rc aboutargsrc "url=filtered/about/&h=1%2b2" >tmp &&
grep "<div id=.about.>a+b /filtered/tree/ ?h=1%2B2 HELLO" tmp
'
test_expect_success 'the site readme leaves the tree url empty' '
echo site >root.txt &&
cgit_rc aboutargsrc "p=about" >tmp &&
grep "root.txt SITE" tmp
'
test_expect_success 'only a repository page hands filters its variables' '
write_script env.sh <<-\EOF &&
cat >/dev/null
echo "repo url: $CGIT_REPO_URL."
EOF
sed "s|^about-filter=.*|about-filter=exec:$PWD/env.sh|" aboutargsrc >envrc &&
cgit_rc envrc "url=filtered/about/" >tmp &&
grep "repo url: filtered\." tmp &&
cgit_rc envrc "p=about" >tmp &&
grep "repo url: \." tmp
'
test_expect_success 'a content filter that exits non-zero still renders the page' '
write_script failing-filter <<-\EOF &&
cat
exit 3
EOF
cgitrc_repo failingrc foo "repo.commit-filter=exec:$PWD/failing-filter" &&
cgit_ok failingrc "url=foo/commit/" >tmp &&
test_grep "<div class=.commit-subject.>commit 5" tmp
'
test_expect_success CGIT_LUA 'a failing lua filter still renders an error page' '
cat >broken.lua <<-\EOF &&
function filter_open(...) error("boom") end
function filter_write(str) end
function filter_close() return 0 end
EOF
{
echo "cache-size=0" &&
echo "repo.url=foo" &&
echo "repo.path=$PWD/repos/foo/.git" &&
echo "auth-filter=lua:$PWD/broken.lua"
} >brokenrc &&
cgit_rc brokenrc "url=foo/commit" >tmp 2>err &&
grep "Status: 500" tmp &&
grep "Lua error in" err
'
test_expect_success CGIT_LUA 'a lua filter raising a table is named by type' '
cat >tabled.lua <<-\EOF &&
function filter_open(...) error({}) end
function filter_write(str) end
function filter_close() return 0 end
EOF
sed "s|broken.lua|tabled.lua|" brokenrc >tabledrc &&
cgit_rc tabledrc "url=foo/commit" >tmp 2>err &&
grep "Status: 500" tmp &&
grep "Lua error in .*tabled.lua: (error object is a table value)" err
'
test_expect_success CGIT_LUA 'a lua filter that writes while loading still renders' '
cat >loud.lua <<-\EOF &&
html("written while loading")
function filter_open(...) end
function filter_write(str) html(str) end
function filter_close() return 0 end
EOF
{
echo "cache-size=0" &&
echo "repo.url=foo" &&
echo "repo.path=$PWD/repos/foo/.git" &&
echo "repo.commit-filter=lua:$PWD/loud.lua"
} >loudrc &&
cgit_rc loudrc "url=foo/commit" >tmp &&
grep "written while loading" tmp &&
grep "</html>" tmp
'
test_expect_success CGIT_LUA 'git can trace before the filters are set up' '
CGIT_CONFIG="$PWD/cgitrc" GIT_TRACE2="$PWD/trace2.out" QUERY_STRING="url=foo/" cgit >tmp &&
grep "</html>" tmp &&
test -s trace2.out
'
test_expect_success CGIT_LUA 'a lua filter can encode an argument and include a file' '
echo "included <b>bold</b>" >include.txt &&
cat >apis.lua <<-EOF &&
function filter_open(...) end
function filter_write(str)
html_url_arg("a b&c")
html("|")
html_include("$PWD/include.txt")
end
function filter_close() return 0 end
EOF
sed "s|loud.lua|apis.lua|" loudrc >apisrc &&
cgit_rc apisrc "url=foo/commit" >tmp &&
grep "a+b%26c|included <b>bold</b>" tmp
'
test_expect_success CGIT_LUA 'a lua filter prints where it runs' '
cat >print.lua <<-\EOF &&
function filter_open(...) end
function filter_write(str)
print("printed", 1, nil, true)
html(str)
end
function filter_close() return 0 end
EOF
sed "s|loud.lua|print.lua|" loudrc >printrc &&
cgit_rc printrc "url=foo/commit" >tmp &&
tab=$(printf "\t") &&
grep "<div class=.commit-subject.>printed${tab}1${tab}nil${tab}true$" tmp &&
sed -n "/<\/html>/,\$p" tmp >tail &&
! grep printed tail
'
test_done
|