1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Python 3: there's no __builtin__ module

This caused an ImportError in a test module and showed up as one test
failure.  Now the test module can get imported and many more tests fail
(on Python 3).  Such is life.  ;-)
This commit is contained in:
Marius Gedminas 2015-09-30 09:01:09 +03:00
parent ddafed4403
commit f7571cb37f

View file

@ -20,10 +20,9 @@
from __future__ import (absolute_import, division) from __future__ import (absolute_import, division)
__metaclass__ = type __metaclass__ = type
import __builtin__
import errno import errno
from nose.tools import timed from six.moves import builtins
from ansible.compat.tests import unittest from ansible.compat.tests import unittest
from ansible.compat.tests.mock import patch, MagicMock, mock_open, Mock from ansible.compat.tests.mock import patch, MagicMock, mock_open, Mock
@ -37,16 +36,16 @@ class TestModuleUtilsBasic(unittest.TestCase):
pass pass
def test_module_utils_basic_imports(self): def test_module_utils_basic_imports(self):
realimport = __builtin__.__import__ realimport = builtins.__import__
def _mock_import(name, *args, **kwargs): def _mock_import(name, *args, **kwargs):
if name == 'json': if name == 'json':
raise ImportError() raise ImportError()
realimport(name, *args, **kwargs) realimport(name, *args, **kwargs)
with patch.object(__builtin__, '__import__', _mock_import, create=True) as m: with patch.object(builtins, '__import__', _mock_import, create=True) as m:
m('ansible.module_utils.basic') m('ansible.module_utils.basic')
__builtin__.__import__('ansible.module_utils.basic') builtins.__import__('ansible.module_utils.basic')
def test_module_utils_basic_get_platform(self): def test_module_utils_basic_get_platform(self):
with patch('platform.system', return_value='foo'): with patch('platform.system', return_value='foo'):
@ -510,7 +509,7 @@ class TestModuleUtilsBasic(unittest.TestCase):
m = mock_open() m = mock_open()
m.side_effect = OSError m.side_effect = OSError
with patch('__builtin__.open', m, create=True): with patch.object(builtins, 'open', m, create=True):
self.assertEqual(am.is_special_selinux_path('/some/path/that/should/be/nfs'), (False, None)) self.assertEqual(am.is_special_selinux_path('/some/path/that/should/be/nfs'), (False, None))
mount_data = [ mount_data = [
@ -524,7 +523,7 @@ class TestModuleUtilsBasic(unittest.TestCase):
m = mock_open(read_data=''.join(mount_data)) m = mock_open(read_data=''.join(mount_data))
m.return_value.readlines.return_value = mount_data m.return_value.readlines.return_value = mount_data
with patch('__builtin__.open', m, create=True): with patch.object(builtins, 'open', m, create=True):
self.assertEqual(am.is_special_selinux_path('/some/random/path'), (False, None)) self.assertEqual(am.is_special_selinux_path('/some/random/path'), (False, None))
self.assertEqual(am.is_special_selinux_path('/some/path/that/should/be/nfs'), (True, ['foo_u', 'foo_r', 'foo_t', 's0'])) self.assertEqual(am.is_special_selinux_path('/some/path/that/should/be/nfs'), (True, ['foo_u', 'foo_r', 'foo_t', 's0']))
self.assertEqual(am.is_special_selinux_path('/weird/random/fstype/path'), (True, ['foo_u', 'foo_r', 'foo_t', 's0'])) self.assertEqual(am.is_special_selinux_path('/weird/random/fstype/path'), (True, ['foo_u', 'foo_r', 'foo_t', 's0']))