def handle_tracebacks(
    show_traceback: bool = False,
    cls: Type[TracebackHandler] = TracebackHandler,
) -> ContextManager:
    """
	Context manager to conditionally handle tracebacks, usually based on the value of a command line flag.

	.. versionadded:: 0.8.0

	:param show_traceback: If :py:obj:`True`, the full Python traceback will be shown on errors.
		If :py:obj:`False`, only the summary of the traceback will be shown.
		In either case the program execution will stop on error.
	:param cls: The class to use to handle the tracebacks.

	.. versionchanged:: 1.0.0  Added the ``cls`` parameter.

	.. seealso::

		* :func:`~.traceback_handler`
		* :class:`~.TracebackHandler`
	"""

    if show_traceback:
        return nullcontext()
    else:
        return cls()()
示例#2
0
	def cleanup(self):
		context: ContextManager

		if sys.platform == "win32":
			context = suppress(PermissionError, NotADirectoryError)
		else:
			context = nullcontext()

		with context:
			super().cleanup()
示例#3
0
    def cleanup(self) -> None:
        """
		Cleanup the temporary directory by removing it and its contents.

		If the :class:`~.TemporaryPathPlus` is used as a context manager
		this is called when leaving the :keyword:`with` block.
		"""

        context: ContextManager

        if sys.platform == "win32":  # pragma: no cover (!Windows)
            context = contextlib.suppress(PermissionError, NotADirectoryError)
        else:  # pragma: no cover (Windows)
            context = nullcontext()

        with context:
            super().cleanup()
示例#4
0
def test_install_requirements(tmp_pathplus: PathPlus):
    args = [
        str(tmp_pathplus / "venv"),
        "--prompt",
        f"(demo_venv) ",
        "--seeder",
        "pip",
        "--download",
    ]

    of_session = session_via_cli(args)

    assert of_session.seeder.enabled

    with of_session:
        of_session.run()

    (tmp_pathplus / "requirements.txt").write_text("click")

    cm: ContextManager

    if repo_helper_devenv.__version__ == "0.4.0":
        cm = nullcontext()
    else:
        cm = pytest.warns(DeprecationWarning)

    with cm:
        install_requirements(of_session, tmp_pathplus / "requirements.txt")

    # Check list of packages in virtualenv
    venv_dir = tmp_pathplus / "venv"

    if sys.platform == "win32":
        version_dirs = [(venv_dir / "Lib")]
    elif PYPY:
        version_dirs = [venv_dir]
    else:
        version_dirs = list((venv_dir / "lib").glob("py*"))

    for version_dir in version_dirs:

        assert (version_dir / "site-packages" / "click").is_dir()
# stdlib
import shutil
from typing import ContextManager, List

# 3rd party
import pytest
from domdf_python_tools.compat import nullcontext
from domdf_python_tools.paths import PathPlus

# this package
from sphinxcontrib.extras_require.sources import requirements_from_file


@pytest.mark.parametrize("requirements, extra, expects, warningfilter", [
    ("faker\npytest\ntox", "extra_c", ["faker", "pytest", "tox"
                                       ], nullcontext()),
    (
        '\n'.join(["faker", "pytest", 'tox; python_version <= "3.6"']),
        "extra_c",
        ["faker", "pytest", 'tox; python_version <= "3.6"'],
        nullcontext(),
    ),
    (
        '\n'.join(["faker", "pytest", "tox; python<=3.6"]),
        "extra_c",
        ["faker", "pytest"],
        pytest.warns(UserWarning,
                     match="Ignored invalid requirement 'tox; python<=3.6'"),
    ),
])
def test_from_file(
示例#6
0
def test_nullcontext():
    with nullcontext("foo") as f:
        assert f == "foo"

    assert f == "foo"