Skip to content

Don't remove comments when formatting #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion crossplane/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@


def format(filename, indent=4, tabs=False):
payload = parse(filename, single=True, check_ctx=False, check_args=False)
payload = parse(
filename,
comments=True,
single=True,
check_ctx=False,
check_args=False
)

if payload['status'] != 'ok':
e = payload['errors'][0]
Expand Down
28 changes: 26 additions & 2 deletions tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ def test_format_messy_config():
output = crossplane.format(config)
assert output == '\n'.join([
'user nobody;',
r'# hello\n\\n\\\n worlddd \#\\#\\\# dfsf\n \\n \\\n ',
'events {',
' worker_connections 2048;',
'}',
'http {',
'http { #forteen',
' # this is a comment',
' access_log off;',
' default_type text/plain;',
' error_log off;',
Expand All @@ -33,7 +35,7 @@ def test_format_messy_config():
' location /bar {',
' }',
' location /\{\;\}\ #\ ab {',
' }',
' } # hello',
' if ($request_method = P\{O\)\###\;ST) {',
' }',
' location /status.html {',
Expand Down Expand Up @@ -75,3 +77,25 @@ def test_format_args_not_analyzed():
'http {',
'}'
])


def test_format_with_comments():
dirname = os.path.join(here, 'configs', 'with-comments')
config = os.path.join(dirname, 'nginx.conf')
output = crossplane.format(config)
assert output == '\n'.join([
'events {',
' worker_connections 1024;',
'}',
'#comment',
'http {',
' server {',
' listen 127.0.0.1:8080; #listen',
' server_name default_server;',
' location / { ## this is brace',
' # location /',
" return 200 'foo bar baz';",
' }',
' }',
'}'
])