33
44"""Unit tests for nginxfmt module."""
55import contextlib
6+ import io
67import logging
78import pathlib
89import shutil
1617
1718
1819class 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 \n allow 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 \n allow 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
381404class 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
399444if __name__ == '__main__' :
0 commit comments