This commit is contained in:
Spencer Phillip Young
2020-07-16 12:01:48 +07:00
committed by GitHub
3 changed files with 92 additions and 3 deletions

View File

@@ -95,6 +95,16 @@ class CommandLineTestCase(unittest.TestCase):
# dos line endings yaml
'dos.yml': '---\r\n'
'dos: true',
# UTF-16 Little Endian BOM
'non-ascii/utf-16-le':
b'\xff\xfe' + u'---\nutf16le: true\n'.encode('utf-16-le'),
# UTF-16 Big Endian
'non-ascii/utf-16-be':
b'\xfe\xff' + u'---\nutf16be: true\n'.encode('utf-16-be'),
# UTF-8 BOM
'non-ascii/utf-8': b'\xef\xbb\xbf---\nutf8: true\n',
# Random bytes that have no possible encoding
'non-ascii/undetectable': b'\x05\xfc\x17A\xb6\x15\x15\x90>9'
})
@classmethod
@@ -178,6 +188,10 @@ class CommandLineTestCase(unittest.TestCase):
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/undetectable'),
os.path.join(self.wd, 'non-ascii/utf-16-be'),
os.path.join(self.wd, 'non-ascii/utf-16-le'),
os.path.join(self.wd, 'non-ascii/utf-8'),
os.path.join(self.wd, 'non-ascii/éçäγλνπ¥/utf-8'),
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/directory.yaml/empty.yml'),
@@ -197,6 +211,10 @@ class CommandLineTestCase(unittest.TestCase):
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/undetectable'),
os.path.join(self.wd, 'non-ascii/utf-16-be'),
os.path.join(self.wd, 'non-ascii/utf-16-le'),
os.path.join(self.wd, 'non-ascii/utf-8'),
os.path.join(self.wd, 'non-ascii/éçäγλνπ¥/utf-8'),
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/directory.yaml/empty.yml'),
@@ -211,7 +229,8 @@ class CommandLineTestCase(unittest.TestCase):
' - \'**/utf-8\'\n')
self.assertEqual(
sorted(cli.find_files_recursively([self.wd], conf)),
[os.path.join(self.wd, 'non-ascii/éçäγλνπ¥/utf-8')]
[os.path.join(self.wd, 'non-ascii/utf-8'),
os.path.join(self.wd, 'non-ascii/éçäγλνπ¥/utf-8')]
)
def test_run_with_bad_arguments(self):
@@ -547,3 +566,55 @@ class CommandLineTestCase(unittest.TestCase):
'\n' % path)
self.assertEqual(
(ctx.returncode, ctx.stdout, ctx.stderr), (1, expected_out, ''))
def test_encoding_detection_utf16le(self):
path = os.path.join(self.wd, 'non-ascii/utf-16-le')
with RunContext(self) as ctx:
cli.run(('-f', 'parsable', path))
self.assertEqual((ctx.returncode, ctx.stdout, ctx.stderr), (0, '', ''))
def test_encoding_detection_utf16be(self):
path = os.path.join(self.wd, 'non-ascii/utf-16-be')
with RunContext(self) as ctx:
cli.run(('-f', 'parsable', path))
self.assertEqual((ctx.returncode, ctx.stdout, ctx.stderr), (0, '', ''))
def test_encoding_detection_utf8(self):
path = os.path.join(self.wd, 'non-ascii/utf-8')
with RunContext(self) as ctx:
cli.run(('-f', 'parsable', path))
self.assertEqual((ctx.returncode, ctx.stdout, ctx.stderr), (0, '', ''))
def test_detected_encoding_utf8(self):
path = os.path.join(self.wd, 'non-ascii/éçäγλνπ¥/utf-8')
with cli.yamlopen(path) as yaml_file:
yaml_file.read()
self.assertEqual(yaml_file.encoding, 'utf-8')
def test_detected_encoding_utf8_sig(self):
path = os.path.join(self.wd, 'non-ascii/utf-8')
with cli.yamlopen(path) as yaml_file:
yaml_file.read()
self.assertEqual(yaml_file.encoding, 'UTF-8-SIG')
def test_detected_encoding_utf16(self):
path = os.path.join(self.wd, 'non-ascii/utf-16-le')
with cli.yamlopen(path) as yaml_file:
yaml_file.read()
self.assertEqual(yaml_file.encoding, 'UTF-16')
path = os.path.join(self.wd, 'non-ascii/utf-16-be')
with cli.yamlopen(path) as yaml_file:
yaml_file.read()
self.assertEqual(yaml_file.encoding, 'UTF-16')
def test_explicit_encoding(self):
path = os.path.join(self.wd, 'a.yaml')
with cli.yamlopen(path, encoding='windows-1252') as yaml_file:
yaml_file.read()
self.assertEqual(yaml_file.encoding, 'windows-1252')
def test_default_encoding(self):
path = os.path.join(self.wd, 'non-ascii/undetectable')
with cli.yamlopen(path) as yaml_file:
encoding = yaml_file.encoding
self.assertEqual(encoding, 'utf-8')