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

rule new-lines: add type: platform configuration option #474

Merged
merged 3 commits into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
36 changes: 36 additions & 0 deletions tests/rules/test_new_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from unittest import mock

from tests.common import RuleTestCase


Expand Down Expand Up @@ -58,3 +60,37 @@ def test_dos_type(self):
self.check('\r\n', conf)
self.check('---\ntext\n', conf, problem=(1, 4))
self.check('---\r\ntext\r\n', conf)

def test_platform_type(self):
conf = ('new-line-at-end-of-file: disable\n'
'new-lines: {type: platform}\n')

self.check('', conf)

# mock the Linux new-line-character
with mock.patch('yamllint.rules.new_lines.linesep', '\n'):
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\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))

# 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\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))
30 changes: 18 additions & 12 deletions yamllint/rules/new_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@

.. rubric:: Options

* Set ``type`` to ``unix`` to use UNIX-typed new line characters (``\\n``), or
``dos`` to use DOS-typed new line characters (``\\r\\n``).
* Set ``type`` to ``unix`` to enforce UNIX-typed new line characters (``\\n``),
set ``type`` to ``dos`` to enforce DOS-typed new line characters
(``\\r\\n``), or set ``type`` to ``platform`` to infer the type from the
system running yamllint (``\\n`` on POSIX / UNIX / Linux / Mac OS systems or
``\\r\\n`` on DOS / Windows systems).

.. rubric:: Default values (when enabled)

Expand All @@ -30,24 +33,27 @@
type: unix
"""

from os import linesep

from yamllint.linter import LintProblem


ID = 'new-lines'
TYPE = 'line'
CONF = {'type': ('unix', 'dos')}
CONF = {'type': ('unix', 'dos', 'platform')}
DEFAULT = {'type': 'unix'}


def check(conf, line):
if conf['type'] == 'unix':
newline_char = '\n'
elif conf['type'] == 'platform':
newline_char = linesep
elif conf['type'] == 'dos':
newline_char = '\r\n'

if line.start == 0 and len(line.buffer) > line.end:
if conf['type'] == 'dos':
if (line.end + 2 > len(line.buffer) or
line.buffer[line.end:line.end + 2] != '\r\n'):
yield LintProblem(1, line.end - line.start + 1,
'wrong new line character: expected \\r\\n')
else:
if line.buffer[line.end] == '\r':
yield LintProblem(1, line.end - line.start + 1,
'wrong new line character: expected \\n')
if line.buffer[line.end:line.end + len(newline_char)] != newline_char:
yield LintProblem(1, line.end - line.start + 1,
'wrong new line character: expected {}'
.format(repr(newline_char).strip('\'')))