From a8514c4dda668beecb7ca63ee96fe3ae968161b9 Mon Sep 17 00:00:00 2001 From: Alex Wood Date: Thu, 14 May 2015 16:34:47 -0400 Subject: [PATCH] Correct Python version incompatibilities. --- src/tito/common.py | 4 +++- src/tito/compat.py | 2 +- src/tito/release/main.py | 2 +- test/functional/fetch_tests.py | 2 +- test/unit/__init__.py | 30 ++++++++++++++++++++++++++---- test/unit/test_tar.py | 2 +- 6 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/tito/common.py b/src/tito/common.py index fc7b23a..b25c7ff 100644 --- a/src/tito/common.py +++ b/src/tito/common.py @@ -876,7 +876,9 @@ def get_script_path(scriptname): return scriptpath -def mkdir_p(path, mode=0777): +# 511 is 777 in octal. Python 2 and Python 3 disagree about the right +# way to represent octal numbers. +def mkdir_p(path, mode=511): try: os.makedirs(path, mode) except OSError as e: diff --git a/src/tito/compat.py b/src/tito/compat.py index 6969a82..b29b416 100644 --- a/src/tito/compat.py +++ b/src/tito/compat.py @@ -61,7 +61,7 @@ def dictionary_override(d1, d2): if PY2: overrides = d1.items() + d2.items() else: - overrides = d1.items() | d2.items() + overrides = list(d1.items()) + list(d2.items()) return dict(overrides) diff --git a/src/tito/release/main.py b/src/tito/release/main.py index 7e8e337..e0dd875 100644 --- a/src/tito/release/main.py +++ b/src/tito/release/main.py @@ -52,7 +52,7 @@ class Releaser(ConfigObject): ConfigObject.__init__(self, config=config) config_builder_args = self._parse_builder_args(releaser_config, target) if test: - config_builder_args['test'] = True # builder must know to build from HEAD + config_builder_args['test'] = [True] # builder must know to build from HEAD # Override with builder args from command line if any were given: if 'builder_args' in kwargs: diff --git a/test/functional/fetch_tests.py b/test/functional/fetch_tests.py index de3301e..c58b813 100644 --- a/test/functional/fetch_tests.py +++ b/test/functional/fetch_tests.py @@ -22,7 +22,7 @@ import tempfile from os.path import join -from tito.common import run_command, tito_config_dir +from tito.common import run_command from tito.compat import * # NOQA from functional.fixture import TitoGitTestFixture, tito diff --git a/test/unit/__init__.py b/test/unit/__init__.py index 9f185ab..16dfec5 100644 --- a/test/unit/__init__.py +++ b/test/unit/__init__.py @@ -12,8 +12,10 @@ # in this software or its documentation. from contextlib import contextmanager -from mock import mock_open, patch -from StringIO import StringIO +from mock import patch, MagicMock +from tito.compat import PY2, StringIO + +file_spec = None @contextmanager @@ -21,9 +23,29 @@ def open_mock(content, **kwargs): """Mock's mock_open only supports read() and write() which is not very useful. This context manager adds support for getting the value of what was written out and for iterating through a file line by line.""" + + global file_spec + if file_spec is None: + # set on first use + if PY2: + file_spec = file + else: + import _io + file_spec = list(set(dir(_io.TextIOWrapper)).union(set(dir(_io.BytesIO)))) + + m = MagicMock(name='open', spec=open) + + handle = MagicMock(spec=file_spec) + handle.__enter__.return_value = handle + m.return_value = handle + content_out = StringIO() - m = mock_open() - with patch('__builtin__.open', m, create=True, **kwargs) as mo: + + if PY2: + patch_module = "__builtin__.open" + else: + patch_module = "builtins.open" + with patch(patch_module, m, create=True, **kwargs) as mo: stream = StringIO(content) rv = mo.return_value rv.write = lambda x: content_out.write(bytes(x, "utf-8")) diff --git a/test/unit/test_tar.py b/test/unit/test_tar.py index a10da51..f395bf7 100644 --- a/test/unit/test_tar.py +++ b/test/unit/test_tar.py @@ -2,7 +2,7 @@ import hashlib import os import unittest -from StringIO import StringIO +from tito.compat import StringIO from tito.tar import TarFixer from mock import Mock