Skip to content

Commit 91763f5

Browse files
bz2adrienverge
authored andcommitted
Fix new-lines rule on Python 3
Use io.open() when reading files in cli which has the same behaviour in Python 2 and Python 3, and supply the newline='' parameter which handles but does not translate line endings. Add dos.yml test file with windows newlines. Also add to file finding test expected output. Add test for new-lines rule through the cli. Validates files are read with the correct universal newlines setting. Fixes #228
1 parent 5b049e4 commit 91763f5

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

tests/test_cli.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ def setUpClass(cls):
8888
u'# 19.99 €\n'
8989
u'- お早う御座います。\n'
9090
u'# الأَبْجَدِيَّة العَرَبِيَّة\n').encode('utf-8'),
91+
# dos line endings yaml
92+
'dos.yml': '---\r\n'
93+
'dos: true',
9194
})
9295

9396
@classmethod
@@ -101,6 +104,7 @@ def test_find_files_recursively(self):
101104
self.assertEqual(
102105
sorted(cli.find_files_recursively([self.wd], conf)),
103106
[os.path.join(self.wd, 'a.yaml'),
107+
os.path.join(self.wd, 'dos.yml'),
104108
os.path.join(self.wd, 'empty.yml'),
105109
os.path.join(self.wd, 's/s/s/s/s/s/s/s/s/s/s/s/s/s/s/file.yaml'),
106110
os.path.join(self.wd, 'sub/ok.yaml'),
@@ -146,7 +150,8 @@ def test_find_files_recursively(self):
146150
' - \'*.yml\'\n')
147151
self.assertEqual(
148152
sorted(cli.find_files_recursively([self.wd], conf)),
149-
[os.path.join(self.wd, 'empty.yml')]
153+
[os.path.join(self.wd, 'dos.yml'),
154+
os.path.join(self.wd, 'empty.yml')]
150155
)
151156

152157
conf = config.YamlLintConfig('extends: default\n'
@@ -163,6 +168,7 @@ def test_find_files_recursively(self):
163168
self.assertEqual(
164169
sorted(cli.find_files_recursively([self.wd], conf)),
165170
[os.path.join(self.wd, 'a.yaml'),
171+
os.path.join(self.wd, 'dos.yml'),
166172
os.path.join(self.wd, 'empty.yml'),
167173
os.path.join(self.wd, 'no-yaml.json'),
168174
os.path.join(self.wd, 'non-ascii/éçäγλνπ¥/utf-8'),
@@ -179,6 +185,7 @@ def test_find_files_recursively(self):
179185
self.assertEqual(
180186
sorted(cli.find_files_recursively([self.wd], conf)),
181187
[os.path.join(self.wd, 'a.yaml'),
188+
os.path.join(self.wd, 'dos.yml'),
182189
os.path.join(self.wd, 'empty.yml'),
183190
os.path.join(self.wd, 'no-yaml.json'),
184191
os.path.join(self.wd, 'non-ascii/éçäγλνπ¥/utf-8'),
@@ -493,3 +500,20 @@ def test_run_no_warnings_and_strict(self):
493500
with RunContext(self) as ctx:
494501
cli.run((path, '--no-warnings', '-s'))
495502
self.assertEqual(ctx.returncode, 2)
503+
504+
def test_run_non_universal_newline(self):
505+
path = os.path.join(self.wd, 'dos.yml')
506+
507+
with RunContext(self) as ctx:
508+
cli.run(('-d', 'rules:\n new-lines:\n type: dos', path))
509+
self.assertEqual((ctx.returncode, ctx.stdout, ctx.stderr), (0, '', ''))
510+
511+
with RunContext(self) as ctx:
512+
cli.run(('-d', 'rules:\n new-lines:\n type: unix', path))
513+
expected_out = (
514+
'%s\n'
515+
' 1:4 error wrong new line character: expected \\n'
516+
' (new-lines)\n'
517+
'\n' % path)
518+
self.assertEqual(
519+
(ctx.returncode, ctx.stdout, ctx.stderr), (1, expected_out, ''))

yamllint/cli.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from __future__ import print_function
1818

1919
import argparse
20+
import io
2021
import os
2122
import platform
2223
import sys
@@ -176,7 +177,7 @@ def run(argv=None):
176177
for file in find_files_recursively(args.files, conf):
177178
filepath = file[2:] if file.startswith('./') else file
178179
try:
179-
with open(file) as f:
180+
with io.open(file, newline='') as f:
180181
problems = linter.run(f, conf, filepath)
181182
except EnvironmentError as e:
182183
print(e, file=sys.stderr)

0 commit comments

Comments
 (0)