-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from nginxinc/add-better-formatter
Added crossplane.format
- Loading branch information
Showing
7 changed files
with
122 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# -*- coding: utf-8 -*- | ||
from .errors import NgxParserBaseException | ||
from .builder import build | ||
from .parser import parse | ||
|
||
|
||
def format(filename, indent=4, tabs=False): | ||
payload = parse(filename, single=True, check_ctx=False, check_args=False) | ||
|
||
if payload['status'] != 'ok': | ||
e = payload['errors'][0] | ||
raise NgxParserBaseException(e['error'], e['file'], e['line']) | ||
|
||
parsed = payload['config'][0]['parsed'] | ||
output = build(parsed, indent=indent, tabs=tabs) | ||
return output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
user; | ||
events {} | ||
http {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# -*- coding: utf-8 -*- | ||
import os | ||
|
||
import crossplane | ||
from . import here | ||
|
||
|
||
def test_format_messy_config(): | ||
dirname = os.path.join(here, 'configs', 'messy') | ||
config = os.path.join(dirname, 'nginx.conf') | ||
output = crossplane.format(config) | ||
assert output == '\n'.join([ | ||
'user nobody;', | ||
'events {', | ||
' worker_connections 2048;', | ||
'}', | ||
'http {', | ||
' access_log off;', | ||
' default_type text/plain;', | ||
' error_log off;', | ||
' server {', | ||
' listen 8083;', | ||
r""" return 200 'Ser" \' \' ver\\ \ $server_addr:\$server_port\n\nTime: $time_local\n\n';""", | ||
' }', | ||
' server {', | ||
' listen 8080;', | ||
' root /usr/share/nginx/html;', | ||
" location ~ '/hello/world;' {", | ||
' return 301 /status.html;', | ||
' }', | ||
' location /foo {', | ||
' }', | ||
' location /bar {', | ||
' }', | ||
' location /\{\;\}\ #\ ab {', | ||
' }', | ||
' if ($request_method = P\{O\)\###\;ST) {', | ||
' }', | ||
' location /status.html {', | ||
" try_files '/abc/${uri} /abc/${uri}.html' =404;", | ||
' }', | ||
r" location '/sta;\n tus' {", | ||
' return 302 /status.html;', | ||
' }', | ||
' location /upstream_conf {', | ||
' return 200 /status.html;', | ||
' }', | ||
' }', | ||
' server {', | ||
' }', | ||
'}' | ||
]) | ||
|
||
|
||
def test_format_not_main_file(): | ||
dirname = os.path.join(here, 'configs', 'includes-globbed', 'servers') | ||
config = os.path.join(dirname, 'server1.conf') | ||
output = crossplane.format(config) | ||
assert output == '\n'.join([ | ||
'server {', | ||
' listen 8080;', | ||
' include locations/*.conf;', | ||
'}' | ||
]) | ||
|
||
|
||
def test_format_args_not_analyzed(): | ||
dirname = os.path.join(here, 'configs', 'bad-args') | ||
config = os.path.join(dirname, 'nginx.conf') | ||
output = crossplane.format(config) | ||
assert output == '\n'.join([ | ||
'user;', | ||
'events {', | ||
'}', | ||
'http {', | ||
'}' | ||
]) |