From 91c977fd9aee0a873da2571ced5e3657252aed58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20S=C5=82oczy=C5=84ski?= Date: Tue, 2 Apr 2019 18:41:28 +0200 Subject: [PATCH] configfiles: Add NginxBear This bear verifies syntax of nginx configuration files. Closes https://github.com/coala/coala-bears/issues/1638 --- bears/configfiles/NginxBear.py | 40 ++++++++++++++++++++++++++++++ tests/configfiles/NginxBearTest.py | 35 ++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 bears/configfiles/NginxBear.py create mode 100644 tests/configfiles/NginxBearTest.py 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, ))