Put all messages into separate package

Change-Id: If519f36515e2125238ee9b531fe7ad896d11915f
This commit is contained in:
Stanislav Kudriashev
2014-10-10 11:55:27 +03:00
parent dbc021c9ee
commit d5d7460439
3 changed files with 39 additions and 14 deletions

View File

@@ -12,11 +12,15 @@
# License for the specific language governing permissions and limitations
# under the License.
from __future__ import absolute_import
import argparse
import fileinput
import re
import sys
from bashate import messages
def not_continuation(line):
return not re.search('\\\\$', line)
@@ -34,30 +38,28 @@ def check_for_do(line, report):
if re.search('for \([^\(]', line):
return
if not re.search(';\s*do$', line):
report.print_error(('E010: Do not on same line as %s' %
operator), line)
report.print_error((messages.E010 % operator), line)
def check_if_then(line, report):
if not_continuation(line):
if re.search('^\s*(el)?if \[', line):
if not re.search(';\s*then$', line):
report.print_error('E011: Then keyword is not on same line '
'as if or elif keyword', line)
report.print_error(messages.E011, line)
def check_no_trailing_whitespace(line, report):
if re.search('[ \t]+$', line):
report.print_error('E001: Trailing Whitespace', line)
report.print_error(messages.E001, line)
def check_indents(line, report):
m = re.search('^(?P<indent>[ \t]+)', line)
if m:
if re.search('\t', m.group('indent')):
report.print_error('E002: Tab indents', line)
report.print_error(messages.E002, line)
if (len(m.group('indent')) % 4) != 0:
report.print_error('E003: Indent not multiple of 4', line)
report.print_error(messages.E003, line)
def check_function_decl(line, report):
@@ -72,8 +74,7 @@ def check_function_decl(line, report):
failed = True
if failed:
report.print_error('E020: Function declaration not in format '
'"^function name {$"', line)
report.print_error(messages.E020, line)
def starts_multiline(line):
@@ -92,8 +93,7 @@ def end_of_multiline(line, token):
def check_arithmetic(line, report):
if "$[" in line:
report.print_error('E041: Arithmetic expansion using $[ '
'is deprecated for $((', line)
report.print_error(messages.E041, line)
class BashateRun(object):
@@ -146,7 +146,7 @@ class BashateRun(object):
# find the end of a heredoc in the last file.
if in_multiline:
report.print_error(
'E012: heredoc did not end before EOF',
messages.E012,
multiline_line,
filename=prev_file,
filelineno=multiline_start)
@@ -156,7 +156,7 @@ class BashateRun(object):
# newline
if prev_file and not prev_line.endswith('\n'):
report.print_error(
'E004: file did not end with a newline',
messages.E004,
prev_line,
filename=prev_file,
filelineno=prev_lineno)

24
bashate/messages.py Normal file
View File

@@ -0,0 +1,24 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
E001 = "E001: Trailing Whitespace"
E002 = "E002: Tab indents"
E003 = "E003: Indent not multiple of 4"
E004 = "E004: File did not end with a newline"
E010 = "E010: Do not on same line as %s"
E011 = "E011: Then keyword is not on same line as if or elif keyword"
E012 = "E012: heredoc did not end before EOF"
E020 = "E020: Function declaration not in format ^function name {$"
E041 = "E041: Arithmetic expansion using $[ is deprecated for $(('"

View File

@@ -20,6 +20,7 @@ Tests for `bashate` module.
import mock
from bashate import bashate
from bashate import messages
from bashate.tests import base
@@ -63,7 +64,7 @@ class TestBashate(base.TestCase):
bashate.check_for_do(test_line, self.run)
m_print_error.assert_called_once_with(
'E010: Do not on same line as while', test_line)
messages.E010 % 'while', test_line)
class TestBashateSamples(base.TestCase):