diff options
author | Mike Frysinger <vapier@chromium.org> | 2015-10-21 02:13:18 -0400 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2015-10-21 14:28:42 -0700 |
commit | bed3479f4f77fc2bda35cdf4fd902530f50bd9a3 (patch) | |
tree | 00d8ea49758469c384a0e4f36258d9f749a6f8a1 | |
parent | 900c6718243026e43377e6a2c4d4986adba372db (diff) | |
download | chromite-bed3479f4f77fc2bda35cdf4fd902530f50bd9a3.tar.gz |
cros lint: detect & warn about trailing newlines
So we don't have to have reviewers catch these errors, add a linter check.
BUG=None
TEST=`cros lint` detected a few files with bad trailing newlines
Change-Id: I6dd90e25516e77902fa91227b2d4c4036cc7d11a
Reviewed-on: https://chromium-review.googlesource.com/307175
Commit-Ready: Mike Frysinger <vapier@chromium.org>
Tested-by: Mike Frysinger <vapier@chromium.org>
Reviewed-by: Paul Hobbs <phobbs@google.com>
-rw-r--r-- | cli/cros/lint.py | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/cli/cros/lint.py b/cli/cros/lint.py index f54c8a6b9..5f9547105 100644 --- a/cli/cros/lint.py +++ b/cli/cros/lint.py @@ -385,6 +385,7 @@ class SourceChecker(BaseChecker): class _MessageR9200(object): pass class _MessageR9201(object): pass class _MessageR9202(object): pass + class _MessageR9210(object): pass # pylint: enable=class-missing-docstring,multiple-statements name = 'source_checker' @@ -397,6 +398,8 @@ class SourceChecker(BaseChecker): ('missing-shebang'), _MessageR9201), 'R9202': ('Shebang is set, but file is not executable', ('spurious-shebang'), _MessageR9202), + 'R9210': ('Trailing new lines found at end of file', + ('excess-trailing-newlines'), _MessageR9210), } options = () @@ -405,6 +408,7 @@ class SourceChecker(BaseChecker): stream = node.file_stream stream.seek(0) self._check_shebang(node, stream) + self._check_trailing_lines(node, stream) def _check_shebang(self, _node, stream): """Verify the shebang is version specific""" @@ -424,6 +428,14 @@ class SourceChecker(BaseChecker): if parts[0] not in ('#!/usr/bin/python2', '#!/usr/bin/python3'): self.add_message('R9200') + def _check_trailing_lines(self, _node, stream): + """Reject trailing lines""" + st = os.fstat(stream.fileno()) + if st.st_size > 1: + stream.seek(st.st_size - 2) + if not stream.read().strip('\n'): + self.add_message('R9210') + class ChromiteLoggingChecker(BaseChecker): """Make sure we enforce rules on importing logging.""" |