Skip to content

Commit 026e288

Browse files
committed
Add some tests. #31
1 parent e05a46e commit 026e288

File tree

2 files changed

+59
-14
lines changed

2 files changed

+59
-14
lines changed

nginxfmt.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,9 +403,9 @@ def _standalone_run(program_arguments):
403403

404404
if args.pipe:
405405
original_content = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8')
406-
print(formatter.format_string(original_content.read()))
406+
print(formatter.format_string(original_content.read()), end="")
407407
elif args.print_result:
408-
print(formatter.get_formatted_string_from_file(pathlib.Path(args.config_files[0])))
408+
print(formatter.get_formatted_string_from_file(pathlib.Path(args.config_files[0])), end="")
409409
else:
410410
for config_file_path in args.config_files:
411411
backup_file_path = pathlib.Path(config_file_path + '~') if args.backup_original else None

test_nginxfmt.py

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
"""Unit tests for nginxfmt module."""
55
import contextlib
6+
import io
67
import logging
78
import pathlib
89
import shutil
@@ -16,14 +17,21 @@
1617

1718

1819
class TestFormatter(unittest.TestCase):
19-
fmt = nginxfmt.Formatter()
2020

2121
def __init__(self, method_name: str = ...) -> None:
2222
super().__init__(method_name)
2323
logging.basicConfig(level=logging.DEBUG) # todo fix logging in debug
2424

25-
def check_formatting(self, original_text: str, formatted_text: str):
26-
self.assertMultiLineEqual(formatted_text, self.fmt.format_string(original_text))
25+
fmt_options = nginxfmt.FormatterOptions()
26+
fmt_options.line_endings = '\n' # force Unix LF for tests
27+
self.fmt = nginxfmt.Formatter(fmt_options)
28+
29+
fmt_options_crlf = nginxfmt.FormatterOptions()
30+
fmt_options_crlf.line_endings = '\r\n' # CRLF formatter
31+
self.fmt_crlf = nginxfmt.Formatter(fmt_options_crlf)
32+
33+
def check_formatting(self, original_text: str, formatted_text: str, formatter=None):
34+
self.assertMultiLineEqual(formatted_text, (formatter if formatter else self.fmt).format_string(original_text))
2735

2836
def check_stays_the_same(self, text: str):
2937
self.assertMultiLineEqual(text, self.fmt.format_string(text))
@@ -240,14 +248,16 @@ def test_multi_semicolon(self):
240248
'}\n')
241249

242250
def test_quotes1(self):
243-
self.check_formatting('''add_header Alt-Svc 'h3-25=":443"; ma=86400'; add_header Alt-Svc 'h3-29=":443"; ma=86400';''',
244-
'''add_header Alt-Svc 'h3-25=":443"; ma=86400';\n''' +
245-
'''add_header Alt-Svc 'h3-29=":443"; ma=86400';\n''')
251+
self.check_formatting(
252+
'''add_header Alt-Svc 'h3-25=":443"; ma=86400'; add_header Alt-Svc 'h3-29=":443"; ma=86400';''',
253+
'''add_header Alt-Svc 'h3-25=":443"; ma=86400';\n''' +
254+
'''add_header Alt-Svc 'h3-29=":443"; ma=86400';\n''')
246255

247256
def test_quotes2(self):
248-
self.check_formatting('''add_header Alt-Svc "h3-23=':443'; ma=86400"; add_header Alt-Svc 'h3-29=":443"; ma=86400';''',
249-
'''add_header Alt-Svc "h3-23=':443'; ma=86400";\n''' +
250-
'''add_header Alt-Svc 'h3-29=":443"; ma=86400';\n''')
257+
self.check_formatting(
258+
'''add_header Alt-Svc "h3-23=':443'; ma=86400"; add_header Alt-Svc 'h3-29=":443"; ma=86400';''',
259+
'''add_header Alt-Svc "h3-23=':443'; ma=86400";\n''' +
260+
'''add_header Alt-Svc 'h3-29=":443"; ma=86400';\n''')
251261

252262
def test_loading_utf8_file(self):
253263
tmp_file = pathlib.Path(tempfile.mkstemp('utf-8')[1])
@@ -374,16 +384,34 @@ def test_custom_indentation(self):
374384
" foo bar;\n"
375385
"}\n"))
376386

387+
def test_crlf_1(self):
388+
self.check_formatting(
389+
"location /example \r\n{\r\nallow 192.168.0.0/16; deny all;\r\n}\n",
390+
"location /example {\n"
391+
" allow 192.168.0.0/16;\n"
392+
" deny all;\n"
393+
"}\n")
377394

378-
#todo tests for unix and windows line endings
395+
self.check_formatting(
396+
"location /example \r\n{\r\nallow 192.168.0.0/16; deny all;\r\n}\n",
397+
"location /example {\r\n"
398+
" allow 192.168.0.0/16;\r\n"
399+
" deny all;\r\n"
400+
"}\r\n",
401+
formatter=self.fmt_crlf)
379402

380403

381404
class TestStandaloneRun(unittest.TestCase):
382405

406+
def __init__(self, method_name: str = ...) -> None:
407+
super().__init__(method_name)
408+
logging.basicConfig(level=logging.DEBUG) # todo fix logging in debug
409+
383410
@contextlib.contextmanager
384411
def input_test_file(self, file_name):
385412
tmp_file = pathlib.Path(tempfile.mkstemp('utf-8')[1])
386413
try:
414+
logging.debug("Input file saved to", tmp_file)
387415
shutil.copy('test-files/' + file_name, tmp_file)
388416
yield str(tmp_file)
389417
# todo perform some tests on result file
@@ -392,8 +420,25 @@ def input_test_file(self, file_name):
392420

393421
# todo better tests of standalone mode?
394422
def test_print_result(self):
395-
with self.input_test_file('not-formatted-1.conf') as input:
396-
nginxfmt._standalone_run(['-p', input])
423+
with self.input_test_file('not-formatted-1.conf') as input_file:
424+
nginxfmt._standalone_run(['-p', input_file])
425+
426+
def test_print_result_line_endings_windows(self):
427+
f = io.StringIO()
428+
with self.input_test_file('not-formatted-1.conf') as input_file:
429+
with contextlib.redirect_stdout(f):
430+
nginxfmt._standalone_run(['--line-endings=windows', '-p', input_file])
431+
output = f.getvalue()
432+
self.assertEqual(output.count('\r\n'), 5)
433+
434+
def test_print_result_line_endings_unix(self):
435+
f = io.StringIO()
436+
with self.input_test_file('not-formatted-1.conf') as input_file:
437+
with contextlib.redirect_stdout(f):
438+
nginxfmt._standalone_run(['--line-endings=unix', '-p', input_file])
439+
output = f.getvalue()
440+
self.assertEqual(output.count('\r\n'), 0)
441+
self.assertEqual(output.count('\n'), 5)
397442

398443

399444
if __name__ == '__main__':

0 commit comments

Comments
 (0)