def warn(self, fmt, **args):
        from _pytest.warnings import _issue_config_warning
        from _pytest.warning_types import PytestWarning

        _issue_config_warning(
            PytestWarning(fmt.format(**args) if args else fmt), self._config
        )
def _prepareconfig(args=None, plugins=None):
    warning = None
    if args is None:
        args = sys.argv[1:]
    elif isinstance(args, py.path.local):
        args = [str(args)]
    elif not isinstance(args, (tuple, list)):
        if not isinstance(args, str):
            raise ValueError("not a string or argument list: %r" % (args, ))
        args = shlex.split(args, posix=sys.platform != "win32")
        from _pytest import deprecated

        warning = deprecated.MAIN_STR_ARGS
    config = get_config()
    pluginmanager = config.pluginmanager
    try:
        if plugins:
            for plugin in plugins:
                if isinstance(plugin, six.string_types):
                    pluginmanager.consider_pluginarg(plugin)
                else:
                    pluginmanager.register(plugin)
        if warning:
            from _pytest.warnings import _issue_config_warning

            _issue_config_warning(warning, config=config)
        return pluginmanager.hook.pytest_cmdline_parse(
            pluginmanager=pluginmanager, args=args)
    except BaseException:
        config._ensure_unconfigure()
        raise
Example #3
0
def _prepareconfig(args=None, plugins=None):
    warning = None
    if args is None:
        args = sys.argv[1:]
    elif isinstance(args, py.path.local):
        args = [str(args)]
    elif not isinstance(args, (tuple, list)):
        if not isinstance(args, str):
            raise ValueError("not a string or argument list: %r" % (args,))
        args = shlex.split(args, posix=sys.platform != "win32")
        from _pytest import deprecated

        warning = deprecated.MAIN_STR_ARGS
    config = get_config()
    pluginmanager = config.pluginmanager
    try:
        if plugins:
            for plugin in plugins:
                if isinstance(plugin, six.string_types):
                    pluginmanager.consider_pluginarg(plugin)
                else:
                    pluginmanager.register(plugin)
        if warning:
            from _pytest.warnings import _issue_config_warning

            _issue_config_warning(warning, config=config, stacklevel=4)
        return pluginmanager.hook.pytest_cmdline_parse(
            pluginmanager=pluginmanager, args=args
        )
    except BaseException:
        config._ensure_unconfigure()
        raise
Example #4
0
    def warn(self, fmt, **args):
        from _pytest.warnings import _issue_config_warning
        from _pytest.warning_types import PytestWarning

        _issue_config_warning(
            PytestWarning(fmt.format(**args) if args else fmt), self._config
        )
Example #5
0
    def _warn_already_imported(self, name):
        from _pytest.warning_types import PytestWarning
        from _pytest.warnings import _issue_config_warning

        _issue_config_warning(
            PytestWarning("Module already imported so cannot be rewritten: %s" % name),
            self.config,
        )
Example #6
0
    def _warn_already_imported(self, name):
        from _pytest.warning_types import PytestWarning
        from _pytest.warnings import _issue_config_warning

        _issue_config_warning(
            PytestWarning("Module already imported so cannot be rewritten: %s" % name),
            self.config,
        )
Example #7
0
def determine_setup(inifile, args, rootdir_cmd_arg=None, config=None):
    dirs = get_dirs_from_args(args)
    if inifile:
        iniconfig = py.iniconfig.IniConfig(inifile)
        is_cfg_file = str(inifile).endswith(".cfg")
        # TODO: [pytest] section in *.cfg files is depricated. Need refactoring.
        sections = ["tool:pytest", "pytest"] if is_cfg_file else ["pytest"]
        for section in sections:
            try:
                inicfg = iniconfig[section]
                if is_cfg_file and section == "pytest" and config is not None:
                    from _pytest.deprecated import CFG_PYTEST_SECTION
                    from _pytest.warning_types import RemovedInPytest4Warning
                    from _pytest.warnings import _issue_config_warning

                    _issue_config_warning(
                        RemovedInPytest4Warning(
                            CFG_PYTEST_SECTION.format(filename=str(inifile))
                        ),
                        config,
                    )
                break
            except KeyError:
                inicfg = None
        rootdir = get_common_ancestor(dirs)
    else:
        ancestor = get_common_ancestor(dirs)
        rootdir, inifile, inicfg = getcfg([ancestor], config=config)
        if rootdir is None:
            for rootdir in ancestor.parts(reverse=True):
                if rootdir.join("setup.py").exists():
                    break
            else:
                rootdir, inifile, inicfg = getcfg(dirs, config=config)
                if rootdir is None:
                    rootdir = get_common_ancestor([py.path.local(), ancestor])
                    is_fs_root = os.path.splitdrive(str(rootdir))[1] == "/"
                    if is_fs_root:
                        rootdir = ancestor
    if rootdir_cmd_arg:
        rootdir_abs_path = py.path.local(os.path.expandvars(rootdir_cmd_arg))
        if not os.path.isdir(str(rootdir_abs_path)):
            raise UsageError(
                "Directory '{}' not found. Check your '--rootdir' option.".format(
                    rootdir_abs_path
                )
            )
        rootdir = rootdir_abs_path
    return rootdir, inifile, inicfg or {}
def pytest_configure(config):
    resultlog = config.option.resultlog
    # prevent opening resultlog on slave nodes (xdist)
    if resultlog and not hasattr(config, "slaveinput"):
        dirname = os.path.dirname(os.path.abspath(resultlog))
        if not os.path.isdir(dirname):
            os.makedirs(dirname)
        logfile = open(resultlog, "w", 1)  # line buffered
        config._resultlog = ResultLog(config, logfile)
        config.pluginmanager.register(config._resultlog)

        from _pytest.deprecated import RESULT_LOG
        from _pytest.warnings import _issue_config_warning

        _issue_config_warning(RESULT_LOG, config)
Example #9
0
def pytest_configure(config):
    resultlog = config.option.resultlog
    # prevent opening resultlog on slave nodes (xdist)
    if resultlog and not hasattr(config, "slaveinput"):
        dirname = os.path.dirname(os.path.abspath(resultlog))
        if not os.path.isdir(dirname):
            os.makedirs(dirname)
        logfile = open(resultlog, "w", 1)  # line buffered
        config._resultlog = ResultLog(config, logfile)
        config.pluginmanager.register(config._resultlog)

        from _pytest.deprecated import RESULT_LOG
        from _pytest.warnings import _issue_config_warning

        _issue_config_warning(RESULT_LOG, config, stacklevel=2)
def determine_setup(inifile, args, rootdir_cmd_arg=None, config=None):
    dirs = get_dirs_from_args(args)
    if inifile:
        iniconfig = py.iniconfig.IniConfig(inifile)
        is_cfg_file = str(inifile).endswith(".cfg")
        sections = ["tool:pytest", "pytest"] if is_cfg_file else ["pytest"]
        for section in sections:
            try:
                inicfg = iniconfig[section]
                if is_cfg_file and section == "pytest" and config is not None:
                    from _pytest.deprecated import CFG_PYTEST_SECTION
                    from _pytest.warnings import _issue_config_warning

                    # TODO: [pytest] section in *.cfg files is deprecated. Need refactoring once
                    # the deprecation expires.
                    _issue_config_warning(
                        CFG_PYTEST_SECTION.format(filename=str(inifile)),
                        config,
                        stacklevel=2,
                    )
                break
            except KeyError:
                inicfg = None
        rootdir = get_common_ancestor(dirs)
    else:
        ancestor = get_common_ancestor(dirs)
        rootdir, inifile, inicfg = getcfg([ancestor], config=config)
        if rootdir is None:
            for rootdir in ancestor.parts(reverse=True):
                if rootdir.join("setup.py").exists():
                    break
            else:
                rootdir, inifile, inicfg = getcfg(dirs, config=config)
                if rootdir is None:
                    rootdir = get_common_ancestor([py.path.local(), ancestor])
                    is_fs_root = os.path.splitdrive(str(rootdir))[1] == "/"
                    if is_fs_root:
                        rootdir = ancestor
    if rootdir_cmd_arg:
        rootdir_abs_path = py.path.local(os.path.expandvars(rootdir_cmd_arg))
        if not os.path.isdir(str(rootdir_abs_path)):
            raise UsageError(
                "Directory '{}' not found. Check your '--rootdir' option.".format(
                    rootdir_abs_path
                )
            )
        rootdir = rootdir_abs_path
    return rootdir, inifile, inicfg or {}
def getcfg(args, config=None):
    """
    Search the list of arguments for a valid ini-file for pytest,
    and return a tuple of (rootdir, inifile, cfg-dict).

    note: config is optional and used only to issue warnings explicitly (#2891).
    """
    from _pytest.deprecated import CFG_PYTEST_SECTION

    inibasenames = ["pytest.ini", "tox.ini", "setup.cfg"]
    args = [x for x in args if not str(x).startswith("-")]
    if not args:
        args = [py.path.local()]
    for arg in args:
        arg = py.path.local(arg)
        for base in arg.parts(reverse=True):
            for inibasename in inibasenames:
                p = base.join(inibasename)
                if exists(p):
                    iniconfig = py.iniconfig.IniConfig(p)
                    if "pytest" in iniconfig.sections:
                        if inibasename == "setup.cfg" and config is not None:
                            from _pytest.warnings import _issue_config_warning
                            from _pytest.warning_types import RemovedInPytest4Warning

                            _issue_config_warning(
                                RemovedInPytest4Warning(
                                    CFG_PYTEST_SECTION.format(filename=inibasename)
                                ),
                                config=config,
                                stacklevel=2,
                            )
                        return base, p, iniconfig["pytest"]
                    if (
                        inibasename == "setup.cfg"
                        and "tool:pytest" in iniconfig.sections
                    ):
                        return base, p, iniconfig["tool:pytest"]
                    elif inibasename == "pytest.ini":
                        # allowed to be empty
                        return base, p, {}
    return None, None, None