diff options
Diffstat (limited to 'src/snakeoil/test/__init__.py')
-rw-r--r-- | src/snakeoil/test/__init__.py | 29 |
1 files changed, 20 insertions, 9 deletions
diff --git a/src/snakeoil/test/__init__.py b/src/snakeoil/test/__init__.py index bb9381f7..b93094a8 100644 --- a/src/snakeoil/test/__init__.py +++ b/src/snakeoil/test/__init__.py @@ -13,18 +13,20 @@ from snakeoil import klass def random_str(length): """Return a random string of specified length.""" - return ''.join(random.choices(string.ascii_letters + string.digits, k=length)) + return "".join(random.choices(string.ascii_letters + string.digits, k=length)) def coverage(): """Extract coverage instance (if it exists) from the current running context.""" cov = None import inspect + try: import coverage + frame = inspect.currentframe() while frame is not None: - cov = getattr(frame.f_locals.get('self'), 'coverage', None) + cov = getattr(frame.f_locals.get("self"), "coverage", None) if isinstance(cov, coverage.coverage): break frame = frame.f_back @@ -33,7 +35,7 @@ def coverage(): return cov -@klass.patch('os._exit') +@klass.patch("os._exit") def _os_exit(orig_exit, val): """Monkeypatch os._exit() to save coverage data before exit.""" cov = coverage() @@ -51,7 +53,9 @@ def protect_process(functor, name=None): if os.environ.get(_PROTECT_ENV_VAR, False): return functor(self) if name is None: - name = f"{self.__class__.__module__}.{self.__class__.__name__}.{method_name}" + name = ( + f"{self.__class__.__module__}.{self.__class__.__name__}.{method_name}" + ) runner_path = __file__ if runner_path.endswith(".pyc") or runner_path.endswith(".pyo"): runner_path = runner_path.rsplit(".", maxsplit=1)[0] + ".py" @@ -59,11 +63,18 @@ def protect_process(functor, name=None): try: os.environ[_PROTECT_ENV_VAR] = "yes" args = [sys.executable, __file__, name] - p = subprocess.Popen(args, shell=False, env=os.environ.copy(), - stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + p = subprocess.Popen( + args, + shell=False, + env=os.environ.copy(), + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + ) stdout, _ = p.communicate() ret = p.wait() - assert ret == 0, f"subprocess run: {args!r}\nnon zero exit: {ret}\nstdout:\n{stdout}" + assert ( + ret == 0 + ), f"subprocess run: {args!r}\nnon zero exit: {ret}\nstdout:\n{stdout}" finally: if wipe: os.environ.pop(_PROTECT_ENV_VAR, None) @@ -71,7 +82,7 @@ def protect_process(functor, name=None): for x in ("__doc__", "__name__"): if hasattr(functor, x): setattr(_inner_run, x, getattr(functor, x)) - method_name = getattr(functor, '__name__', None) + method_name = getattr(functor, "__name__", None) return _inner_run @@ -84,4 +95,4 @@ def hide_imports(*import_names: str): raise ImportError() return orig_import(name, *args, **kwargs) - return patch('builtins.__import__', side_effect=mock_import) + return patch("builtins.__import__", side_effect=mock_import) |