-
Notifications
You must be signed in to change notification settings - Fork 64
/
test_nginxfmt.py
397 lines (341 loc) · 16.5 KB
/
test_nginxfmt.py
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Unit tests for nginxfmt module."""
import contextlib
import logging
import pathlib
import shutil
import tempfile
import unittest
import nginxfmt
__author__ = "Michał Słomkowski"
__license__ = "Apache 2.0"
class TestFormatter(unittest.TestCase):
fmt = nginxfmt.Formatter()
def __init__(self, method_name: str = ...) -> None:
super().__init__(method_name)
logging.basicConfig(level=logging.DEBUG) # todo fix logging in debug
def check_formatting(self, original_text: str, formatted_text: str):
self.assertMultiLineEqual(formatted_text, self.fmt.format_string(original_text))
def check_stays_the_same(self, text: str):
self.assertMultiLineEqual(text, self.fmt.format_string(text))
def _check_variable_tags_symmetry(self, text):
self.assertMultiLineEqual(text,
self.fmt._strip_variable_template_tags(self.fmt._apply_variable_template_tags(text)))
def test_collapse_variable1(self):
self.check_formatting(" lorem ipsum ${ dol } amet", "lorem ipsum ${dol} amet\n")
def test_join_opening_parenthesis(self):
self.assertEqual(["foo", "bar {", "johan {", "tee", "ka", "}"],
self.fmt._join_opening_bracket(("foo", "bar {", "johan", "{", "tee", "ka", "}")))
def test_clean_lines(self):
self.assertEqual(["ala", "ma", "{", "kota", "}", "to;", "", "ook"],
self.fmt._clean_lines(("ala", "ma {", "kota", "}", "to;", "", "ook")))
self.assertEqual(["ala", "ma", "{", "{", "kota", "}", "to", "}", "ook"],
self.fmt._clean_lines(("ala", "ma {{", "kota", "}", "to}", "ook")))
self.assertEqual(["{", "ala", "ma", "{", "{", "kota", "}", "to", "}"],
self.fmt._clean_lines(("{", "ala ", "ma {{", " kota ", "}", " to} ")))
self.assertEqual(["{", "ala", "# ma {{", "kota", "}", "to", "}", "# }"],
self.fmt._clean_lines(("{", "ala ", "# ma {{", " kota ", "}", " to} ", "# }")))
self.assertEqual(["{", "ala", "# ma {{", r"rewrite /([\d]{2}) /up/$1.html last;", "}", "to", "}"],
self.fmt._clean_lines(
("{", "ala ", "# ma {{", r" rewrite /([\d]{2}) /up/$1.html last; ", "}", " to", "}")))
self.assertEqual(["{", "ala", "# ma {{", "aa last;", "bb to;", "}"],
self.fmt._clean_lines(("{", "ala ", "# ma {{", " aa last; bb to; ", "}")))
self.assertEqual(["{", "aa;", "b b \"cc; dd; ee \";", "ssss;", "}"],
self.fmt._clean_lines(("{", "aa; b b \"cc; dd; ee \"; ssss;", "}")))
self.assertEqual([r"location ~ /\.ht", "{"], self.fmt._clean_lines([r"location ~ /\.ht {", ]))
def test_perform_indentation(self):
self.assertEqual([
"foo bar {",
" fizz bazz;",
"}"], self.fmt._perform_indentation(("foo bar {", "fizz bazz;", "}")))
self.assertEqual([
"foo bar {",
" fizz bazz {",
" lorem ipsum;",
" asdf asdf;",
" }",
"}"], self.fmt._perform_indentation(("foo bar {", "fizz bazz {", "lorem ipsum;", "asdf asdf;", "}", "}")))
self.assertEqual([
"foo bar {",
" fizz bazz {",
" lorem ipsum;",
" # }",
" }",
"}",
"}",
"foo {"],
self.fmt._perform_indentation(("foo bar {", "fizz bazz {", "lorem ipsum;", "# }", "}", "}", "}", "foo {")))
self.assertEqual([
"foo bar {",
" fizz bazz {",
" lorem ipsum;",
" }",
"}",
"}",
"foo {"],
self.fmt._perform_indentation(("foo bar {", "fizz bazz {", "lorem ipsum;", "}", "}", "}", "foo {")))
def test_strip_line(self):
self.assertEqual("foo", self.fmt._strip_line(" foo "))
self.assertEqual("bar foo", self.fmt._strip_line(" bar foo "))
self.assertEqual("bar foo", self.fmt._strip_line(" bar \t foo "))
self.assertEqual('lorem ipsum " foo bar zip "', self.fmt._strip_line(' lorem ipsum " foo bar zip " '))
self.assertEqual('lorem ipsum " foo bar zip " or " dd aa " mi',
self.fmt._strip_line(' lorem ipsum " foo bar zip " or \t " dd aa " mi'))
def test_apply_bracket_template_tags(self):
self.assertEqual(
"\"aaa___TEMPLATE_BRACKET_OPENING_TAG___dd___TEMPLATE_BRACKET_CLOSING_TAG___bbb\"".splitlines(),
self.fmt._apply_bracket_template_tags("\"aaa{dd}bbb\"".splitlines()))
self.assertEqual(
"\"aaa___TEMPLATE_BRACKET_OPENING_TAG___dd___TEMPLATE_BRACKET_CLOSING_TAG___bbb\"cc{cc}cc\"dddd___TEMPLATE_BRACKET_OPENING_TAG___eee___TEMPLATE_BRACKET_CLOSING_TAG___fff\"".splitlines(),
self.fmt._apply_bracket_template_tags("\"aaa{dd}bbb\"cc{cc}cc\"dddd{eee}fff\"".splitlines()))
def test_strip_bracket_template_tags1(self):
self.assertEqual("\"aaa{dd}bbb\"", self.fmt._strip_bracket_template_tags(
"\"aaa___TEMPLATE_BRACKET_OPENING_TAG___dd___TEMPLATE_BRACKET_CLOSING_TAG___bbb\""))
def test_apply_bracket_template_tags1(self):
self.assertEqual(
"\"aaa___TEMPLATE_BRACKET_OPENING_TAG___dd___TEMPLATE_BRACKET_CLOSING_TAG___bbb\"cc{cc}cc\"dddd___TEMPLATE_BRACKET_OPENING_TAG___eee___TEMPLATE_BRACKET_CLOSING_TAG___fff\"".splitlines(),
self.fmt._apply_bracket_template_tags("\"aaa{dd}bbb\"cc{cc}cc\"dddd{eee}fff\"".splitlines()))
def test_variable_template_tags(self):
self.assertEqual("foo bar ___TEMPLATE_VARIABLE_OPENING_TAG___myvar___TEMPLATE_VARIABLE_CLOSING_TAG___",
self.fmt._apply_variable_template_tags("foo bar ${myvar}"))
self._check_variable_tags_symmetry("lorem ipsum ${dolor} $amet")
self._check_variable_tags_symmetry("lorem ipsum ${dolor} $amet\nother $var and ${var_name2}")
def test_umlaut_in_string(self):
self.check_formatting(
"# Statusseite für Monitoring freigeben \n" +
"# line above contains german umlaut causing problems \n" +
"location /nginx_status {\n" +
" stub_status on;\n" +
" access_log off;\n" +
" allow 127.0.0.1;\n" +
" deny all;\n" +
"}",
"# Statusseite für Monitoring freigeben\n" +
"# line above contains german umlaut causing problems\n" +
"location /nginx_status {\n" +
" stub_status on;\n" +
" access_log off;\n" +
" allow 127.0.0.1;\n" +
" deny all;\n" +
"}\n"
)
def test_empty_lines_removal(self):
self.check_formatting(
"\n foo bar {\n" +
" lorem ipsum;\n" +
"}\n\n\n",
"foo bar {\n" +
" lorem ipsum;\n" +
"}\n")
self.check_formatting(
"\n foo bar {\n\n\n\n\n\n" +
" lorem ipsum;\n" +
"}\n\n\n",
"foo bar {\n\n\n" +
" lorem ipsum;\n" +
"}\n")
self.check_formatting(
" foo bar {\n" +
" lorem ipsum;\n" +
" kee {\n" +
"caak; \n" +
"}}",
"foo bar {\n" +
" lorem ipsum;\n" +
" kee {\n" +
" caak;\n" +
" }\n" +
"}\n")
def test_template_variables_with_dollars1(self):
self.check_formatting('server {\n' +
' # commented ${line} should not be touched\n' +
'listen 80 default_server;\n' +
'server_name localhost;\n' +
'location / {\n' +
'proxy_set_header X-User-Auth "In ${cookie_access_token} ${ other}";\n' +
'proxy_set_header X-User-Other "foo ${bar}";\n' +
'}\n' +
'}',
'server {\n' +
' # commented ${line} should not be touched\n' +
' listen 80 default_server;\n' +
' server_name localhost;\n' +
' location / {\n' +
' proxy_set_header X-User-Auth "In ${cookie_access_token} ${ other}";\n' +
' proxy_set_header X-User-Other "foo ${bar}";\n' +
' }\n' +
'}\n')
def test_template_variables_with_dollars2(self):
self.check_formatting(' some_tag { with_templates "my ${var} and other ${ invalid_variable_use } "; }\n' +
'# in my line\n',
'some_tag {\n' +
' with_templates "my ${var} and other ${ invalid_variable_use } ";\n' +
'}\n' +
'# in my line\n')
def test_backslash3(self):
self.check_formatting('location ~ /\.ht {\n' +
'deny all;\n' +
'}',
'location ~ /\.ht {\n' +
' deny all;\n' +
'}\n')
def test_backslash2(self):
"""If curly braces are withing quotation marks, we treat them as part of the string, not syntax structure.
Writing '${ var }' is not valid in nginx anyway, so we slip collapsing these altogether. May be changed in
the future. """
self.check_formatting(
' tag { wt ~ /\.ht \t "my ${some some} and ~ /\.ht \tother ${comething in curly braces } "; }\n' +
'# in my line\n',
'tag {\n' +
' wt ~ /\.ht "my ${some some} and ~ /\.ht \tother ${comething in curly braces } ";\n' +
'}\n' +
'# in my line\n')
def test_multi_semicolon(self):
self.check_formatting('location /a { \n' +
'allow 127.0.0.1; allow 10.0.0.0/8; deny all; \n' +
'}\n',
'location /a {\n' +
' allow 127.0.0.1;\n' +
' allow 10.0.0.0/8;\n' +
' deny all;\n' +
'}\n')
def test_quotes1(self):
self.check_formatting('''add_header Alt-Svc 'h3-25=":443"; ma=86400'; add_header Alt-Svc 'h3-29=":443"; ma=86400';''',
'''add_header Alt-Svc 'h3-25=":443"; ma=86400';\n''' +
'''add_header Alt-Svc 'h3-29=":443"; ma=86400';\n''')
def test_quotes2(self):
self.check_formatting('''add_header Alt-Svc "h3-23=':443'; ma=86400"; add_header Alt-Svc 'h3-29=":443"; ma=86400';''',
'''add_header Alt-Svc "h3-23=':443'; ma=86400";\n''' +
'''add_header Alt-Svc 'h3-29=":443"; ma=86400';\n''')
def test_loading_utf8_file(self):
tmp_file = pathlib.Path(tempfile.mkstemp('utf-8')[1])
try:
shutil.copy('test-files/umlaut-utf8.conf', tmp_file)
self.fmt.format_file(tmp_file)
# todo perform some tests on result file
finally:
tmp_file.unlink()
def test_loading_latin1_file(self):
tmp_file = pathlib.Path(tempfile.mkstemp('latin1')[1])
try:
shutil.copy('test-files/umlaut-latin1.conf', tmp_file)
self.fmt.format_file(tmp_file)
# todo perform some tests on result file
finally:
tmp_file.unlink()
def test_issue_15(self):
self.check_formatting(
'section { server_name "~^(?<tag>[0-9a-f]{8}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{12})\.a\.b\.com$"; }',
'section {\n server_name "~^(?<tag>[0-9a-f]{8}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{12})\.a\.b\.com$";\n}\n')
def test_issue_11(self):
self.check_formatting(" # 3 spaces\n" +
"# 2 spaces\n" +
" # 1 space",
"# 3 spaces\n" +
"# 2 spaces\n" +
"# 1 space\n")
# everything after # is left as is (except trimming trailing whitespaces)
self.check_formatting(""" #if (!-f $request_filename) {
# rewrite ^/static/?(.*)$ /static.php?resource=$1 last;
#""",
"#if (!-f $request_filename) {\n" +
"# rewrite ^/static/?(.*)$ /static.php?resource=$1 last;\n" +
"#\n")
def test_issue_20_1(self):
self.check_stays_the_same("# comment 1\n" +
"tag {\n" +
" # comment 2\n" +
" code;\n" +
" # comment 3\n" +
" subtag {\n" +
" code;\n" +
" # comment 4\n" +
" #\n" +
" }\n" +
" # comment 5\n" +
"}\n")
def test_issue_20_2(self):
self.check_formatting(
"location /nginx_status {\n" +
"# Don't break \n" +
" stub_status on;\n" +
" access_log off;\n" +
" allow 127.0.0.1;\n" +
" deny all;\n" +
"}",
"location /nginx_status {\n" +
" # Don't break\n" +
" stub_status on;\n" +
" access_log off;\n" +
" allow 127.0.0.1;\n" +
" deny all;\n" +
"}\n"
)
self.check_formatting(
"location /nginx_status {\n" +
"# Don\"t break \n" +
" stub_status on;\n" +
" access_log off;\n" +
" allow 127.0.0.1;\n" +
" deny all;\n" +
"}",
"location /nginx_status {\n" +
" # Don\"t break\n" +
" stub_status on;\n" +
" access_log off;\n" +
" allow 127.0.0.1;\n" +
" deny all;\n" +
"}\n"
)
def test_issue_16(self):
self.check_formatting(
"location /example { allow 192.168.0.0/16; deny all; }",
"location /example {\n"
" allow 192.168.0.0/16;\n"
" deny all;\n"
"}\n")
def test_issue_9(self):
self.check_formatting(
(
"""http {\n"""
""" log_format le_json '{"time":"$time_iso8601", '\n"""
""" '"client_agent":"$client_agent",\n"""
""" '"user_agent":"$http_user_agent"}';\n"""
"""}\n"""
),
(
"""http {\n"""
""" log_format le_json '{"time":"$time_iso8601", '\n"""
""" '"client_agent":"$client_agent",\n"""
""" '"user_agent":"$http_user_agent"}';\n"""
"""}\n"""
),
)
def test_custom_indentation(self):
fo = nginxfmt.FormatterOptions()
fo.indentation = 2
fmt2 = nginxfmt.Formatter(fo)
self.assertMultiLineEqual("{\n"
" foo bar;\n"
"}\n",
fmt2.format_string(
" { \n"
" foo bar;\n"
"}\n"))
class TestStandaloneRun(unittest.TestCase):
@contextlib.contextmanager
def input_test_file(self, file_name):
tmp_file = pathlib.Path(tempfile.mkstemp('utf-8')[1])
try:
shutil.copy('test-files/' + file_name, tmp_file)
yield str(tmp_file)
# todo perform some tests on result file
finally:
tmp_file.unlink()
# todo better tests of standalone mode?
def test_print_result(self):
with self.input_test_file('not-formatted-1.conf') as input:
nginxfmt._standalone_run(['-p', input])
if __name__ == '__main__':
unittest.main()