Skip to content

Commit ad643a0

Browse files
committed
Don't remove comments when formatting
1 parent 59186f5 commit ad643a0

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

crossplane/formatter.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@
55

66

77
def format(filename, indent=4, tabs=False):
8-
payload = parse(filename, single=True, check_ctx=False, check_args=False)
8+
payload = parse(
9+
filename,
10+
comments=True,
11+
single=True,
12+
check_ctx=False,
13+
check_args=False
14+
)
915

1016
if payload['status'] != 'ok':
1117
e = payload['errors'][0]

tests/test_format.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ def test_format_messy_config():
1111
output = crossplane.format(config)
1212
assert output == '\n'.join([
1313
'user nobody;',
14+
r'# hello\n\\n\\\n worlddd \#\\#\\\# dfsf\n \\n \\\n ',
1415
'events {',
1516
' worker_connections 2048;',
1617
'}',
17-
'http {',
18+
'http { #forteen',
19+
' # this is a comment',
1820
' access_log off;',
1921
' default_type text/plain;',
2022
' error_log off;',
@@ -33,7 +35,7 @@ def test_format_messy_config():
3335
' location /bar {',
3436
' }',
3537
' location /\{\;\}\ #\ ab {',
36-
' }',
38+
' } # hello',
3739
' if ($request_method = P\{O\)\###\;ST) {',
3840
' }',
3941
' location /status.html {',
@@ -75,3 +77,25 @@ def test_format_args_not_analyzed():
7577
'http {',
7678
'}'
7779
])
80+
81+
82+
def test_format_with_comments():
83+
dirname = os.path.join(here, 'configs', 'with-comments')
84+
config = os.path.join(dirname, 'nginx.conf')
85+
output = crossplane.format(config)
86+
assert output == '\n'.join([
87+
'events {',
88+
' worker_connections 1024;',
89+
'}',
90+
'#comment',
91+
'http {',
92+
' server {',
93+
' listen 127.0.0.1:8080; #listen',
94+
' server_name default_server;',
95+
' location / { ## this is brace',
96+
' # location /',
97+
" return 200 'foo bar baz';",
98+
' }',
99+
' }',
100+
'}'
101+
])

0 commit comments

Comments
 (0)