Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply new-lines check to all lines of a file, not just the first one #693

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions tests/rules/test_new_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_unix_type(self):
self.check('\n', conf)
self.check('\r\n', conf, problem=(1, 1))
self.check('---\ntext\n', conf)
self.check('---\r\ntext\r\n', conf, problem=(1, 4))
self.check('---\r\ntext\r\n', conf, problem1=(1, 4), problem2=(2, 5))

def test_unix_type_required_st_sp(self):
# If we find a CRLF when looking for Unix newlines, yamllint
Expand All @@ -49,7 +49,7 @@ def test_unix_type_required_st_sp(self):
'new-lines: {type: unix}\n'
'comments:\n'
' require-starting-space: true\n')
self.check('---\r\n#\r\n', conf, problem=(1, 4))
self.check('---\r\n#\r\n', conf, problem1=(1, 4), problem2=(2, 2))

def test_dos_type(self):
conf = ('new-line-at-end-of-file: disable\n'
Expand All @@ -58,7 +58,7 @@ def test_dos_type(self):
self.check('\r', conf)
self.check('\n', conf, problem=(1, 1))
self.check('\r\n', conf)
self.check('---\ntext\n', conf, problem=(1, 4))
self.check('---\ntext\n', conf, problem1=(1, 4), problem2=(2, 5))
self.check('---\r\ntext\r\n', conf)

def test_platform_type(self):
Expand All @@ -72,25 +72,17 @@ def test_platform_type(self):
self.check('\n', conf)
self.check('\r\n', conf, problem=(1, 1))
self.check('---\ntext\n', conf)
self.check('---\r\ntext\r\n', conf, problem=(1, 4))
self.check('---\r\n#\r\n', conf, problem1=(1, 4), problem2=(2, 2))
self.check('---\r\ntext\n', conf, problem=(1, 4))
# FIXME: the following tests currently don't work
# because only the first line is checked for line-endings
# see: issue #475
# ---
# self.check('---\ntext\r\nfoo\n', conf, problem=(2, 4))
# self.check('---\ntext\r\n', conf, problem=(2, 4))
self.check('---\ntext\r\nfoo\n', conf, problem=(2, 5))
self.check('---\ntext\r\n', conf, problem=(2, 5))

# mock the Windows new-line-character
with mock.patch('yamllint.rules.new_lines.linesep', '\r\n'):
self.check('\r\n', conf)
self.check('\n', conf, problem=(1, 1))
self.check('---\r\ntext\r\n', conf)
self.check('---\ntext\n', conf, problem=(1, 4))
self.check('---\ntext\n', conf, problem1=(1, 4), problem2=(2, 5))
self.check('---\ntext\r\n', conf, problem=(1, 4))
# FIXME: the following tests currently don't work
# because only the first line is checked for line-endings
# see: issue #475
# ---
# self.check('---\r\ntext\nfoo\r\n', conf, problem=(2, 4))
# self.check('---\r\ntext\n', conf, problem=(2, 4))
self.check('---\r\ntext\nfoo\r\n', conf, problem=(2, 5))
self.check('---\r\ntext\n', conf, problem=(2, 5))
4 changes: 2 additions & 2 deletions yamllint/rules/new_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ def check(conf, line):
elif conf['type'] == 'dos':
newline_char = '\r\n'

if line.start == 0 and len(line.buffer) > line.end:
if len(line.buffer) > line.end:
if line.buffer[line.end:line.end + len(newline_char)] != newline_char:
c = repr(newline_char).strip('\'')
yield LintProblem(1, line.end - line.start + 1,
yield LintProblem(line.line_no, line.end - line.start + 1,
f'wrong new line character: expected {c}')
Loading