|
1 | 1 | # -*- coding: utf-8 -*- |
2 | | -"""Helper methods for tmuxp unittests.""" |
| 2 | +"""Helper methods for tmuxp unittests. |
| 3 | +
|
| 4 | +_CallableContext, WhateverIO, decorator and stdouts are from the case project, |
| 5 | +https://github.com/celery/case, license BSD 3-clause. |
| 6 | +""" |
3 | 7 |
|
4 | 8 | from __future__ import (absolute_import, division, print_function, |
5 | 9 | unicode_literals, with_statement) |
6 | 10 |
|
7 | 11 | import contextlib |
| 12 | +import functools |
| 13 | +import inspect |
| 14 | +import io |
8 | 15 | import logging |
| 16 | +import sys |
| 17 | +from contextlib import contextmanager |
9 | 18 | from random import randint |
10 | 19 |
|
11 | 20 | from tmuxp import exc |
@@ -194,3 +203,136 @@ def bootstrap(self): |
194 | 203 | self.TEST_SESSION_NAME = TEST_SESSION_NAME |
195 | 204 | self.server = t |
196 | 205 | self.session = session |
| 206 | + |
| 207 | + |
| 208 | +StringIO = io.StringIO |
| 209 | +_SIO_write = StringIO.write |
| 210 | +_SIO_init = StringIO.__init__ |
| 211 | + |
| 212 | + |
| 213 | +def update_wrapper(wrapper, wrapped, *args, **kwargs): |
| 214 | + wrapper = functools.update_wrapper(wrapper, wrapped, *args, **kwargs) |
| 215 | + wrapper.__wrapped__ = wrapped |
| 216 | + return wrapper |
| 217 | + |
| 218 | + |
| 219 | +def wraps(wrapped, |
| 220 | + assigned=functools.WRAPPER_ASSIGNMENTS, |
| 221 | + updated=functools.WRAPPER_UPDATES): |
| 222 | + return functools.partial(update_wrapper, wrapped=wrapped, |
| 223 | + assigned=assigned, updated=updated) |
| 224 | + |
| 225 | + |
| 226 | +class _CallableContext(object): |
| 227 | + |
| 228 | + def __init__(self, context, cargs, ckwargs, fun): |
| 229 | + self.context = context |
| 230 | + self.cargs = cargs |
| 231 | + self.ckwargs = ckwargs |
| 232 | + self.fun = fun |
| 233 | + |
| 234 | + def __call__(self, *args, **kwargs): |
| 235 | + return self.fun(*args, **kwargs) |
| 236 | + |
| 237 | + def __enter__(self): |
| 238 | + self.ctx = self.context(*self.cargs, **self.ckwargs) |
| 239 | + return self.ctx.__enter__() |
| 240 | + |
| 241 | + def __exit__(self, *einfo): |
| 242 | + if self.ctx: |
| 243 | + return self.ctx.__exit__(*einfo) |
| 244 | + |
| 245 | + |
| 246 | +def decorator(predicate): |
| 247 | + context = contextmanager(predicate) |
| 248 | + |
| 249 | + @wraps(predicate) |
| 250 | + def take_arguments(*pargs, **pkwargs): |
| 251 | + |
| 252 | + @wraps(predicate) |
| 253 | + def decorator(cls): |
| 254 | + if inspect.isclass(cls): |
| 255 | + orig_setup = cls.setUp |
| 256 | + orig_teardown = cls.tearDown |
| 257 | + |
| 258 | + @wraps(cls.setUp) |
| 259 | + def around_setup(*args, **kwargs): |
| 260 | + try: |
| 261 | + contexts = args[0].__rb3dc_contexts__ |
| 262 | + except AttributeError: |
| 263 | + contexts = args[0].__rb3dc_contexts__ = [] |
| 264 | + p = context(*pargs, **pkwargs) |
| 265 | + p.__enter__() |
| 266 | + contexts.append(p) |
| 267 | + return orig_setup(*args, **kwargs) |
| 268 | + around_setup.__wrapped__ = cls.setUp |
| 269 | + cls.setUp = around_setup |
| 270 | + |
| 271 | + @wraps(cls.tearDown) |
| 272 | + def around_teardown(*args, **kwargs): |
| 273 | + try: |
| 274 | + contexts = args[0].__rb3dc_contexts__ |
| 275 | + except AttributeError: |
| 276 | + pass |
| 277 | + else: |
| 278 | + for context in contexts: |
| 279 | + context.__exit__(*sys.exc_info()) |
| 280 | + orig_teardown(*args, **kwargs) |
| 281 | + around_teardown.__wrapped__ = cls.tearDown |
| 282 | + cls.tearDown = around_teardown |
| 283 | + |
| 284 | + return cls |
| 285 | + else: |
| 286 | + @wraps(cls) |
| 287 | + def around_case(self, *args, **kwargs): |
| 288 | + with context(*pargs, **pkwargs) as context_args: |
| 289 | + context_args = context_args or () |
| 290 | + if not isinstance(context_args, tuple): |
| 291 | + context_args = (context_args,) |
| 292 | + return cls(*(self,) + args + context_args, **kwargs) |
| 293 | + return around_case |
| 294 | + |
| 295 | + if len(pargs) == 1 and callable(pargs[0]): |
| 296 | + fun, pargs = pargs[0], () |
| 297 | + return decorator(fun) |
| 298 | + return _CallableContext(context, pargs, pkwargs, decorator) |
| 299 | + assert take_arguments.__wrapped__ |
| 300 | + return take_arguments |
| 301 | + |
| 302 | + |
| 303 | +class WhateverIO(StringIO): |
| 304 | + |
| 305 | + def __init__(self, v=None, *a, **kw): |
| 306 | + _SIO_init(self, v.decode() if isinstance(v, bytes) else v, *a, **kw) |
| 307 | + |
| 308 | + def write(self, data): |
| 309 | + _SIO_write(self, data.decode() if isinstance(data, bytes) else data) |
| 310 | + |
| 311 | + |
| 312 | +@decorator |
| 313 | +def stdouts(): |
| 314 | + """Override `sys.stdout` and `sys.stderr` with `StringIO` |
| 315 | + instances. |
| 316 | + Decorator example:: |
| 317 | + @mock.stdouts |
| 318 | + def test_foo(self, stdout, stderr): |
| 319 | + something() |
| 320 | + self.assertIn('foo', stdout.getvalue()) |
| 321 | + Context example:: |
| 322 | + with mock.stdouts() as (stdout, stderr): |
| 323 | + something() |
| 324 | + self.assertIn('foo', stdout.getvalue()) |
| 325 | + """ |
| 326 | + prev_out, prev_err = sys.stdout, sys.stderr |
| 327 | + prev_rout, prev_rerr = sys.__stdout__, sys.__stderr__ |
| 328 | + mystdout, mystderr = WhateverIO(), WhateverIO() |
| 329 | + sys.stdout = sys.__stdout__ = mystdout |
| 330 | + sys.stderr = sys.__stderr__ = mystderr |
| 331 | + |
| 332 | + try: |
| 333 | + yield mystdout, mystderr |
| 334 | + finally: |
| 335 | + sys.stdout = prev_out |
| 336 | + sys.stderr = prev_err |
| 337 | + sys.__stdout__ = prev_rout |
| 338 | + sys.__stderr__ = prev_rerr |
0 commit comments