Use io.StringIO and io.BytesIO instead of StringIO.StringIO for compat with py3

This commit is contained in:
Toshio Kuratomi 2016-02-26 16:42:18 -08:00
parent c29f51804b
commit b70bf3b056
8 changed files with 38 additions and 27 deletions

View file

@ -23,9 +23,9 @@ __metaclass__ = type
import copy
import json
import sys
from io import BytesIO
from ansible.compat.tests import unittest
from ansible.compat.six import StringIO
from ansible.module_utils import basic
from ansible.module_utils.basic import heuristic_log_sanitize
@ -41,7 +41,7 @@ class TestAnsibleModuleExitJson(unittest.TestCase):
basic.MODULE_COMPLEX_ARGS = '{}'
self.old_stdout = sys.stdout
self.fake_stream = StringIO()
self.fake_stream = BytesIO()
sys.stdout = self.fake_stream
self.module = basic.AnsibleModule(argument_spec=dict())
@ -127,7 +127,7 @@ class TestAnsibleModuleExitValuesRemoved(unittest.TestCase):
def test_exit_json_removes_values(self):
self.maxDiff = None
for args, return_val, expected in self.dataset:
sys.stdout = StringIO()
sys.stdout = BytesIO()
basic.MODULE_COMPLEX_ARGS = json.dumps(args)
module = basic.AnsibleModule(
argument_spec = dict(
@ -146,7 +146,7 @@ class TestAnsibleModuleExitValuesRemoved(unittest.TestCase):
expected = copy.deepcopy(expected)
del expected['changed']
expected['failed'] = True
sys.stdout = StringIO()
sys.stdout = BytesIO()
basic.MODULE_COMPLEX_ARGS = json.dumps(args)
module = basic.AnsibleModule(
argument_spec = dict(

View file

@ -22,16 +22,16 @@ __metaclass__ = type
import errno
import sys
import time
from io import BytesIO
from ansible.compat.tests import unittest
from ansible.compat.six import StringIO, BytesIO
from ansible.compat.tests.mock import call, MagicMock, Mock, patch, sentinel
from ansible.module_utils import basic
from ansible.module_utils.basic import AnsibleModule
class OpenStringIO(StringIO):
"""StringIO with dummy close() method
class OpenBytesIO(BytesIO):
"""BytesIO with dummy close() method
So that you can inspect the content after close() was called.
"""
@ -77,7 +77,7 @@ class TestAnsibleModuleRunCommand(unittest.TestCase):
self.subprocess = patch('ansible.module_utils.basic.subprocess').start()
self.cmd = Mock()
self.cmd.returncode = 0
self.cmd.stdin = OpenStringIO()
self.cmd.stdin = OpenBytesIO()
self.cmd.stdout.fileno.return_value = sentinel.stdout
self.cmd.stderr.fileno.return_value = sentinel.stderr
self.subprocess.Popen.return_value = self.cmd