diff --git a/bears/configfiles/NginxBear.py b/bears/configfiles/NginxBear.py new file mode 100644 index 0000000000..c88b2bc760 --- /dev/null +++ b/bears/configfiles/NginxBear.py @@ -0,0 +1,40 @@ +from coalib.bearlib.abstractions.Linter import linter +from dependency_management.requirements.DistributionRequirement \ + import DistributionRequirement +from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY + + +@linter(executable='nginx', + output_format='regex', + output_regex=r'\[(?P.+)\](?P.+)' + r':(?P\d+)', + severity_map={'emerg': RESULT_SEVERITY.MAJOR, + 'alert': RESULT_SEVERITY.MAJOR, + 'warn': RESULT_SEVERITY.NORMAL}, + use_stderr=True) +class NginxBear: + """ + Checks syntax of nginx configuration files using `nginx -tc` command. + """ + + LANGUAGES = {'nginx'} + REQUIREMENTS = { + DistributionRequirement( + apt_get='nginx', + brew='nginx', + dnf='nginx', + portage=None, + xbps='nginx', + yum='nginx', + zypper='nginx', + ), + } + AUTHORS = {'The coala developers'} + AUTHORS_EMAILS = {'coala-devel@googlegroups.com'} + LICENSE = 'AGPL-3.0' + CAN_DETECT = {'Syntax'} + SEE_MORE = 'https://nginx.com' + + @staticmethod + def create_arguments(filename, file, config_file): + return ('-tc', filename) diff --git a/tests/configfiles/NginxBearTest.py b/tests/configfiles/NginxBearTest.py new file mode 100644 index 0000000000..2b2c78a882 --- /dev/null +++ b/tests/configfiles/NginxBearTest.py @@ -0,0 +1,35 @@ +from bears.configfiles.NginxBear import NginxBear +from coalib.testing.LocalBearTestHelper import verify_local_bear + +good_file = """ +http{ + server { + listen 80; + + server_name example.com www.example.com; + + root /usr/local/www/example.com; + + access_log /var/log/nginx/example.access.log; + error_log /var/log/nginx/example.error.log; + } +} +""" + +bad_file = """ +http{ + server { + listen 80; + + server_name example.com www.example.com; + + root /usr/local/www/example.com; + + access_log /var/log/nginx/example.access.log; + error_log /var/log/nginx/example.error.log; + } +""" + +NginxBearTest = verify_local_bear(NginxBear, + valid_files=(good_file, ), + invalid_files=(bad_file, ))