Fix bug with loading buffer in ZipLoader in py3
Change-Id: I3a33ac60086bc2345742c18cb26940aa2ba7039b
This commit is contained in:
parent
c9e8de4050
commit
02544c5a61
|
@ -14,7 +14,6 @@
|
|||
|
||||
|
||||
import abc
|
||||
import io
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
|
@ -46,7 +45,7 @@ class FileWrapper(object):
|
|||
|
||||
def yaml(self):
|
||||
if self._yaml is None:
|
||||
sio = io.StringIO(six.text_type(self.raw()))
|
||||
sio = six.BytesIO(self.raw())
|
||||
setattr(sio, 'name', self._name)
|
||||
self._yaml = list(yaml.load_all(sio,
|
||||
yaml_loader.YamlLoader))
|
||||
|
|
|
@ -39,11 +39,11 @@ class DictLoader(pkg_loader.BaseLoader):
|
|||
|
||||
def open_file(self, path, mode='r'):
|
||||
if self.pkg[path]['format'] == 'raw':
|
||||
sio = io.StringIO(six.text_type(self.pkg[path]['content']))
|
||||
sio = io.BytesIO(six.b(self.pkg[path]['content']))
|
||||
setattr(sio, 'name', path)
|
||||
elif self.pkg[path]['format'] == 'yaml':
|
||||
content = yaml.safe_dump(self.pkg[path]['content'])
|
||||
sio = io.StringIO(six.text_type(content))
|
||||
sio = io.BytesIO(six.b(content))
|
||||
setattr(sio, 'name', path)
|
||||
else:
|
||||
raise ValueError('Unknown type of content')
|
||||
|
|
|
@ -31,9 +31,9 @@ class FileWrapperTest(base.TestCase):
|
|||
m_yaml.load_all.side_effect = yaml.load_all
|
||||
fake_pkg = mock.Mock()
|
||||
fake_pkg.open_file.side_effect = \
|
||||
lambda f: mock.mock_open(read_data='text')()
|
||||
lambda f: mock.mock_open(read_data=six.b('text'))()
|
||||
f = pkg_loader.FileWrapper(fake_pkg, 'fake_path')
|
||||
self.assertEqual('text', f.raw())
|
||||
self.assertEqual(six.b('text'), f.raw())
|
||||
|
||||
self.assertEqual(['text'], f.yaml())
|
||||
m_yaml.load_all.assert_called()
|
||||
|
@ -43,9 +43,9 @@ class FileWrapperTest(base.TestCase):
|
|||
m_yaml.load_all.assert_not_called()
|
||||
|
||||
fake_pkg.open_file.side_effect = \
|
||||
lambda f: mock.mock_open(read_data='!@#$%')()
|
||||
f = pkg_loader.FileWrapper(fake_pkg, 'fake_path')
|
||||
self.assertEqual('!@#$%', f.raw())
|
||||
lambda f: mock.mock_open(read_data=six.b('!@#$%'))()
|
||||
f = pkg_loader.FileWrapper(fake_pkg, six.b('fake_path'))
|
||||
self.assertEqual(six.b('!@#$%'), f.raw())
|
||||
self.assertRaises(yaml.error.YAMLError, f.yaml)
|
||||
|
||||
|
||||
|
@ -219,7 +219,7 @@ class ZipLoaderTest(base.TestCase):
|
|||
m_read):
|
||||
m_read.return_value.yaml.return_value = [{'FullName': 'fake'}]
|
||||
m_exists.return_value = True
|
||||
m_content = str('fake') if six.PY2 else bytes('fake', 'latin-1')
|
||||
m_content = six.b('fake')
|
||||
loader = pkg_loader.ZipLoader.try_load(six.BytesIO(m_content))
|
||||
self.assertIsNotNone(loader)
|
||||
m_try_set_format.assert_called_once_with({'FullName': 'fake'})
|
||||
|
|
|
@ -24,6 +24,7 @@ from muranopkgcheck.i18n import _
|
|||
FQN_REGEX = re.compile('^([a-zA-Z_$][\w$]*\.)*[a-zA-Z_$][\w$]*$')
|
||||
NAME_REGEX = re.compile('^[A-Za-z_][\w]*$')
|
||||
|
||||
error.register.E002(description='Yaml Error')
|
||||
error.register.E005(description='YAML multi document is not allowed')
|
||||
error.register.E020(description='Missing required key')
|
||||
error.register.E021(description='Unknown keyword')
|
||||
|
|
Loading…
Reference in New Issue