Add check D005 - no newline at end of file

Change-Id: I160bb7e9b3c904655567caee999ac91366668109
This commit is contained in:
Christian Berendt 2014-09-30 21:18:50 +02:00
parent 8b8f22329b
commit 5a4341727e
7 changed files with 38 additions and 2 deletions

View File

@ -10,4 +10,4 @@ the workflow documented at:
http://wiki.openstack.org/GerritWorkflow
Pull requests submitted through GitHub will be ignored.
Pull requests submitted through GitHub will be ignored.

View File

@ -1,4 +1,4 @@
doc8 Style Commandments
===============================================
Read the OpenStack Style Commandments http://docs.openstack.org/developer/hacking/
Read the OpenStack Style Commandments http://docs.openstack.org/developer/hacking/

View File

@ -43,6 +43,7 @@ Command line usage
- no trailing whitespace - D002
- no tabulation for indentation - D003
- no carriage returns (use unix newlines) - D004
- no newline at end of file - D005
positional arguments:
path path to scan for doc files (default: os.getcwd())

View File

@ -73,6 +73,17 @@ class CheckCarriageReturn(LineCheck):
yield ('D004', 'Found literal carriage return')
class CheckNewlineEndOfFile(ContentCheck):
REPORTS = frozenset(["D005"])
def __init__(self, cfg):
super(CheckNewlineEndOfFile, self).__init__(cfg)
def report_iter(self, parsed_file):
if parsed_file.lines and not parsed_file.lines[-1].endswith('\n'):
yield (len(parsed_file.lines), 'D005', 'No newline at end of file')
class CheckValidity(ContentCheck):
REPORTS = frozenset(["D000"])
EXT_MATCHER = re.compile(r"(.*)[.]rst", re.I)

View File

@ -27,6 +27,7 @@ What is checked:
- no trailing whitespace - D002
- no tabulation for indentation - D003
- no carriage returns (use unix newlines) - D004
- no newline at end of file - D005
"""
import argparse
@ -131,6 +132,7 @@ def fetch_checks(cfg):
checks.CheckIndentationNoTab(cfg),
checks.CheckCarriageReturn(cfg),
checks.CheckMaxLineLength(cfg),
checks.CheckNewlineEndOfFile(cfg),
]
mgr = extension.ExtensionManager(
namespace='doc8.extension.check',

View File

@ -96,6 +96,11 @@ class ParsedFile(object):
line = line[0:-1]
yield line
@property
def lines(self):
self._read()
return self._lines
@property
def extension(self):
return self._extension

View File

@ -120,3 +120,20 @@ test
check = checks.CheckMaxLineLength(conf)
errors = list(check.report_iter(parsed_file))
self.assertEqual(expected_errors, len(errors))
class TestNewlineEndOfFile(testtools.TestCase):
def test_newline(self):
tests = [(1, "testing"),
(1, "testing\ntesting"),
(0, "testing\n"),
(0, "testing\ntesting\n")]
for expected_errors, line in tests:
with tempfile.NamedTemporaryFile() as fh:
fh.write(line)
fh.flush()
parsed_file = parser.ParsedFile(fh.name)
check = checks.CheckNewlineEndOfFile({})
errors = list(check.report_iter(parsed_file))
self.assertEqual(expected_errors, len(errors))