diff options
author | Brant Knudson <bknudson@us.ibm.com> | 2016-01-31 08:40:48 -0600 |
---|---|---|
committer | Brant Knudson <bknudson@us.ibm.com> | 2016-01-31 08:40:48 -0600 |
commit | af01a0d031af9efad31ba0c77e23d698cf0ea04b (patch) | |
tree | 5a5d2ca829d29d89fee7c37ebff13a9d92087226 | |
parent | 3bcdba2afee38809681fa93a9e3df5bf48f556bc (diff) | |
download | oslo-utils-af01a0d031af9efad31ba0c77e23d698cf0ea04b.tar.gz |
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
-rw-r--r-- | oslo_utils/encodeutils.py | 8 | ||||
-rw-r--r-- | oslo_utils/tests/tests_encodeutils.py | 20 |
2 files changed, 11 insertions, 17 deletions
diff --git a/oslo_utils/encodeutils.py b/oslo_utils/encodeutils.py index 3827631..761aef7 100644 --- a/oslo_utils/encodeutils.py +++ b/oslo_utils/encodeutils.py @@ -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 diff --git a/oslo_utils/tests/tests_encodeutils.py b/oslo_utils/tests/tests_encodeutils.py index 8cceb8e..ded44c2 100644 --- a/oslo_utils/tests/tests_encodeutils.py +++ b/oslo_utils/tests/tests_encodeutils.py @@ -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) |