Ejemplo n.º 1
0
    def _highlight(self, source: str) -> str:
        """Highlight the given source code if we have markup support."""
        from _pytest.config.exceptions import UsageError

        if not self.hasmarkup or not self.code_highlight:
            return source
        try:
            from pygments.formatters.terminal import TerminalFormatter
            from pygments.lexers.python import PythonLexer
            from pygments import highlight
            import pygments.util
        except ImportError:
            return source
        else:
            try:
                highlighted: str = highlight(
                    source,
                    PythonLexer(),
                    TerminalFormatter(
                        bg=os.getenv("PYTEST_THEME_MODE", "dark"),
                        style=os.getenv("PYTEST_THEME"),
                    ),
                )
                return highlighted
            except pygments.util.ClassNotFound:
                raise UsageError(
                    "PYTEST_THEME environment variable had an invalid value: '{}'. "
                    "Only valid pygment styles are allowed.".format(
                        os.getenv("PYTEST_THEME")))
            except pygments.util.OptionError:
                raise UsageError(
                    "PYTEST_THEME_MODE environment variable had an invalid value: '{}'. "
                    "The only allowed values are 'dark' and 'light'.".format(
                        os.getenv("PYTEST_THEME_MODE")))
Ejemplo n.º 2
0
    def _import_pdb_cls(cls, capman):
        if not cls._config:
            # Happens when using pytest.set_trace outside of a test.
            return pdb.Pdb

        usepdb_cls = cls._config.getvalue("usepdb_cls")

        if cls._wrapped_pdb_cls and cls._wrapped_pdb_cls[0] == usepdb_cls:
            return cls._wrapped_pdb_cls[1]

        if usepdb_cls:
            modname, classname = usepdb_cls

            try:
                __import__(modname)
                mod = sys.modules[modname]

                # Handle --pdbcls=pdb:pdb.Pdb (useful e.g. with pdbpp).
                parts = classname.split(".")
                pdb_cls = getattr(mod, parts[0])
                for part in parts[1:]:
                    pdb_cls = getattr(pdb_cls, part)
            except Exception as exc:
                value = ":".join((modname, classname))
                raise UsageError("--pdbcls: could not import {!r}: {}".format(
                    value, exc))
        else:
            pdb_cls = pdb.Pdb

        wrapped_cls = cls._get_pdb_wrapper_class(pdb_cls, capman)
        cls._wrapped_pdb_cls = (usepdb_cls, wrapped_cls)
        return wrapped_cls
Ejemplo n.º 3
0
    def error(self, message):
        """Transform argparse error message into UsageError."""
        msg = "%s: error: %s" % (self.prog, message)

        if hasattr(self._parser, "_config_source_hint"):
            msg = "%s (%s)" % (msg, self._parser._config_source_hint)

        raise UsageError(self.format_usage() + msg)
Ejemplo n.º 4
0
    def error(self, message):
        """Transform argparse error message into UsageError."""
        msg = "{}: error: {}".format(self.prog, message)

        if hasattr(self._parser, "_config_source_hint"):
            msg = "{} ({})".format(msg, self._parser._config_source_hint)

        raise UsageError(self.format_usage() + msg)
Ejemplo n.º 5
0
    def error(self, message: str) -> "NoReturn":
        """Transform argparse error message into UsageError."""
        msg = f"{self.prog}: error: {message}"

        if hasattr(self._parser, "_config_source_hint"):
            # Type ignored because the attribute is set dynamically.
            msg = f"{msg} ({self._parser._config_source_hint})"  # type: ignore

        raise UsageError(self.format_usage() + msg)
Ejemplo n.º 6
0
def _import_pdbcls(modname, classname):
    try:
        __import__(modname)
        mod = sys.modules[modname]

        # Handle --pdbcls=pdb:pdb.Pdb (useful e.g. with pdbpp).
        parts = classname.split(".")
        pdb_cls = getattr(mod, parts[0])
        for part in parts[1:]:
            pdb_cls = getattr(pdb_cls, part)

        return pdb_cls
    except Exception as exc:
        value = ":".join((modname, classname))
        raise UsageError("--pdbcls: could not import {!r}: {}".format(value, exc))