File: _util.py

package info (click to toggle)
python-rjsmin 1.2.5%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,256 kB
  • sloc: javascript: 8,503; python: 5,315; ansic: 821; sh: 100; makefile: 19
file content (239 lines) | stat: -rw-r--r-- 6,052 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# -*- coding: ascii -*-
u"""
:Copyright:

 Copyright 2014 - 2025
 Andr\xe9 Malo or his licensors, as applicable

:License:

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

================
 Test Utilities
================

Test utilities.
"""
__author__ = u"Andr\xe9 Malo"

import contextlib as _contextlib
import sys as _sys
import types as _types

from pytest import skip  # noqa pylint: disable = unused-import

try:
    import mock
except ImportError:
    from unittest import mock

try:
    reload  # pylint: disable = used-before-assignment
except NameError:
    # pylint: disable = redefined-builtin
    try:
        from importlib import reload
    except ImportError:
        from imp import reload  # noqa pylint: disable = deprecated-module

_unset = type("_unset", (object,), {})()  # pylint: disable=invalid-name


# pylint: disable = useless-object-inheritance


class Bunch(object):
    """Bunch object"""

    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)


@_contextlib.contextmanager
def patched_import(what, how=_unset):
    """
    Context manager to mock an import statement temporarily

    Parameters:
      what (str):
        Name of the module to mock

      how (any):
        How should it be replaced? If omitted or `unset`, a new MagicMock
        instance is created. The result is yielded as context.
    """
    # basically stolen from opensource.perlig.de/tdi/

    class_types = (type,)
    if str is bytes:
        # pylint: disable = no-member
        class_types += (_types.ClassType,)

    # pylint: disable = unnecessary-lambda-assignment
    _is_exc = lambda obj: isinstance(obj, BaseException) or (
        isinstance(obj, class_types) and issubclass(obj, BaseException)
    )

    class FinderLoader(object):
        """Finder / Loader for meta path"""

        def __init__(self, fullname, module):
            self.module = module
            self.name = fullname
            extra = "%s." % fullname
            for key in list(_sys.modules):
                if key.startswith(extra):
                    del _sys.modules[key]
            if fullname in _sys.modules:
                del _sys.modules[fullname]

        def find_spec(self, name, path=None, target=None):
            """Find spec (Python 3.10+)"""
            # pylint: disable = unused-argument
            if name != self.name:
                return None

            from importlib import util

            return util.spec_from_loader(name, self)

        def find_module(self, fullname, path=None):
            """Find the module"""
            # pylint: disable = unused-argument
            if fullname == self.name:
                return self
            return None

        def load_module(self, fullname):
            """Load the module"""
            if _is_exc(self.module):
                raise self.module
            _sys.modules[fullname] = self.module
            return self.module

        def create_module(self, spec):
            """Create module"""
            if _is_exc(self.module):
                raise self.module
            return self.module

        def exec_module(self, module):
            """Execute module"""

    realmodules = _sys.modules.copy()
    try:
        obj = FinderLoader(what, mock.MagicMock() if how is _unset else how)
        realpath = _sys.meta_path[:]
        try:
            _sys.meta_path[:] = [obj] + realpath
            old, parts = _unset, what.rsplit(".", 1)
            if len(parts) == 2:
                parent, base = parts[0], parts[1]
                if parent in _sys.modules:
                    parent = _sys.modules[parent]
                    if hasattr(parent, base):
                        old = getattr(parent, base)
                        setattr(parent, base, obj.module)
            try:
                yield obj.module
            finally:
                if old is not _unset:
                    setattr(parent, base, old)
        finally:
            _sys.meta_path[:] = realpath
    finally:
        _sys.modules.clear()
        _sys.modules.update(realmodules)


def uni(value):
    """
    Create unicode from raw string with unicode escapes

    Parameters:
      value (str):
        String, which encodes to ascii and decodes as unicode_escape

    Returns:
      str: The decoded string
    """
    return value.encode("ascii").decode("unicode_escape")


class badstr(object):  # pylint: disable = invalid-name
    """bad string"""

    def __str__(self):
        raise RuntimeError("yo")


badstr = badstr()


class badbytes(object):  # pylint: disable = invalid-name
    """bad bytes"""

    def __bytes__(self):
        raise RuntimeError("yoyo")

    if str is bytes:
        __str__ = __bytes__


badbytes = badbytes()


class badbool(object):  # pylint: disable = invalid-name
    """bad bool"""

    def __bool__(self):
        raise RuntimeError("yoyo")

    if str is bytes:
        __nonzero__ = __bool__


badbool = badbool()


class baditer(object):  # pylint: disable = invalid-name
    """bad iter"""

    def __init__(self, *what):
        self._what = iter(what)

    def __iter__(self):
        return self

    def __next__(self):
        for item in self._what:
            if isinstance(item, Exception):
                raise item
            return item

    next = __next__


def calls(value):
    """
    Map calls to list of tuples

    Parameters:
      value (Mock):
        The mock object to inspect

    Returns:
      list: The List of call tuples
    """
    return list(map(tuple, value.mock_calls))