Narrow mock for getfilesystemencoding

Rather than have the tests guess how many times
sys.getfilesystemencoding is going to be called, narrow the mock to
just the encodeutils module.

Change-Id: Ia52d4f380e4053e8962ce9dfc674e732baa167fb
This commit is contained in:
Brant Knudson 2016-01-31 08:40:48 -06:00
parent 3bcdba2afe
commit af01a0d031
2 changed files with 11 additions and 17 deletions

View File

@ -18,6 +18,12 @@ import sys
import six
# NOTE(blk-u): This provides a symbol that can be overridden just for this
# module during testing. sys.getfilesystemencoding() is called by coverage so
# mocking it globally caused the coverage job to fail.
_getfilesystemencoding = sys.getfilesystemencoding
def safe_decode(text, incoming=None, errors='strict'):
"""Decodes incoming text/bytes string using `incoming` if they're not
already unicode.
@ -169,7 +175,7 @@ def exception_to_unicode(exc):
# Try the locale encoding, most error messages are encoded to this encoding
# (ex: os.strerror(errno))
encoding = sys.getfilesystemencoding()
encoding = _getfilesystemencoding()
try:
return msg.decode(encoding)
except UnicodeDecodeError: # nosec

View File

@ -15,8 +15,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import sys
import mock
from oslotest import base as test_base
import six
@ -137,18 +135,6 @@ class ExceptionToUnicodeTest(test_base.BaseTestCase):
def __str__(self):
return self.value
def mock_getfilesystemencoding(test_value):
# NOTE(blk-u): sys.getfilesystemencoding is called by coverage
# while processing the functions/files called by the test so
# mocking getfilesystemencoding was causing the coverage job to
# fail since it doesn't know about koi8_r encoding, and the
# encoding might be incorrect when set in other cases. The
# workaround is to return the test value when it's called by
# oslo.utils and the original value when called later by coverage.
return mock.patch.object(
sys, 'getfilesystemencoding',
side_effect=[test_value, sys.getfilesystemencoding()])
# On Python 3, an exception which returns bytes with is __str__()
# method (like StrException(bytes)) is probably a bug, but it was not
# harder to support this silly case in exception_to_unicode().
@ -164,7 +150,8 @@ class ExceptionToUnicodeTest(test_base.BaseTestCase):
u'utf-8 \xe9\u20ac')
# Force the locale encoding to ASCII to test the fallback
with mock_getfilesystemencoding('ascii'):
with mock.patch.object(encodeutils, '_getfilesystemencoding',
return_value='ascii'):
# Fallback: decode from ISO-8859-1
exc = StrException(b'rawbytes \x80\xff')
self.assertEqual(encodeutils.exception_to_unicode(exc),
@ -181,7 +168,8 @@ class ExceptionToUnicodeTest(test_base.BaseTestCase):
u'unicode \xe9\u20ac')
# Test the locale encoding
with mock_getfilesystemencoding('koi8_r'):
with mock.patch.object(encodeutils, '_getfilesystemencoding',
return_value='koi8_r'):
exc = StrException(b'\xf2\xd5\xd3\xd3\xcb\xc9\xca')
# Decode from the locale encoding
# (the message cannot be decoded from ASCII nor UTF-8)