From 1d958a97f5073f5aaaacdda16f0caca3af0d918b Mon Sep 17 00:00:00 2001 From: Jan Wille Date: Mon, 20 Jun 2022 17:27:42 +0200 Subject: [PATCH] new-lines: add `type: platform` config option The new option infers the correct newline character(s) from the operating system running yamllint. --- tests/rules/test_new_lines.py | 36 +++++++++++++++++++++++++++++++++++ yamllint/rules/new_lines.py | 12 +++++++++--- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/tests/rules/test_new_lines.py b/tests/rules/test_new_lines.py index be370fd..80334ea 100644 --- a/tests/rules/test_new_lines.py +++ b/tests/rules/test_new_lines.py @@ -13,6 +13,8 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +from unittest import mock + from tests.common import RuleTestCase @@ -58,3 +60,37 @@ class NewLinesTestCase(RuleTestCase): 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)) diff --git a/yamllint/rules/new_lines.py b/yamllint/rules/new_lines.py index 9c5a022..b3f018a 100644 --- a/yamllint/rules/new_lines.py +++ b/yamllint/rules/new_lines.py @@ -18,8 +18,11 @@ Use this rule to force the type of new line characters. .. 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) @@ -30,19 +33,22 @@ Use this rule to force the type of new line characters. 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'