diff --git a/tests/test_cli.py b/tests/test_cli.py index b8c0cf9..517bc62 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -88,6 +88,9 @@ class CommandLineTestCase(unittest.TestCase): u'# 19.99 €\n' u'- お早う御座います。\n' u'# الأَبْجَدِيَّة العَرَبِيَّة\n').encode('utf-8'), + # dos line endings yaml + 'dos.yml': '---\r\n' + 'dos: true', }) @classmethod @@ -101,6 +104,7 @@ class CommandLineTestCase(unittest.TestCase): self.assertEqual( sorted(cli.find_files_recursively([self.wd], conf)), [os.path.join(self.wd, 'a.yaml'), + os.path.join(self.wd, 'dos.yml'), os.path.join(self.wd, 'empty.yml'), os.path.join(self.wd, 's/s/s/s/s/s/s/s/s/s/s/s/s/s/s/file.yaml'), os.path.join(self.wd, 'sub/ok.yaml'), @@ -146,7 +150,8 @@ class CommandLineTestCase(unittest.TestCase): ' - \'*.yml\'\n') self.assertEqual( sorted(cli.find_files_recursively([self.wd], conf)), - [os.path.join(self.wd, 'empty.yml')] + [os.path.join(self.wd, 'dos.yml'), + os.path.join(self.wd, 'empty.yml')] ) conf = config.YamlLintConfig('extends: default\n' @@ -163,6 +168,7 @@ class CommandLineTestCase(unittest.TestCase): self.assertEqual( sorted(cli.find_files_recursively([self.wd], conf)), [os.path.join(self.wd, 'a.yaml'), + os.path.join(self.wd, 'dos.yml'), os.path.join(self.wd, 'empty.yml'), os.path.join(self.wd, 'no-yaml.json'), os.path.join(self.wd, 'non-ascii/éçäγλνπ¥/utf-8'), @@ -179,6 +185,7 @@ class CommandLineTestCase(unittest.TestCase): self.assertEqual( sorted(cli.find_files_recursively([self.wd], conf)), [os.path.join(self.wd, 'a.yaml'), + os.path.join(self.wd, 'dos.yml'), os.path.join(self.wd, 'empty.yml'), os.path.join(self.wd, 'no-yaml.json'), os.path.join(self.wd, 'non-ascii/éçäγλνπ¥/utf-8'), @@ -493,3 +500,20 @@ class CommandLineTestCase(unittest.TestCase): with RunContext(self) as ctx: cli.run((path, '--no-warnings', '-s')) self.assertEqual(ctx.returncode, 2) + + def test_run_non_universal_newline(self): + path = os.path.join(self.wd, 'dos.yml') + + with RunContext(self) as ctx: + cli.run(('-d', 'rules:\n new-lines:\n type: dos', path)) + self.assertEqual((ctx.returncode, ctx.stdout, ctx.stderr), (0, '', '')) + + with RunContext(self) as ctx: + cli.run(('-d', 'rules:\n new-lines:\n type: unix', path)) + expected_out = ( + '%s\n' + ' 1:4 error wrong new line character: expected \\n' + ' (new-lines)\n' + '\n' % path) + self.assertEqual( + (ctx.returncode, ctx.stdout, ctx.stderr), (1, expected_out, '')) diff --git a/yamllint/cli.py b/yamllint/cli.py index 26bdb1f..e99fd2c 100644 --- a/yamllint/cli.py +++ b/yamllint/cli.py @@ -17,6 +17,7 @@ from __future__ import print_function import argparse +import io import os import platform import sys @@ -176,7 +177,7 @@ def run(argv=None): for file in find_files_recursively(args.files, conf): filepath = file[2:] if file.startswith('./') else file try: - with open(file) as f: + with io.open(file, newline='') as f: problems = linter.run(f, conf, filepath) except EnvironmentError as e: print(e, file=sys.stderr)